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

Respect sampled bit in probability sampler #798

Merged
merged 11 commits into from
Feb 20, 2020
1 change: 1 addition & 0 deletions packages/opentelemetry-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@ export * from './trace/NoRecordingSpan';
export * from './trace/sampler/ProbabilitySampler';
export * from './trace/spancontext-utils';
export * from './trace/TraceState';
export * from './utils/trace-flags';
dyladan marked this conversation as resolved.
Show resolved Hide resolved
export * from './utils/url';
export * from './utils/wrap';
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

import { Sampler, SpanContext } from '@opentelemetry/api';
import { isSampled } from '../../utils/trace-flags';

/** Sampler that samples a given fraction of traces. */
export class ProbabilitySampler implements Sampler {
Expand All @@ -23,6 +24,8 @@ export class ProbabilitySampler implements Sampler {
}

shouldSample(parentContext?: SpanContext) {
if (parentContext && parentContext.traceFlags !== undefined)
dyladan marked this conversation as resolved.
Show resolved Hide resolved
return isSampled(parentContext.traceFlags);
if (this._probability >= 1.0) return true;
else if (this._probability <= 0) return false;
return Math.random() < this._probability;
Expand Down
26 changes: 26 additions & 0 deletions packages/opentelemetry-core/src/utils/trace-flags.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*!
dyladan marked this conversation as resolved.
Show resolved Hide resolved
* Copyright 2020, OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { TraceFlags } from '@opentelemetry/api';

/**
* Given a bitmask, determine if the SAMPLED flag is set
*
* @param traceFlags 8 bit mask of tracing flags
*/
export function isSampled(traceFlags: TraceFlags = TraceFlags.UNSAMPLED) {
dyladan marked this conversation as resolved.
Show resolved Hide resolved
return (TraceFlags.SAMPLED & traceFlags) === TraceFlags.SAMPLED;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@
* limitations under the License.
*/

import { TraceFlags } from '@opentelemetry/api';
import { unrefTimer } from '@opentelemetry/core';
import { SpanProcessor } from '../SpanProcessor';
import { SpanExporter } from './SpanExporter';
import { Span } from '../Span';
import { ReadableSpan } from './ReadableSpan';
import { SpanProcessor } from '../SpanProcessor';
import { BufferConfig } from '../types';
import { ReadableSpan } from './ReadableSpan';
import { SpanExporter } from './SpanExporter';

const DEFAULT_BUFFER_SIZE = 100;
const DEFAULT_BUFFER_TIMEOUT_MS = 20_000;
Expand Down Expand Up @@ -56,7 +55,6 @@ export class BatchSpanProcessor implements SpanProcessor {
onStart(span: Span): void {}

onEnd(span: Span): void {
if (span.context().traceFlags !== TraceFlags.SAMPLED) return;
dyladan marked this conversation as resolved.
Show resolved Hide resolved
this._addToBuffer(span.toReadableSpan());
}

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

import { TraceFlags } from '@opentelemetry/api';
import { Span } from '../Span';
import { SpanProcessor } from '../SpanProcessor';
import { SpanExporter } from './SpanExporter';
import { Span } from '../Span';

/**
* An implementation of the {@link SpanProcessor} that converts the {@link Span}
Expand All @@ -32,7 +31,6 @@ export class SimpleSpanProcessor implements SpanProcessor {
onStart(span: Span): void {}

onEnd(span: Span): void {
if (span.context().traceFlags !== TraceFlags.SAMPLED) return;
this._exporter.export([span.toReadableSpan()], () => {});
}

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

import { ALWAYS_SAMPLER } from '@opentelemetry/core';
import * as assert from 'assert';
import * as sinon from 'sinon';
import {
Span,
BasicTracerProvider,
InMemorySpanExporter,
BatchSpanProcessor,
InMemorySpanExporter,
Span,
} from '../../src';
import { NEVER_SAMPLER, ALWAYS_SAMPLER, NoopLogger } from '@opentelemetry/core';

function createSampledSpan(spanName: string): Span {
const tracer = new BasicTracerProvider({
Expand All @@ -33,16 +33,6 @@ function createSampledSpan(spanName: string): Span {
return span as Span;
}

function createUnSampledSpan(spanName: string): Span {
const tracer = new BasicTracerProvider({
sampler: NEVER_SAMPLER,
logger: new NoopLogger(),
}).getTracer('default');
const span = tracer.startSpan(spanName, { isRecording: false });
span.end();
return span as Span;
}

describe('BatchSpanProcessor', () => {
const name = 'span-name';
const defaultBufferConfig = {
Expand Down Expand Up @@ -97,16 +87,6 @@ describe('BatchSpanProcessor', () => {
assert.strictEqual(exporter.getFinishedSpans().length, 0);
});

it('should not export the unsampled spans', () => {
const processor = new BatchSpanProcessor(exporter, defaultBufferConfig);
for (let i = 0; i < defaultBufferConfig.bufferSize * 2; i++) {
const span = createUnSampledSpan(`${name}_${i}`);
processor.onEnd(span);
assert.strictEqual(exporter.getFinishedSpans().length, 0);
}
processor.shutdown();
});

it('should force flush when timeout exceeded', done => {
const clock = sinon.useFakeTimers();
const processor = new BatchSpanProcessor(exporter, defaultBufferConfig);
Expand Down