UNPKG

@dynatrace/opentelemetry-gcf

Version:

Dynatrace OpenTelemetry package for Google Cloud Functions

104 lines (101 loc) 4.78 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. */ var _a; Object.defineProperty(exports, "__esModule", { value: true }); exports.gcfDetector = exports.cFunctionName = void 0; const api_1 = require("@opentelemetry/api"); const gcpMetadata = require("gcp-metadata"); const opentelemetry_core_1 = require("@dynatrace/opentelemetry-core"); // ============================================================================ exports.cFunctionName = (_a = process.env.K_SERVICE) !== null && _a !== void 0 ? _a : ""; // ============================================================================ class GcfResourceDetector { constructor() { this._logger = new opentelemetry_core_1.ComponentLogger("GcfResourceDetector"); // TODO exchange with OTel logger } // ---------------------------------------------------------------------------- async detect() { const attrs = { [opentelemetry_core_1.SemConvResource.CLOUD_PROVIDER]: opentelemetry_core_1.SemConvResource.CloudProviderValues.GCP, [opentelemetry_core_1.SemConvResource.CLOUD_PLATFORM]: opentelemetry_core_1.SemConvResource.CloudPlatformValues.GCP_CLOUD_FUNCTIONS }; let projectId = ""; let region = ""; await api_1.context.with((0, opentelemetry_core_1.dtSuppressTracing)(api_1.context.active()), async () => { if (await gcpMetadata.isAvailable()) { [projectId, region] = await Promise.all([ this._getProjectId(), this._getRegion() ]); } else { this._logger.warn("GCP Metadata Server not available."); } }); this._addAttribute(attrs, opentelemetry_core_1.SemConvResource.FAAS_NAME, exports.cFunctionName); this._addAttribute(attrs, opentelemetry_core_1.SemConvResource.FAAS_VERSION, process.env.K_REVISION); this._addAttribute(attrs, opentelemetry_core_1.SemConvResource.CLOUD_ACCOUNT_ID, projectId); this._addAttribute(attrs, opentelemetry_core_1.SemConvResource.CLOUD_REGION, region); this._addAttribute(attrs, opentelemetry_core_1.SemConvResource.GCP_REGION, region); this._addAttribute(attrs, opentelemetry_core_1.SemConvResource.GCP_PROJECT_ID, projectId); this._addAttribute(attrs, opentelemetry_core_1.SemConvResource.GCP_INSTANCE_NAME, exports.cFunctionName); this._addFaasId(attrs, exports.cFunctionName, projectId, region); return attrs; } // ---------------------------------------------------------------------------- _addAttribute(attrs, key, value) { if (value) { attrs[key] = value; } else { this._logger.warn(`Could not detect resource "${key}"`); } } // ---------------------------------------------------------------------------- _addFaasId(attrs, functionName, projectId, region) { const key = opentelemetry_core_1.SemConvResource.FAAS_ID; if (functionName && projectId && region) { // ID in resource name format: https://cloud.google.com/asset-inventory/docs/resource-name-format attrs[key] = `//cloudfunctions.googleapis.com/projects/${projectId}/locations/${region}/functions/${functionName}`; } else { this._logger.warn(`Could not detect resource "${key}`); } } // ---------------------------------------------------------------------------- async _getProjectId() { try { return await gcpMetadata.project("project-id"); } catch (_a) { return ""; } } // ---------------------------------------------------------------------------- async _getRegion() { try { // returned string should have the form "projects/<numeric-project-id>/regions/<region>"" const region = await gcpMetadata.instance("region"); if (region) { return region.split("/").pop() || ""; } } catch (_a) { // do nothing } return ""; } } exports.gcfDetector = new GcfResourceDetector(); //# sourceMappingURL=Resources.js.map