forked from open-telemetry/opentelemetry-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sdk-node): configure trace exporter with environment variables (o…
…pen-telemetry#3143) * feat(sdk-node): wip add functionality to retrieve list of exporters Signed-off-by: Svetlana Brennan <[email protected]> * feat(sdk-node): wip add functionality to configure otlp Signed-off-by: Svetlana Brennan <[email protected]> * feat(sdk-node): wip add otlp protocol retrieval Signed-off-by: Svetlana Brennan <[email protected]> * feat(sdk-node): wip configure span processor Signed-off-by: Svetlana Brennan <[email protected]> * feat(sdk-node): wip create node tracer providers Signed-off-by: Svetlana Brennan <[email protected]> * feat(sdk-node): add otlp exporter tests Signed-off-by: Svetlana Brennan <[email protected]> * feat(sdk-node): refactor otlp protocol logic Signed-off-by: Svetlana Brennan <[email protected]> * feat(sdk-node): refactor configure exporter function Signed-off-by: Svetlana Brennan <[email protected]> * feat(sdk-node): add zipkin and jaeger exporters and tests Signed-off-by: Svetlana Brennan <[email protected]> * feat(sdk-node): add console exporter and tests Signed-off-by: Svetlana Brennan <[email protected]> * feat(sdk-node): add console exporter and tests Signed-off-by: Svetlana Brennan <[email protected]> * feat(sdk-node): handle invalid exporters case Signed-off-by: Svetlana Brennan <[email protected]> * feat(sdk-node): fix tests failing due to newly added feature Signed-off-by: Svetlana Brennan <[email protected]> * feat(sdk-node): add more unit tests Signed-off-by: Svetlana Brennan <[email protected]> * feat(sdk-node): add changelog Signed-off-by: Svetlana Brennan <[email protected]> * feat(sdk-node): add documentation to readme Signed-off-by: Svetlana Brennan <[email protected]> * feat(sdk-node): add comments to public methods Signed-off-by: Svetlana Brennan <[email protected]> * feat(sdk-node): add integration tests Signed-off-by: Svetlana Brennan <[email protected]> * feat(sdk-node): fix lint Signed-off-by: Svetlana Brennan <[email protected]> * feat(sdk-node): fix lint in readme Signed-off-by: Svetlana Brennan <[email protected]> * feat(sdk-node): fix lint in readme Signed-off-by: Svetlana Brennan <[email protected]> * feat(sdk-node): refactor to use class for tracer with env exporters Signed-off-by: Svetlana Brennan <[email protected]> * feat(sdk-node): fix lint Signed-off-by: Svetlana Brennan <[email protected]> * feat(sdk-node): refactor sdk class and tests Signed-off-by: Svetlana Brennan <[email protected]> * feat(sdk-node): fix typo Signed-off-by: Svetlana Brennan <[email protected]> * feat(sdk-node): fix typo Signed-off-by: Svetlana Brennan <[email protected]> * feat(sdk-node): fix metric test Signed-off-by: Svetlana Brennan <[email protected]> * feat(sdk-node): fix lint and refactor Signed-off-by: Svetlana Brennan <[email protected]> * feat(sdk-node): fix lint Signed-off-by: Svetlana Brennan <[email protected]> * Rename tracerProviderWithEnvExporter.ts to TracerProviderWithEnvExporter.ts * feat(sdk-node): move adding span processors to different class Signed-off-by: Svetlana Brennan <[email protected]> * feat(sdk-node): add more tests and reconfigure some tests Signed-off-by: Svetlana Brennan <[email protected]> * feat(sdk-node): add missing env value deletion after test Signed-off-by: Svetlana Brennan <[email protected]> * feat(sdk-node): fix lint Signed-off-by: Svetlana Brennan <[email protected]> * feat(sdk-node): override register method and upate logic and tests Signed-off-by: Svetlana Brennan <[email protected]> * feat(sdk-node): add override for addspanprocessor method Signed-off-by: Svetlana Brennan <[email protected]> * feat(sdk-node): undo accidental changelog removal Signed-off-by: Svetlana Brennan <[email protected]> * Undo accidental empty change heading Signed-off-by: Svetlana Brennan <[email protected]> Co-authored-by: Valentin Marchaud <[email protected]>
- Loading branch information
1 parent
afe0657
commit 6b8bb15
Showing
9 changed files
with
672 additions
and
19 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 { | ||
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 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.