Skip to content

Commit eb37aab

Browse files
authored
fix: Ws resource now uses the request route (#6587)
1 parent 0b25422 commit eb37aab

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

packages/datadog-plugin-ws/src/server.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ class WSServerPlugin extends TracingPlugin {
2121

2222
const protocol = `${getRequestProtocol(req)}:`
2323
const host = options.headers.host
24-
const path = req.url
25-
const uri = `${protocol}//${host}${path}`
24+
const url = req.url
25+
const indexOfParam = url.indexOf('?')
26+
const route = indexOfParam === -1 ? url : url.slice(0, indexOfParam)
27+
const uri = `${protocol}//${host}${route}`
2628

2729
ctx.args = { options }
2830

@@ -34,7 +36,7 @@ class WSServerPlugin extends TracingPlugin {
3436
'http.upgraded': 'websocket',
3537
'http.method': options.method,
3638
'http.url': uri,
37-
'resource.name': `${options.method} ${path}`,
39+
'resource.name': `${options.method} ${route}`,
3840
'span.kind': 'server'
3941

4042
}

packages/datadog-plugin-ws/test/index.spec.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ describe('Plugin', () => {
1111
let clientPort = 6015
1212
let client
1313
let messageReceived
14+
let route
1415

1516
describe('ws', () => {
1617
withVersions('ws', 'ws', '>=8.0.0', version => {
1718
describe('when using WebSocket', () => {
19+
route = 'test'
1820
beforeEach(async () => {
1921
await agent.load(['ws'], [{
2022
service: 'some',
@@ -24,7 +26,7 @@ describe('Plugin', () => {
2426

2527
wsServer = new WebSocket.Server({ port: clientPort })
2628

27-
client = new WebSocket(`ws://localhost:${clientPort}`)
29+
client = new WebSocket(`ws://localhost:${clientPort}/${route}?active=true`)
2830
})
2931

3032
afterEach(async () => {
@@ -99,6 +101,7 @@ describe('Plugin', () => {
99101
})
100102
agent.assertSomeTraces(traces => {
101103
expect(traces[0][0]).to.have.property('name', 'websocket.receive')
104+
expect(traces[0][0]).to.have.property('resource', `websocket /${route}`)
102105
})
103106
.then(done)
104107
.catch(done)
@@ -132,7 +135,7 @@ describe('Plugin', () => {
132135

133136
wsServer = new WebSocket.Server({ port: clientPort })
134137

135-
client = new WebSocket(`ws://localhost:${clientPort}`)
138+
client = new WebSocket(`ws://localhost:${clientPort}/${route}?active=true`)
136139
})
137140

138141
afterEach(async () => {
@@ -156,17 +159,16 @@ describe('Plugin', () => {
156159
wsServer.on('connection', (ws) => {
157160
ws.send('test message')
158161
})
159-
messageReceived = false
160162

161163
client.on('message', (data) => {
162164
expect(data.toString()).to.equal('test message')
163-
messageReceived = true
164165
})
165166

166167
return agent.assertSomeTraces(traces => {
167-
expect(traces[0][0]).to.have.property('service', 'custom-ws-service')
168+
expect(traces[0][0]).to.have.property('resource', `websocket /${route}`)
168169
expect(traces[0][0]).to.have.property('name', 'websocket.send')
169170
expect(traces[0][0]).to.have.property('type', 'websocket')
171+
expect(traces[0][0]).to.have.property('service', 'custom-ws-service')
170172
})
171173
})
172174

@@ -177,7 +179,7 @@ describe('Plugin', () => {
177179
})
178180
wsServer.on('message', (data) => {
179181
expect(data.toString()).to.equal('test message')
180-
expect(messageReceived).prototype.equal(true)
182+
expect(messageReceived).to.equal(true)
181183
})
182184

183185
client.on('message', (data) => {

0 commit comments

Comments
 (0)