diff --git a/packages/opentelemetry-core/src/index.ts b/packages/opentelemetry-core/src/index.ts index 9245f0148a..871f02dc85 100644 --- a/packages/opentelemetry-core/src/index.ts +++ b/packages/opentelemetry-core/src/index.ts @@ -34,3 +34,4 @@ export * from './trace/TraceState'; export * from './trace/IdGenerator'; export * from './utils/url'; export * from './utils/wrap'; +export * from './utils/sampling'; diff --git a/packages/opentelemetry-core/src/utils/environment.ts b/packages/opentelemetry-core/src/utils/environment.ts index 8786cd3e1c..9554fe64a5 100644 --- a/packages/opentelemetry-core/src/utils/environment.ts +++ b/packages/opentelemetry-core/src/utils/environment.ts @@ -15,6 +15,7 @@ */ import { DiagLogLevel } from '@opentelemetry/api'; +import { TracesSamplerValues } from './sampling'; const DEFAULT_LIST_SEPARATOR = ','; @@ -104,8 +105,8 @@ export const DEFAULT_ENVIRONMENT: Required = { 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', }; /** diff --git a/packages/opentelemetry-core/src/utils/sampling.ts b/packages/opentelemetry-core/src/utils/sampling.ts new file mode 100644 index 0000000000..d18f59b6fc --- /dev/null +++ b/packages/opentelemetry-core/src/utils/sampling.ts @@ -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', +} diff --git a/packages/opentelemetry-core/test/utils/environment.test.ts b/packages/opentelemetry-core/test/utils/environment.test.ts index d2112e69df..a993f10083 100644 --- a/packages/opentelemetry-core/test/utils/environment.test.ts +++ b/packages/opentelemetry-core/test/utils/environment.test.ts @@ -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 = {}; @@ -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 + ); }); }); }); diff --git a/packages/opentelemetry-tracing/src/config.ts b/packages/opentelemetry-tracing/src/config.ts index 087179e7d1..afc4db73df 100644 --- a/packages/opentelemetry-tracing/src/config.ts +++ b/packages/opentelemetry-tracing/src/config.ts @@ -19,6 +19,7 @@ import { AlwaysOffSampler, AlwaysOnSampler, getEnv, + TracesSamplerValues, ParentBasedSampler, TraceIdRatioBasedSampler, } from '@opentelemetry/core'; @@ -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 @@ -49,27 +52,27 @@ export function buildSamplerFromEnv( env: Required = 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(); }