Skip to content

Commit

Permalink
feat: change the default sampler to be always on (#985)
Browse files Browse the repository at this point in the history
* feat: change the default sampler to be always on

* run prebuilds on node 16
  • Loading branch information
seemk authored Nov 27, 2024
1 parent 099ce4a commit 5de1056
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 2 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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'
Expand Down
16 changes: 16 additions & 0 deletions src/tracing/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
import * as util from 'util';
import {
AlwaysOnSampler,
ConsoleSpanExporter,
SpanExporter,
SpanProcessor,
Expand Down Expand Up @@ -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 = {}
Expand Down Expand Up @@ -88,8 +101,11 @@ export function _setDefaultOptions(
);

const extraTracerConfig = options.tracerConfig || {};

const sampler = defaultSampler(extraTracerConfig);
const tracerConfig = {
resource,
sampler,
...extraTracerConfig,
};

Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
3 changes: 3 additions & 0 deletions test/options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
SimpleSpanProcessor,
SpanExporter,
InMemorySpanExporter,
AlwaysOffSampler,
} from '@opentelemetry/sdk-trace-base';

import { strict as assert } from 'assert';
Expand Down Expand Up @@ -212,6 +213,7 @@ describe('options', () => {
instrumentations: [testInstrumentation],
tracerConfig: {
resource: new Resource({ attr1: 'value1' }),
sampler: new AlwaysOffSampler(),
idGenerator: idGenerator,
},
resourceFactory,
Expand All @@ -230,6 +232,7 @@ describe('options', () => {
instrumentations: [testInstrumentation],
tracerConfig: {
resource: new Resource({ attr1: 'value1' }),
sampler: new AlwaysOffSampler(),
idGenerator: idGenerator,
},
spanExporterFactory: testSpanExporterFactory,
Expand Down
7 changes: 7 additions & 0 deletions test/tracing/default_setup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
});
8 changes: 8 additions & 0 deletions test/tracing/env_options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
});

0 comments on commit 5de1056

Please sign in to comment.