Skip to content

Commit 27e4080

Browse files
committed
feat(generic): add generic protocol support
1 parent 61f0b51 commit 27e4080

File tree

9 files changed

+95
-36
lines changed

9 files changed

+95
-36
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

lib/agent/index.js

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -165,13 +165,17 @@ Agent.prototype.serverReceive = function (data) {
165165
span.events.push({
166166
time: data.time || microtime.now(),
167167
type: Agent.SERVER_RECV,
168-
data: {
169-
rpcId: parentCommId,
170-
endpoint: data.url,
171-
method: data.method,
172-
parent: parentServiceId,
173-
originTime: data.originTime
174-
}
168+
data: assign({}, data.protocolData
169+
? data.protocolData
170+
: {}, {
171+
host: data.host,
172+
protocol: data.protocol,
173+
rpcId: parentCommId,
174+
endpoint: data.url,
175+
method: data.method,
176+
parent: parentServiceId,
177+
originTime: data.originTime
178+
})
175179
})
176180
}
177181

@@ -187,10 +191,14 @@ Agent.prototype.serverSend = function (data) {
187191
span.events.push({
188192
time: data.time || microtime.now(),
189193
type: Agent.SERVER_SEND,
190-
data: {
191-
rpcId: parentCommId,
192-
statusCode: data.statusCode
193-
}
194+
data: assign({}, data.protocolData
195+
? data.protocolData
196+
: {}, {
197+
protocol: data.protocol,
198+
rpcId: parentCommId,
199+
status: data.status === consts.EDGE_STATUS.OK ? 'ok' : 'bad',
200+
statusDescription: data.statusDescription
201+
})
194202
})
195203

196204
span.isForceSampled = span.isForceSampled || data.mustCollect === consts.MUST_COLLECT.ERROR
@@ -230,12 +238,15 @@ Agent.prototype.clientSend = function (data) {
230238
} else {
231239
assign(event, {
232240
type: Agent.CLIENT_SEND,
233-
data: {
234-
rpcId: data.childCommId,
235-
host: data.host,
236-
endpoint: data.url,
237-
method: data.method
238-
}
241+
data: assign({}, data.protocolData
242+
? data.protocolData
243+
: {}, {
244+
protocol: data.protocol,
245+
rpcId: data.childCommId,
246+
host: data.host,
247+
endpoint: data.url,
248+
method: data.method
249+
})
239250
})
240251
}
241252

@@ -279,10 +290,14 @@ Agent.prototype.clientReceive = function (data) {
279290
span.events.push({
280291
time: data.time,
281292
type: Agent.CLIENT_RECV,
282-
data: {
283-
rpcId: data.childCommId,
284-
statusCode: data.statusCode
285-
}
293+
data: assign({}, data.protocolData
294+
? data.protocolData
295+
: {}, {
296+
protocol: data.protocol,
297+
rpcId: data.childCommId,
298+
status: data.status === consts.EDGE_STATUS.OK ? 'ok' : 'bad',
299+
statusDescription: data.statusDescription
300+
})
286301
})
287302
}
288303

@@ -356,6 +371,7 @@ Agent.prototype.report = function (name, data, error) {
356371
time: microtime.now()
357372
}
358373
if (error) {
374+
span.isForceSampled = true
359375
assign(event, {
360376
type: Agent.ERROR,
361377
data: {

lib/agent/index.spec.js

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ describe('The Trace agent', function () {
8787
type: 'sr',
8888
time: time,
8989
data: {
90+
host: host,
91+
protocol: 'http',
9092
rpcId: parentCommId,
9193
endpoint: url,
9294
method: 'GET',
@@ -182,6 +184,8 @@ describe('The Trace agent', function () {
182184
type: 'sr',
183185
time: time,
184186
data: {
187+
host: host,
188+
protocol: 'http',
185189
rpcId: parentCommId,
186190
endpoint: url,
187191
method: 'GET',
@@ -198,6 +202,7 @@ describe('The Trace agent', function () {
198202
requestId: requestId,
199203
parentCommId: parentCommId,
200204
parentServiceId: parentServiceId,
205+
protocol: 'http',
201206
url: url,
202207
host: host,
203208
method: 'GET'
@@ -207,7 +212,10 @@ describe('The Trace agent', function () {
207212
requestId: requestId,
208213
parentCommId: parentCommId,
209214
responseTime: responseTime,
210-
statusCode: statusCode,
215+
protocol: 'http',
216+
protocolData: { statusCode: statusCode },
217+
statusDescription: String(statusCode),
218+
status: 0,
211219
mustCollect: '1'
212220
})
213221

@@ -221,6 +229,8 @@ describe('The Trace agent', function () {
221229
type: 'sr',
222230
time: time,
223231
data: {
232+
host: host,
233+
protocol: 'http',
224234
rpcId: parentCommId,
225235
endpoint: url,
226236
method: 'GET',
@@ -231,7 +241,10 @@ describe('The Trace agent', function () {
231241
type: 'ss',
232242
time: time,
233243
data: {
244+
protocol: 'http',
245+
status: 'ok',
234246
rpcId: parentCommId,
247+
statusDescription: String(statusCode),
235248
statusCode: statusCode
236249
}
237250
}]
@@ -245,6 +258,7 @@ describe('The Trace agent', function () {
245258
parentCommId: parentCommId,
246259
parentServiceId: parentServiceId,
247260
url: url,
261+
protocol: 'http',
248262
host: host,
249263
method: 'GET'
250264
})
@@ -253,7 +267,8 @@ describe('The Trace agent', function () {
253267
childCommId: childCommId,
254268
method: 'POST',
255269
host: 'remote-host',
256-
url: '/products'
270+
url: '/products',
271+
protocol: 'http'
257272
})
258273

259274
expect(agent.totalRequestCount).to.eql(1)
@@ -266,6 +281,8 @@ describe('The Trace agent', function () {
266281
type: 'sr',
267282
time: time,
268283
data: {
284+
host: host,
285+
protocol: 'http',
269286
rpcId: parentCommId,
270287
endpoint: url,
271288
method: 'GET',
@@ -276,6 +293,7 @@ describe('The Trace agent', function () {
276293
type: 'cs',
277294
time: time,
278295
data: {
296+
protocol: 'http',
279297
rpcId: childCommId,
280298
method: 'POST',
281299
host: 'remote-host',
@@ -288,6 +306,7 @@ describe('The Trace agent', function () {
288306
it('does report', function () {
289307
agent.sampleRate = 1
290308
agent.serverReceive({
309+
protocol: 'http',
291310
requestId: requestId,
292311
parentCommId: parentCommId,
293312
parentServiceId: parentServiceId,
@@ -308,6 +327,8 @@ describe('The Trace agent', function () {
308327
type: 'sr',
309328
time: time,
310329
data: {
330+
host: host,
331+
protocol: 'http',
311332
rpcId: parentCommId,
312333
endpoint: url,
313334
method: 'GET',
@@ -331,6 +352,7 @@ describe('The Trace agent', function () {
331352
it('does report error', function () {
332353
agent.sampleRate = 1
333354
agent.serverReceive({
355+
protocol: 'http',
334356
requestId: requestId,
335357
parentCommId: parentCommId,
336358
parentServiceId: parentServiceId,
@@ -346,12 +368,14 @@ describe('The Trace agent', function () {
346368
expect(agent.reservoirSampler.getItems()).to.eql([])
347369
expect(agent.partials[requestId]).to.eql({
348370
requestId: requestId,
349-
isForceSampled: false,
371+
isForceSampled: true,
350372
isSampled: false,
351373
events: [{
352374
type: 'sr',
353375
time: time,
354376
data: {
377+
host: host,
378+
protocol: 'http',
355379
rpcId: parentCommId,
356380
endpoint: url,
357381
method: 'GET',
@@ -402,6 +426,7 @@ describe('The Trace agent', function () {
402426
agent.reservoirSampler.addReturnsSuccess(1)
403427
}
404428
agent.serverReceive({
429+
protocol: 'http',
405430
requestId: requestId,
406431
parentCommId: parentCommId,
407432
parentServiceId: parentServiceId,
@@ -411,10 +436,13 @@ describe('The Trace agent', function () {
411436
})
412437

413438
agent.serverSend({
439+
protocol: 'http',
414440
requestId: requestId,
415441
parentCommId: parentCommId,
416442
responseTime: responseTime,
417-
statusCode: statusCode,
443+
status: 0,
444+
statusDescription: String(statusCode),
445+
protocolData: { statusCode: statusCode },
418446
mustCollect: '1'
419447
})
420448

lib/instrumentations/core/http/request.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,15 +126,19 @@ function wrapRequest (originalHttpRequest, agent, mustCollectStore) {
126126
requestId: requestId,
127127
childCommId: childCommId,
128128
protocol: consts.PROTOCOLS.HTTP,
129+
protocolData: {
130+
statusCode: incomingMessage.statusCode
131+
},
129132
host: requestParams.host,
130133
url: util.formatDataUrl(requestParams.path),
131-
statusCode: incomingMessage.statusCode,
134+
132135
mustCollect: mustCollectStore[requestId],
133136
targetServiceKey: incomingMessage.headers['x-parent'],
134137
responseTime: clientReceiveTime - clientSendTime,
135138
networkDelayIncoming: networkDelayIncoming,
136139
networkDelayOutgoing: networkDelayOutgoing,
137-
status: incomingMessage.statusCode > 399 ? consts.EDGE_STATUS.NOT_OK : consts.EDGE_STATUS.OK
140+
status: incomingMessage.statusCode > 399 ? consts.EDGE_STATUS.NOT_OK : consts.EDGE_STATUS.OK,
141+
statusDescription: String(incomingMessage.statusCode)
138142
}
139143

140144
agent.clientReceive(collectorDataBag)

lib/instrumentations/core/http/request.spec.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,13 @@ describe('The http.request wrapper module', function () {
106106
requestId: requestId,
107107
mustCollect: undefined,
108108
childCommId: childCommId,
109-
statusCode: 200,
109+
protocolData: { statusCode: 200 },
110110
url: '/',
111111
protocol: 'http',
112112
responseTime: 0,
113113
targetServiceKey: targetServiceKey,
114114
status: 0,
115+
statusDescription: '200',
115116
networkDelayIncoming: 10,
116117
networkDelayOutgoing: 20
117118
})

lib/instrumentations/core/http/server.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,11 @@ function wrapListener (listener, agent, mustCollectStore) {
113113
url: util.formatDataUrl(requestUrl),
114114
time: serverSendTime,
115115
protocol: consts.PROTOCOLS.HTTP,
116-
statusCode: response.statusCode,
116+
protocolData: {
117+
statusCode: response.statusCode
118+
},
119+
status: response.statusCode > 399 ? consts.EDGE_STATUS.NOT_OK : consts.EDGE_STATUS.OK,
120+
statusDescription: String(response.statusCode),
117121
responseTime: responseTime
118122
}
119123

lib/instrumentations/core/http/server.spec.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,9 @@ describe('The http.Server.prototype wrapper module', function () {
215215
requestId: requestId,
216216
mustCollect: undefined,
217217
parentCommId: undefined,
218-
statusCode: statusCode,
218+
status: 0,
219+
statusDescription: String(statusCode),
220+
protocolData: { statusCode: statusCode },
219221
time: 12345678,
220222
url: '/',
221223
responseTime: 0,
@@ -280,7 +282,9 @@ describe('The http.Server.prototype wrapper module', function () {
280282
host: 'localhost',
281283
requestId: requestId,
282284
parentCommId: parentCommId,
283-
statusCode: statusCode,
285+
status: 0,
286+
statusDescription: String(statusCode),
287+
protocolData: { statusCode: statusCode },
284288
mustCollect: consts.MUST_COLLECT.ERROR,
285289
time: 12345678,
286290
url: '/',

lib/instrumentations/utils/wrapQuery.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,11 @@ function wrapQuery (original, args, agent, params) {
1313
var disableCallback = _params.disableCallback
1414

1515
// custom error parsing depending on the instrumentation
16-
var parseError = _params.parseError || function (err) {
17-
return err ? 400 : 200
18-
}
16+
var parseError = _params.parseError || function () { }
1917

2018
var reportSend = function reportSend () {
2119
agent.clientSend({
20+
protocol: protocol,
2221
requestId: requestId,
2322
childCommId: childCommId,
2423
host: host,
@@ -42,7 +41,7 @@ function wrapQuery (original, args, agent, params) {
4241
mustCollect: err ? consts.MUST_COLLECT.ERROR : undefined,
4342
responseTime: clientReceiveTime - clientSendTime,
4443
status: err ? consts.EDGE_STATUS.NOT_OK : consts.EDGE_STATUS.OK,
45-
statusCode: parseError(err)
44+
statusDescription: parseError(err)
4645
})
4746
}
4847

lib/instrumentations/utils/wrapQuery.spec.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ describe('wrapQuery', function () {
1818
var query = this.sandbox.spy()
1919
var agent = fakeAgent(this.sandbox)
2020
wrapQuery(query, [], agent, {
21+
protocol: 'protocol',
2122
url: 'fakeUrl',
2223
host: 'fakeHost',
2324
parameter: 'fakeParam',
2425
method: 'fakeMethod'
2526
})
2627
expect(agent.clientSend).to.have.been.calledWith({
28+
protocol: 'protocol',
2729
host: 'fakeHost',
2830
requestId: 'fakeRequestId',
2931
method: 'fakeMethod',
@@ -85,7 +87,7 @@ describe('wrapQuery', function () {
8587
responseTime: 0,
8688
childCommId: 'fakeChildCommId',
8789
status: 0,
88-
statusCode: 200,
90+
statusDescription: undefined,
8991
time: 42,
9092
url: 'fakeUrl'
9193
})

0 commit comments

Comments
 (0)