Skip to content

Commit

Permalink
[Autoinstrumentation Nodejs] Support exporting traces via http using …
Browse files Browse the repository at this point in the history
…`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
  • Loading branch information
atsu85 authored Nov 6, 2024
1 parent 05a55b6 commit 313ee2a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
16 changes: 16 additions & 0 deletions .chloggen/issue-3412-support-http-protocol.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. collector, target allocator, auto-instrumentation, opamp, github action)
component: auto-instrumentation

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Support `http/json` and `http/protobuf` via OTEL_EXPORTER_OTLP_PROTOCOL environment variable in addition to default `grpc` for exporting traces

# One or more tracking issues related to the change
issues: [3412]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:
22 changes: 20 additions & 2 deletions autoinstrumentation/nodejs/src/autoinstrumentation.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node';
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-grpc';
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 { OTLPMetricExporter } from '@opentelemetry/exporter-metrics-otlp-grpc';
import { PrometheusExporter } from '@opentelemetry/exporter-prometheus';
import { PeriodicExportingMetricReader } from '@opentelemetry/sdk-metrics';
Expand All @@ -12,6 +14,22 @@ import { diag } from '@opentelemetry/api';

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

function getTraceExporter() {
let protocol = process.env.OTEL_EXPORTER_OTLP_PROTOCOL;
switch (protocol) {
case undefined:
case '':
case 'grpc':
return new OTLPGrpcTraceExporter();
case 'http/json':
return new OTLPHttpTraceExporter();
case 'http/protobuf':
return new OTLPProtoTraceExporter();
default:
throw Error(`Creating traces exporter based on "${protocol}" protocol (configured via environment variable OTEL_EXPORTER_OTLP_PROTOCOL) is not implemented!`);
}
}

function getMetricReader() {
switch (process.env.OTEL_METRICS_EXPORTER) {
case undefined:
Expand All @@ -35,7 +53,7 @@ function getMetricReader() {
const sdk = new NodeSDK({
autoDetectResources: true,
instrumentations: [getNodeAutoInstrumentations()],
traceExporter: new OTLPTraceExporter(),
traceExporter: getTraceExporter(),
metricReader: getMetricReader(),
resourceDetectors:
[
Expand Down

0 comments on commit 313ee2a

Please sign in to comment.