Skip to content

Commit 58f793c

Browse files
committed
propagate op
1 parent 29604aa commit 58f793c

2 files changed

Lines changed: 33 additions & 0 deletions

File tree

packages/opentelemetry/src/sampler.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,9 @@ export class SentrySampler implements Sampler {
224224
attributes: {
225225
// We set the sample rate on the span when a local sample rate was applied to better understand how traces were sampled in Sentry
226226
[SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE]: localSampleRateWasApplied ? sampleRate : undefined,
227+
// For streamed spans, propagate the inferred op onto the span so it's included in the serialized attributes.
228+
// Non-streamed spans get the op set during export in the SentrySpanExporter.
229+
...(this._isSpanStreaming && op ? { [SEMANTIC_ATTRIBUTE_SENTRY_OP]: op } : {}),
227230
},
228231
};
229232
}

packages/opentelemetry/test/sampler.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,5 +348,35 @@ describe('SentrySampler', () => {
348348
expect(spyOnDroppedEvent).toHaveBeenCalledTimes(1);
349349
expect(spyOnDroppedEvent).toHaveBeenCalledWith('sample_rate', 'span');
350350
});
351+
352+
it('propagates inferred sentry.op onto streamed span attributes', () => {
353+
const client = new TestClient(getDefaultTestClientOptions({ tracesSampleRate: 1, traceLifecycle: 'stream' }));
354+
const sampler = new SentrySampler(client);
355+
356+
const ctx = context.active();
357+
const traceId = generateTraceId();
358+
const spanName = 'GET /users';
359+
const spanKind = SpanKind.SERVER;
360+
const spanAttributes = { [ATTR_HTTP_REQUEST_METHOD]: 'GET' };
361+
362+
const actual = sampler.shouldSample(ctx, traceId, spanName, spanKind, spanAttributes, undefined);
363+
expect(actual.decision).toBe(SamplingDecision.RECORD_AND_SAMPLED);
364+
expect(actual.attributes?.['sentry.op']).toBe('http.server');
365+
});
366+
367+
it('does not propagate sentry.op for non-streamed spans', () => {
368+
const client = new TestClient(getDefaultTestClientOptions({ tracesSampleRate: 1 }));
369+
const sampler = new SentrySampler(client);
370+
371+
const ctx = context.active();
372+
const traceId = generateTraceId();
373+
const spanName = 'GET /users';
374+
const spanKind = SpanKind.SERVER;
375+
const spanAttributes = { [ATTR_HTTP_REQUEST_METHOD]: 'GET' };
376+
377+
const actual = sampler.shouldSample(ctx, traceId, spanName, spanKind, spanAttributes, undefined);
378+
expect(actual.decision).toBe(SamplingDecision.RECORD_AND_SAMPLED);
379+
expect(actual.attributes?.['sentry.op']).toBeUndefined();
380+
});
351381
});
352382
});

0 commit comments

Comments
 (0)