Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Added otel compliant attributes for database spans #2173

Merged
merged 3 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, true)
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, true)
agentAttributes.port_path_or_id = null
}

Expand Down
8 changes: 6 additions & 2 deletions 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 @@ -261,7 +263,6 @@ tap.test('fromSegment()', (t) => {
// Should have not http properties.
const hasOwnAttribute = Object.hasOwnProperty.bind(attributes)
t.notOk(hasOwnAttribute('http.url'))
t.notOk(hasOwnAttribute('server.address'))
t.notOk(hasOwnAttribute('http.method'))
t.notOk(hasOwnAttribute('http.request.method'))

Expand All @@ -270,6 +271,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
8 changes: 6 additions & 2 deletions 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 @@ -249,7 +251,6 @@ tap.test('fromSegment()', (t) => {
// Should have not http properties.
const hasOwnAttribute = Object.hasOwnProperty.bind(agentAttributes)
t.notOk(hasOwnAttribute('http.url'))
t.notOk(hasOwnAttribute('server.address'))
t.notOk(hasOwnAttribute('http.method'))
t.notOk(hasOwnAttribute('http.request.method'))

Expand All @@ -258,6 +259,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
Loading