UNPKG

@dynatrace/opentelemetry-azure-functions

Version:

Dynatrace OpenTelemetry package for Azure Functions

85 lines (82 loc) 3.81 kB
"use strict"; /* 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.detectResource = void 0; const util_1 = require("util"); const opentelemetry_core_1 = require("@dynatrace/opentelemetry-core"); const Logger_1 = require("./Logger"); // ============================================================================ // example: af7ce8a8-ec27-4b24-af88-324b2712077a+GstHttp1Res-GermanyWestCentralwebspace-Linux const ownerNamePattern = /([^+]+)\+(.+)-([^-]+)webspace(?:-[^-]+)?/; // ---------------------------------------------------------------------------- /** * Detects azure function resource attributes. * * @note we could consider to split this into a static and dynamic part for efficency reasons but * there is some risk that environment gets updated by runtime. * * @param context the function call context * @returns detected resource attributes */ function detectResource(functionName) { const res = { [opentelemetry_core_1.SemConvResource.CLOUD_PROVIDER]: opentelemetry_core_1.SemConvResource.CloudProviderValues.AZURE, [opentelemetry_core_1.SemConvResource.CLOUD_PLATFORM]: opentelemetry_core_1.SemConvResource.CloudPlatformValues.AZURE_FUNCTIONS }; const functionAppName = process.env.WEBSITE_SITE_NAME; if (typeof (functionName) === "string") { res[opentelemetry_core_1.SemConvResource.FAAS_NAME] = functionAppName != null ? `${functionAppName}/${functionName}` : functionName; } else { Logger_1.logger.warn(`unable to detect functionName - ${typeof (functionName)}`); } const ownerName = process.env.WEBSITE_OWNER_NAME; let resourceGroup = process.env.WEBSITE_RESOURCE_GROUP; let regionName = process.env.REGION_NAME; let subscriptionId; if (ownerName != null) { const m = ownerNamePattern.exec(ownerName); if (m != null) { subscriptionId = m[1]; if (resourceGroup == null) { resourceGroup = m[2]; } if (regionName == null) { regionName = m[3]; } } } else { Logger_1.logger.warn("unable to detect ownerName"); } if (regionName != null) { res[opentelemetry_core_1.SemConvResource.CLOUD_REGION] = regionName; if (resourceGroup != null && subscriptionId != null && functionAppName != null && functionName != null) { res[opentelemetry_core_1.SemConvResource.FAAS_ID] = `/subscriptions/${subscriptionId}/resourceGroups/${resourceGroup}/providers/Microsoft.Web/sites/${functionAppName}/functions/${functionName}`; } else { Logger_1.logger.warn(`unable to build faas.id - resourceGroup: ${resourceGroup}, subscriptionId: ${subscriptionId}, ` + `functionAppName: ${functionAppName}, functionName: ${functionName}, ownerName: ${ownerName}`); } } else { Logger_1.logger.warn(`unable to detect regionName - ownerName: ${ownerName}`); } if (Logger_1.logger.debugEnabled) { Logger_1.logger.debug(`detected resource: ${(0, util_1.inspect)(res)}`); } return res; } exports.detectResource = detectResource; //# sourceMappingURL=ResourceDetector.js.map