Skip to content

Commit

Permalink
refactor: extract OTEL_TRACES_SAMPLER values to enum
Browse files Browse the repository at this point in the history
  • Loading branch information
jtmalinowski committed Apr 20, 2021
1 parent 1806cbe commit c049704
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 10 deletions.
1 change: 1 addition & 0 deletions packages/opentelemetry-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ export * from './trace/TraceState';
export * from './trace/IdGenerator';
export * from './utils/url';
export * from './utils/wrap';
export * from './utils/sampling';
3 changes: 2 additions & 1 deletion packages/opentelemetry-core/src/utils/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

import { DiagLogLevel } from '@opentelemetry/api';
import { TracesSamplerValues } from './sampling';

const DEFAULT_LIST_SEPARATOR = ',';

Expand Down Expand Up @@ -104,8 +105,8 @@ export const DEFAULT_ENVIRONMENT: Required<ENVIRONMENT> = {
OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT: 128,
OTEL_SPAN_EVENT_COUNT_LIMIT: 128,
OTEL_SPAN_LINK_COUNT_LIMIT: 128,
OTEL_TRACES_SAMPLER: TracesSamplerValues.ParentBasedAlwaysOn,
OTEL_TRACES_SAMPLER_ARG: '',
OTEL_TRACES_SAMPLER: 'parentbased_always_on',
};

/**
Expand Down
24 changes: 24 additions & 0 deletions packages/opentelemetry-core/src/utils/sampling.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* 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.
*/

export enum TracesSamplerValues {
AlwaysOff = 'always_off',
AlwaysOn = 'always_on',
ParentBasedAlwaysOff = 'parentbased_always_off',
ParentBasedAlwaysOn = 'parentbased_always_on',
ParentBasedTraceIdRatio = 'parentbased_traceidratio',
TraceIdRatio = 'traceidratio',
}
8 changes: 6 additions & 2 deletions packages/opentelemetry-core/test/utils/environment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
import * as assert from 'assert';
import * as sinon from 'sinon';
import { DiagLogLevel } from '@opentelemetry/api';
import { TracesSamplerValues } from '../../src';

let lastMock: RAW_ENVIRONMENT = {};

Expand Down Expand Up @@ -146,12 +147,15 @@ describe('environment', () => {
it('should remove a mock environment', () => {
mockEnvironment({
OTEL_LOG_LEVEL: 'DEBUG',
OTEL_TRACES_SAMPLER: 'always_off',
OTEL_TRACES_SAMPLER: TracesSamplerValues.AlwaysOff,
});
removeMockEnvironment();
const env = getEnv();
assert.strictEqual(env.OTEL_LOG_LEVEL, DiagLogLevel.INFO);
assert.strictEqual(env.OTEL_TRACES_SAMPLER, 'parentbased_always_on');
assert.strictEqual(
env.OTEL_TRACES_SAMPLER,
TracesSamplerValues.ParentBasedAlwaysOn
);
});
});
});
17 changes: 10 additions & 7 deletions packages/opentelemetry-tracing/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
AlwaysOffSampler,
AlwaysOnSampler,
getEnv,
TracesSamplerValues,
ParentBasedSampler,
TraceIdRatioBasedSampler,
} from '@opentelemetry/core';
Expand All @@ -41,6 +42,8 @@ export const DEFAULT_CONFIG = {
},
};

const FALLBACK_OTEL_TRACES_SAMPLER = TracesSamplerValues.AlwaysOn;

/**
* Based on environment, builds a sampler, complies with specification.
* @param env optional, by default uses getEnv(), but allows passing a value to reuse parsed environment
Expand All @@ -49,27 +52,27 @@ export function buildSamplerFromEnv(
env: Required<ENVIRONMENT> = getEnv()
): Sampler {
switch (env.OTEL_TRACES_SAMPLER) {
case 'always_on':
case TracesSamplerValues.AlwaysOn:
return new AlwaysOnSampler();
case 'always_off':
case TracesSamplerValues.AlwaysOff:
return new AlwaysOffSampler();
case 'parentbased_always_on':
case TracesSamplerValues.ParentBasedAlwaysOn:
return new ParentBasedSampler({
root: new AlwaysOnSampler(),
});
case 'parentbased_always_off':
case TracesSamplerValues.ParentBasedAlwaysOff:
return new ParentBasedSampler({
root: new AlwaysOffSampler(),
});
case 'traceidratio':
case TracesSamplerValues.TraceIdRatio:
return new TraceIdRatioBasedSampler(getSamplerProbabilityFromEnv(env));
case 'parentbased_traceidratio':
case TracesSamplerValues.ParentBasedTraceIdRatio:
return new ParentBasedSampler({
root: new TraceIdRatioBasedSampler(getSamplerProbabilityFromEnv(env)),
});
default:
diag.error(
`OTEL_TRACES_SAMPLER value "${env.OTEL_TRACES_SAMPLER} invalid, defaulting to always_on".`
`OTEL_TRACES_SAMPLER value "${env.OTEL_TRACES_SAMPLER} invalid, defaulting to ${FALLBACK_OTEL_TRACES_SAMPLER}".`
);
return new AlwaysOnSampler();
}
Expand Down

0 comments on commit c049704

Please sign in to comment.