Skip to content
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

fix: respect sampled flag in Span Processors, fix associated tests #2396

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import { context } from '@opentelemetry/api';
import { context, TraceFlags } from '@opentelemetry/api';
import {
ExportResultCode,
getEnv,
Expand Down Expand Up @@ -77,6 +77,11 @@ export abstract class BatchSpanProcessorBase<T extends BufferConfig> implements
if (this._isShutdown) {
return;
}

if ((span.spanContext().traceFlags & TraceFlags.SAMPLED) === 0) {
return;
}

this._addToBuffer(span);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import { context } from '@opentelemetry/api';
import { context, TraceFlags } from '@opentelemetry/api';
import {
ExportResultCode,
globalErrorHandler,
Expand Down Expand Up @@ -50,6 +50,10 @@ export class SimpleSpanProcessor implements SpanProcessor {
return;
}

if ((span.spanContext().traceFlags & TraceFlags.SAMPLED) === 0) {
return;
}

// prevent downstream exporter calls from generating spans
context.with(suppressTracing(context.active()), () => {
this._exporter.export([span], result => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import { diag } from '@opentelemetry/api';
import {
AlwaysOffSampler,
AlwaysOnSampler,
ExportResultCode,
loggingErrorHandler,
Expand All @@ -38,6 +39,15 @@ function createSampledSpan(spanName: string): Span {
return span as Span;
}

function createUnsampledSpan(spanName: string): Span {
const tracer = new BasicTracerProvider({
sampler: new AlwaysOffSampler(),
}).getTracer('default');
const span = tracer.startSpan(spanName);
span.end();
return span as Span;
}

class BatchSpanProcessor extends BatchSpanProcessorBase<BufferConfig> {
onInit() {}
onShutdown() {}
Expand Down Expand Up @@ -141,6 +151,20 @@ describe('BatchSpanProcessorBase', () => {
assert.strictEqual(exporter.getFinishedSpans().length, 0);
});

it('should not export unsampled spans', async () => {
const processor = new BatchSpanProcessor(exporter, defaultBufferConfig);
const spy: sinon.SinonSpy = sinon.spy(exporter, 'export') as any;

const span = createUnsampledSpan(`${name}_0`);
Flarna marked this conversation as resolved.
Show resolved Hide resolved

processor.onEnd(span);

await processor.forceFlush();
assert.strictEqual(processor['_finishedSpans'].length, 0);
assert.strictEqual(exporter.getFinishedSpans().length, 0);
assert.strictEqual(spy.args.length, 0);
});

it('should export the sampled spans with buffer size reached', done => {
const clock = sinon.useFakeTimers();
const processor = new BatchSpanProcessor(exporter, defaultBufferConfig);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,14 @@ import { TestStackContextManager } from './TestStackContextManager';
import { TestTracingSpanExporter } from './TestTracingSpanExporter';

describe('SimpleSpanProcessor', () => {
const provider = new BasicTracerProvider();
const exporter = new InMemorySpanExporter();
let provider: BasicTracerProvider;
let exporter: InMemorySpanExporter;


beforeEach(() => {
Flarna marked this conversation as resolved.
Show resolved Hide resolved
provider = new BasicTracerProvider();
exporter = new InMemorySpanExporter();
});

describe('constructor', () => {
it('should create a SimpleSpanProcessor instance', () => {
Expand Down Expand Up @@ -103,7 +109,7 @@ describe('SimpleSpanProcessor', () => {
const spanContext: SpanContext = {
traceId: 'a3cda95b652f4a1592b449d5929fda1b',
spanId: '5e0c63257de34c92',
traceFlags: TraceFlags.NONE,
traceFlags: TraceFlags.SAMPLED,
};
const span = new Span(
provider.getTracer('default'),
Expand Down