-
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
Merged
legendecas
merged 49 commits into
open-telemetry:main
from
svetlanabrennan:add-trace-exporter-from-env
Sep 29, 2022
Merged
Changes from all commits
Commits
Show all changes
49 commits
Select commit
Hold shift + click to select a range
cf64bff
feat(sdk-node): wip add functionality to retrieve list of exporters
svetlanabrennan f6ff700
feat(sdk-node): wip add functionality to configure otlp
svetlanabrennan 2259761
feat(sdk-node): wip add otlp protocol retrieval
svetlanabrennan 651b5c7
feat(sdk-node): wip configure span processor
svetlanabrennan 053175c
feat(sdk-node): wip create node tracer providers
svetlanabrennan a792d5d
feat(sdk-node): add otlp exporter tests
svetlanabrennan d2d54f1
Merge branch 'main' into add-trace-exporter-from-env
svetlanabrennan d852a9d
feat(sdk-node): refactor otlp protocol logic
svetlanabrennan b2eee41
feat(sdk-node): refactor configure exporter function
svetlanabrennan 0d3b819
feat(sdk-node): add zipkin and jaeger exporters and tests
svetlanabrennan 0ea7a6a
feat(sdk-node): add console exporter and tests
svetlanabrennan 51c3203
feat(sdk-node): add console exporter and tests
svetlanabrennan 4c50c25
feat(sdk-node): handle invalid exporters case
svetlanabrennan 07f5df3
Merge branch 'main' into add-trace-exporter-from-env
svetlanabrennan 663483c
feat(sdk-node): fix tests failing due to newly added feature
svetlanabrennan 697e614
Merge branch 'main' into add-trace-exporter-from-env
svetlanabrennan de026d6
feat(sdk-node): add more unit tests
svetlanabrennan d579063
feat(sdk-node): add changelog
svetlanabrennan 26ebf54
feat(sdk-node): add documentation to readme
svetlanabrennan 3c35019
feat(sdk-node): add comments to public methods
svetlanabrennan 0b2a104
feat(sdk-node): add integration tests
svetlanabrennan a9497c5
feat(sdk-node): fix lint
svetlanabrennan b718812
feat(sdk-node): fix lint in readme
svetlanabrennan 9f94d56
feat(sdk-node): fix lint in readme
svetlanabrennan 75c1404
feat(sdk-node): refactor to use class for tracer with env exporters
svetlanabrennan e7543b4
feat(sdk-node): fix lint
svetlanabrennan d807632
feat(sdk-node): refactor sdk class and tests
svetlanabrennan a979922
Merge branch 'main' into add-trace-exporter-from-env
svetlanabrennan 405609d
feat(sdk-node): fix typo
svetlanabrennan d73b932
feat(sdk-node): fix typo
svetlanabrennan aed97d1
Merge branch 'add-trace-exporter-from-env' of github.com:svetlanabren…
svetlanabrennan 0c634a0
feat(sdk-node): fix metric test
svetlanabrennan ec26779
feat(sdk-node): fix lint and refactor
svetlanabrennan fe888b9
feat(sdk-node): fix lint
svetlanabrennan c76d9d3
Rename tracerProviderWithEnvExporter.ts to TracerProviderWithEnvExpor…
svetlanabrennan c6e0a19
Merge branch 'main' into add-trace-exporter-from-env
svetlanabrennan e02d739
Merge branch 'main' into add-trace-exporter-from-env
svetlanabrennan 0630bce
feat(sdk-node): move adding span processors to different class
svetlanabrennan c318a87
feat(sdk-node): add more tests and reconfigure some tests
svetlanabrennan f6d3ad7
Merge branch 'main' into add-trace-exporter-from-env
svetlanabrennan 93f6367
feat(sdk-node): add missing env value deletion after test
svetlanabrennan e8a548c
Merge branch 'add-trace-exporter-from-env' of github.com:svetlanabren…
svetlanabrennan a42710e
feat(sdk-node): fix lint
svetlanabrennan 83febe8
feat(sdk-node): override register method and upate logic and tests
svetlanabrennan 6933ee4
Merge branch 'main' into add-trace-exporter-from-env
vmarchaud a5c2d93
feat(sdk-node): add override for addspanprocessor method
svetlanabrennan c2122f6
Merge branch 'add-trace-exporter-from-env' of github.com:svetlanabren…
svetlanabrennan 6bc218e
feat(sdk-node): undo accidental changelog removal
svetlanabrennan 7db936e
Undo accidental empty change heading
svetlanabrennan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
experimental/packages/opentelemetry-sdk-node/src/TracerProviderWithEnvExporter.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| /* | ||
| * 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; | ||
| private _hasSpanProcessors: boolean = false; | ||
|
|
||
| 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 addSpanProcessor(spanProcessor: SpanProcessor) { | ||
| super.addSpanProcessor(spanProcessor); | ||
| this._hasSpanProcessors = true; | ||
| } | ||
|
|
||
| override register(config?: SDKRegistrationConfig) { | ||
| if (this._hasSpanProcessors) { | ||
| 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 !== ''); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.