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
feat(otel): Add inject functionality to SentryPropagator #6114
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,207 @@ | ||
import { defaultTextMapSetter, ROOT_CONTEXT, trace, TraceFlags } from '@opentelemetry/api'; | ||
import { suppressTracing } from '@opentelemetry/core'; | ||
import { Hub, makeMain } from '@sentry/core'; | ||
import { addExtensionMethods, Transaction } from '@sentry/tracing'; | ||
import { TransactionContext } from '@sentry/types'; | ||
|
||
import { SENTRY_BAGGAGE_HEADER, SENTRY_TRACE_HEADER } from '../src/constants'; | ||
import { SentryPropagator } from '../src/propagator'; | ||
import { SENTRY_SPAN_PROCESSOR_MAP } from '../src/spanprocessor'; | ||
|
||
beforeAll(() => { | ||
addExtensionMethods(); | ||
}); | ||
|
||
describe('SentryPropagator', () => { | ||
const propogator = new SentryPropagator(); | ||
let carrier: { [key: string]: unknown }; | ||
|
||
beforeEach(() => { | ||
carrier = {}; | ||
}); | ||
|
||
it('returns fields set', () => { | ||
expect(propogator.fields()).toEqual([SENTRY_TRACE_HEADER, SENTRY_BAGGAGE_HEADER]); | ||
}); | ||
|
||
describe('inject', () => { | ||
describe('sentry-trace', () => { | ||
it.each([ | ||
[ | ||
'should set sentry-trace header when sampled', | ||
{ | ||
traceId: 'd4cda95b652f4a1592b449d5929fda1b', | ||
spanId: '6e0c63257de34c92', | ||
traceFlags: TraceFlags.SAMPLED, | ||
}, | ||
'd4cda95b652f4a1592b449d5929fda1b-6e0c63257de34c92-1', | ||
], | ||
[ | ||
'should set sentry-trace header when not sampled', | ||
{ | ||
traceId: 'd4cda95b652f4a1592b449d5929fda1b', | ||
spanId: '6e0c63257de34c92', | ||
traceFlags: TraceFlags.NONE, | ||
}, | ||
'd4cda95b652f4a1592b449d5929fda1b-6e0c63257de34c92-0', | ||
], | ||
[ | ||
'should NOT set sentry-trace header when traceId is empty', | ||
{ | ||
traceId: '', | ||
spanId: '6e0c63257de34c92', | ||
traceFlags: TraceFlags.SAMPLED, | ||
}, | ||
undefined, | ||
], | ||
[ | ||
'should NOT set sentry-trace header when spanId is empty', | ||
{ | ||
traceId: 'd4cda95b652f4a1592b449d5929fda1b', | ||
spanId: '', | ||
traceFlags: TraceFlags.NONE, | ||
}, | ||
undefined, | ||
], | ||
])('%s', (_name, spanContext, expected) => { | ||
const context = trace.setSpanContext(ROOT_CONTEXT, spanContext); | ||
propogator.inject(context, carrier, defaultTextMapSetter); | ||
expect(carrier[SENTRY_TRACE_HEADER]).toBe(expected); | ||
}); | ||
|
||
it('should NOT set sentry-trace header if instrumentation is supressed', () => { | ||
const spanContext = { | ||
traceId: 'd4cda95b652f4a1592b449d5929fda1b', | ||
spanId: '6e0c63257de34c92', | ||
traceFlags: TraceFlags.SAMPLED, | ||
}; | ||
const context = suppressTracing(trace.setSpanContext(ROOT_CONTEXT, spanContext)); | ||
propogator.inject(context, carrier, defaultTextMapSetter); | ||
expect(carrier[SENTRY_TRACE_HEADER]).toBe(undefined); | ||
}); | ||
}); | ||
|
||
describe('baggage', () => { | ||
const client = { | ||
getOptions: () => ({ | ||
environment: 'production', | ||
release: '1.0.0', | ||
}), | ||
getDsn: () => ({ | ||
publicKey: 'abc', | ||
}), | ||
}; | ||
// @ts-ignore Use mock client for unit tests | ||
const hub: Hub = new Hub(client); | ||
makeMain(hub); | ||
|
||
afterEach(() => { | ||
SENTRY_SPAN_PROCESSOR_MAP.clear(); | ||
}); | ||
|
||
enum PerfType { | ||
Transaction = 'transaction', | ||
Span = 'span', | ||
} | ||
|
||
function createTransactionAndMaybeSpan(type: PerfType, transactionContext: TransactionContext) { | ||
const transaction = new Transaction(transactionContext, hub); | ||
SENTRY_SPAN_PROCESSOR_MAP.set(transaction.spanId, transaction); | ||
if (type === PerfType.Span) { | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
const { spanId, ...ctx } = transactionContext; | ||
const span = transaction.startChild({ ...ctx, description: transaction.name }); | ||
SENTRY_SPAN_PROCESSOR_MAP.set(span.spanId, span); | ||
} | ||
} | ||
|
||
describe.each([PerfType.Transaction, PerfType.Span])('with active %s', type => { | ||
it.each([ | ||
[ | ||
'should set baggage header when sampled', | ||
{ | ||
traceId: 'd4cda95b652f4a1592b449d5929fda1b', | ||
spanId: '6e0c63257de34c92', | ||
traceFlags: TraceFlags.SAMPLED, | ||
}, | ||
{ | ||
name: 'sampled-transaction', | ||
traceId: 'd4cda95b652f4a1592b449d5929fda1b', | ||
spanId: '6e0c63257de34c92', | ||
sampled: true, | ||
}, | ||
'sentry-environment=production,sentry-release=1.0.0,sentry-transaction=sampled-transaction,sentry-public_key=abc,sentry-trace_id=d4cda95b652f4a1592b449d5929fda1b', | ||
], | ||
[ | ||
'should NOT set baggage header when not sampled', | ||
{ | ||
traceId: 'd4cda95b652f4a1592b449d5929fda1b', | ||
spanId: '6e0c63257de34c92', | ||
traceFlags: TraceFlags.NONE, | ||
}, | ||
{ | ||
name: 'not-sampled-transaction', | ||
traceId: 'd4cda95b652f4a1592b449d5929fda1b', | ||
spanId: '6e0c63257de34c92', | ||
sampled: false, | ||
}, | ||
'sentry-environment=production,sentry-release=1.0.0,sentry-transaction=not-sampled-transaction,sentry-public_key=abc,sentry-trace_id=d4cda95b652f4a1592b449d5929fda1b', | ||
], | ||
[ | ||
'should NOT set baggage header when traceId is empty', | ||
{ | ||
traceId: '', | ||
spanId: '6e0c63257de34c92', | ||
traceFlags: TraceFlags.SAMPLED, | ||
}, | ||
{ | ||
name: 'empty-traceId-transaction', | ||
traceId: '', | ||
spanId: '6e0c63257de34c92', | ||
sampled: true, | ||
}, | ||
undefined, | ||
], | ||
[ | ||
'should NOT set baggage header when spanId is empty', | ||
{ | ||
traceId: 'd4cda95b652f4a1592b449d5929fda1b', | ||
spanId: '', | ||
traceFlags: TraceFlags.SAMPLED, | ||
}, | ||
{ | ||
name: 'empty-spanId-transaction', | ||
traceId: 'd4cda95b652f4a1592b449d5929fda1b', | ||
spanId: '', | ||
sampled: true, | ||
}, | ||
undefined, | ||
], | ||
])('%s', (_name, spanContext, transactionContext, expected) => { | ||
createTransactionAndMaybeSpan(type, transactionContext); | ||
const context = trace.setSpanContext(ROOT_CONTEXT, spanContext); | ||
propogator.inject(context, carrier, defaultTextMapSetter); | ||
expect(carrier[SENTRY_BAGGAGE_HEADER]).toBe(expected); | ||
}); | ||
|
||
it('should NOT set sentry-trace header if instrumentation is supressed', () => { | ||
const spanContext = { | ||
traceId: 'd4cda95b652f4a1592b449d5929fda1b', | ||
spanId: '6e0c63257de34c92', | ||
traceFlags: TraceFlags.SAMPLED, | ||
}; | ||
const transactionContext = { | ||
name: 'sampled-transaction', | ||
traceId: 'd4cda95b652f4a1592b449d5929fda1b', | ||
spanId: '6e0c63257de34c92', | ||
sampled: true, | ||
}; | ||
createTransactionAndMaybeSpan(type, transactionContext); | ||
const context = suppressTracing(trace.setSpanContext(ROOT_CONTEXT, spanContext)); | ||
propogator.inject(context, carrier, defaultTextMapSetter); | ||
expect(carrier[SENTRY_TRACE_HEADER]).toBe(undefined); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is still a
propogator
leftover ;)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dang it!!!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed with bff9421