UNPKG

skywalking-backend-js-netease

Version:

The NodeJS agent for Apache SkyWalking

102 lines 5.01 kB
"use strict"; /*! * * 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 }); const tslib_1 = require("tslib"); const ContextManager_1 = tslib_1.__importDefault(require("../trace/context/ContextManager")); const Component_1 = require("../trace/Component"); const Tag_1 = tslib_1.__importDefault(require("../Tag")); const Tracing_pb_1 = require("../proto/language-agent/Tracing_pb"); const ContextCarrier_1 = require("../trace/context/ContextCarrier"); class AMQPLibPlugin { constructor() { this.module = 'amqplib'; this.versions = '*'; } install(installer) { const { BaseChannel } = installer.require('amqplib/lib/channel'); this.interceptProducer(BaseChannel); this.interceptConsumer(BaseChannel); } interceptProducer(BaseChannel) { const _sendMessage = BaseChannel.prototype.sendMessage; BaseChannel.prototype.sendMessage = function (fields, properties, content) { const topic = fields.exchange || ''; const queue = fields.routingKey || ''; const peer = `${this.connection.stream.remoteAddress}:${this.connection.stream.remotePort}`; const span = ContextManager_1.default.current.newExitSpan('RabbitMQ/' + topic + '/' + queue + '/Producer', Component_1.Component.RABBITMQ_PRODUCER); span.start(); try { span.inject().items.forEach((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)); const ret = _sendMessage.call(this, fields, properties, content); span.stop(); return ret; } catch (e) { span.error(e); span.stop(); throw e; } }; } interceptConsumer(BaseChannel) { const _dispatchMessage = BaseChannel.prototype.dispatchMessage; BaseChannel.prototype.dispatchMessage = function (fields, message) { var _a, _b, _c; const topic = ((_a = message === null || message === void 0 ? void 0 : message.fields) === null || _a === void 0 ? void 0 : _a.exchange) || ''; const queue = ((_b = message === null || message === void 0 ? void 0 : message.fields) === null || _b === void 0 ? void 0 : _b.routingKey) || ''; const carrier = ContextCarrier_1.ContextCarrier.from(((_c = message === null || message === void 0 ? void 0 : message.properties) === null || _c === void 0 ? void 0 : _c.headers) || {}); const 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); const ret = _dispatchMessage.call(this, fields, message); span.stop(); return ret; } catch (e) { span.error(e); span.stop(); throw e; } }; } } // noinspection JSUnusedGlobalSymbols exports.default = new AMQPLibPlugin(); //# sourceMappingURL=AMQPLibPlugin.js.map