skywalking-backend-js-netease
Version:
The NodeJS agent for Apache SkyWalking
76 lines • 4.43 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 AWSLambdaGatewayAPIHTTP extends AWSLambdaTriggerPlugin_1.AWSLambdaTriggerPlugin {
start(event, context) {
var _a, _b, _c, _d, _e;
const headers = event.headers;
const reqCtx = event.requestContext;
const http = reqCtx === null || reqCtx === void 0 ? void 0 : reqCtx.http;
const method = http === null || http === void 0 ? void 0 : http.method;
const proto = (http === null || http === void 0 ? void 0 : http.protocol) ? http.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 = (_a = headers === null || headers === void 0 ? void 0 : headers.host) !== null && _a !== void 0 ? _a : ((reqCtx === null || reqCtx === void 0 ? void 0 : reqCtx.domainName) || '');
const hostport = host ? (port ? `${host}:${port}` : host) : port;
const operation = (_c = (_b = http === null || http === void 0 ? void 0 : http.path) !== null && _b !== void 0 ? _b : event.rawPath) !== null && _c !== void 0 ? _c : (context.functionName ? `/${context.functionName}` : '/');
const query = event.rawQueryString
? `?${event.rawQueryString}`
: 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_GATEWAYAPIHTTP;
span.peer = (_e = (_d = http === null || http === void 0 ? void 0 : http.sourceIp) !== null && _d !== void 0 ? _d : headers === null || headers === void 0 ? void 0 : headers['x-forwarded-for']) !== null && _e !== void 0 ? _e : '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 AWSLambdaGatewayAPIHTTP();
//# sourceMappingURL=AWSLambdaGatewayAPIHTTP.js.map