-
Notifications
You must be signed in to change notification settings - Fork 5
/
trace_app_with_opentelemetry_automatic-instrumentation.js
52 lines (44 loc) · 1.96 KB
/
trace_app_with_opentelemetry_automatic-instrumentation.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// trace_app_with_opentelemetry_automatic-instrumentation.js
//
// Aternity Tech-Community
// 107-opentelemetry-autoinstrumentation-nodejs-app
// version: 22.11.221128
//
// Intialize OpenTelemetry tracing on a javascript app with OpenTelemetry automatic instrumentation
//
// Usage
//
// - Set environement variable used for OpenTelemetry instrumentation ZipKin exporter
// * OTEL_SERVICE_NAME. For example: "service107-js"
// * OTEL_EXPORTER_OTLP_ENDPOINT. For example: http://localhost:4317 or http://aternity-opentelemetry-collector:4317
//
// - Run the app with tracing:
// node -r ./trace_app_with_opentelemetry_automatic-instrumentation.js app.js
//
// Example in PowerShell
//
// $env:OTEL_SERVICE_NAME="service107-js"
// $env:OTEL_EXPORTER_OTLP_ENDPOINT="aternity-opentelemetry-collector:4317"
// node -r ./trace_app_with_opentelemetry_automatic-instrumentation.js app.js
'use strict';
const { NodeTracerProvider } = require('@opentelemetry/sdk-trace-node');
const { getNodeAutoInstrumentations } = require("@opentelemetry/auto-instrumentations-node");
const { Resource } = require('@opentelemetry/resources');
const { SemanticResourceAttributes } = require('@opentelemetry/semantic-conventions');
const { SimpleSpanProcessor } = require("@opentelemetry/sdk-trace-base");
const { OTLPTraceExporter } = require('@opentelemetry/exporter-trace-otlp-grpc');
const { diag, DiagConsoleLogger, DiagLogLevel } = require("@opentelemetry/api");
diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.NONE);
const provider = new NodeTracerProvider({
resource: new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: process.env.OTEL_SERVICE_NAME || "service-js",
}),
instrumentations: [getNodeAutoInstrumentations()]
});
provider.addSpanProcessor(
new SimpleSpanProcessor(
new OTLPTraceExporter()
)
);
provider.register();
console.log("OpenTelemetry tracing initialized with OTLP-gRPC automatic instrumentation");