Skip to content

Commit

Permalink
support none value for OTEL_TRACES_EXPORTER and OTEL_METRICS_EXPORTER
Browse files Browse the repository at this point in the history
  • Loading branch information
seemk committed Aug 28, 2024
1 parent 1650ee5 commit 46ecf19
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 19 deletions.
4 changes: 2 additions & 2 deletions docs/advanced-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ This distribution supports all the configuration options supported by the compon
| `OTEL_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMIT` | | Stable | Maximum allowed attribute value size. Empty value is treated as infinity
| `OTEL_SPAN_EVENT_COUNT_LIMIT` | `128` | Stable |
| `OTEL_SPAN_LINK_COUNT_LIMIT` | `1000`\* | Stable |
| `OTEL_TRACES_EXPORTER`<br>`tracing.spanExporterFactory` | `otlp` | Stable | Chooses the trace exporters. Shortcut for setting `spanExporterFactory`. Comma-delimited list of exporters. Currently supported values: `otlp`, `console`.
| `OTEL_TRACES_EXPORTER`<br>`tracing.spanExporterFactory` | `otlp` | Stable | Chooses the trace exporters. Shortcut for setting `spanExporterFactory`. Comma-delimited list of exporters. Currently supported values: `otlp`, `console`, `none`.
| `OTEL_TRACES_SAMPLER` | `parentbased_always_on` | Stable | Sampler to be used for traces. See [Sampling](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/sdk.md#sampling)
| `OTEL_TRACES_SAMPLER_ARG` | | Stable | String value to be used as the sampler argument. Only be used if OTEL_TRACES_SAMPLER is set.
| `SPLUNK_ACCESS_TOKEN`<br>`accessToken` | | Stable | The optional access token for exporting signal data directly to SignalFx API.
Expand Down Expand Up @@ -99,7 +99,7 @@ The following config options can be set by passing them as tracing arguments to
| `OTEL_SERVICE_NAME`<br>`serviceName` | `unnamed-node-service` | Stable | The service name of this Node service.
| `OTEL_EXPORTER_OTLP_METRICS_ENDPOINT`<br>`endpoint` | `http://localhost:4317` | Stable | The OTLP endpoint to export to.
| `OTEL_METRIC_EXPORT_INTERVAL`<br>`metrics.exportIntervalMillis` | `30000` | Stable | The interval, in milliseconds, of metrics collection and exporting.
| `OTEL_METRICS_EXPORTER`<br>`metrics.metricReaderFactory` | `otlp` | Stable | Chooses the metric exporters. Comma-delimited list of exporters. Currently supported values: `otlp`, `console`.
| `OTEL_METRICS_EXPORTER`<br>`metrics.metricReaderFactory` | `otlp` | Stable | Chooses the metric exporters. Comma-delimited list of exporters. Currently supported values: `otlp`, `console`, `none`.
| `OTEL_EXPORTER_OTLP_METRICS_PROTOCOL`<br>`metrics.metricReaderFactory` | `grpc` | Stable | Chooses the metric exporter protocol. Currently supported values: `grpc`, `http/protobuf`.
| `OTEL_RESOURCE_ATTRIBUTES` | | Stable | The resource attributes to metric data. <details><summary>Environment variable example</summary>`key1=val1,key2=val2`</details>
| `SPLUNK_METRICS_ENABLED`<br>n/a (enabled by calling `start`) | `false` | Experimental | Sets up the metrics pipeline (global meter provider, exporters).
Expand Down
4 changes: 2 additions & 2 deletions src/logging/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,13 @@ export function _setDefaultOptions(
};
}

const SUPPORTED_EXPORTER_TYPES = ['console', 'otlp'];
const SUPPORTED_EXPORTER_TYPES = ['console', 'otlp', 'none'];

function areValidExporterTypes(types: string[]): boolean {
return types.every((t) => SUPPORTED_EXPORTER_TYPES.includes(t));
}

