skywalking-backend-js-netease
Version:
The NodeJS agent for Apache SkyWalking
94 lines • 4.29 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 url_1 = require("url");
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 DummySpan_1 = tslib_1.__importDefault(require("../trace/span/DummySpan"));
const AgentConfig_1 = require("../config/AgentConfig");
class AxiosPlugin {
constructor() {
this.module = 'axios';
this.versions = '*';
}
install(installer) {
this.interceptClientRequest(installer);
}
interceptClientRequest(installer) {
const Axios = installer.require('axios/lib/core/Axios');
const _request = Axios.prototype.request;
Axios.prototype.request = function (url, config) {
var _a, _b;
if (typeof url === 'string')
config = config ? Object.assign(Object.assign({}, config), { url }) : { url };
else
config = url ? Object.assign({}, url) : {};
const { origin, host, pathname: operation } = new url_1.URL(config.url, (_a = config.baseURL) !== null && _a !== void 0 ? _a : (_b = this.defaults) === null || _b === void 0 ? void 0 : _b.baseURL);
const method = (config.method || 'GET').toUpperCase();
const span = AgentConfig_1.ignoreHttpMethodCheck(method)
? DummySpan_1.default.create()
: ContextManager_1.default.current.newExitSpan(operation, Component_1.Component.AXIOS, Component_1.Component.HTTP);
span.start();
try {
config.headers = config.headers ? Object.assign({}, config.headers) : {};
span.component = Component_1.Component.AXIOS;
span.layer = Tracing_pb_1.SpanLayer.HTTP;
span.peer = host;
span.tag(Tag_1.default.httpURL(origin + operation));
span.tag(Tag_1.default.httpMethod(method));
span.inject().items.forEach((item) => (config.headers[item.key] = item.value));
const copyStatus = (response) => {
if (response) {
if (response.status) {
span.tag(Tag_1.default.httpStatusCode(response.status));
if (response.status >= 400)
span.errored = true;
}
if (response.statusText)
span.tag(Tag_1.default.httpStatusMsg(response.statusText));
}
};
const ret = _request.call(this, config).then((res) => {
copyStatus(res);
span.stop();
return res;
}, (err) => {
copyStatus(err.response);
span.error(err);
span.stop();
return Promise.reject(err);
});
span.async();
return ret;
}
catch (err) {
span.error(err);
span.stop();
throw err;
}
};
}
}
// noinspection JSUnusedGlobalSymbols
exports.default = new AxiosPlugin();
//# sourceMappingURL=AxiosPlugin.js.map