-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(opentelemetry): Set
response
context for http.server spans (#1…
…4634) This implements the `response` context for http.server spans (https://develop.sentry.dev/sdk/data-model/event-payloads/contexts/#response-context). I opted to not implement this for the browser, as we do not really expect server spans there. I added a test there anyhow to show the shape of the transaction event, so we can adjust this easier if we want in the future. Closes #14619 Closes #14634
- Loading branch information
Showing
5 changed files
with
201 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import { ATTR_HTTP_RESPONSE_STATUS_CODE } from '@opentelemetry/semantic-conventions'; | ||
import { SDK_VERSION, SEMANTIC_ATTRIBUTE_SENTRY_OP, startInactiveSpan } from '@sentry/core'; | ||
import { createTransactionForOtelSpan } from '../src/spanExporter'; | ||
import { cleanupOtel, mockSdkInit } from './helpers/mockSdkInit'; | ||
|
||
describe('createTransactionForOtelSpan', () => { | ||
beforeEach(() => { | ||
mockSdkInit({ | ||
enableTracing: true, | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
cleanupOtel(); | ||
}); | ||
|
||
it('works with a basic span', () => { | ||
const span = startInactiveSpan({ name: 'test', startTime: 1733821670000 }); | ||
span.end(1733821672000); | ||
|
||
const event = createTransactionForOtelSpan(span as any); | ||
// we do not care about this here | ||
delete event.sdkProcessingMetadata; | ||
|
||
expect(event).toEqual({ | ||
contexts: { | ||
trace: { | ||
span_id: expect.stringMatching(/[a-f0-9]{16}/), | ||
trace_id: expect.stringMatching(/[a-f0-9]{32}/), | ||
data: { | ||
'sentry.source': 'custom', | ||
'sentry.sample_rate': 1, | ||
'sentry.origin': 'manual', | ||
}, | ||
origin: 'manual', | ||
status: 'ok', | ||
}, | ||
otel: { | ||
resource: { | ||
'service.name': 'opentelemetry-test', | ||
'telemetry.sdk.language': 'nodejs', | ||
'telemetry.sdk.name': 'opentelemetry', | ||
'telemetry.sdk.version': expect.any(String), | ||
'service.namespace': 'sentry', | ||
'service.version': SDK_VERSION, | ||
}, | ||
}, | ||
}, | ||
spans: [], | ||
start_timestamp: 1733821670, | ||
timestamp: 1733821672, | ||
transaction: 'test', | ||
type: 'transaction', | ||
transaction_info: { source: 'custom' }, | ||
}); | ||
}); | ||
|
||
it('works with a http.server span', () => { | ||
const span = startInactiveSpan({ | ||
name: 'test', | ||
startTime: 1733821670000, | ||
attributes: { | ||
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'http.server', | ||
[ATTR_HTTP_RESPONSE_STATUS_CODE]: 200, | ||
}, | ||
}); | ||
span.end(1733821672000); | ||
|
||
const event = createTransactionForOtelSpan(span as any); | ||
// we do not care about this here | ||
delete event.sdkProcessingMetadata; | ||
|
||
expect(event).toEqual({ | ||
contexts: { | ||
trace: { | ||
span_id: expect.stringMatching(/[a-f0-9]{16}/), | ||
trace_id: expect.stringMatching(/[a-f0-9]{32}/), | ||
data: { | ||
'sentry.source': 'custom', | ||
'sentry.sample_rate': 1, | ||
'sentry.origin': 'manual', | ||
'sentry.op': 'http.server', | ||
'http.response.status_code': 200, | ||
}, | ||
origin: 'manual', | ||
status: 'ok', | ||
op: 'http.server', | ||
}, | ||
otel: { | ||
resource: { | ||
'service.name': 'opentelemetry-test', | ||
'telemetry.sdk.language': 'nodejs', | ||
'telemetry.sdk.name': 'opentelemetry', | ||
'telemetry.sdk.version': expect.any(String), | ||
'service.namespace': 'sentry', | ||
'service.version': SDK_VERSION, | ||
}, | ||
}, | ||
response: { | ||
status_code: 200, | ||
}, | ||
}, | ||
spans: [], | ||
start_timestamp: 1733821670, | ||
timestamp: 1733821672, | ||
transaction: 'test', | ||
type: 'transaction', | ||
transaction_info: { source: 'custom' }, | ||
}); | ||
}); | ||
}); |