Skip to content

Commit 9452a1d

Browse files
authored
feat(otel): Handle inconsistent instrumenter option (#6153)
* feat(otel): Set `otel` instrumenter in otel integration on spans * feat(tracing): Error when using inconsistent `instrumenter`
1 parent a99d894 commit 9452a1d

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

packages/opentelemetry-node/src/spanprocessor.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export class SentrySpanProcessor implements OtelSpanProcessor {
4545
if (sentryParentSpan) {
4646
const sentryChildSpan = sentryParentSpan.startChild({
4747
description: otelSpan.name,
48-
// instrumentor: 'otel',
48+
instrumenter: 'otel',
4949
startTimestamp: convertOtelTimeToSeconds(otelSpan.startTime),
5050
spanId: otelSpanId,
5151
});
@@ -56,7 +56,7 @@ export class SentrySpanProcessor implements OtelSpanProcessor {
5656
const transaction = hub.startTransaction({
5757
name: otelSpan.name,
5858
...traceCtx,
59-
// instrumentor: 'otel',
59+
instrumenter: 'otel',
6060
startTimestamp: convertOtelTimeToSeconds(otelSpan.startTime),
6161
spanId: otelSpanId,
6262
});

packages/tracing/src/hubextensions.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,19 @@ function _startTransaction(
166166
const client = this.getClient();
167167
const options: Partial<ClientOptions> = (client && client.getOptions()) || {};
168168

169+
const configInstrumenter = options.instrumenter || 'sentry';
170+
const transactionInstrumenter = transactionContext.instrumenter || 'sentry';
171+
172+
if (configInstrumenter !== transactionInstrumenter) {
173+
__DEBUG_BUILD__ &&
174+
logger.error(
175+
`A transaction was started with instrumenter=\`${transactionInstrumenter}\`, but the SDK is configured with the \`${configInstrumenter}\` instrumenter.
176+
The transaction will not be sampled. Please use the ${configInstrumenter} instrumentation to start transactions.`,
177+
);
178+
179+
transactionContext.sampled = false;
180+
}
181+
169182
let transaction = new Transaction(transactionContext, this);
170183
transaction = sample(transaction, options, {
171184
parentSampled: transactionContext.parentSampled,

packages/tracing/test/hub.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const mathRandom = jest.spyOn(Math, 'random');
2121
jest.spyOn(Transaction.prototype, 'setMetadata');
2222
jest.spyOn(logger, 'warn');
2323
jest.spyOn(logger, 'log');
24+
jest.spyOn(logger, 'error');
2425
jest.spyOn(utilsModule, 'isNodeEnv');
2526

2627
addDOMPropertiesToGlobal(['XMLHttpRequest', 'Event', 'location', 'document']);
@@ -377,6 +378,27 @@ describe('Hub', () => {
377378
expect(client.captureEvent).not.toBeCalled();
378379
});
379380

381+
it('should drop transactions when using wrong instrumenter', () => {
382+
const options = getDefaultBrowserClientOptions({ tracesSampleRate: 1, instrumenter: 'otel' });
383+
const client = new BrowserClient(options);
384+
jest.spyOn(client, 'captureEvent');
385+
386+
const hub = new Hub(client);
387+
makeMain(hub);
388+
const transaction = hub.startTransaction({ name: 'dogpark' });
389+
390+
jest.spyOn(transaction, 'finish');
391+
transaction.finish();
392+
393+
expect(transaction.sampled).toBe(false);
394+
expect(transaction.finish).toReturnWith(undefined);
395+
expect(client.captureEvent).not.toBeCalled();
396+
expect(logger.error).toHaveBeenCalledWith(
397+
`A transaction was started with instrumenter=\`sentry\`, but the SDK is configured with the \`otel\` instrumenter.
398+
The transaction will not be sampled. Please use the otel instrumentation to start transactions.`,
399+
);
400+
});
401+
380402
describe('sampling inheritance', () => {
381403
it('should propagate sampling decision to child spans', () => {
382404
const options = getDefaultBrowserClientOptions({ tracesSampleRate: Math.random() });

0 commit comments

Comments
 (0)