@dynatrace/opentelemetry-azure-functions
Version:
Dynatrace OpenTelemetry package for Azure Functions
85 lines (82 loc) • 3.51 kB
JavaScript
;
/*
Copyright 2022 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.isAzureHttpRequestV3 = exports.isAzureHttpRequest = exports._testClearTracer = exports.getTracer = exports.extractContext = void 0;
const opentelemetry_core_1 = require("@dynatrace/opentelemetry-core");
const api_1 = require("@opentelemetry/api");
const version_1 = require("./version");
// ============================================================================
function extractContext(triggerType, req) {
if (triggerType === opentelemetry_core_1.SemConvTrace.FaasTriggerValues.HTTP && req != null) {
// req instanceof HttpRequest is not safe as the passed request object might not come from the same
// package (in case of multiple @azure/functions versions) or if Azure runtime passes plain object which looks
// like HttpRequest but is not created by the HttpRequest constructor.
if (isAzureHttpRequestHeaders(req.headers)) {
const headersTextMapGetter = {
get(carrier, key) {
var _a;
return (_a = carrier.get(key)) !== null && _a !== void 0 ? _a : undefined;
},
keys(carrier) {
const hNames = [];
carrier.forEach((value, key) => hNames.push(key));
return hNames;
}
};
return api_1.propagation.extract(api_1.ROOT_CONTEXT, req.headers, headersTextMapGetter);
}
return api_1.propagation.extract(api_1.ROOT_CONTEXT, req.headers);
}
return api_1.ROOT_CONTEXT;
}
exports.extractContext = extractContext;
let tracer;
function getTracer() {
if (tracer == null) {
tracer = api_1.trace.getTracer("@dynatrace/opentelemetry-azure-functions", version_1.VERSIONS.fullVersion);
}
return tracer;
}
exports.getTracer = getTracer;
function _testClearTracer() {
tracer = undefined;
}
exports._testClearTracer = _testClearTracer;
function isAzureHttpRequest(obj) {
return isAzureHttpRequestInternal(obj);
}
exports.isAzureHttpRequest = isAzureHttpRequest;
function isAzureHttpRequestV3(obj) {
return isAzureHttpRequestV3Internal(obj);
}
exports.isAzureHttpRequestV3 = isAzureHttpRequestV3;
function isAzureHttpRequestInternal(obj) {
if (isObject(obj)) {
return isAzureHttpRequestHeaders(obj.headers) &&
typeof (obj.method) === "string" &&
typeof (obj.url) === "string";
}
return false;
}
function isAzureHttpRequestHeaders(maybeHeaders) {
return isObject(maybeHeaders) && typeof (maybeHeaders.get) === "function" &&
typeof (maybeHeaders.forEach) === "function";
}
function isAzureHttpRequestV3Internal(obj) {
return isObject(obj) && isObject(obj.headers);
}
function isObject(maybeObj) {
return maybeObj != null && typeof (maybeObj) === "object";
}
//# sourceMappingURL=Util.js.map