@kangc/skywalking-backend-js
Version:
The NodeJS agent for Apache SkyWalking
103 lines • 5.17 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 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 ContextCarrier_1 = require("../trace/context/ContextCarrier");
var AMQPLibPlugin = /** @class */ (function () {
function AMQPLibPlugin() {
this.module = 'amqplib';
this.versions = '*';
}
AMQPLibPlugin.prototype.install = function (installer) {
var BaseChannel = installer.require('amqplib/lib/channel').BaseChannel;
this.interceptProducer(BaseChannel);
this.interceptConsumer(BaseChannel);
};
AMQPLibPlugin.prototype.interceptProducer = function (BaseChannel) {
var _sendMessage = BaseChannel.prototype.sendMessage;
BaseChannel.prototype.sendMessage = function (fields, properties, content) {
var topic = fields.exchange || '';
var queue = fields.routingKey || '';
var peer = this.connection.stream.remoteAddress + ":" + this.connection.stream.remotePort;
var span = ContextManager_1.default.current.newExitSpan('RabbitMQ/' + topic + '/' + queue + '/Producer', Component_1.Component.RABBITMQ_PRODUCER);
span.start();
try {
span.inject().items.forEach(function (item) {
fields.headers[item.key] = item.value;
});
span.component = Component_1.Component.RABBITMQ_PRODUCER;
span.layer = Tracing_pb_1.SpanLayer.MQ;
span.peer = peer;
span.tag(Tag_1.default.mqBroker((this.connection.stream.constructor.name === 'Socket' ? 'amqp://' : 'amqps://') + peer));
if (topic)
span.tag(Tag_1.default.mqTopic(topic));
if (queue)
span.tag(Tag_1.default.mqQueue(queue));
var ret = _sendMessage.call(this, fields, properties, content);
span.stop();
return ret;
}
catch (e) {
span.error(e);
span.stop();
throw e;
}
};
};
AMQPLibPlugin.prototype.interceptConsumer = function (BaseChannel) {
var _dispatchMessage = BaseChannel.prototype.dispatchMessage;
BaseChannel.prototype.dispatchMessage = function (fields, message) {
var _a, _b, _c;
var topic = ((_a = message === null || message === void 0 ? void 0 : message.fields) === null || _a === void 0 ? void 0 : _a.exchange) || '';
var queue = ((_b = message === null || message === void 0 ? void 0 : message.fields) === null || _b === void 0 ? void 0 : _b.routingKey) || '';
var carrier = ContextCarrier_1.ContextCarrier.from(((_c = message === null || message === void 0 ? void 0 : message.properties) === null || _c === void 0 ? void 0 : _c.headers) || {});
var span = ContextManager_1.default.current.newEntrySpan('RabbitMQ/' + topic + '/' + queue + '/Consumer', carrier);
span.start();
try {
span.component = Component_1.Component.RABBITMQ_CONSUMER;
span.layer = Tracing_pb_1.SpanLayer.MQ;
span.peer = this.connection.stream.remoteAddress + ":" + this.connection.stream.remotePort;
span.tag(Tag_1.default.mqBroker((this.connection.stream.constructor.name === 'Socket' ? 'amqp://' : 'amqps://') + span.peer));
if (topic)
span.tag(Tag_1.default.mqTopic(topic));
if (queue)
span.tag(Tag_1.default.mqQueue(queue));
if (message === null)
span.log('Cancel', true);
var ret = _dispatchMessage.call(this, fields, message);
span.stop();
return ret;
}
catch (e) {
span.error(e);
span.stop();
throw e;
}
};
};
return AMQPLibPlugin;
}());
// noinspection JSUnusedGlobalSymbols
exports.default = new AMQPLibPlugin();
//# sourceMappingURL=AMQPLibPlugin.js.map