Skip to content

Commit

Permalink
Update env exporter configuration logic and add tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
JacksonWeber committed Jan 16, 2024
1 parent 996172f commit 685dd6c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 27 deletions.
54 changes: 27 additions & 27 deletions experimental/packages/opentelemetry-sdk-node/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import { LogRecordProcessor, LoggerProvider } from '@opentelemetry/sdk-logs';
import { MeterProvider, MetricReader, View } from '@opentelemetry/sdk-metrics';
import {
BatchSpanProcessor,
SpanExporter,
SpanProcessor,
} from '@opentelemetry/sdk-trace-base';
import {
Expand Down Expand Up @@ -93,7 +92,7 @@ export class NodeSDK {
private _loggerProvider?: LoggerProvider;
private _meterProvider?: MeterProvider;
private _serviceName?: string;
private _traceExporter?: SpanExporter;
private _configuration?: Partial<NodeSDKConfiguration>;

private _disabled?: boolean;

Expand All @@ -103,11 +102,6 @@ export class NodeSDK {
public constructor(configuration: Partial<NodeSDKConfiguration> = {}) {
const env = getEnv();
const envWithoutDefaults = getEnvWithoutDefaults();
const envExporterWithConfiguration =
env.OTEL_TRACES_EXPORTER &&
(configuration.sampler ||
configuration.spanLimits ||
configuration.idGenerator);

if (env.OTEL_SDK_DISABLED) {
this._disabled = true;
Expand All @@ -123,7 +117,8 @@ export class NodeSDK {
});
}

this._traceExporter = configuration.traceExporter;
this._configuration = configuration;

this._resource = configuration.resource ?? new Resource({});
this._resourceDetectors = configuration.resourceDetectors ?? [
envDetector,
Expand All @@ -134,12 +129,8 @@ export class NodeSDK {

this._autoDetectResources = configuration.autoDetectResources ?? true;

// If there are configuration options to read, we need to create a new TracerProvider even if the exporter is configured in the environment
if (
configuration.traceExporter ||
configuration.spanProcessor ||
envExporterWithConfiguration
) {
// If a tracer provider can be created from manual configuration, create it
if (configuration.traceExporter || configuration.spanProcessor) {
const tracerProviderConfig: NodeTracerConfig = {};

if (configuration.sampler) {
Expand Down Expand Up @@ -308,7 +299,6 @@ export class NodeSDK {
* Call this method to construct SDK components and register them with the OpenTelemetry API.
*/
public start(): void {
const env = getEnv();
if (this._disabled) {
return;
}
Expand All @@ -330,18 +320,28 @@ export class NodeSDK {
})
);

// if there is a defined tracerProviderConfig, the traces exporter is defined and not none and there is no traceExporter defined in manual config
const Provider =
(this._tracerProviderConfig &&
(!env.OTEL_TRACES_EXPORTER || env.OTEL_TRACES_EXPORTER === 'none')) ||
this._traceExporter
? NodeTracerProvider
: TracerProviderWithEnvExporters;

const tracerProvider = new Provider({
...this._tracerProviderConfig?.tracerConfig,
resource: this._resource,
});
// if there is a tracerProviderConfig (traceExporter/spanProcessor was set manually) or the traceExporter is set manually, use NodeTracerProvider
const Provider = this._tracerProviderConfig
? NodeTracerProvider
: TracerProviderWithEnvExporters;

let tracerProvider;

// If the Provider is configured with Env Exporters, we need to check if the SDK had any manual configurations, else configure normally
if (
typeof Provider === typeof TracerProviderWithEnvExporters &&
this._configuration
) {
tracerProvider = new Provider({
...this._configuration,
resource: this._resource,
});
} else {
tracerProvider = new Provider({
...this._tracerProviderConfig?.tracerConfig,

Check warning on line 341 in experimental/packages/opentelemetry-sdk-node/src/sdk.ts

View check run for this annotation

Codecov / codecov/patch

experimental/packages/opentelemetry-sdk-node/src/sdk.ts#L340-L341

Added lines #L340 - L341 were not covered by tests
resource: this._resource,
});
}

this._tracerProvider = tracerProvider;

Expand Down
18 changes: 18 additions & 0 deletions experimental/packages/opentelemetry-sdk-node/test/sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import {
BatchSpanProcessor,
NoopSpanProcessor,
IdGenerator,
AlwaysOffSampler,
} from '@opentelemetry/sdk-trace-base';
import * as assert from 'assert';
import * as semver from 'semver';
Expand Down Expand Up @@ -896,6 +897,23 @@ describe('setup exporter from env', () => {
assert(listOfProcessors[0] instanceof BatchSpanProcessor);
delete env.OTEL_TRACES_EXPORTER;
});
it('should only create one span processor when configured using env vars and config', async () => {
env.OTEL_TRACES_EXPORTER = 'console';
const sdk = new NodeSDK({
sampler: new AlwaysOffSampler(),
});
sdk.start();
const listOfProcessors =
sdk['_tracerProvider']!['_registeredSpanProcessors']!;
assert(
sdk['_tracerProvider'] instanceof TracerProviderWithEnvExporters === true
);
assert(
sdk['_tracerProvider']!['_config']?.sampler instanceof AlwaysOffSampler
)
assert(listOfProcessors.length === 1);
delete env.OTEL_TRACES_EXPORTER;
});
it('use otlp exporter and defined exporter protocol env value', async () => {
env.OTEL_TRACES_EXPORTER = 'otlp';
env.OTEL_EXPORTER_OTLP_TRACES_PROTOCOL = 'grpc';
Expand Down

0 comments on commit 685dd6c

Please sign in to comment.