Skip to content

Commit

Permalink
fix: respect sampled flag in Span Processors, fix associated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
quickgiant committed Aug 4, 2021
1 parent e1b3e98 commit 9d66288
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 5 deletions.
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
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
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`);

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
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(() => {
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

0 comments on commit 9d66288

Please sign in to comment.