UNPKG

enhanced-adot-node-autoinstrumentation

Version:

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

87 lines 4.44 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.OTLPAwsSpanExporter = void 0; // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 const exporter_trace_otlp_proto_1 = require("@opentelemetry/exporter-trace-otlp-proto"); const otlp_transformer_1 = require("@opentelemetry/otlp-transformer"); const core_1 = require("@opentelemetry/core"); const aws_authenticator_1 = require("../common/aws-authenticator"); const api_logs_1 = require("@opentelemetry/api-logs"); const llo_handler_1 = require("../../../../llo-handler"); const utils_1 = require("../../../../utils"); const sdk_logs_1 = require("@opentelemetry/sdk-logs"); /** * This exporter extends the functionality of the OTLPProtoTraceExporter to allow spans to be exported * to the XRay OTLP endpoint https://xray.[AWSRegion].amazonaws.com/v1/traces. Utilizes the aws-sdk * library to sign and directly inject SigV4 Authentication to the exported request's headers. <a * href="https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch-OTLPEndpoint.html">...</a> * * This only works with version >=16 Node.js environments. */ class OTLPAwsSpanExporter extends exporter_trace_otlp_proto_1.OTLPTraceExporter { constructor(endpoint, config, loggerProvider) { let modifiedConfig = { url: endpoint, }; if (config) { modifiedConfig = Object.assign(Object.assign({}, config), { url: endpoint }); } super(modifiedConfig); this.region = endpoint.split('.')[1]; this.endpoint = endpoint; this.authenticator = new aws_authenticator_1.AwsAuthenticator(this.region, 'xray'); this.lloHandler = undefined; this.loggerProvider = loggerProvider; } // Lazily initialize LLO handler when needed to avoid initialization order issues""" ensureLloHandler() { if (!this.lloHandler && (0, utils_1.isAgentObservabilityEnabled)()) { // If logger_provider wasn't provided, try to get the current one if (!this.loggerProvider) { try { this.loggerProvider = api_logs_1.logs.getLoggerProvider(); } catch (e) { return false; } } if (this.loggerProvider && this.loggerProvider instanceof sdk_logs_1.LoggerProvider) { this.lloHandler = new llo_handler_1.LLOHandler(this.loggerProvider); return true; } } return !!this.lloHandler; } /** * Overrides the upstream implementation of export. All behaviors are the same except if the * endpoint is an XRay OTLP endpoint, we will sign the request with SigV4 in headers before * sending it to the endpoint. Otherwise, we will skip signing. */ async export(items, resultCallback) { var _a, _b, _c; let itemsToSerialize = items; if ((0, utils_1.isAgentObservabilityEnabled)() && this.ensureLloHandler() && this.lloHandler) { // items to serialize are now the lloProcessedSpans itemsToSerialize = this.lloHandler.processSpans(items); } const serializedSpans = otlp_transformer_1.ProtobufTraceSerializer.serializeRequest(itemsToSerialize); if (serializedSpans === undefined) { resultCallback({ code: core_1.ExportResultCode.FAILED, error: new Error('Nothing to send'), }); return; } const headers = (_c = (_b = (_a = this['_delegate']._transport) === null || _a === void 0 ? void 0 : _a._transport) === null || _b === void 0 ? void 0 : _b._parameters) === null || _c === void 0 ? void 0 : _c.headers(); if (headers) { const signedRequest = await this.authenticator.authenticate(this.endpoint, headers, serializedSpans); // See type: https://github.com/open-telemetry/opentelemetry-js/blob/experimental/v0.57.1/experimental/packages/otlp-exporter-base/src/transport/http-transport-types.ts#L31 const newHeaders = () => signedRequest; this['_delegate']._transport._transport._parameters.headers = newHeaders; } super.export(itemsToSerialize, resultCallback); } } exports.OTLPAwsSpanExporter = OTLPAwsSpanExporter; //# sourceMappingURL=otlp-aws-span-exporter.js.map