Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update nodejs autoinstrumetation, fixes #2626 #3570

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions autoinstrumentation/nodejs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
"typescript": "^5.6.3"
},
"dependencies": {
"@opentelemetry/exporter-metrics-otlp-grpc": "0.55.0",
"@opentelemetry/auto-instrumentations-node": "0.53.0",
"@opentelemetry/exporter-prometheus": "0.55.0"
"@opentelemetry/exporter-metrics-otlp-grpc": "0.57.0",
"@opentelemetry/auto-instrumentations-node": "0.55.0",
"@opentelemetry/exporter-prometheus": "0.57.0"
}
}
61 changes: 34 additions & 27 deletions autoinstrumentation/nodejs/src/autoinstrumentation.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node';
import { getNodeAutoInstrumentations, getResourceDetectorsFromEnv } from '@opentelemetry/auto-instrumentations-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 { OTLPMetricExporter } from '@opentelemetry/exporter-metrics-otlp-grpc';
import { PrometheusExporter } from '@opentelemetry/exporter-prometheus';
import { PeriodicExportingMetricReader } from '@opentelemetry/sdk-metrics';
import { alibabaCloudEcsDetector } from '@opentelemetry/resource-detector-alibaba-cloud';
import { awsEc2Detector, awsEksDetector } from '@opentelemetry/resource-detector-aws';
import { containerDetector } from '@opentelemetry/resource-detector-container';
import { gcpDetector } from '@opentelemetry/resource-detector-gcp';
import { envDetector, hostDetector, osDetector, processDetector } from '@opentelemetry/resources';
import { diag } from '@opentelemetry/api';
import { diag, DiagConsoleLogger } from '@opentelemetry/api';

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

diag.setLogger(
new DiagConsoleLogger(),
core.getEnv().OTEL_LOG_LEVEL
);

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

function getTraceExporter() {
let protocol = process.env.OTEL_EXPORTER_OTLP_PROTOCOL;
Expand Down Expand Up @@ -52,26 +53,32 @@ function getMetricReader() {

const sdk = new NodeSDK({
autoDetectResources: true,
instrumentations: [getNodeAutoInstrumentations()],
instrumentations: getNodeAutoInstrumentations(),
traceExporter: getTraceExporter(),
metricReader: getMetricReader(),
resourceDetectors:
[
// Standard resource detectors.
containerDetector,
envDetector,
hostDetector,
osDetector,
processDetector,

// Cloud resource detectors.
alibabaCloudEcsDetector,
// Ordered AWS Resource Detectors as per:
// https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/main/processor/resourcedetectionprocessor/README.md#ordering
awsEksDetector,
awsEc2Detector,
gcpDetector,
],
resourceDetectors: getResourceDetectorsFromEnv()
});

sdk.start();
try {
sdk.start();
diag.info('OpenTelemetry automatic instrumentation started successfully');
} catch (error) {
diag.error(
'Error initializing OpenTelemetry SDK. Your application is not instrumented and will not produce telemetry',
error
);
}

async function shutdown(): Promise<void> {
try {
await sdk.shutdown();
diag.debug('OpenTelemetry SDK terminated');
} catch (error) {
diag.error('Error terminating OpenTelemetry SDK', error);
}
}

// Gracefully shutdown SDK if a SIGTERM is received
process.on('SIGTERM', shutdown);
// Gracefully shutdown SDK if Node.js is exiting normally
process.once('beforeExit', shutdown);