@dynatrace/opentelemetry-azure-functions
Version:
Dynatrace OpenTelemetry package for Azure Functions
151 lines (148 loc) • 6.08 kB
JavaScript
;
/*
Copyright 2023 Dynatrace LLC
Licensed 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 });
exports.getExitAttributes = exports.getStartAttributes = void 0;
const util_1 = require("util");
const functions_1 = require("@azure/functions");
const opentelemetry_core_1 = require("@dynatrace/opentelemetry-core");
const Bindings_1 = require("./Bindings");
const Logger_1 = require("./Logger");
// ============================================================================
/**
* Returns span attributes from captured http headers (e.g. referer, user-agent, etc.. )
* @param req the azure function HttpRequest request object
*/
function captureHttpHeaders(req) {
const attrs = {};
const headersToCapture = (0, opentelemetry_core_1.getHttpHeadersToCapture)();
for (const [headerName, semConvKey] of Object.entries(headersToCapture)) {
const headerValue = headerGetter(req, headerName);
if (headerValue != null) {
attrs[semConvKey] = headerValue;
}
}
const forwarded = headerGetter(req, "forwarded");
if (forwarded != null) {
attrs[opentelemetry_core_1.SemConvTrace.DT_HTTP_REQUEST_HEADER_FORWARDED] = forwarded;
}
else {
const xForwardedFor = headerGetter(req, "x-forwarded-for");
if (xForwardedFor != null) {
attrs[opentelemetry_core_1.SemConvTrace.DT_HTTP_REQUEST_HEADER_X_FORWARDED_FOR] = xForwardedFor;
}
}
return attrs;
}
// ============================================================================
/**
* Helper method to get header values from two different request types used by @azure/functions v3 and v4.
* @param req Http request object with headers
* @param headerName name of the header to look for
* @returns header value or null
*/
function headerGetter(req, headerName) {
if (req instanceof functions_1.HttpRequest) {
return req.headers.get(headerName);
}
else {
return req.headers[headerName];
}
}
// ============================================================================
/**
* Get Span start attributes depending on the given trigger type.
*
* @note currently only Http trigger is supported
*
* @param triggerType the azure function trigger type
* @param context the azure function context
* @param args the arguments passed to the function
* @returns the detected span start attributes
*/
function getStartAttributes(triggerType, req) {
let attributes;
if (triggerType === opentelemetry_core_1.SemConvTrace.FaasTriggerValues.HTTP && req != null) {
attributes = Object.assign({ [opentelemetry_core_1.SemConvTrace.FAAS_TRIGGER]: triggerType, [opentelemetry_core_1.SemConvTrace.HTTP_METHOD]: req.method || "GET", [opentelemetry_core_1.SemConvTrace.HTTP_URL]: req.url }, captureHttpHeaders(req));
}
else {
attributes = {
[opentelemetry_core_1.SemConvTrace.FAAS_TRIGGER]: triggerType
};
}
if (Logger_1.logger.debugEnabled) {
Logger_1.logger.debug(`startAttributes: ${(0, util_1.inspect)(attributes)}`);
}
return attributes;
}
exports.getStartAttributes = getStartAttributes;
// ----------------------------------------------------------------------------
/**
* Get Span exit attributes depending on the given trigger type.
*
* @note currently only Http trigger is supported
*
* @param triggerType the trigger type
* @param context the azure function context
* @param retVal the retrun value of the handler
* @returns the detected span exit attributes
*/
function getExitAttributes(triggerType, context, retVal) {
let attributes = {};
if (triggerType === opentelemetry_core_1.SemConvTrace.FaasTriggerValues.HTTP) {
const name = (0, Bindings_1.getHttpOutBindingName)(context);
if (name != null) {
let httpResponse;
if (name === "$return") {
httpResponse = retVal;
}
else {
if (context.res != null && context.bindings[name] == null) {
httpResponse = context.res;
}
else {
httpResponse = context.bindings[name];
}
}
if (httpResponse != null) {
// statusCode field is preferred, use status only if it is no function/setter
let httpStatus = httpResponse.statusCode;
if (httpStatus == null && typeof (httpResponse.status) !== "function") {
httpStatus = httpResponse.status;
}
// no status given defaults to 200
httpStatus = +(httpStatus !== null && httpStatus !== void 0 ? httpStatus : 200);
if (!isNaN(httpStatus)) {
attributes = {
[opentelemetry_core_1.SemConvTrace.HTTP_STATUS_CODE]: httpStatus
};
}
}
else {
// if no response object is given Azure defaults to 204
attributes = {
[opentelemetry_core_1.SemConvTrace.HTTP_STATUS_CODE]: 204
};
}
}
else {
Logger_1.logger.info("can't get HTTP out binding name");
}
}
if (Logger_1.logger.debugEnabled) {
Logger_1.logger.debug(`exitAttributes: ${(0, util_1.inspect)(attributes)}`);
}
return attributes;
}
exports.getExitAttributes = getExitAttributes;
//# sourceMappingURL=AttributeDetector.js.map