UNPKG

@kangc/skywalking-backend-js

Version:

The NodeJS agent for Apache SkyWalking

94 lines 4.41 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 }); 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 DummySpan_1 = tslib_1.__importDefault(require("../trace/span/DummySpan")); var AgentConfig_1 = require("../config/AgentConfig"); var AxiosPlugin = /** @class */ (function () { function AxiosPlugin() { this.module = 'axios'; this.versions = '*'; } AxiosPlugin.prototype.install = function (installer) { this.interceptClientRequest(installer); }; AxiosPlugin.prototype.interceptClientRequest = function (installer) { var Axios = installer.require('axios/lib/core/Axios'); var _request = Axios.prototype.request; Axios.prototype.request = function (url, config) { if (typeof url === 'string') config = config ? tslib_1.__assign(tslib_1.__assign({}, config), { url: url }) : { url: url }; else config = url ? tslib_1.__assign({}, url) : {}; var _a = new url_1.URL(config.url, config.baseURL ?? this.defaults?.baseURL), origin = _a.origin, host = _a.host, operation = _a.pathname; // TODO: this may throw invalid URL var method = (config.method || 'GET').toUpperCase(); var 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 ? tslib_1.__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(function (item) { return (config.headers[item.key] = item.value); }); var copyStatus_1 = function (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)); } }; var ret = _request.call(this, config).then(function (res) { copyStatus_1(res); span.stop(); return res; }, function (err) { copyStatus_1(err.response); span.error(err); span.stop(); return Promise.reject(err); }); span.async(); return ret; } catch (err) { span.error(err); span.stop(); throw err; } }; }; return AxiosPlugin; }()); // noinspection JSUnusedGlobalSymbols exports.default = new AxiosPlugin(); //# sourceMappingURL=AxiosPlugin.js.map