skywalking-backend-js
Version:
The NodeJS agent for Apache SkyWalking
108 lines • 4.96 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 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 AWS2SNSPlugin = /** @class */ (function () {
function AWS2SNSPlugin() {
this.module = 'aws-sdk';
this.versions = '2.*';
}
AWS2SNSPlugin.prototype.install = function (installer) {
var AWS = SDK2_1.getAWS(installer);
var _SNS = AWS.SNS;
function SNS() {
var sns = _SNS.apply(this, arguments);
function instrument(name, addTraceId) {
var _func = sns[name];
sns[name] = function (params, callback) {
var to = params.TopicArn
? "Topic/" + params.TopicArn.slice(params.TopicArn.lastIndexOf(':') + 1)
: params.TargetArn
? "Target/" + params.TargetArn.slice(params.TargetArn.lastIndexOf(':') + 1)
: params.PhoneNumber
? "Phone/" + params.PhoneNumber
: '???';
var operation = "AWS/SNS/" + name + "/" + to;
var span = ContextManager_1.default.current.newExitSpan(operation, Component_1.Component.AWS_SNS, Component_1.Component.HTTP);
var arn = params.TopicArn || params.TargetArn;
span.component = Component_1.Component.AWS_SNS;
span.layer = Tracing_pb_1.SpanLayer.MQ;
if (arn)
span.tag(Tag_1.default.arn(arn));
if (params.TopicArn)
params = addTraceId(params, span);
return SDK2_1.execute(span, this, _func, params, callback, 'mqBroker');
};
}
instrument('publish', 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;
});
instrument('publishBatch', function (params, span) {
var traceId = { __revdTraceId: { DataType: 'String', StringValue: span.inject().value + "/" + os_1.hostname() } };
params = Object.assign({}, params);
params.PublishBatchRequestEntries = params.PublishBatchRequestEntries.map(function (e) {
return (e = Object.assign({}, e, {
MessageAttributes: e.MessageAttributes ? Object.assign({}, e.MessageAttributes, traceId) : traceId,
}));
});
return params;
});
return sns;
}
Object.assign(SNS, _SNS);
SNS.prototype = _SNS.prototype;
AWS.SNS = SNS;
};
return AWS2SNSPlugin;
}());
// noinspection JSUnusedGlobalSymbols
exports.default = new AWS2SNSPlugin();
// // Example code for test maybe:
// const AWS = require("aws-sdk");
// AWS.config.update({region: 'your-region'});
// const sns = new AWS.SNS();
// function callback(err, data) {
// console.log('... callback err:', err);
// console.log('... callback data:', data);
// }
// const message = {
// Message: 'MESSAGE_TEXT', /* required */
// TopicArn: 'topic_arn', /* or other destinations */
// };
// sns.publish(message, callback);
// // OR:
// sns.publish(message).send(callback);
// // OR:
// sns.publish(message).promise()
// .then(r => { console.log('... promise res:', r); })
// .catch(e => { console.log('... promise err:', e); });
//# sourceMappingURL=AWS2SNSPlugin.js.map