Skip to content

feat(otel): Do not sample options and head requests #11467

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

Merged
merged 1 commit into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion packages/opentelemetry/src/sampler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import type { Client, SpanAttributes } from '@sentry/types';
import { logger } from '@sentry/utils';
import { SENTRY_TRACE_STATE_SAMPLED_NOT_RECORDING } from './constants';

import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import { DEBUG_BUILD } from './debug-build';
import { getPropagationContextFromSpan } from './propagator';
import { getSamplingDecision } from './utils/getSamplingDecision';
import { setIsSetup } from './utils/setupCheck';

/**
* A custom OTEL sampler that uses Sentry sampling rates to make it's decision
* A custom OTEL sampler that uses Sentry sampling rates to make its decision
*/
export class SentrySampler implements Sampler {
private _client: Client;
Expand Down Expand Up @@ -72,6 +73,16 @@ export class SentrySampler implements Sampler {
[SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE]: sampleRate,
};

const method = `${spanAttributes[SemanticAttributes.HTTP_METHOD]}`.toUpperCase();
if (method === 'OPTIONS' || method === 'HEAD') {
DEBUG_BUILD && logger.log(`[Tracing] Not sampling span because HTTP method is '${method}' for ${spanName}`);
return {
decision: SamplingDecision.NOT_RECORD,
attributes,
traceState: traceState.set(SENTRY_TRACE_STATE_SAMPLED_NOT_RECORDING, '1'),
};
}

if (!sampled) {
return {
decision: SamplingDecision.NOT_RECORD,
Expand Down
71 changes: 71 additions & 0 deletions packages/opentelemetry/test/trace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
import type { Event, Scope } from '@sentry/types';
import { makeTraceState } from '../src/propagator';

import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import { continueTrace, startInactiveSpan, startSpan, startSpanManual } from '../src/trace';
import type { AbstractSpan } from '../src/types';
import { getDynamicSamplingContextFromSpan } from '../src/utils/dynamicSamplingContext';
Expand Down Expand Up @@ -1358,6 +1359,76 @@ describe('trace (sampling)', () => {
});
});

describe('HTTP methods (sampling)', () => {
beforeEach(() => {
mockSdkInit({ enableTracing: true });
});

afterEach(() => {
cleanupOtel();
});

it('does sample when HTTP method is other than OPTIONS or HEAD', () => {
const spanGET = startSpanManual(
{ name: 'test span', attributes: { [SemanticAttributes.HTTP_METHOD]: 'GET' } },
span => {
return span;
},
);
expect(spanIsSampled(spanGET)).toBe(true);
expect(getSamplingDecision(spanGET.spanContext())).toBe(true);

const spanPOST = startSpanManual(
{ name: 'test span', attributes: { [SemanticAttributes.HTTP_METHOD]: 'POST' } },
span => {
return span;
},
);
expect(spanIsSampled(spanPOST)).toBe(true);
expect(getSamplingDecision(spanPOST.spanContext())).toBe(true);

const spanPUT = startSpanManual(
{ name: 'test span', attributes: { [SemanticAttributes.HTTP_METHOD]: 'PUT' } },
span => {
return span;
},
);
expect(spanIsSampled(spanPUT)).toBe(true);
expect(getSamplingDecision(spanPUT.spanContext())).toBe(true);

const spanDELETE = startSpanManual(
{ name: 'test span', attributes: { [SemanticAttributes.HTTP_METHOD]: 'DELETE' } },
span => {
return span;
},
);
expect(spanIsSampled(spanDELETE)).toBe(true);
expect(getSamplingDecision(spanDELETE.spanContext())).toBe(true);
});

it('does not sample when HTTP method is OPTIONS', () => {
const span = startSpanManual(
{ name: 'test span', attributes: { [SemanticAttributes.HTTP_METHOD]: 'OPTIONS' } },
span => {
return span;
},
);
expect(spanIsSampled(span)).toBe(false);
expect(getSamplingDecision(span.spanContext())).toBe(false);
});

it('does not sample when HTTP method is HEAD', () => {
const span = startSpanManual(
{ name: 'test span', attributes: { [SemanticAttributes.HTTP_METHOD]: 'HEAD' } },
span => {
return span;
},
);
expect(spanIsSampled(span)).toBe(false);
expect(getSamplingDecision(span.spanContext())).toBe(false);
});
});

describe('continueTrace', () => {
beforeEach(() => {
mockSdkInit({ enableTracing: true });
Expand Down