-
Notifications
You must be signed in to change notification settings - Fork 323
/
Copy pathgoogle-cloud-pubsub.js
173 lines (142 loc) · 4.46 KB
/
google-cloud-pubsub.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
'use strict'
const {
channel,
addHook,
AsyncResource
} = require('./helpers/instrument')
const shimmer = require('../../datadog-shimmer')
const requestStartCh = channel('apm:google-cloud-pubsub:request:start')
const requestFinishCh = channel('apm:google-cloud-pubsub:request:finish')
const requestErrorCh = channel('apm:google-cloud-pubsub:request:error')
const receiveStartCh = channel(`apm:google-cloud-pubsub:receive:start`)
const receiveFinishCh = channel('apm:google-cloud-pubsub:receive:finish')
const receiveErrorCh = channel('apm:google-cloud-pubsub:receive:error')
const publisherMethods = [
'createTopic',
'updateTopic',
'publish',
'getTopic',
'listTopics',
'listTopicSubscriptions',
'listTopicSnapshots',
'deleteTopic',
'detachSubscription'
]
const schemaServiceMethods = [
'createSchema',
'getSchema',
'listSchemas',
'listSchemaRevisions',
'commitSchema',
'rollbackSchema',
'deleteSchemaRevision',
'deleteSchema',
'validateSchema',
'validateMessage'
]
const subscriberMethods = [
'createSubscription',
'getSubscription',
'updateSubscription',
'listSubscriptions',
'deleteSubscription',
'modifyAckDeadline',
'acknowledge',
'pull',
'streamingPull',
'modifyPushConfig',
'getSnapshot',
'listSnapshots',
'createSnapshot',
'updateSnapshot',
'deleteSnapshot',
'seek'
]
function wrapMethod (method) {
const api = method.name
return function (request) {
if (!requestStartCh.hasSubscribers) return method.apply(this, arguments)
const innerAsyncResource = new AsyncResource('bound-anonymous-fn')
return innerAsyncResource.runInAsyncScope(() => {
const projectId = this.auth._cachedProjectId
const cb = arguments[arguments.length - 1]
requestStartCh.publish({ request, api, projectId })
if (typeof cb === 'function') {
const outerAsyncResource = new AsyncResource('bound-anonymous-fn')
arguments[arguments.length - 1] = innerAsyncResource.bind(function (error) {
if (error) {
requestErrorCh.publish(error)
}
requestFinishCh.publish()
return outerAsyncResource.runInAsyncScope(() => cb.apply(this, arguments))
})
return method.apply(this, arguments)
} else {
return method.apply(this, arguments)
.then(
response => {
requestFinishCh.publish()
return response
},
error => {
requestErrorCh.publish(error)
requestFinishCh.publish()
throw error
}
)
}
})
}
}
function massWrap (obj, methods, wrapper) {
for (const method of methods) {
if (typeof obj[method] === 'function') {
shimmer.wrap(obj, method, wrapper)
}
}
}
addHook({ name: '@google-cloud/pubsub', versions: ['>=1.2'] }, (obj) => {
const Subscription = obj.Subscription
shimmer.wrap(Subscription.prototype, 'emit', emit => function (eventName, message) {
if (eventName !== 'message' || !message) return emit.apply(this, arguments)
const asyncResource = new AsyncResource('bound-anonymous-fn')
return asyncResource.runInAsyncScope(() => {
try {
return emit.apply(this, arguments)
} catch (err) {
receiveErrorCh.publish(err)
throw err
}
})
})
return obj
})
addHook({ name: '@google-cloud/pubsub', versions: ['>=1.2'], file: 'build/src/lease-manager.js' }, (obj) => {
const LeaseManager = obj.LeaseManager
shimmer.wrap(LeaseManager.prototype, '_dispense', dispense => function (message) {
if (receiveStartCh.hasSubscribers) {
receiveStartCh.publish({ message })
}
return dispense.apply(this, arguments)
})
shimmer.wrap(LeaseManager.prototype, 'remove', remove => function (message) {
receiveFinishCh.publish({ message })
return remove.apply(this, arguments)
})
shimmer.wrap(LeaseManager.prototype, 'clear', clear => function () {
for (const message of this._messages) {
receiveFinishCh.publish({ message })
}
return clear.apply(this, arguments)
})
return obj
})
addHook({ name: '@google-cloud/pubsub', versions: ['>=1.2'] }, (obj) => {
const { PublisherClient, SchemaServiceClient, SubscriberClient } = obj.v1
massWrap(PublisherClient.prototype, publisherMethods, wrapMethod)
massWrap(SubscriberClient.prototype, subscriberMethods, wrapMethod)
if (SchemaServiceClient) {
massWrap(SchemaServiceClient.prototype, schemaServiceMethods, wrapMethod)
}
return obj
})