function createExporters(options: LoggingOptions) {
export function createExporters(options: LoggingOptions) {
const logExporters: string[] = getEnvArray('OTEL_LOGS_EXPORTER', ['otlp']);

if (!areValidExporterTypes(logExporters)) {
Expand Down
2 changes: 1 addition & 1 deletion src/metrics/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ function areValidExporterTypes(types: string[]): boolean {
return types.every((t) => SUPPORTED_EXPORTER_TYPES.includes(t));
}

function createOtlpExporter(options: MetricsOptions) {
export function createOtlpExporter(options: MetricsOptions) {
let protocol = getEnvValueByPrecedence([
'OTEL_EXPORTER_OTLP_METRICS_PROTOCOL',
'OTEL_EXPORTER_OTLP_PROTOCOL',
Expand Down
23 changes: 12 additions & 11 deletions src/tracing/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,19 +147,16 @@ export function _setDefaultOptions(options: Partial<Options> = {}): Options {
...extraTracerConfig,
};

const exporterTypes = getExporterTypes(options);

// factories
if (options.spanExporterFactory === undefined) {
options.spanExporterFactory = resolveTraceExporters(exporterTypes);
options.spanExporterFactory = defaultSpanExporterFactory(options);
}

options.spanProcessorFactory =
options.spanProcessorFactory || defaultSpanProcessorFactory;

options.propagatorFactory =
options.propagatorFactory || defaultPropagatorFactory;

// instrumentations
if (options.instrumentations === undefined) {
options.instrumentations = getInstrumentations();
}
Expand Down Expand Up @@ -190,13 +187,14 @@ export function _setDefaultOptions(options: Partial<Options> = {}): Options {
};
}

const SUPPORTED_EXPORTER_TYPES = ['console', 'otlp'];
const SUPPORTED_EXPORTER_TYPES = ['console', 'otlp', 'none'];

type ExporterType = (typeof SUPPORTED_EXPORTER_TYPES)[number];
export type ExporterType = (typeof SUPPORTED_EXPORTER_TYPES)[number];

const SpanExporterMap: Record<ExporterType, SpanExporterFactory> = {
console: consoleSpanExporterFactory,
otlp: otlpSpanExporterFactory,
none: () => [],
};

function containsSupportedRealmExporter(exporterTypes: string[]) {
Expand Down Expand Up @@ -239,11 +237,14 @@ function getExporterTypes(options: Partial<Options>): ExporterType[] {
return traceExporters;
}

function resolveTraceExporters(
exporterTypes: ExporterType[]
export function defaultSpanExporterFactory(
options: Partial<Options>
): SpanExporterFactory {
const factories = exporterTypes.map((t) => SpanExporterMap[t]);
return (options) => factories.flatMap((factory) => factory(options));
const exporterTypes = getExporterTypes(options);
return (options) => {
const factories = exporterTypes.map((t) => SpanExporterMap[t]);
return factories.flatMap((factory) => factory(options));
};
}

export function otlpSpanExporterFactory(options: Options): SpanExporter {
Expand Down
15 changes: 12 additions & 3 deletions test/options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,8 @@ describe('options', () => {
});

describe('OTEL_TRACES_EXPORTER', () => {
beforeEach(utils.cleanEnvironment);

it('accepts a single key', () => {
process.env.OTEL_TRACES_EXPORTER = 'console';
const options = _setDefaultOptions();
Expand All @@ -290,6 +292,15 @@ describe('options', () => {
assert(exporters[0] instanceof ConsoleSpanExporter);
});

it('can be set to none', () => {
process.env.OTEL_TRACES_EXPORTER = 'none';
const options = _setDefaultOptions();
const exporters = options.spanExporterFactory(options);

assert(Array.isArray(exporters));
assert.deepStrictEqual(exporters.length, 0);
});

it('accepts multiple keys', () => {
process.env.OTEL_TRACES_EXPORTER = 'otlp,console';
const options = _setDefaultOptions();
Expand Down Expand Up @@ -404,9 +415,7 @@ describe('options', () => {
});

describe('OTLP span exporter factory', () => {
beforeEach(() => {
beforeEach(utils.cleanEnvironment);
});
beforeEach(utils.cleanEnvironment);

it('throws when called with an unsupported OTLP protocol', () => {
process.env.OTEL_EXPORTER_OTLP_TRACES_PROTOCOL = 'http/json';
Expand Down

0 comments on commit 46ecf19

Please sign in to comment.