skywalking-backend-js-netease
Version:
The NodeJS agent for Apache SkyWalking
72 lines • 3.43 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 });
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 ContextCarrier_1 = require("../trace/context/ContextCarrier");
const DummySpan_1 = tslib_1.__importDefault(require("../trace/span/DummySpan"));
const AgentConfig_1 = require("../config/AgentConfig");
const HttpPlugin_1 = tslib_1.__importDefault(require("./HttpPlugin"));
const CustomMonitor_1 = tslib_1.__importDefault(require("./CustomMonitor"));
class ExpressPlugin {
constructor() {
this.module = 'express';
this.versions = '*';
}
install(installer) {
this.interceptServerRequest(installer);
}
interceptServerRequest(installer) {
const router = installer.require('express/lib/router');
const _handle = router.handle;
router.handle = function (req, res, next) {
var _a;
const carrier = ContextCarrier_1.ContextCarrier.from(req.headers || {});
const operation = (req.url || '/');
const span = AgentConfig_1.ignoreHttpMethodCheck((_a = req.method) !== null && _a !== void 0 ? _a : 'GET')
? DummySpan_1.default.create()
: ContextManager_1.default.current.newEntrySpan(operation, carrier, [Component_1.Component.HTTP_SERVER, Component_1.Component.EXPRESS]);
span.component = Component_1.Component.EXPRESS;
if (span.depth)
// if we inherited from http then just change component ID and let http do the work
return _handle.apply(this, arguments);
// collect
CustomMonitor_1.default(req, span);
return HttpPlugin_1.default.wrapHttpResponse(span, req, res, () => {
// http plugin disabled, we use its mechanism anyway
try {
return _handle.call(this, req, res, (err) => {
span.error(err);
next.call(this, err);
});
}
finally {
// req.protocol is only possibly available after call to _handle()
span.tag(Tag_1.default.httpURL((req.protocol ? req.protocol + '://' : '') + (req.headers.host || '') + req.url));
}
});
};
}
}
// noinspection JSUnusedGlobalSymbols
exports.default = new ExpressPlugin();
//# sourceMappingURL=ExpressPlugin.js.map