UNPKG

@aws/aws-distro-opentelemetry-node-autoinstrumentation

Version:

This package provides Amazon Web Services distribution of the OpenTelemetry Node Instrumentation, which allows for auto-instrumentation of NodeJS applications.

97 lines (94 loc) 5.44 kB
"use strict"; // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 // Modifications Copyright The OpenTelemetry Authors. Licensed under the Apache License 2.0 License. Object.defineProperty(exports, "__esModule", { value: true }); exports.instrumentationConfigs = exports.setAwsDefaultEnvironmentVariables = void 0; // Short-term workaround to avoid Upsteam OTel emitting logs such as: // - `OTEL_TRACES_SAMPLER value "xray invalid, defaulting to always_on".` // OTel dependencies will always load a default Sampler configuration. Although unused, that // load process will read the `OTEL_TRACES_SAMPLER` value and may emit the above log, which is // unwanted for `xray` value. Thus we temporarily remove this env var to avoid the unwanted log. let useXraySampler = false; if (process.env.OTEL_TRACES_SAMPLER === 'xray') { delete process.env.OTEL_TRACES_SAMPLER; useXraySampler = true; } const api_1 = require("@opentelemetry/api"); const auto_instrumentations_node_1 = require("@opentelemetry/auto-instrumentations-node"); const opentelemetry = require("@opentelemetry/sdk-node"); const aws_opentelemetry_configurator_1 = require("./aws-opentelemetry-configurator"); const instrumentation_patch_1 = require("./patches/instrumentation-patch"); api_1.diag.setLogger(new api_1.DiagConsoleLogger(), opentelemetry.core.getEnv().OTEL_LOG_LEVEL); /* Sets up default environment variables and apply patches Set default OTEL_EXPORTER_OTLP_PROTOCOL to be `http/protobuf` to remain consistent with other ADOT languages. This must be run before `configurator.configure()`, which will use this value to create an OTel Metric Exporter that is used for the customized AWS Span Procesors. The default value of OTEL_EXPORTER_OTLP_PROTOCOL should be `http/protobuf`: https://github.com/open-telemetry/opentelemetry-js/blob/34003c9b7ef7e7e95e86986550d1c7fb6c1c56c6/packages/opentelemetry-core/src/utils/environment.ts#L233 Also sets default OTEL_PROPAGATORS to ensure good compatibility with X-Ray and Application Signals. This file may also be used to apply patches to upstream instrumentation - usually these are stopgap measures until we can contribute long-term changes to upstream. */ function setAwsDefaultEnvironmentVariables() { if (!process.env.OTEL_EXPORTER_OTLP_PROTOCOL) { process.env.OTEL_EXPORTER_OTLP_PROTOCOL = 'http/protobuf'; } if (!process.env.OTEL_PROPAGATORS) { process.env.OTEL_PROPAGATORS = 'xray,tracecontext'; } // Disable the following instrumentations by default // This auto-instrumentation for the `fs` module generates many low-value spans. `dns` is similar. // https://github.com/open-telemetry/opentelemetry-js-contrib/issues/1344#issuecomment-1618993178 if (!process.env.OTEL_NODE_DISABLED_INSTRUMENTATIONS) { process.env.OTEL_NODE_DISABLED_INSTRUMENTATIONS = 'fs,dns'; } } exports.setAwsDefaultEnvironmentVariables = setAwsDefaultEnvironmentVariables; setAwsDefaultEnvironmentVariables(); exports.instrumentationConfigs = { '@opentelemetry/instrumentation-aws-lambda': { eventContextExtractor: instrumentation_patch_1.customExtractor, }, '@opentelemetry/instrumentation-aws-sdk': { suppressInternalInstrumentation: true, }, '@opentelemetry/instrumentation-mongoose': { suppressInternalInstrumentation: true, }, }; const instrumentations = (0, auto_instrumentations_node_1.getNodeAutoInstrumentations)(exports.instrumentationConfigs); // Apply instrumentation patches (0, instrumentation_patch_1.applyInstrumentationPatches)(instrumentations); const configurator = new aws_opentelemetry_configurator_1.AwsOpentelemetryConfigurator(instrumentations, useXraySampler); const configuration = configurator.configure(); const sdk = new opentelemetry.NodeSDK(configuration); // The OpenTelemetry Authors code // We need to copy OpenTelemetry's register.ts file in order to provide the configuration // created by AwsOpentelemetryConfigurator, which cannot be done by otherwise. In the long term, // we wish to make contributions to upstream to improve customizability of the Node auto-instrumentation. try { sdk.start(); api_1.diag.info('Setting TraceProvider for instrumentations at the end of initialization'); for (const instrumentation of instrumentations) { instrumentation.setTracerProvider(api_1.trace.getTracerProvider()); } api_1.diag.debug(`Environment variable OTEL_PROPAGATORS is set to '${process.env.OTEL_PROPAGATORS}'`); api_1.diag.debug(`Environment variable OTEL_EXPORTER_OTLP_PROTOCOL is set to '${process.env.OTEL_EXPORTER_OTLP_PROTOCOL}'`); api_1.diag.info('AWS Distro of OpenTelemetry automatic instrumentation started successfully'); } catch (error) { api_1.diag.error('Error initializing AWS Distro of OpenTelemetry SDK. Your application is not instrumented and will not produce telemetry', error); } process.on('SIGTERM', () => { sdk .shutdown() .then(() => api_1.diag.debug('AWS Distro of OpenTelemetry SDK terminated')) .catch(error => api_1.diag.error('Error terminating AWS Distro of OpenTelemetry SDK', error)); }); // END The OpenTelemetry Authors code // Respect original `OTEL_TRACES_SAMPLER` as we previously deleted it temporarily for value `xray` if (useXraySampler) { process.env.OTEL_TRACES_SAMPLER = 'xray'; } //# sourceMappingURL=register.js.map