UNPKG

@kangc/skywalking-backend-js

Version:

The NodeJS agent for Apache SkyWalking

89 lines 5.18 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 AWSLambdaTriggerPlugin_1 = require("./AWSLambdaTriggerPlugin"); var AWSLambdaGatewayAPIREST = /** @class */ (function (_super) { tslib_1.__extends(AWSLambdaGatewayAPIREST, _super); function AWSLambdaGatewayAPIREST() { return _super !== null && _super.apply(this, arguments) || this; } AWSLambdaGatewayAPIREST.prototype.start = function (event, context) { var _a, _b, _c, _d, _e, _f, _g; var headers = event.headers; var reqCtx = event.requestContext; var method = (_a = reqCtx === null || reqCtx === void 0 ? void 0 : reqCtx.httpMethod) !== null && _a !== void 0 ? _a : event.httpMethod; var 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']; var port = (headers === null || headers === void 0 ? void 0 : headers['X-Forwarded-Port']) || ''; var 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) || ''); var hostport = host ? (port ? host + ":" + port : host) : port; var 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 : '/'); var query = event.multiValueQueryStringParameters ? '?' + Object.entries(event.multiValueQueryStringParameters) .map(function (_a) { var k = _a[0], vs = _a[1]; return vs.map(function (v) { return k + "=" + v; }).join('&'); }) .join('&') : event.queryStringParameters ? '?' + Object.entries(event.queryStringParameters) .map(function (_a) { var k = _a[0], v = _a[1]; return k + "=" + v; }) .join('&') : ''; var carrier = headers && ContextCarrier_1.ContextCarrier.from(headers); var 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; }; AWSLambdaGatewayAPIREST.prototype.stop = function (span, err, res) { var 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(); }; return AWSLambdaGatewayAPIREST; }(AWSLambdaTriggerPlugin_1.AWSLambdaTriggerPlugin)); exports.default = new AWSLambdaGatewayAPIREST(); //# sourceMappingURL=AWSLambdaGatewayAPIREST.js.map