Skip to content

Commit

Permalink
address feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
khanayan123 committed Jun 10, 2024
1 parent 81c43a3 commit dffe200
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
11 changes: 4 additions & 7 deletions packages/dd-trace/src/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,10 @@ function extractSpanEvents (trace, span) {
const events = []
if (span._events) {
for (const event of span._events) {
const formattedEvent = {}

formattedEvent.name = event.name
formattedEvent.time_unix_nano = Math.round(event.startTime * 1e6)

if (event.attributes && Object.keys(event.attributes).length > 0) {
formattedEvent.attributes = event.attributes
const formattedEvent = {
name: event.name,
time_unix_nano: Math.round(event.startTime * 1e6),
attributes: event.attributes && Object.keys(event.attributes).length > 0 ? event.attributes : undefined
}

events.push(formattedEvent)
Expand Down
40 changes: 39 additions & 1 deletion packages/dd-trace/test/opentelemetry/span.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

require('../setup/tap')

const { expect } = require('chai')
const sinon = require('sinon')
const { performance } = require('perf_hooks')
const { timeOrigin } = performance
const { timeInputToHrTime } = require('@opentelemetry/core')

const { expect } = require('chai')
const tracer = require('../../').init()

const api = require('@opentelemetry/api')
Expand Down Expand Up @@ -368,6 +372,40 @@ describe('OTel Span', () => {
}])
})

it('should record exception without passing in time', () => {
const stub = sinon.stub(performance, 'now').returns(60000)
const span = makeSpan('name')

class TestError extends Error {
constructor () {
super('test message')
}
}

const time = timeInputToHrTime(60000 + timeOrigin)
const timeInMilliseconds = time[0] * 1e3 + time[1] / 1e6

const error = new TestError()
span.recordException(error)

const { _tags } = span._ddSpan.context()
expect(_tags).to.have.property(ERROR_TYPE, error.name)
expect(_tags).to.have.property(ERROR_MESSAGE, error.message)
expect(_tags).to.have.property(ERROR_STACK, error.stack)

const events = span._ddSpan._events
expect(events).to.have.lengthOf(1)
expect(events).to.deep.equal([{
name: error.name,
attributes: {
'exception.message': error.message,
'exception.stacktrace': error.stack
},
startTime: timeInMilliseconds
}])
stub.restore()
})

it('should not set status on already ended spans', () => {
const span = makeSpan('name')
span.end()
Expand Down

0 comments on commit dffe200

Please sign in to comment.