From dffe20053ab0c34941d43b96aa24b10137e47c62 Mon Sep 17 00:00:00 2001 From: Ayan Khan Date: Mon, 10 Jun 2024 15:16:31 -0400 Subject: [PATCH] address feedback --- packages/dd-trace/src/format.js | 11 ++--- .../dd-trace/test/opentelemetry/span.spec.js | 40 ++++++++++++++++++- 2 files changed, 43 insertions(+), 8 deletions(-) diff --git a/packages/dd-trace/src/format.js b/packages/dd-trace/src/format.js index 85aa691f563..fcb2a07d01d 100644 --- a/packages/dd-trace/src/format.js +++ b/packages/dd-trace/src/format.js @@ -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) diff --git a/packages/dd-trace/test/opentelemetry/span.spec.js b/packages/dd-trace/test/opentelemetry/span.spec.js index ecbb5ec6372..ecdea99a1fa 100644 --- a/packages/dd-trace/test/opentelemetry/span.spec.js +++ b/packages/dd-trace/test/opentelemetry/span.spec.js @@ -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') @@ -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()