skywalking-backend-js-netease
Version:
The NodeJS agent for Apache SkyWalking
106 lines • 4.42 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 ContextCarrier_1 = require("../trace/context/ContextCarrier");
const DummySpan_1 = tslib_1.__importDefault(require("../trace/span/DummySpan"));
const AgentConfig_1 = require("../config/AgentConfig");
class AzureHttpTriggerPlugin {
wrap(func) {
return function (context) {
let outRet = true;
let outName;
for (const def of context.bindingDefinitions || []) {
if (def.type === 'http' && def.directioun === 'out') {
outName = def.name;
outRet = outName === '$return';
}
}
const req = context.req;
const url = new url_1.URL(req.url);
const operation = url.pathname.replace(/\?.*$/g, '');
const carrier = ContextCarrier_1.ContextCarrier.from(req.headers);
const 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();
let stop = (_ret) => {
stop = (v) => v;
const res = outRet ? _ret : context.bindings[outName] || context.res;
const 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;
};
const done = context.done;
let 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);
};
let 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((_ret) => {
return stop(_ret);
}, (err) => {
span.error(err);
stop();
return Promise.reject(err);
});
}
span.async();
return ret;
};
}
}
// noinspection JSUnusedGlobalSymbols
exports.default = new AzureHttpTriggerPlugin();
//# sourceMappingURL=AzureHttpTriggerPlugin.js.map