Skip to content

Commit 3fdcab4

Browse files
authored
chore: Added otel compliant attributes for database spans (#2173)
Signed-off-by: mrickard <maurice@mauricerickard.com>
1 parent b0a3e6d commit 3fdcab4

4 files changed

Lines changed: 18 additions & 4 deletions

File tree

lib/spans/span-event.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ class DatastoreSpanEvent extends SpanEvent {
231231

232232
if (attributes.product) {
233233
this.intrinsics.component = attributes.product
234+
this.addAttribute('db.system', attributes.product)
234235
attributes.product = null
235236
}
236237

@@ -260,10 +261,12 @@ class DatastoreSpanEvent extends SpanEvent {
260261

261262
if (attributes.host) {
262263
this.addAttribute('peer.hostname', attributes.host)
264+
this.addAttribute('server.address', attributes.host)
263265

264266
if (attributes.port_path_or_id) {
265267
const address = `${attributes.host}:${attributes.port_path_or_id}`
266268
this.addAttribute('peer.address', address)
269+
this.addAttribute('server.port', attributes.port_path_or_id, true)
267270
attributes.port_path_or_id = null
268271
}
269272
attributes.host = null

lib/spans/streaming-span-event.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ class StreamingDatastoreSpanEvent extends StreamingSpanEvent {
243243

244244
if (agentAttributes.product) {
245245
this.addIntrinsicAttribute('component', agentAttributes.product)
246+
this.addAgentAttribute('db.system', agentAttributes.product)
246247
agentAttributes.product = null
247248
}
248249

@@ -272,10 +273,12 @@ class StreamingDatastoreSpanEvent extends StreamingSpanEvent {
272273

273274
if (agentAttributes.host) {
274275
this.addAgentAttribute('peer.hostname', agentAttributes.host)
276+
this.addAgentAttribute('server.address', agentAttributes.host)
275277

276278
if (agentAttributes.port_path_or_id) {
277279
const address = `${agentAttributes.host}:${agentAttributes.port_path_or_id}`
278280
this.addAgentAttribute('peer.address', address)
281+
this.addAgentAttribute('server.port', agentAttributes.port_path_or_id, true)
279282
agentAttributes.port_path_or_id = null
280283
}
281284

test/unit/spans/span-event.test.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ tap.test('fromSegment()', (t) => {
115115
// Should have no datastore properties.
116116
t.notOk(hasOwnAttribute('db.statement'))
117117
t.notOk(hasOwnAttribute('db.instance'))
118+
t.notOk(hasOwnAttribute('db.system'))
118119
t.notOk(hasOwnAttribute('peer.hostname'))
119120
t.notOk(hasOwnAttribute('peer.address'))
120121

@@ -182,6 +183,7 @@ tap.test('fromSegment()', (t) => {
182183
const hasOwnAttribute = Object.hasOwnProperty.bind(attributes)
183184
t.notOk(hasOwnAttribute('db.statement'))
184185
t.notOk(hasOwnAttribute('db.instance'))
186+
t.notOk(hasOwnAttribute('db.system'))
185187
t.notOk(hasOwnAttribute('peer.hostname'))
186188
t.notOk(hasOwnAttribute('peer.address'))
187189

@@ -191,7 +193,7 @@ tap.test('fromSegment()', (t) => {
191193
})
192194
})
193195

194-
t.test('should create an datastore span with an datastore segment', (t) => {
196+
t.test('should create a datastore span with a datastore segment', (t) => {
195197
agent.config.transaction_tracer.record_sql = 'raw'
196198

197199
const shim = new DatastoreShim(agent, 'test-data-store')
@@ -261,7 +263,6 @@ tap.test('fromSegment()', (t) => {
261263
// Should have not http properties.
262264
const hasOwnAttribute = Object.hasOwnProperty.bind(attributes)
263265
t.notOk(hasOwnAttribute('http.url'))
264-
t.notOk(hasOwnAttribute('server.address'))
265266
t.notOk(hasOwnAttribute('http.method'))
266267
t.notOk(hasOwnAttribute('http.request.method'))
267268

@@ -270,6 +271,9 @@ tap.test('fromSegment()', (t) => {
270271
t.equal(attributes['db.collection'], 'my-collection')
271272
t.equal(attributes['peer.hostname'], 'my-db-host')
272273
t.equal(attributes['peer.address'], 'my-db-host:/path/to/db.sock')
274+
t.equal(attributes['db.system'], 'TestStore') // same as intrinsics.component
275+
t.equal(attributes['server.address'], 'my-db-host')
276+
t.equal(attributes['server.port'], '/path/to/db.sock')
273277

274278
const statement = attributes['db.statement']
275279
t.ok(statement)

test/unit/spans/streaming-span-event.test.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ tap.test('fromSegment()', (t) => {
107107
// Should have no datastore properties.
108108
t.notOk(hasOwnAttribute('db.statement'))
109109
t.notOk(hasOwnAttribute('db.instance'))
110+
t.notOk(hasOwnAttribute('db.system'))
110111
t.notOk(hasOwnAttribute('peer.hostname'))
111112
t.notOk(hasOwnAttribute('peer.address'))
112113

@@ -167,6 +168,7 @@ tap.test('fromSegment()', (t) => {
167168
const hasOwnAttribute = Object.hasOwnProperty.bind(agentAttributes)
168169
t.notOk(hasOwnAttribute('db.statement'))
169170
t.notOk(hasOwnAttribute('db.instance'))
171+
t.notOk(hasOwnAttribute('db.system'))
170172
t.notOk(hasOwnAttribute('peer.hostname'))
171173
t.notOk(hasOwnAttribute('peer.address'))
172174

@@ -176,7 +178,7 @@ tap.test('fromSegment()', (t) => {
176178
})
177179
})
178180

179-
t.test('should create an datastore span with an datastore segment', (t) => {
181+
t.test('should create a datastore span with a datastore segment', (t) => {
180182
agent.config.transaction_tracer.record_sql = 'raw'
181183

182184
const shim = new DatastoreShim(agent, 'test-data-store')
@@ -249,7 +251,6 @@ tap.test('fromSegment()', (t) => {
249251
// Should have not http properties.
250252
const hasOwnAttribute = Object.hasOwnProperty.bind(agentAttributes)
251253
t.notOk(hasOwnAttribute('http.url'))
252-
t.notOk(hasOwnAttribute('server.address'))
253254
t.notOk(hasOwnAttribute('http.method'))
254255
t.notOk(hasOwnAttribute('http.request.method'))
255256

@@ -258,6 +259,9 @@ tap.test('fromSegment()', (t) => {
258259
t.same(agentAttributes['db.collection'], { [STRING_TYPE]: 'my-collection' })
259260
t.same(agentAttributes['peer.hostname'], { [STRING_TYPE]: 'my-db-host' })
260261
t.same(agentAttributes['peer.address'], { [STRING_TYPE]: 'my-db-host:/path/to/db.sock' })
262+
t.same(agentAttributes['db.system'], { [STRING_TYPE]: 'TestStore' }) // same as intrinsics.component
263+
t.same(agentAttributes['server.address'], { [STRING_TYPE]: 'my-db-host' })
264+
t.same(agentAttributes['server.port'], { [STRING_TYPE]: '/path/to/db.sock' })
261265

262266
const statement = agentAttributes['db.statement']
263267
t.ok(statement)

0 commit comments

Comments
 (0)