Skip to content

Commit

Permalink
chore: Added otel compliant attributes for database spans
Browse files Browse the repository at this point in the history
Signed-off-by: mrickard <maurice@mauricerickard.com>
  • Loading branch information
mrickard committed Apr 30, 2024
1 parent b0a3e6d commit 004880f
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 2 deletions.
3 changes: 3 additions & 0 deletions lib/spans/span-event.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ class DatastoreSpanEvent extends SpanEvent {

if (attributes.product) {
this.intrinsics.component = attributes.product
this.addAttribute('db.system', attributes.product)
attributes.product = null
}

Expand Down Expand Up @@ -260,10 +261,12 @@ class DatastoreSpanEvent extends SpanEvent {

if (attributes.host) {
this.addAttribute('peer.hostname', attributes.host)
this.addAttribute('server.address', attributes.host)

if (attributes.port_path_or_id) {
const address = `${attributes.host}:${attributes.port_path_or_id}`
this.addAttribute('peer.address', address)
this.addAttribute('server.port', attributes.port_path_or_id)
attributes.port_path_or_id = null
}
attributes.host = null
Expand Down
3 changes: 3 additions & 0 deletions lib/spans/streaming-span-event.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ class StreamingDatastoreSpanEvent extends StreamingSpanEvent {

if (agentAttributes.product) {
this.addIntrinsicAttribute('component', agentAttributes.product)
this.addAgentAttribute('db.system', agentAttributes.product)
agentAttributes.product = null
}

Expand Down Expand Up @@ -272,10 +273,12 @@ class StreamingDatastoreSpanEvent extends StreamingSpanEvent {

if (agentAttributes.host) {
this.addAgentAttribute('peer.hostname', agentAttributes.host)
this.addAgentAttribute('server.address', agentAttributes.host)

if (agentAttributes.port_path_or_id) {
const address = `${agentAttributes.host}:${agentAttributes.port_path_or_id}`
this.addAgentAttribute('peer.address', address)
this.addAgentAttribute('server.port', agentAttributes.port_path_or_id)
agentAttributes.port_path_or_id = null
}

Expand Down
7 changes: 6 additions & 1 deletion test/unit/spans/span-event.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ tap.test('fromSegment()', (t) => {
// Should have no datastore properties.
t.notOk(hasOwnAttribute('db.statement'))
t.notOk(hasOwnAttribute('db.instance'))
t.notOk(hasOwnAttribute('db.system'))
t.notOk(hasOwnAttribute('peer.hostname'))
t.notOk(hasOwnAttribute('peer.address'))

Expand Down Expand Up @@ -182,6 +183,7 @@ tap.test('fromSegment()', (t) => {
const hasOwnAttribute = Object.hasOwnProperty.bind(attributes)
t.notOk(hasOwnAttribute('db.statement'))
t.notOk(hasOwnAttribute('db.instance'))
t.notOk(hasOwnAttribute('db.system'))
t.notOk(hasOwnAttribute('peer.hostname'))
t.notOk(hasOwnAttribute('peer.address'))

Expand All @@ -191,7 +193,7 @@ tap.test('fromSegment()', (t) => {
})
})

t.test('should create an datastore span with an datastore segment', (t) => {
t.test('should create a datastore span with a datastore segment', (t) => {
agent.config.transaction_tracer.record_sql = 'raw'

const shim = new DatastoreShim(agent, 'test-data-store')
Expand Down Expand Up @@ -270,6 +272,9 @@ tap.test('fromSegment()', (t) => {
t.equal(attributes['db.collection'], 'my-collection')
t.equal(attributes['peer.hostname'], 'my-db-host')
t.equal(attributes['peer.address'], 'my-db-host:/path/to/db.sock')
t.equal(attributes['db.system'], 'TestStore') // same as intrinsics.component
t.equal(attributes['server.address'], 'my-db-host')
t.equal(attributes['server.port'], '/path/to/db.sock')

const statement = attributes['db.statement']
t.ok(statement)
Expand Down
7 changes: 6 additions & 1 deletion test/unit/spans/streaming-span-event.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ tap.test('fromSegment()', (t) => {
// Should have no datastore properties.
t.notOk(hasOwnAttribute('db.statement'))
t.notOk(hasOwnAttribute('db.instance'))
t.notOk(hasOwnAttribute('db.system'))
t.notOk(hasOwnAttribute('peer.hostname'))
t.notOk(hasOwnAttribute('peer.address'))

Expand Down Expand Up @@ -167,6 +168,7 @@ tap.test('fromSegment()', (t) => {
const hasOwnAttribute = Object.hasOwnProperty.bind(agentAttributes)
t.notOk(hasOwnAttribute('db.statement'))
t.notOk(hasOwnAttribute('db.instance'))
t.notOk(hasOwnAttribute('db.system'))
t.notOk(hasOwnAttribute('peer.hostname'))
t.notOk(hasOwnAttribute('peer.address'))

Expand All @@ -176,7 +178,7 @@ tap.test('fromSegment()', (t) => {
})
})

t.test('should create an datastore span with an datastore segment', (t) => {
t.test('should create a datastore span with a datastore segment', (t) => {
agent.config.transaction_tracer.record_sql = 'raw'

const shim = new DatastoreShim(agent, 'test-data-store')
Expand Down Expand Up @@ -258,6 +260,9 @@ tap.test('fromSegment()', (t) => {
t.same(agentAttributes['db.collection'], { [STRING_TYPE]: 'my-collection' })
t.same(agentAttributes['peer.hostname'], { [STRING_TYPE]: 'my-db-host' })
t.same(agentAttributes['peer.address'], { [STRING_TYPE]: 'my-db-host:/path/to/db.sock' })
t.same(agentAttributes['db.system'], { [STRING_TYPE]: 'TestStore' }) // same as intrinsics.component
t.same(agentAttributes['server.address'], { [STRING_TYPE]: 'my-db-host' })
t.same(agentAttributes['server.port'], { [STRING_TYPE]: '/path/to/db.sock' })

const statement = agentAttributes['db.statement']
t.ok(statement)
Expand Down

0 comments on commit 004880f

Please sign in to comment.