Skip to content

Commit

Permalink
feat(core): Emit client reports for unsampled root spans on span start (
Browse files Browse the repository at this point in the history
#14936)

With this PR, the `sample_rate`,`transaction` client report is now
consistently emitted at the time when the root span is sampled.

Previously, in Node (OTEL) we did not emit this at all, while in browser
we emit it at span end time. This is inconsistent and not ideal.
Emitting it in OTEL at span end time is difficult because `span.end()`
is a no-op there and you do not get access to it anywhere. So doing it
at span start (=sample time) is easier, and also makes more sense as
this is also actually the time when the span is dropped.
  • Loading branch information
mydea authored Jan 9, 2025
1 parent 3e73850 commit b0c0004
Show file tree
Hide file tree
Showing 15 changed files with 181 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ test('outgoing http requests are correctly instrumented when not sampled', done
.then(([SERVER_URL, closeTestServer]) => {
createRunner(__dirname, 'scenario.ts')
.withEnv({ SERVER_URL })
.ignore('client_report')
.expect({
event: {
exception: {
Expand Down
6 changes: 6 additions & 0 deletions packages/core/src/tracing/idleSpan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ export function startIdleSpan(startSpanOptions: StartSpanOptions, options: Parti
beforeSpanEnd(span);
}

// If the span is non-recording, nothing more to do here...
// This is the case if tracing is enabled but this specific span was not sampled
if (thisArg instanceof SentryNonRecordingSpan) {
return;
}

// Just ensuring that this keeps working, even if we ever have more arguments here
const [definedEndTimestamp, ...rest] = args;
const timestamp = definedEndTimestamp || timestampInSeconds();
Expand Down
9 changes: 0 additions & 9 deletions packages/core/src/tracing/sentrySpan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,17 +333,8 @@ export class SentrySpan implements Span {
}

const { scope: capturedSpanScope, isolationScope: capturedSpanIsolationScope } = getCapturedScopesOnSpan(this);
const scope = capturedSpanScope || getCurrentScope();
const client = scope.getClient() || getClient();

if (this._sampled !== true) {
// At this point if `sampled !== true` we want to discard the transaction.
DEBUG_BUILD && logger.log('[Tracing] Discarding transaction because its trace was not chosen to be sampled.');

if (client) {
client.recordDroppedEvent('sample_rate', 'transaction');
}

return undefined;
}

Expand Down
6 changes: 6 additions & 0 deletions packages/core/src/tracing/trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,12 @@ function _startRootSpan(spanArguments: SentrySpanArguments, scope: Scope, parent
},
sampled,
});

if (!sampled && client) {
DEBUG_BUILD && logger.log('[Tracing] Discarding root span because its trace was not chosen to be sampled.');
client.recordDroppedEvent('sample_rate', 'transaction');
}

if (sampleRate !== undefined) {
rootSpan.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE, sampleRate);
}
Expand Down
5 changes: 5 additions & 0 deletions packages/opentelemetry/src/sampler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ export class SentrySampler implements Sampler {
}

if (!sampled) {
if (parentSampled === undefined) {
DEBUG_BUILD && logger.log('[Tracing] Discarding root span because its trace was not chosen to be sampled.');
this._client.recordDroppedEvent('sample_rate', 'transaction');
}

return {
...wrapSamplingDecision({ decision: SamplingDecision.NOT_RECORD, context, spanAttributes }),
attributes,
Expand Down
3 changes: 1 addition & 2 deletions packages/opentelemetry/test/helpers/TestClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const TestClient = wrapClientClass(BaseTestClient);
export type TestClientInterface = Client & OpenTelemetryClient;

export function init(options: Partial<Options> = {}): void {
const client = new TestClient(getDefaultTestClientOptions(options));
const client = new TestClient(getDefaultTestClientOptions({ tracesSampleRate: 1, ...options }));

// The client is on the current scope, from where it generally is inherited
getCurrentScope().setClient(client);
Expand All @@ -42,7 +42,6 @@ export function init(options: Partial<Options> = {}): void {

export function getDefaultTestClientOptions(options: Partial<Options> = {}): ClientOptions {
return {
enableTracing: true,
integrations: [],
transport: () => createTransport({ recordDroppedEvent: () => undefined }, _ => resolvedSyncPromise({})),
stackParser: () => [],
Expand Down
10 changes: 5 additions & 5 deletions packages/opentelemetry/test/integration/breadcrumbs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ describe('Integration | breadcrumbs', () => {
const beforeSend = jest.fn(() => null);
const beforeBreadcrumb = jest.fn(breadcrumb => breadcrumb);

mockSdkInit({ beforeSend, beforeBreadcrumb, beforeSendTransaction, enableTracing: true });
mockSdkInit({ beforeSend, beforeBreadcrumb, beforeSendTransaction, tracesSampleRate: 1 });

const client = getClient() as TestClientInterface;

Expand Down Expand Up @@ -143,7 +143,7 @@ describe('Integration | breadcrumbs', () => {
const beforeSend = jest.fn(() => null);
const beforeBreadcrumb = jest.fn(breadcrumb => breadcrumb);

mockSdkInit({ beforeSend, beforeBreadcrumb, beforeSendTransaction, enableTracing: true });
mockSdkInit({ beforeSend, beforeBreadcrumb, beforeSendTransaction, tracesSampleRate: 1 });

const client = getClient() as TestClientInterface;

Expand Down Expand Up @@ -195,7 +195,7 @@ describe('Integration | breadcrumbs', () => {
const beforeSend = jest.fn(() => null);
const beforeBreadcrumb = jest.fn(breadcrumb => breadcrumb);

mockSdkInit({ beforeSend, beforeBreadcrumb, beforeSendTransaction, enableTracing: true });
mockSdkInit({ beforeSend, beforeBreadcrumb, beforeSendTransaction, tracesSampleRate: 1 });

const client = getClient() as TestClientInterface;

Expand Down Expand Up @@ -236,7 +236,7 @@ describe('Integration | breadcrumbs', () => {
const beforeSend = jest.fn(() => null);
const beforeBreadcrumb = jest.fn(breadcrumb => breadcrumb);

mockSdkInit({ beforeSend, beforeBreadcrumb, beforeSendTransaction, enableTracing: true });
mockSdkInit({ beforeSend, beforeBreadcrumb, beforeSendTransaction, tracesSampleRate: 1 });

const client = getClient() as TestClientInterface;

Expand Down Expand Up @@ -294,7 +294,7 @@ describe('Integration | breadcrumbs', () => {
const beforeSend = jest.fn(() => null);
const beforeBreadcrumb = jest.fn(breadcrumb => breadcrumb);

mockSdkInit({ beforeSend, beforeBreadcrumb, beforeSendTransaction, enableTracing: true });
mockSdkInit({ beforeSend, beforeBreadcrumb, beforeSendTransaction, tracesSampleRate: 1 });

const client = getClient() as TestClientInterface;

Expand Down
6 changes: 5 additions & 1 deletion packages/opentelemetry/test/integration/scope.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ describe('Integration | Scope', () => {
const beforeSend = jest.fn(() => null);
const beforeSendTransaction = jest.fn(() => null);

mockSdkInit({ enableTracing, beforeSend, beforeSendTransaction });
mockSdkInit({
tracesSampleRate: enableTracing ? 1 : 0,
beforeSend,
beforeSendTransaction,
});

const client = getClient() as TestClientInterface;

Expand Down
14 changes: 7 additions & 7 deletions packages/opentelemetry/test/integration/transactions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe('Integration | Transactions', () => {
});

mockSdkInit({
enableTracing: true,
tracesSampleRate: 1,
beforeSendTransaction,
release: '8.0.0',
});
Expand Down Expand Up @@ -178,7 +178,7 @@ describe('Integration | Transactions', () => {
it('correctly creates concurrent transaction & spans', async () => {
const beforeSendTransaction = jest.fn(() => null);

mockSdkInit({ enableTracing: true, beforeSendTransaction });
mockSdkInit({ tracesSampleRate: 1, beforeSendTransaction });

const client = getClient() as TestClientInterface;

Expand Down Expand Up @@ -339,7 +339,7 @@ describe('Integration | Transactions', () => {
traceState,
};

mockSdkInit({ enableTracing: true, beforeSendTransaction });
mockSdkInit({ tracesSampleRate: 1, beforeSendTransaction });

const client = getClient() as TestClientInterface;

Expand Down Expand Up @@ -443,7 +443,7 @@ describe('Integration | Transactions', () => {
const logs: unknown[] = [];
jest.spyOn(logger, 'log').mockImplementation(msg => logs.push(msg));

mockSdkInit({ enableTracing: true, beforeSendTransaction });
mockSdkInit({ tracesSampleRate: 1, beforeSendTransaction });

const provider = getProvider();
const multiSpanProcessor = provider?.activeSpanProcessor as
Expand Down Expand Up @@ -516,7 +516,7 @@ describe('Integration | Transactions', () => {
const transactions: Event[] = [];

mockSdkInit({
enableTracing: true,
tracesSampleRate: 1,
beforeSendTransaction: event => {
transactions.push(event);
return null;
Expand Down Expand Up @@ -573,7 +573,7 @@ describe('Integration | Transactions', () => {
const transactions: Event[] = [];

mockSdkInit({
enableTracing: true,
tracesSampleRate: 1,
beforeSendTransaction: event => {
transactions.push(event);
return null;
Expand Down Expand Up @@ -644,7 +644,7 @@ describe('Integration | Transactions', () => {
};

mockSdkInit({
enableTracing: true,
tracesSampleRate: 1,
beforeSendTransaction,
release: '7.0.0',
});
Expand Down
2 changes: 1 addition & 1 deletion packages/opentelemetry/test/propagator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('SentryPropagator', () => {
mockSdkInit({
environment: 'production',
release: '1.0.0',
enableTracing: true,
tracesSampleRate: 1,
dsn: 'https://abc@domain/123',
});
});
Expand Down
136 changes: 136 additions & 0 deletions packages/opentelemetry/test/sampler.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
import { SpanKind, context, trace } from '@opentelemetry/api';
import { TraceState } from '@opentelemetry/core';
import { SamplingDecision } from '@opentelemetry/sdk-trace-base';
import { ATTR_HTTP_REQUEST_METHOD } from '@opentelemetry/semantic-conventions';
import { generateSpanId, generateTraceId } from '@sentry/core';
import { SENTRY_TRACE_STATE_SAMPLED_NOT_RECORDING } from '../src/constants';
import { SentrySampler } from '../src/sampler';
import { TestClient, getDefaultTestClientOptions } from './helpers/TestClient';
import { cleanupOtel } from './helpers/mockSdkInit';

describe('SentrySampler', () => {
afterEach(() => {
cleanupOtel();
});

it('works with tracesSampleRate=0', () => {
const client = new TestClient(getDefaultTestClientOptions({ tracesSampleRate: 0 }));
const spyOnDroppedEvent = jest.spyOn(client, 'recordDroppedEvent');
const sampler = new SentrySampler(client);

const ctx = context.active();
const traceId = generateTraceId();
const spanName = 'test';
const spanKind = SpanKind.INTERNAL;
const spanAttributes = {};
const links = undefined;

const actual = sampler.shouldSample(ctx, traceId, spanName, spanKind, spanAttributes, links);
expect(actual).toEqual({
decision: SamplingDecision.NOT_RECORD,
attributes: { 'sentry.sample_rate': 0 },
traceState: new TraceState().set('sentry.sampled_not_recording', '1'),
});
expect(spyOnDroppedEvent).toHaveBeenCalledTimes(1);
expect(spyOnDroppedEvent).toHaveBeenCalledWith('sample_rate', 'transaction');

spyOnDroppedEvent.mockReset();
});

it('works with tracesSampleRate=0 & for a child span', () => {
const client = new TestClient(getDefaultTestClientOptions({ tracesSampleRate: 0 }));
const spyOnDroppedEvent = jest.spyOn(client, 'recordDroppedEvent');
const sampler = new SentrySampler(client);

const traceId = generateTraceId();
const ctx = trace.setSpanContext(context.active(), {
spanId: generateSpanId(),
traceId,
traceFlags: 0,
traceState: new TraceState().set(SENTRY_TRACE_STATE_SAMPLED_NOT_RECORDING, '1'),
});
const spanName = 'test';
const spanKind = SpanKind.INTERNAL;
const spanAttributes = {};
const links = undefined;

const actual = sampler.shouldSample(ctx, traceId, spanName, spanKind, spanAttributes, links);
expect(actual).toEqual({
decision: SamplingDecision.NOT_RECORD,
attributes: { 'sentry.sample_rate': 0 },
traceState: new TraceState().set(SENTRY_TRACE_STATE_SAMPLED_NOT_RECORDING, '1'),
});
expect(spyOnDroppedEvent).toHaveBeenCalledTimes(0);

spyOnDroppedEvent.mockReset();
});

it('works with tracesSampleRate=1', () => {
const client = new TestClient(getDefaultTestClientOptions({ tracesSampleRate: 1 }));
const spyOnDroppedEvent = jest.spyOn(client, 'recordDroppedEvent');
const sampler = new SentrySampler(client);

const ctx = context.active();
const traceId = generateTraceId();
const spanName = 'test';
const spanKind = SpanKind.INTERNAL;
const spanAttributes = {};
const links = undefined;

const actual = sampler.shouldSample(ctx, traceId, spanName, spanKind, spanAttributes, links);
expect(actual).toEqual({
decision: SamplingDecision.RECORD_AND_SAMPLED,
attributes: { 'sentry.sample_rate': 1 },
traceState: new TraceState(),
});
expect(spyOnDroppedEvent).toHaveBeenCalledTimes(0);

spyOnDroppedEvent.mockReset();
});

it('works with traceSampleRate=undefined', () => {
const client = new TestClient(getDefaultTestClientOptions({ tracesSampleRate: undefined }));
const spyOnDroppedEvent = jest.spyOn(client, 'recordDroppedEvent');
const sampler = new SentrySampler(client);

const ctx = context.active();
const traceId = generateTraceId();
const spanName = 'test';
const spanKind = SpanKind.INTERNAL;
const spanAttributes = {};
const links = undefined;

const actual = sampler.shouldSample(ctx, traceId, spanName, spanKind, spanAttributes, links);
expect(actual).toEqual({
decision: SamplingDecision.NOT_RECORD,
traceState: new TraceState(),
});
expect(spyOnDroppedEvent).toHaveBeenCalledTimes(0);

spyOnDroppedEvent.mockReset();
});

it('ignores local http client root spans', () => {
const client = new TestClient(getDefaultTestClientOptions({ tracesSampleRate: 0 }));
const spyOnDroppedEvent = jest.spyOn(client, 'recordDroppedEvent');
const sampler = new SentrySampler(client);

const ctx = context.active();
const traceId = generateTraceId();
const spanName = 'test';
const spanKind = SpanKind.CLIENT;
const spanAttributes = {
[ATTR_HTTP_REQUEST_METHOD]: 'GET',
};
const links = undefined;

const actual = sampler.shouldSample(ctx, traceId, spanName, spanKind, spanAttributes, links);
expect(actual).toEqual({
decision: SamplingDecision.NOT_RECORD,
traceState: new TraceState(),
});
expect(spyOnDroppedEvent).toHaveBeenCalledTimes(0);

spyOnDroppedEvent.mockReset();
});
});
2 changes: 1 addition & 1 deletion packages/opentelemetry/test/spanExporter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { cleanupOtel, mockSdkInit } from './helpers/mockSdkInit';
describe('createTransactionForOtelSpan', () => {
beforeEach(() => {
mockSdkInit({
enableTracing: true,
tracesSampleRate: 1,
});
});

Expand Down
Loading

0 comments on commit b0c0004

Please # to comment.