skywalking-backend-js-netease
Version:
The NodeJS agent for Apache SkyWalking
78 lines • 4.67 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");
const AWSLambdaTriggerPlugin_1 = require("./AWSLambdaTriggerPlugin");
class AWSLambdaGatewayAPIREST extends AWSLambdaTriggerPlugin_1.AWSLambdaTriggerPlugin {
start(event, context) {
var _a, _b, _c, _d, _e, _f, _g;
const headers = event.headers;
const reqCtx = event.requestContext;
const method = (_a = reqCtx === null || reqCtx === void 0 ? void 0 : reqCtx.httpMethod) !== null && _a !== void 0 ? _a : event.httpMethod;
const proto = (reqCtx === null || reqCtx === void 0 ? void 0 : reqCtx.protocol) ? reqCtx.protocol.split('/')[0].toLowerCase() : headers === null || headers === void 0 ? void 0 : headers['X-Forwarded-Proto'];
const port = (headers === null || headers === void 0 ? void 0 : headers['X-Forwarded-Port']) || '';
const host = (_b = headers === null || headers === void 0 ? void 0 : headers.Host) !== null && _b !== void 0 ? _b : ((reqCtx === null || reqCtx === void 0 ? void 0 : reqCtx.domainName) || '');
const hostport = host ? (port ? `${host}:${port}` : host) : port;
const operation = (_d = (_c = reqCtx === null || reqCtx === void 0 ? void 0 : reqCtx.path) !== null && _c !== void 0 ? _c : event.path) !== null && _d !== void 0 ? _d : (context.functionName ? `/${context.functionName}` : '/');
const query = event.multiValueQueryStringParameters
? '?' +
Object.entries(event.multiValueQueryStringParameters)
.map(([k, vs]) => vs.map((v) => `${k}=${v}`).join('&'))
.join('&')
: event.queryStringParameters
? '?' +
Object.entries(event.queryStringParameters)
.map(([k, v]) => `${k}=${v}`)
.join('&')
: '';
const carrier = headers && ContextCarrier_1.ContextCarrier.from(headers);
const span = method && AgentConfig_1.ignoreHttpMethodCheck(method)
? DummySpan_1.default.create()
: ContextManager_1.default.current.newEntrySpan(operation, carrier);
span.layer = Tracing_pb_1.SpanLayer.HTTP;
span.component = Component_1.Component.AWSLAMBDA_GATEWAYAPIREST;
span.peer = (_g = (_f = (_e = reqCtx === null || reqCtx === void 0 ? void 0 : reqCtx.identity) === null || _e === void 0 ? void 0 : _e.sourceIp) !== null && _f !== void 0 ? _f : headers === null || headers === void 0 ? void 0 : headers['X-Forwarded-For']) !== null && _g !== void 0 ? _g : 'Unknown';
if (method)
span.tag(Tag_1.default.httpMethod(method));
if (hostport && proto)
span.tag(Tag_1.default.httpURL(new url_1.URL(`${proto}://${hostport}${operation}${query}`).toString()));
span.start();
return span;
}
stop(span, err, res) {
const statusCode = (res === null || res === void 0 ? void 0 : res.statusCode) || (typeof res === 'number' ? res : err ? 500 : null);
if (statusCode) {
if (statusCode >= 400)
span.errored = true;
span.tag(Tag_1.default.httpStatusCode(statusCode));
}
span.stop();
}
}
exports.default = new AWSLambdaGatewayAPIREST();
//# sourceMappingURL=AWSLambdaGatewayAPIREST.js.map