UNPKG

@kangc/skywalking-backend-js

Version:

The NodeJS agent for Apache SkyWalking

110 lines 4.64 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 ContextCarrier_1 = require("../trace/context/ContextCarrier"); var DummySpan_1 = tslib_1.__importDefault(require("../trace/span/DummySpan")); var AgentConfig_1 = require("../config/AgentConfig"); var AzureHttpTriggerPlugin = /** @class */ (function () { function AzureHttpTriggerPlugin() { } AzureHttpTriggerPlugin.prototype.wrap = function (func) { return function (context) { var outRet = true; var outName; for (var _i = 0, _a = context.bindingDefinitions || []; _i < _a.length; _i++) { var def = _a[_i]; if (def.type === 'http' && def.directioun === 'out') { outName = def.name; outRet = outName === '$return'; } } var req = context.req; var url = new url_1.URL(req.url); var operation = url.pathname.replace(/\?.*$/g, ''); var carrier = ContextCarrier_1.ContextCarrier.from(req.headers); var span = AgentConfig_1.ignoreHttpMethodCheck(req.method) ? DummySpan_1.default.create() : ContextManager_1.default.current.newEntrySpan(operation, carrier); span.layer = Tracing_pb_1.SpanLayer.HTTP; span.component = Component_1.Component.AZURE_HTTPTRIGGER; span.peer = (req.headers['x-forwarded-for'] || '???').split(',').shift(); span.tag(Tag_1.default.httpMethod(req.method)); span.tag(Tag_1.default.httpURL(url.origin + url.pathname)); span.start(); var stop = function (_ret) { stop = function (v) { return v; }; var res = outRet ? _ret : context.bindings[outName] || context.res; var status = span.errored ? 500 : !res ? 0 : res.status || 200; if (status) { span.tag(Tag_1.default.httpStatusCode(status)); if (status >= 400) span.errored = true; } span.stop(); return _ret; }; var done = context.done; var did = false; context.done = function (err, _ret) { if (!did) { if (err) span.error(err); if (arguments.length >= 2) arguments[1] = stop(_ret); else stop(); did = true; } return done.apply(this, arguments); }; var ret; try { ret = func.apply(this, arguments); } catch (err) { span.error(err); stop(); throw err; } if (ret && typeof ret.then === 'function') { // generic Promise check ret = ret.then(function (_ret) { return stop(_ret); }, function (err) { span.error(err); stop(); return Promise.reject(err); }); } span.async(); return ret; }; }; return AzureHttpTriggerPlugin; }()); // noinspection JSUnusedGlobalSymbols exports.default = new AzureHttpTriggerPlugin(); //# sourceMappingURL=AzureHttpTriggerPlugin.js.map