@treelab/skywalking-backend-js
Version:
The NodeJS agent for Apache SkyWalking
96 lines • 4.26 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 GrpcPlugin = /** @class */ (function () {
function GrpcPlugin() {
this.module = 'grpc';
this.versions = '*';
}
GrpcPlugin.prototype.install = function (installer) {
this.interceptServerRequest(installer);
};
GrpcPlugin.prototype.interceptServerRequest = function (installer) {
try { // catch no module
var grpcInterceptors = installer.require('grpc/src/client_interceptors');
var _getInterceptingCall_1 = grpcInterceptors.getInterceptingCall;
var _getLastListener_1 = grpcInterceptors.getLastListener;
var span_1;
grpcInterceptors.getInterceptingCall = function () {
var InterceptingCall = _getInterceptingCall_1.apply(this, arguments);
var _start = InterceptingCall.start;
var _cancelWithStatus = InterceptingCall.cancelWithStatus;
var _cancel = InterceptingCall.cancel;
var path = arguments[0].path;
var operation = path;
var split = path.split('/');
var peer = '';
if (split.length && split.length === 3) {
operation = split[2];
peer = split[1];
}
InterceptingCall.start = function () {
span_1 = ContextManager_1.default.current.newExitSpan(operation, Component_1.Component.GRPC);
span_1.component = Component_1.Component.GRPC;
span_1.peer = peer;
span_1.start();
return _start.apply(this, arguments);
};
InterceptingCall.cancel = function () {
span_1.stop();
return _cancel.apply(this, arguments);
};
InterceptingCall.cancelWithStatus = function () {
span_1.stop();
return _cancelWithStatus.apply(this, arguments);
};
return InterceptingCall;
};
grpcInterceptors.getLastListener = function () {
var LastListener = _getLastListener_1.apply(this, arguments);
var _onReceiveStatus = LastListener.onReceiveStatus;
span_1 = ContextManager_1.default.currentSpan;
LastListener.onReceiveStatus = function () {
if (span_1) {
var status_1 = arguments[0].code;
span_1.tag(Tag_1.default.httpStatusCode(status_1));
if (status_1 !== 0) {
span_1.errored = true;
}
span_1.async();
span_1.stop();
}
return _onReceiveStatus.apply(this, arguments);
};
return LastListener;
};
}
catch (e) {
console.log(e);
}
};
return GrpcPlugin;
}());
// noinspection JSUnusedGlobalSymbols
exports.default = new GrpcPlugin();
//# sourceMappingURL=GrpcPlugin.js.map