-
Notifications
You must be signed in to change notification settings - Fork 323
/
Copy pathindex.spec.js
299 lines (272 loc) · 9.72 KB
/
index.spec.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
'use strict'
const agent = require('../../dd-trace/test/plugins/agent')
const { expectSomeSpan, withDefaults } = require('../../dd-trace/test/plugins/helpers')
const id = require('../../dd-trace/src/id')
const { ERROR_MESSAGE, ERROR_TYPE, ERROR_STACK } = require('../../dd-trace/src/constants')
// The roundtrip to the pubsub emulator takes time. Sometimes a *long* time.
const TIMEOUT = 30000
describe('Plugin', () => {
let tracer
describe('google-cloud-pubsub', function () {
this.timeout(TIMEOUT)
before(() => {
process.env.PUBSUB_EMULATOR_HOST = 'localhost:8081'
})
after(() => {
delete process.env.PUBSUB_EMULATOR_HOST
})
afterEach(() => {
return agent.close({ ritmReset: false })
})
withVersions('google-cloud-pubsub', '@google-cloud/pubsub', version => {
let pubsub
let project
let topicName
let resource
let v1
let gax
describe('without configuration', () => {
beforeEach(() => {
return agent.load('google-cloud-pubsub')
})
beforeEach(() => {
tracer = require('../../dd-trace')
gax = require(`../../../versions/google-gax@3.5.7`).get()
const lib = require(`../../../versions/@google-cloud/pubsub@${version}`).get()
project = getProjectId()
topicName = getTopic()
resource = `projects/${project}/topics/${topicName}`
v1 = lib.v1
pubsub = new lib.PubSub({ projectId: project })
})
describe('createTopic', () => {
it('should be instrumented', async () => {
const expectedSpanPromise = expectSpanWithDefaults({
meta: {
'pubsub.method': 'createTopic',
'span.kind': 'client',
'component': 'google-cloud-pubsub'
}
})
await pubsub.createTopic(topicName)
return expectedSpanPromise
})
it('should be instrumented when using the internal API', async () => {
const publisher = new v1.PublisherClient({
grpc: gax.grpc,
projectId: project,
servicePath: 'localhost',
port: 8081,
sslCreds: gax.grpc.credentials.createInsecure()
}, gax)
const expectedSpanPromise = expectSpanWithDefaults({
meta: {
'pubsub.method': 'createTopic',
'span.kind': 'client',
'component': 'google-cloud-pubsub'
}
})
const name = `projects/${project}/topics/${topicName}`
const promise = publisher.createTopic({ name })
await promise
return expectedSpanPromise
})
it('should be instrumented w/ error', async () => {
const expectedSpanPromise = expectSpanWithDefaults({
error: 1,
meta: {
'pubsub.method': 'createTopic',
'component': 'google-cloud-pubsub'
}
})
const publisher = new v1.PublisherClient({ projectId: project })
const name = `projects/${project}/topics/${topicName}`
try {
await publisher.createTopic({ name })
} catch (e) {
// this is just to prevent mocha from crashing
}
return expectedSpanPromise
})
it('should propagate context', () => {
const firstSpan = tracer.scope().active()
return pubsub.createTopic(topicName)
.then(() => {
expect(tracer.scope().active()).to.equal(firstSpan)
})
})
})
describe('publish', () => {
it('should be instrumented', async () => {
const expectedSpanPromise = expectSpanWithDefaults({
meta: {
'pubsub.topic': resource,
'pubsub.method': 'publish',
'span.kind': 'producer',
'component': 'google-cloud-pubsub'
}
})
const [topic] = await pubsub.createTopic(topicName)
await publish(topic, { data: Buffer.from('hello') })
return expectedSpanPromise
})
it('should propagate context', () => {
const firstSpan = tracer.scope().active()
return pubsub.createTopic(topicName)
.then(([topic]) =>
publish(topic, { data: Buffer.from('hello') })
)
.then(() => {
expect(tracer.scope().active()).to.equal(firstSpan)
})
})
})
describe('onmessage', () => {
it('should be instrumented', async () => {
const expectedSpanPromise = expectSpanWithDefaults({
name: 'pubsub.receive',
type: 'worker',
meta: {
'component': 'google-cloud-pubsub',
'span.kind': 'consumer',
'pubsub.topic': resource
},
metrics: {
'pubsub.ack': 1
}
})
const [topic] = await pubsub.createTopic(topicName)
const [sub] = await topic.createSubscription('foo')
sub.on('message', msg => msg.ack())
await publish(topic, { data: Buffer.from('hello') })
return expectedSpanPromise
})
it('should give the current span a parentId from the sender', async () => {
const expectedSpanPromise = expectSpanWithDefaults({
name: 'pubsub.receive',
meta: { 'span.kind': 'consumer' }
})
const [topic] = await pubsub.createTopic(topicName)
const [sub] = await topic.createSubscription('foo')
const rxPromise = new Promise((resolve, reject) => {
sub.on('message', msg => {
const receiverSpanContext = tracer.scope().active()._spanContext
try {
expect(receiverSpanContext._parentId).to.be.an('object')
resolve()
msg.ack()
} catch (e) {
reject(e)
}
})
})
await publish(topic, { data: Buffer.from('hello') })
await rxPromise
return expectedSpanPromise
})
it('should be instrumented w/ error', async () => {
const error = new Error('bad')
const expectedSpanPromise = expectSpanWithDefaults({
name: 'pubsub.receive',
error: 1,
meta: {
[ERROR_MESSAGE]: error.message,
[ERROR_TYPE]: error.name,
[ERROR_STACK]: error.stack,
'component': 'google-cloud-pubsub'
}
})
const [topic] = await pubsub.createTopic(topicName)
const [sub] = await topic.createSubscription('foo')
const emit = sub.emit
sub.emit = function emitWrapped (name) {
let err
try {
return emit.apply(this, arguments)
} catch (e) {
err = e
} finally {
if (name === 'message') {
expect(err).to.equal(error)
}
}
}
sub.on('message', msg => {
try {
throw error
} finally {
msg.ack()
}
})
await publish(topic, { data: Buffer.from('hello') })
return expectedSpanPromise
})
})
describe('when disabled', () => {
beforeEach(() => {
tracer.use('google-cloud-pubsub', false)
})
afterEach(() => {
tracer.use('google-cloud-pubsub', true)
})
it('should work normally', async () => {
await pubsub.createTopic(topicName)
})
})
})
describe('with configuration', () => {
beforeEach(() => {
return agent.load('google-cloud-pubsub', {
service: 'a_test_service'
})
})
beforeEach(() => {
tracer = require('../../dd-trace')
const { PubSub } = require(`../../../versions/@google-cloud/pubsub@${version}`).get()
project = getProjectId()
topicName = getTopic()
resource = `projects/${project}/topics/${topicName}`
pubsub = new PubSub({ projectId: project })
})
describe('createTopic', () => {
it('should be instrumented', async () => {
const expectedSpanPromise = expectSpanWithDefaults({
service: 'a_test_service',
meta: { 'pubsub.method': 'createTopic' }
})
await pubsub.createTopic(topicName)
return expectedSpanPromise
})
})
})
function expectSpanWithDefaults (expected) {
const prefixedResource = [expected.meta['pubsub.method'], resource].filter(x => x).join(' ')
const service = expected.meta['pubsub.method'] ? 'test-pubsub' : 'test'
expected = withDefaults({
name: 'pubsub.request',
resource: prefixedResource,
service,
error: 0,
meta: {
'component': 'google-cloud-pubsub',
'gcloud.project_id': project
}
}, expected)
return expectSomeSpan(agent, expected, { timeoutMs: TIMEOUT })
}
})
})
})
function getProjectId () {
return `test-project-${id()}`
}
function getTopic () {
return `test-topic-${id()}`
}
function publish (topic, options) {
if (topic.publishMessage) {
return topic.publishMessage(options)
} else {
return topic.publish(options.data)
}
}