skywalking-backend-js
Version:
The NodeJS agent for Apache SkyWalking
178 lines • 9.07 kB
JavaScript
;
/*!
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var os_1 = require("os");
var AgentConfig_1 = tslib_1.__importDefault(require("../config/AgentConfig"));
var ContextCarrier_1 = require("../trace/context/ContextCarrier");
var ContextManager_1 = tslib_1.__importDefault(require("../trace/context/ContextManager"));
var Component_1 = require("../trace/Component");
var Tag_1 = tslib_1.__importDefault(require("../Tag"));
var Tracing_pb_1 = require("../proto/language-agent/Tracing_pb");
var SDK2_1 = require("../aws/SDK2");
var AWS2SQSPlugin = /** @class */ (function () {
function AWS2SQSPlugin() {
this.module = 'aws-sdk';
this.versions = '2.*';
}
AWS2SQSPlugin.prototype.install = function (installer) {
var AWS = SDK2_1.getAWS(installer);
var _SQS = AWS.SQS;
function SQS() {
var sqs = _SQS.apply(this, arguments);
function instrumentSend(name, addTraceId) {
var _func = sqs[name];
sqs[name] = function (params, callback) {
var queueUrl = params.QueueUrl;
var operation = "AWS/SQS/" + name + "/" + queueUrl.slice(queueUrl.lastIndexOf('/') + 1);
var span = ContextManager_1.default.current.newExitSpan(operation, Component_1.Component.AWS_SQS, Component_1.Component.HTTP);
span.component = Component_1.Component.AWS_SQS;
span.layer = Tracing_pb_1.SpanLayer.MQ;
return SDK2_1.execute(span, this, _func, addTraceId(params, span), callback, 'mqBroker');
};
}
instrumentSend('sendMessage', function (params, span) {
params = Object.assign({}, params);
params.MessageAttributes = params.MessageAttributes ? Object.assign({}, params.MessageAttributes) : {};
params.MessageAttributes.__revdTraceId = {
DataType: 'String',
StringValue: span.inject().value + "/" + os_1.hostname(),
};
return params;
});
instrumentSend('sendMessageBatch', function (params, span) {
var traceId = { __revdTraceId: { DataType: 'String', StringValue: span.inject().value + "/" + os_1.hostname() } };
params = Object.assign({}, params);
params.Entries = params.Entries.map(function (e) {
return (e = Object.assign({}, e, {
MessageAttributes: e.MessageAttributes ? Object.assign({}, e.MessageAttributes, traceId) : traceId,
}));
});
return params;
});
var _receiveMessage = sqs.receiveMessage;
sqs.receiveMessage = function (params, callback) {
params = Object.assign({}, params);
var _MessageAttributeNames = params.MessageAttributeNames;
params.MessageAttributeNames = _MessageAttributeNames
? _MessageAttributeNames.concat(['__revdTraceId'])
: ['__revdTraceId'];
delete params.MaxNumberOfMessages; // limit to 1 message in order to be able to link all Exit and Entry spans
var queueUrl = params.QueueUrl;
var operation = "AWS/SQS/receiveMessage/" + queueUrl.slice(queueUrl.lastIndexOf('/') + 1);
var span = ContextManager_1.default.current.newExitSpan(operation + "<check>", Component_1.Component.AWS_SQS, Component_1.Component.HTTP);
span.component = Component_1.Component.AWS_SQS;
span.layer = Tracing_pb_1.SpanLayer.MQ;
// should always be called on success only, with no err
function beforeCB(span, err, res) {
var _a, _b, _c, _d, _e;
if ((_a = res.Messages) === null || _a === void 0 ? void 0 : _a.length) {
var delall = !_MessageAttributeNames || !_MessageAttributeNames.length;
var traceId = void 0;
// should only be 1
for (var _i = 0, _f = res.Messages; _i < _f.length; _i++) {
var msg = _f[_i];
if (msg.MessageAttributes !== undefined || !AgentConfig_1.default.awsSQSCheckBody)
traceId = (_c = (_b = msg.MessageAttributes) === null || _b === void 0 ? void 0 : _b.__revdTraceId) === null || _c === void 0 ? void 0 : _c.StringValue;
else {
try {
msg = JSON.parse(msg.Body);
traceId = (_e = (_d = msg.MessageAttributes) === null || _d === void 0 ? void 0 : _d.__revdTraceId) === null || _e === void 0 ? void 0 : _e.Value;
}
catch (_g) {
// NOOP
}
}
if (traceId) {
if (delall) {
delete msg.MD5OfMessageAttributes;
delete msg.MessageAttributes;
}
else {
delete msg.MessageAttributes.__revdTraceId;
if (!Object.keys(msg.MessageAttributes).length) {
delete msg.MD5OfMessageAttributes;
delete msg.MessageAttributes;
}
}
}
}
var peer = 'Unknown';
var carrier = undefined;
if (traceId) {
var idx = traceId.lastIndexOf('/');
if (idx !== -1) {
peer = traceId.slice(idx + 1);
traceId = traceId.slice(0, idx);
carrier = ContextCarrier_1.ContextCarrier.from({ sw8: traceId });
}
}
span.stop();
span = ContextManager_1.default.current.newEntrySpan(operation, carrier);
span.component = Component_1.Component.AWS_SQS;
span.layer = Tracing_pb_1.SpanLayer.MQ;
span.peer = peer;
span.tag(Tag_1.default.mqBroker(queueUrl));
span.start();
}
return span;
}
return SDK2_1.execute(span, this, _receiveMessage, params, callback, 'mqBroker', beforeCB);
};
return sqs;
}
Object.assign(SQS, _SQS);
SQS.prototype = _SQS.prototype;
AWS.SQS = SQS;
};
return AWS2SQSPlugin;
}());
// noinspection JSUnusedGlobalSymbols
exports.default = new AWS2SQSPlugin();
// // Example code for test maybe:
// const AWS = require("aws-sdk");
// AWS.config.update({region: 'your-region'});
// const sqs = new AWS.SQS();
// function callback(err, data) {
// console.log('... callback err:', err);
// console.log('... callback data:', data);
// }
// const send = {
// MessageBody: 'Hello World...', /* required */
// QueueUrl: 'https://queue_url', /* required */
// };
// sqs.sendMessage(send, callback);
// // OR:
// sqs.sendMessage(send).send(callback);
// // OR:
// sqs.sendMessage(send).promise()
// .then(r => { console.log('... promise res:', r); })
// .catch(e => { console.log('... promise err:', e); });
// const recv = {
// QueueUrl: 'https://queue_url', /* required */
// };
// sqs.receiveMessage(recv, callback);
// // OR:
// sqs.receiveMessage(recv).send(callback);
// // OR:
// sqs.receiveMessage(recv).promise()
// .then(r => { console.log('... promise res:', r); })
// .catch(e => { console.log('... promise err:', e); });
//# sourceMappingURL=AWS2SQSPlugin.js.map