skywalking-apache
Version:
The NodeJS agent for Apache SkyWalking
111 lines • 5.49 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 url_1 = require("url");
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 logging_1 = require("../logging");
var logger = logging_1.createLogger(__filename);
var HttpPlugin = /** @class */ (function () {
function HttpPlugin() {
this.module = 'http';
this.versions = '*';
}
HttpPlugin.prototype.install = function () {
this.interceptClientRequest();
this.interceptServerRequest();
};
HttpPlugin.prototype.interceptClientRequest = function () {
var http = require('http');
(function (original) {
http.request = function () {
var argc = arguments.length;
var url = arguments[0];
var options = argc > 1 ? (typeof arguments[1] === 'function' ? {} : arguments[1]) : {};
var callback = typeof arguments[argc - 1] === 'function' ? arguments[argc - 1] : undefined;
var _a = url instanceof url_1.URL
? url
: typeof url === 'string'
? new url_1.URL(url)
: {
host: (url.host || url.hostname || 'unknown') + ':' + url.port,
pathname: url.path || '/',
}, host = _a.host, pathname = _a.pathname;
var operation = pathname.replace(/\?.*$/g, '');
var span = ContextManager_1.default.current.newExitSpan(operation, host).start();
span.component = Component_1.Component.HTTP;
span.layer = Tracing_pb_1.SpanLayer.HTTP;
span.tag(Tag_1.default.httpURL(host + pathname));
var snapshot = ContextManager_1.default.current.capture();
var request = original.apply(this, arguments);
span.extract().items.forEach(function (item) {
request.setHeader(item.key, item.value);
});
request.on('response', function (res) {
res.prependListener('end', function () {
span.tag(Tag_1.default.httpStatusCode(res.statusCode)).tag(Tag_1.default.httpStatusMsg(res.statusMessage));
var callbackSpan = ContextManager_1.default.current.newLocalSpan('callback').start();
callbackSpan.layer = Tracing_pb_1.SpanLayer.HTTP;
callbackSpan.component = Component_1.Component.HTTP;
ContextManager_1.default.current.restore(snapshot);
if (callback) {
callback(res);
}
callbackSpan.stop();
});
});
span.stop();
return request;
};
})(http.request);
};
HttpPlugin.prototype.interceptServerRequest = function () {
var http = require('http');
(function (original) {
http.Server.prototype.emit = function () {
if (arguments[0] !== 'request') {
return original.apply(this, arguments);
}
var _a = [arguments[1], arguments[2]], req = _a[0], res = _a[1];
var headers = req.rawHeaders || [];
var headersMap = {};
for (var i = 0; i < headers.length / 2; i += 2) {
headersMap[headers[i]] = headers[i + 1];
}
var carrier = ContextCarrier_1.ContextCarrier.from(headersMap);
var span = ContextManager_1.default.current.newEntrySpan('/', carrier).start();
span.operation = (req.url || '/').replace(/\?.*/g, '');
span.component = Component_1.Component.HTTP_SERVER;
span.layer = Tracing_pb_1.SpanLayer.HTTP;
span.tag(Tag_1.default.httpURL(req.url));
span.tag(Tag_1.default.httpStatusCode(res.statusCode)).tag(Tag_1.default.httpStatusMsg(res.statusMessage)).stop();
return original.apply(this, arguments);
};
})(http.Server.prototype.emit);
};
return HttpPlugin;
}());
// noinspection JSUnusedGlobalSymbols
exports.default = new HttpPlugin();
//# sourceMappingURL=HttpPlugin.js.map