Skip to content

Commit

Permalink
feat(sdk-trace-base): move Sampler declaration into sdk-trace-base
Browse files Browse the repository at this point in the history
  • Loading branch information
legendecas committed Jul 12, 2022
1 parent 747c404 commit b287bd6
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/opentelemetry-sdk-trace-base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
"sinon": "12.0.1",
"ts-loader": "8.3.0",
"ts-mocha": "9.0.2",
"tsd": "0.22.0",
"typescript": "4.4.4",
"webpack": "4.46.0"
},
Expand Down
73 changes: 73 additions & 0 deletions packages/opentelemetry-sdk-trace-base/src/Sampler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { Context, Link, SpanAttributes, SpanKind } from '@opentelemetry/api';

/**
* A sampling decision that determines how a {@link Span} will be recorded
* and collected.
*/
export enum SamplingDecision {
/**
* `Span.isRecording() === false`, span will not be recorded and all events
* and attributes will be dropped.
*/
NOT_RECORD,
/**
* `Span.isRecording() === true`, but `Sampled` flag in {@link TraceFlags}
* MUST NOT be set.
*/
RECORD,
/**
* `Span.isRecording() === true` AND `Sampled` flag in {@link TraceFlags}
* MUST be set.
*/
RECORD_AND_SAMPLED,
}

/**
* A sampling result contains a decision for a {@link Span} and additional
* attributes the sampler would like to added to the Span.
*/
export interface SamplingResult {
/**
* A sampling decision, refer to {@link SamplingDecision} for details.
*/
decision: SamplingDecision;
/**
* The list of attributes returned by SamplingResult MUST be immutable.
* Caller may call {@link Sampler}.shouldSample any number of times and
* can safely cache the returned value.
*/
attributes?: Readonly<SpanAttributes>;
}

/**
* This interface represent a sampler. Sampling is a mechanism to control the
* noise and overhead introduced by OpenTelemetry by reducing the number of
* samples of traces collected and sent to the backend.
*/
export interface Sampler {
/**
* Checks whether span needs to be created and tracked.
*
* @param context Parent Context which may contain a span.
* @param traceId of the span to be created. It can be different from the
* traceId in the {@link SpanContext}. Typically in situations when the
* span to be created starts a new trace.
* @param spanName of the span to be created.
* @param spanKind of the span to be created.
* @param attributes Initial set of SpanAttributes for the Span being constructed.
* @param links Collection of links that will be associated with the Span to
* be created. Typically useful for batch operations.
* @returns a {@link SamplingResult}.
*/
shouldSample(
context: Context,
traceId: string,
spanName: string,
spanKind: SpanKind,
attributes: SpanAttributes,
links: Link[]
): SamplingResult;

/** Returns the sampler name or short description with the configuration. */
toString(): string;
}
3 changes: 2 additions & 1 deletion packages/opentelemetry-sdk-trace-base/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import { diag, Sampler } from '@opentelemetry/api';
import { diag } from '@opentelemetry/api';
import {
AlwaysOffSampler,
AlwaysOnSampler,
Expand All @@ -24,6 +24,7 @@ import {
ENVIRONMENT,
TraceIdRatioBasedSampler,
} from '@opentelemetry/core';
import { Sampler } from './Sampler';

const env = getEnv();
const FALLBACK_OTEL_TRACES_SAMPLER = TracesSamplerValues.AlwaysOn;
Expand Down
14 changes: 14 additions & 0 deletions packages/opentelemetry-sdk-trace-base/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,21 @@ export * from './export/ReadableSpan';
export * from './export/SimpleSpanProcessor';
export * from './export/SpanExporter';
export * from './export/NoopSpanProcessor';
export * from './Sampler';
export * from './Span';
export * from './SpanProcessor';
export * from './TimedEvent';
export * from './types';

/**
* These definition should be moved to @opentelemetry/sdk-trace-base
* in the next major release.
*/
export {
AlwaysOffSampler,
AlwaysOnSampler,
ParentBasedSampler,
TraceIdRatioBasedSampler,
IdGenerator,
RandomIdGenerator,
} from '@opentelemetry/core';
54 changes: 54 additions & 0 deletions packages/opentelemetry-sdk-trace-base/test/common/Sampler.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright The 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 { context, SpanKind } from '@opentelemetry/api';
import { expectAssignable } from 'tsd';
import {
AlwaysOffSampler,
AlwaysOnSampler,
ParentBasedSampler,
Sampler,
SamplingDecision,
SamplingResult,
TraceIdRatioBasedSampler,
} from '../../src';

describe('Sampler', () => {
const samplers = [
new AlwaysOffSampler(),
new AlwaysOnSampler(),
new ParentBasedSampler({ root: new AlwaysOffSampler() }),
new TraceIdRatioBasedSampler(),
] as const;

it('Samplers defined in @opentelemetry/core should fit the interface', () => {
for (const sampler of samplers) {
expectAssignable<Sampler>(sampler);
}
});

it('Sampler return values should fit SamplerResult', () => {
function expectResult<T extends Sampler>(sampler: T) {
const result = sampler.shouldSample(context.active(), 'trace-id', 'span-name', SpanKind.INTERNAL, {}, []);
expectAssignable<SamplingResult>(result);
expectAssignable<SamplingDecision>(result.decision);
}

for (const sampler of samplers) {
expectResult<Sampler>(sampler);
}
});
});

0 comments on commit b287bd6

Please sign in to comment.