Skip to content

Commit 313ee2a

Browse files
authored
[Autoinstrumentation Nodejs] Support exporting traces via http using OTEL_EXPORTER_OTLP_PROTOCOL (#3413)
* refactor(exporter): extract function to create traces exporter Issue #3412 * feat(exporter): Support OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf for exporting traces via http Closes #3412
1 parent 05a55b6 commit 313ee2a

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
2+
change_type: enhancement
3+
4+
# The name of the component, or a single word describing the area of concern, (e.g. collector, target allocator, auto-instrumentation, opamp, github action)
5+
component: auto-instrumentation
6+
7+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
8+
note: Support `http/json` and `http/protobuf` via OTEL_EXPORTER_OTLP_PROTOCOL environment variable in addition to default `grpc` for exporting traces
9+
10+
# One or more tracking issues related to the change
11+
issues: [3412]
12+
13+
# (Optional) One or more lines of additional information to render under the primary note.
14+
# These lines will be padded with 2 spaces and then inserted directly into the document.
15+
# Use pipe (|) for multiline entries.
16+
subtext:

autoinstrumentation/nodejs/src/autoinstrumentation.ts

+20-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node';
2-
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-grpc';
2+
import { OTLPTraceExporter as OTLPProtoTraceExporter } from '@opentelemetry/exporter-trace-otlp-proto';
3+
import { OTLPTraceExporter as OTLPHttpTraceExporter } from '@opentelemetry/exporter-trace-otlp-http';
4+
import { OTLPTraceExporter as OTLPGrpcTraceExporter } from '@opentelemetry/exporter-trace-otlp-grpc';
35
import { OTLPMetricExporter } from '@opentelemetry/exporter-metrics-otlp-grpc';
46
import { PrometheusExporter } from '@opentelemetry/exporter-prometheus';
57
import { PeriodicExportingMetricReader } from '@opentelemetry/sdk-metrics';
@@ -12,6 +14,22 @@ import { diag } from '@opentelemetry/api';
1214

1315
import { NodeSDK } from '@opentelemetry/sdk-node';
1416

17+
function getTraceExporter() {
18+
let protocol = process.env.OTEL_EXPORTER_OTLP_PROTOCOL;
19+
switch (protocol) {
20+
case undefined:
21+
case '':
22+
case 'grpc':
23+
return new OTLPGrpcTraceExporter();
24+
case 'http/json':
25+
return new OTLPHttpTraceExporter();
26+
case 'http/protobuf':
27+
return new OTLPProtoTraceExporter();
28+
default:
29+
throw Error(`Creating traces exporter based on "${protocol}" protocol (configured via environment variable OTEL_EXPORTER_OTLP_PROTOCOL) is not implemented!`);
30+
}
31+
}
32+
1533
function getMetricReader() {
1634
switch (process.env.OTEL_METRICS_EXPORTER) {
1735
case undefined:
@@ -35,7 +53,7 @@ function getMetricReader() {
3553
const sdk = new NodeSDK({
3654
autoDetectResources: true,
3755
instrumentations: [getNodeAutoInstrumentations()],
38-
traceExporter: new OTLPTraceExporter(),
56+
traceExporter: getTraceExporter(),
3957
metricReader: getMetricReader(),
4058
resourceDetectors:
4159
[

0 commit comments

Comments
 (0)