Skip to content

Commit

Permalink
properly handle op
Browse files Browse the repository at this point in the history
  • Loading branch information
mydea committed Jul 17, 2024
1 parent 98eb57f commit 32e6f04
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 25 deletions.
5 changes: 4 additions & 1 deletion packages/opentelemetry/src/sampler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,14 @@ export class SentrySampler implements Sampler {
} = inferSpanData(spanName, spanAttributes, spanKind);

const mergedAttributes = {
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: op,
...inferredAttributes,
...spanAttributes,
};

if (op) {
mergedAttributes[SEMANTIC_ATTRIBUTE_SENTRY_OP] = op;
}

const mutableSamplingDecision = { decision: true };
this._client.emit(
'beforeSampling',
Expand Down
23 changes: 7 additions & 16 deletions packages/opentelemetry/src/trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ export function startSpan<T>(options: OpenTelemetrySpanContext, callback: (span:
const spanOptions = getSpanOptions(options);

return tracer.startActiveSpan(name, spanOptions, ctx, span => {
_applySentryAttributesToSpan(span, options);

return handleCallbackErrors(
() => callback(span),
() => {
Expand Down Expand Up @@ -90,8 +88,6 @@ export function startSpanManual<T>(
const spanOptions = getSpanOptions(options);

return tracer.startActiveSpan(name, spanOptions, ctx, span => {
_applySentryAttributesToSpan(span, options);

return handleCallbackErrors(
() => callback(span, () => span.end()),
() => {
Expand Down Expand Up @@ -131,8 +127,6 @@ export function startInactiveSpan(options: OpenTelemetrySpanContext): Span {

const span = tracer.startSpan(name, spanOptions, ctx);

_applySentryAttributesToSpan(span, options);

return span;
});
}
Expand All @@ -156,22 +150,19 @@ function getTracer(): Tracer {
return (client && client.tracer) || trace.getTracer('@sentry/opentelemetry', SDK_VERSION);
}

function _applySentryAttributesToSpan(span: Span, options: OpenTelemetrySpanContext): void {
const { op } = options;

if (op) {
span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_OP, op);
}
}

function getSpanOptions(options: OpenTelemetrySpanContext): SpanOptions {
const { startTime, attributes, kind } = options;
const { startTime, attributes, kind, op } = options;

// OTEL expects timestamps in ms, not seconds
const fixedStartTime = typeof startTime === 'number' ? ensureTimestampInMilliseconds(startTime) : startTime;

return {
attributes,
attributes: op
? {
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: op,
...attributes,
}
: attributes,
kind,
startTime: fixedStartTime,
};
Expand Down
23 changes: 15 additions & 8 deletions packages/opentelemetry/test/trace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1320,9 +1320,7 @@ describe('trace (sampling)', () => {
expect(tracesSampler).toHaveBeenLastCalledWith({
parentSampled: undefined,
name: 'outer',
attributes: {
[SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE]: 1,
},
attributes: {},
transactionContext: { name: 'outer', parentSampled: undefined },
});

Expand Down Expand Up @@ -1357,16 +1355,25 @@ describe('trace (sampling)', () => {

mockSdkInit({ tracesSampler });

startSpan({ name: 'outer' }, outerSpan => {
expect(outerSpan).toBeDefined();
});
startSpan(
{
name: 'outer',
op: 'test.op',
attributes: { attr1: 'yes', attr2: 1 },
},
outerSpan => {
expect(outerSpan).toBeDefined();
},
);

expect(tracesSampler).toBeCalledTimes(1);
expect(tracesSampler).toHaveBeenCalledTimes(1);
expect(tracesSampler).toHaveBeenLastCalledWith({
parentSampled: undefined,
name: 'outer',
attributes: {
[SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE]: 1,
attr1: 'yes',
attr2: 1,
'sentry.op': 'test.op',
},
transactionContext: { name: 'outer', parentSampled: undefined },
});
Expand Down

0 comments on commit 32e6f04

Please sign in to comment.