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.04 kB
JavaScript
;
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
Object.defineProperty(exports, "__esModule", { value: true });
exports.AwsXraySamplingClient = void 0;
const api_1 = require("@opentelemetry/api");
const core_1 = require("@opentelemetry/core");
const http = require("http");
class AwsXraySamplingClient {
constructor(endpoint, samplerDiag) {
this.getSamplingRulesEndpoint = endpoint + '/GetSamplingRules';
this.samplingTargetsEndpoint = endpoint + '/SamplingTargets';
this.samplerDiag = samplerDiag;
}
fetchSamplingTargets(requestBody, callback) {
this.makeSamplingRequest(this.samplingTargetsEndpoint, callback, this.samplerDiag.debug, JSON.stringify(requestBody));
}
fetchSamplingRules(callback) {
this.makeSamplingRequest(this.getSamplingRulesEndpoint, callback, this.samplerDiag.error);
}
makeSamplingRequest(url, callback, logger, requestBodyJsonString) {
const options = {
method: 'POST',
headers: {},
};
if (requestBodyJsonString) {
options.headers = {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(requestBodyJsonString),
};
}
// Ensure AWS X-Ray Sampler does not generate traces itself
api_1.context.with((0, core_1.suppressTracing)(api_1.context.active()), () => {
const req = http
.request(url, options, response => {
response.setEncoding('utf-8');
let responseData = '';
response.on('data', dataChunk => (responseData += dataChunk));
response.on('end', () => {
if (response.statusCode === 200 && responseData.length > 0) {
let responseObject = undefined;
try {
responseObject = JSON.parse(responseData);
}
catch (e) {
logger(`Error occurred when parsing responseData from ${url}`);
}
if (responseObject) {
callback(responseObject);
}
}
else {
this.samplerDiag.debug(`${url} Response Code is: ${response.statusCode}`);
this.samplerDiag.debug(`${url} responseData is: ${responseData}`);
}
});
})
.on('error', (error) => {
logger(`Error occurred when making an HTTP POST to ${url}: ${error}`);
});
if (requestBodyJsonString) {
req.end(requestBodyJsonString);
}
else {
req.end();
}
});
}
}
exports.AwsXraySamplingClient = AwsXraySamplingClient;
//# sourceMappingURL=aws-xray-sampling-client.js.map