Skip to content

Commit

Permalink
Add OTel support using auto-instrumentation libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
mikesir87 committed Jan 14, 2025
1 parent 26bdaa0 commit 7eb6549
Show file tree
Hide file tree
Showing 6 changed files with 1,620 additions and 65 deletions.
2 changes: 1 addition & 1 deletion .env.yarn
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
DEBUG=testcontainers*
NODE_OPTIONS=--dns-result-order=ipv4first
NODE_OPTIONS=--dns-result-order=ipv4first --require ./src/instrumentation.js
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ COPY ./src ./src

EXPOSE 3000

CMD [ "node", "src/index.js" ]
CMD [ "node", "--require", "src/instrumentation.js", "src/index.js" ]
12 changes: 11 additions & 1 deletion compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,14 @@ services:
KAFKA_CLUSTERS_0_NAME: local
KAFKA_CLUSTERS_0_BOOTSTRAPSERVERS: kafka:9093
depends_on:
- kafka
- kafka

otel-collector:
image: jaegertracing/all-in-one:1.65.0
ports:
- 16686:16686
- 4317:4317
- 4318:4318
- 9411:9411
environment:
COLLECTOR_ZIPKIN_HTTP_PORT: 9411
7 changes: 7 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@
},
"dependencies": {
"@aws-sdk/client-s3": "^3.651.1",
"@opentelemetry/api": "^1.9.0",
"@opentelemetry/auto-instrumentations-node": "^0.55.2",
"@opentelemetry/exporter-metrics-otlp-grpc": "^0.57.1",
"@opentelemetry/exporter-trace-otlp-grpc": "^0.57.1",
"@opentelemetry/sdk-metrics": "^1.30.1",
"@opentelemetry/sdk-node": "^0.57.1",
"@opentelemetry/sdk-trace-node": "^1.30.1",
"dotenv": "^16.4.5",
"express": "^4.21.1",
"kafkajs": "^2.2.4",
Expand Down
41 changes: 41 additions & 0 deletions src/instrumentation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const opentelemetry = require("@opentelemetry/sdk-node");
const {
getNodeAutoInstrumentations,
} = require("@opentelemetry/auto-instrumentations-node");
const {
OTLPTraceExporter,
} = require("@opentelemetry/exporter-trace-otlp-proto");
const {
OTLPMetricExporter,
} = require("@opentelemetry/exporter-metrics-otlp-proto");
const { PeriodicExportingMetricReader } = require("@opentelemetry/sdk-metrics");
const { Resource } = require("@opentelemetry/resources");
const {
ATTR_SERVICE_NAME,
ATTR_SERVICE_VERSION,
} = require("@opentelemetry/semantic-conventions");

const sdk = new opentelemetry.NodeSDK({
resource: new Resource({
[ATTR_SERVICE_NAME]: "catalog-service",
[ATTR_SERVICE_VERSION]: "1.0.0",
}),
traceExporter: new OTLPTraceExporter({
// optional - default url is http://localhost:4318/v1/traces
// url: '<your-otlp-endpoint>/v1/traces',
// optional - collection of custom headers to be sent with each request, empty by default
// headers: {},
}),
metricReader: new PeriodicExportingMetricReader({
exporter: new OTLPMetricExporter({
// url: '<your-otlp-endpoint>/v1/metrics', // url is optional and can be omitted - default is http://localhost:4318/v1/metrics
// headers: {}, // an optional object containing custom headers to be sent with each request
concurrencyLimit: 1, // an optional limit on pending requests
}),
}),
instrumentations: [getNodeAutoInstrumentations()],
});

sdk.start();

console.log("Instrumentation configured");
Loading

0 comments on commit 7eb6549

Please sign in to comment.