-
Notifications
You must be signed in to change notification settings - Fork 983
feat(sdk-node): configure trace exporter with environment variables #3143
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 45 commits
cf64bff
f6ff700
2259761
651b5c7
053175c
a792d5d
d2d54f1
d852a9d
b2eee41
0d3b819
0ea7a6a
51c3203
4c50c25
07f5df3
663483c
697e614
de026d6
d579063
26ebf54
3c35019
0b2a104
a9497c5
b718812
9f94d56
75c1404
e7543b4
d807632
a979922
405609d
d73b932
aed97d1
0c634a0
ec26779
fe888b9
c76d9d3
c6e0a19
e02d739
0630bce
c318a87
f6d3ad7
93f6367
e8a548c
a42710e
83febe8
6933ee4
a5c2d93
c2122f6
6bc218e
7db936e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| import { diag } from '@opentelemetry/api'; | ||
| import { getEnv, getEnvWithoutDefaults } from '@opentelemetry/core'; | ||
| import { ConsoleSpanExporter, SpanExporter, BatchSpanProcessor, SimpleSpanProcessor, SDKRegistrationConfig, SpanProcessor } from '@opentelemetry/sdk-trace-base'; | ||
| import { NodeTracerConfig, NodeTracerProvider } from '@opentelemetry/sdk-trace-node'; | ||
| import { OTLPTraceExporter as OTLPProtoTraceExporter } from '@opentelemetry/exporter-trace-otlp-proto'; | ||
| import { OTLPTraceExporter as OTLPHttpTraceExporter} from '@opentelemetry/exporter-trace-otlp-http'; | ||
| import { OTLPTraceExporter as OTLPGrpcTraceExporter} from '@opentelemetry/exporter-trace-otlp-grpc'; | ||
| import { ZipkinExporter } from '@opentelemetry/exporter-zipkin'; | ||
| import { JaegerExporter } from '@opentelemetry/exporter-jaeger'; | ||
|
|
||
| export class TracerProviderWithEnvExporters extends NodeTracerProvider { | ||
svetlanabrennan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| private _configuredExporters: SpanExporter[] = []; | ||
| private _spanProcessors: SpanProcessor[] | undefined; | ||
|
|
||
| static configureOtlp(): SpanExporter { | ||
| const protocol = this.getOtlpProtocol(); | ||
|
|
||
| switch (protocol) { | ||
| case 'grpc': | ||
| return new OTLPGrpcTraceExporter; | ||
| case 'http/json': | ||
| return new OTLPHttpTraceExporter; | ||
| case 'http/protobuf': | ||
| return new OTLPProtoTraceExporter; | ||
| default: | ||
| diag.warn(`Unsupported OTLP traces protocol: ${protocol}. Using http/protobuf.`); | ||
| return new OTLPProtoTraceExporter; | ||
| } | ||
| } | ||
|
|
||
| static getOtlpProtocol(): string { | ||
| const parsedEnvValues = getEnvWithoutDefaults(); | ||
|
|
||
| return parsedEnvValues.OTEL_EXPORTER_OTLP_TRACES_PROTOCOL ?? | ||
| parsedEnvValues.OTEL_EXPORTER_OTLP_PROTOCOL ?? | ||
| getEnv().OTEL_EXPORTER_OTLP_TRACES_PROTOCOL ?? | ||
| getEnv().OTEL_EXPORTER_OTLP_PROTOCOL; | ||
| } | ||
|
|
||
| protected static override _registeredExporters = new Map< | ||
| string, | ||
| () => SpanExporter | ||
| >([ | ||
| ['otlp', () => this.configureOtlp()], | ||
| ['zipkin', () => new ZipkinExporter], | ||
| ['jaeger', () => new JaegerExporter], | ||
| ['console', () => new ConsoleSpanExporter] | ||
| ]); | ||
|
|
||
| public constructor(config: NodeTracerConfig = {}) { | ||
| super(config); | ||
| let traceExportersList = this.filterBlanksAndNulls(Array.from(new Set(getEnv().OTEL_TRACES_EXPORTER.split(',')))); | ||
|
|
||
| if (traceExportersList.length === 0 || traceExportersList[0] === 'none') { | ||
| diag.warn('OTEL_TRACES_EXPORTER contains "none" or is empty. SDK will not be initialized.'); | ||
| } else { | ||
| if (traceExportersList.length > 1 && traceExportersList.includes('none')) { | ||
| diag.warn('OTEL_TRACES_EXPORTER contains "none" along with other exporters. Using default otlp exporter.'); | ||
| traceExportersList = ['otlp']; | ||
| } | ||
|
|
||
| traceExportersList.forEach(exporterName => { | ||
| const exporter = this._getSpanExporter(exporterName); | ||
| if (exporter) { | ||
| this._configuredExporters.push(exporter); | ||
| } else { | ||
| diag.warn(`Unrecognized OTEL_TRACES_EXPORTER value: ${exporterName}.`); | ||
| } | ||
| }); | ||
|
|
||
| if (this._configuredExporters.length > 0) { | ||
| this._spanProcessors = this.configureSpanProcessors(this._configuredExporters); | ||
| this._spanProcessors.forEach(processor => { | ||
| this.addSpanProcessor(processor); | ||
| }); | ||
| } else { | ||
| diag.warn('Unable to set up trace exporter(s) due to invalid exporter and/or protocol values.'); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| override register(config?: SDKRegistrationConfig) { | ||
| if (this._spanProcessors) { | ||
|
||
| super.register(config); | ||
| } | ||
| } | ||
|
|
||
| private configureSpanProcessors(exporters: SpanExporter[]): (BatchSpanProcessor | SimpleSpanProcessor)[] { | ||
| return exporters.map(exporter => { | ||
| if (exporter instanceof ConsoleSpanExporter) { | ||
| return new SimpleSpanProcessor(exporter); | ||
| } else { | ||
| return new BatchSpanProcessor(exporter); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| private filterBlanksAndNulls(list: string[]): string[] { | ||
| return list.map(item => item.trim()) | ||
| .filter(s => s !== 'null' && s !== ''); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.