Skip to content

Commit 338c4b4

Browse files
committed
ws plugin handles handshake
1 parent 57da289 commit 338c4b4

File tree

8 files changed

+145
-4
lines changed

8 files changed

+145
-4
lines changed

packages/datadog-instrumentations/src/helpers/hooks.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,5 +132,6 @@ module.exports = {
132132
vm: () => require('../vm'),
133133
when: () => require('../when'),
134134
winston: () => require('../winston'),
135-
workerpool: () => require('../mocha')
135+
workerpool: () => require('../mocha'),
136+
ws: () => require('../ws')
136137
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
'use strict'
2+
3+
const {
4+
addHook,
5+
channel,
6+
AsyncResource
7+
} = require('./helpers/instrument')
8+
const shimmer = require('../../datadog-shimmer')
9+
10+
const tracingChannel = require('dc-polyfill').tracingChannel
11+
const ch = tracingChannel('ws:client:connect')
12+
13+
function createWrapRequest (ws, options) {
14+
return function wrapRequest (request) {
15+
return function (headers) {
16+
if (!ch.start.hasSubscribers) return request.apply(this, arguments)
17+
18+
const ctx = { headers, ws, options }
19+
20+
return ch.tracePromise(() => request.call(this), ctx)
21+
}
22+
}
23+
}
24+
25+
function wrapHandleUpgrade (handleUpgrade) {
26+
return function () {
27+
if (!ch.start.hasSubscribers) return handleUpgrade.apply(this, arguments)
28+
29+
const [req, socket, head, cb] = arguments
30+
const ctx = { req }
31+
32+
return ch.tracePromise(() => {
33+
handleUpgrade.call(this, req, socket, head, cb)
34+
}, ctx)
35+
}
36+
}
37+
38+
addHook({
39+
name: 'ws',
40+
file: 'lib/websocket-server.js'
41+
}, ws => {
42+
shimmer.wrap(ws.prototype, 'handleUpgrade', wrapHandleUpgrade)
43+
44+
return ws
45+
})
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
'use strict'
2+
3+
const TracingPlugin = require('../../dd-trace/src/plugins/tracing.js')
4+
5+
class WSPlugin extends TracingPlugin {
6+
static get id () { return 'ws' }
7+
static get prefix () { return 'tracing:ws:client:connect' }
8+
static get type () { return 'websocket' }
9+
static get kind () { return 'consumer' }
10+
11+
bindStart (message) {
12+
const req = message.req
13+
14+
const options = {}
15+
const headers = Object.entries(req.headers)
16+
options.headers = Object.fromEntries(headers)
17+
options.method = req.method
18+
19+
message.args = { options }
20+
21+
const span = this.startSpan(this.operationName(), {
22+
meta: {
23+
service: this.serviceName({ pluginConfig: this.config }),
24+
'span.type': 'ws',
25+
'http.upgraded': 'websocket'
26+
27+
}
28+
29+
}, true)
30+
message.span = span
31+
// message.parentStore = store
32+
message.currentStore = { span }
33+
34+
return message.currentStore
35+
}
36+
37+
asyncStart (ctx) {
38+
console.log('ctx', ctx)
39+
ctx?.currentStore?.span.finish()
40+
return ctx.parentStore
41+
}
42+
// finish ({ req, res, span }) {
43+
// console.log('arguments', arguments)
44+
// if (!span) return
45+
// console.log('span')
46+
// span.finish()
47+
// }
48+
49+
// asyncEnd (message) {
50+
// console.log('async end')
51+
// message.res = message.result
52+
// return this.finish(message)
53+
// }
54+
}
55+
56+
module.exports = WSPlugin

packages/dd-trace/src/plugins/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,5 +97,6 @@ module.exports = {
9797
get sharedb () { return require('../../../datadog-plugin-sharedb/src') },
9898
get tedious () { return require('../../../datadog-plugin-tedious/src') },
9999
get undici () { return require('../../../datadog-plugin-undici/src') },
100-
get winston () { return require('../../../datadog-plugin-winston/src') }
100+
get winston () { return require('../../../datadog-plugin-winston/src') },
101+
get ws () { return require('../../../datadog-plugin-ws/src') }
101102
}

packages/dd-trace/src/service-naming/schemas/v0/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ const storage = require('./storage')
44
const graphql = require('./graphql')
55
const web = require('./web')
66
const serverless = require('./serverless')
7+
const websocket = require('./websocket')
78

8-
module.exports = new SchemaDefinition({ messaging, storage, web, graphql, serverless })
9+
module.exports = new SchemaDefinition({ messaging, storage, web, graphql, serverless, websocket })
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const { identityService } = require('../util')
2+
3+
const websocket = {
4+
consumer: {
5+
ws: {
6+
opName: () => 'ws.request',
7+
serviceName: identityService
8+
}
9+
},
10+
producer: {
11+
ws: {
12+
opName: () => 'ws.request',
13+
serviceName: identityService
14+
}
15+
}
16+
}
17+
18+
module.exports = websocket

packages/dd-trace/src/service-naming/schemas/v1/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ const storage = require('./storage')
44
const graphql = require('./graphql')
55
const web = require('./web')
66
const serverless = require('./serverless')
7+
const websocket = require('./websocket')
78

8-
module.exports = new SchemaDefinition({ messaging, storage, web, graphql, serverless })
9+
module.exports = new SchemaDefinition({ messaging, storage, web, graphql, serverless, websocket })
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const { identityService } = require('../util')
2+
3+
const websocket = {
4+
consumer: {
5+
ws: {
6+
opName: () => 'ws.request',
7+
serviceName: identityService
8+
}
9+
},
10+
producer: {
11+
ws: {
12+
opName: () => 'ws.request',
13+
serviceName: identityService
14+
}
15+
}
16+
}
17+
18+
module.exports = websocket

0 commit comments

Comments
 (0)