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.
71 lines • 3.64 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.OTLPAwsLogExporter = exports.LARGE_LOG_HEADER = void 0;
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
const exporter_logs_otlp_proto_1 = require("@opentelemetry/exporter-logs-otlp-proto");
const otlp_transformer_1 = require("@opentelemetry/otlp-transformer");
const aws_authenticator_1 = require("../common/aws-authenticator");
const core_1 = require("@opentelemetry/core");
/**
* This exporter extends the functionality of the OTLPProtoLogExporter to allow spans to be exported
* to the CloudWatch Logs OTLP endpoint https://logs.[AWSRegion].amazonaws.com/v1/logs. 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.
*/
exports.LARGE_LOG_HEADER = 'x-aws-truncatable-fields';
class OTLPAwsLogExporter extends exporter_logs_otlp_proto_1.OTLPLogExporter {
constructor(endpoint, config) {
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.genAIFlag = false;
this.authenticator = new aws_authenticator_1.AwsAuthenticator(this.region, 'logs');
}
/**
* Overrides the upstream implementation of export. All behaviors are the same except if the
* endpoint is the CloudWatch Logs OTLP endpoint, we will sign the request with SigV4 in headers before
* sending it to the endpoint. Otherwise, we will skip signing.
*/
// Upstream already implements a retry mechanism:
// https://github.com/open-telemetry/opentelemetry-js/blob/main/experimental/packages/otlp-exporter-base/src/retrying-transport.ts
async export(items, resultCallback) {
var _a, _b, _c;
const serializedLogs = otlp_transformer_1.ProtobufLogsSerializer.serializeRequest(items);
if (serializedLogs === 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, serializedLogs);
if (this.genAIFlag) {
signedRequest[exports.LARGE_LOG_HEADER] = 'body/content';
}
else {
delete signedRequest[exports.LARGE_LOG_HEADER];
}
// 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(items, resultCallback);
this.genAIFlag = false;
}
setGenAIFlag() {
this.genAIFlag = true;
}
}
exports.OTLPAwsLogExporter = OTLPAwsLogExporter;
//# sourceMappingURL=otlp-aws-log-exporter.js.map