diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 69cbe036..5560a120 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -14,7 +14,7 @@ jobs: strategy: fail-fast: false matrix: - container: ['node:14.0.0'] + container: ['node:16.0.0'] node_api_target: ['18.0.0'] include: - container: 'node:16.0.0' @@ -42,7 +42,7 @@ jobs: strategy: fail-fast: false matrix: - container: ['node:14.0.0'] + container: ['node:16.0.0'] node_api_target: ['18.0.0'] include: - container: 'node:16.0.0' diff --git a/src/tracing/options.ts b/src/tracing/options.ts index cb6d11b6..7a10bf66 100644 --- a/src/tracing/options.ts +++ b/src/tracing/options.ts @@ -15,6 +15,7 @@ */ import * as util from 'util'; import { + AlwaysOnSampler, ConsoleSpanExporter, SpanExporter, SpanProcessor, @@ -49,6 +50,18 @@ import type { StartTracingOptions, TracingOptions, } from './types'; +import { NodeTracerConfig } from '@opentelemetry/sdk-trace-node'; + +function defaultSampler(config: NodeTracerConfig) { + if ( + config.sampler === undefined && + getNonEmptyEnvVar('OTEL_TRACES_SAMPLER') === undefined + ) { + return new AlwaysOnSampler(); + } + + return undefined; +} export function _setDefaultOptions( options: StartTracingOptions = {} @@ -88,8 +101,11 @@ export function _setDefaultOptions( ); const extraTracerConfig = options.tracerConfig || {}; + + const sampler = defaultSampler(extraTracerConfig); const tracerConfig = { resource, + sampler, ...extraTracerConfig, }; diff --git a/src/types.ts b/src/types.ts index 2d02123b..cfa37cdd 100644 --- a/src/types.ts +++ b/src/types.ts @@ -41,6 +41,7 @@ export type EnvVarKey = | 'OTEL_SERVICE_NAME' | 'OTEL_SPAN_LINK_COUNT_LIMIT' | 'OTEL_TRACES_EXPORTER' + | 'OTEL_TRACES_SAMPLER' | 'SPLUNK_ACCESS_TOKEN' | 'SPLUNK_AUTOINSTRUMENT_PACKAGE_NAMES' | 'SPLUNK_AUTOMATIC_LOG_COLLECTION' diff --git a/test/options.test.ts b/test/options.test.ts index 8ab7cafd..06fefd6d 100644 --- a/test/options.test.ts +++ b/test/options.test.ts @@ -37,6 +37,7 @@ import { SimpleSpanProcessor, SpanExporter, InMemorySpanExporter, + AlwaysOffSampler, } from '@opentelemetry/sdk-trace-base'; import { strict as assert } from 'assert'; @@ -212,6 +213,7 @@ describe('options', () => { instrumentations: [testInstrumentation], tracerConfig: { resource: new Resource({ attr1: 'value1' }), + sampler: new AlwaysOffSampler(), idGenerator: idGenerator, }, resourceFactory, @@ -230,6 +232,7 @@ describe('options', () => { instrumentations: [testInstrumentation], tracerConfig: { resource: new Resource({ attr1: 'value1' }), + sampler: new AlwaysOffSampler(), idGenerator: idGenerator, }, spanExporterFactory: testSpanExporterFactory, diff --git a/test/tracing/default_setup.test.ts b/test/tracing/default_setup.test.ts index 33bc6882..c467d844 100644 --- a/test/tracing/default_setup.test.ts +++ b/test/tracing/default_setup.test.ts @@ -14,11 +14,14 @@ * limitations under the License. */ +import { strict as assert } from 'assert'; import { test } from 'node:test'; import { assertTracingPipeline, setupMocks } from './common'; import { parseOptionsAndConfigureInstrumentations } from '../../src/instrumentations'; import { startTracing, stopTracing } from '../../src/tracing'; +import { trace } from '@opentelemetry/api'; +import { AlwaysOnSampler } from '@opentelemetry/sdk-trace-base'; test('Tracing: set up with defaults', async () => { const mocks = setupMocks(); @@ -29,5 +32,9 @@ test('Tracing: set up with defaults', async () => { 'http://localhost:4318/v1/traces', '@splunk/otel' ); + + const provider = trace.getTracerProvider(); + assert(provider.getTracer('test')['_sampler'] instanceof AlwaysOnSampler); + await stopTracing(); }); diff --git a/test/tracing/env_options.test.ts b/test/tracing/env_options.test.ts index c42cdfea..a0b07de8 100644 --- a/test/tracing/env_options.test.ts +++ b/test/tracing/env_options.test.ts @@ -14,11 +14,14 @@ * limitations under the License. */ +import { strict as assert } from 'assert'; import { test } from 'node:test'; import { assertTracingPipeline, setupMocks } from './common'; import { parseOptionsAndConfigureInstrumentations } from '../../src/instrumentations'; import { startTracing, stopTracing } from '../../src/tracing'; +import { trace } from '@opentelemetry/api'; +import { ParentBasedSampler } from '@opentelemetry/sdk-trace-base'; test('Tracing: set up with env options', async () => { const mocks = setupMocks(); @@ -30,9 +33,14 @@ test('Tracing: set up with env options', async () => { process.env.OTEL_EXPORTER_OTLP_ENDPOINT = url; process.env.OTEL_SERVICE_NAME = serviceName; process.env.SPLUNK_ACCESS_TOKEN = accessToken; + process.env.OTEL_TRACES_SAMPLER = 'parentbased_always_on'; const { tracingOptions } = parseOptionsAndConfigureInstrumentations(); startTracing(tracingOptions); assertTracingPipeline(mocks, `${url}/v1/traces`, serviceName, accessToken); + + const provider = trace.getTracerProvider(); + assert(provider.getTracer('test')['_sampler'] instanceof ParentBasedSampler); + await stopTracing(); });