UNPKG

@opentelemetry/resource-detector-gcp

Version:
129 lines 4.66 kB
"use strict"; /* * Copyright The OpenTelemetry Authors * * 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 * * https://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.gcpDetector = void 0; const gcpMetadata = require("gcp-metadata"); const api_1 = require("@opentelemetry/api"); const core_1 = require("@opentelemetry/core"); const semantic_conventions_1 = require("@opentelemetry/semantic-conventions"); /** * The GcpDetector can be used to detect if a process is running in the Google * Cloud Platform and return a {@link Resource} populated with metadata about * the instance. Returns an empty Resource if detection fails. */ class GcpDetector { detect(_config) { const attributes = api_1.context.with((0, core_1.suppressTracing)(api_1.context.active()), () => this._getAttributes()); return { attributes }; } /** * Asynchronously gather GCP cloud metadata. */ _getAttributes() { const isAvail = gcpMetadata.isAvailable(); const attributes = { [semantic_conventions_1.SEMRESATTRS_CLOUD_PROVIDER]: (async () => { return (await isAvail) ? semantic_conventions_1.CLOUDPROVIDERVALUES_GCP : undefined; })(), [semantic_conventions_1.SEMRESATTRS_CLOUD_ACCOUNT_ID]: this._getProjectId(isAvail), [semantic_conventions_1.SEMRESATTRS_HOST_ID]: this._getInstanceId(isAvail), [semantic_conventions_1.SEMRESATTRS_HOST_NAME]: this._getHostname(isAvail), [semantic_conventions_1.SEMRESATTRS_CLOUD_AVAILABILITY_ZONE]: this._getZone(isAvail), }; // Add resource attributes for K8s. if (process.env.KUBERNETES_SERVICE_HOST) { attributes[semantic_conventions_1.SEMRESATTRS_K8S_CLUSTER_NAME] = this._getClusterName(isAvail); attributes[semantic_conventions_1.SEMRESATTRS_K8S_NAMESPACE_NAME] = (async () => { return (await isAvail) ? process.env.NAMESPACE : undefined; })(); attributes[semantic_conventions_1.SEMRESATTRS_K8S_POD_NAME] = (async () => { return (await isAvail) ? process.env.HOSTNAME : undefined; })(); attributes[semantic_conventions_1.SEMRESATTRS_CONTAINER_NAME] = (async () => { return (await isAvail) ? process.env.CONTAINER_NAME : undefined; })(); } return attributes; } /** Gets project id from GCP project metadata. */ async _getProjectId(isAvail) { if (!(await isAvail)) { return undefined; } try { return await gcpMetadata.project('project-id'); } catch { return ''; } } /** Gets instance id from GCP instance metadata. */ async _getInstanceId(isAvail) { if (!(await isAvail)) { return undefined; } try { const id = await gcpMetadata.instance('id'); return id.toString(); } catch { return ''; } } /** Gets zone from GCP instance metadata. */ async _getZone(isAvail) { if (!(await isAvail)) { return undefined; } try { const zoneId = await gcpMetadata.instance('zone'); if (zoneId) { return zoneId.split('/').pop(); } return ''; } catch { return ''; } } /** Gets cluster name from GCP instance metadata. */ async _getClusterName(isAvail) { if (!(await isAvail)) { return undefined; } try { return await gcpMetadata.instance('attributes/cluster-name'); } catch { return ''; } } /** Gets hostname from GCP instance metadata. */ async _getHostname(isAvail) { if (!(await isAvail)) { return undefined; } try { return await gcpMetadata.instance('hostname'); } catch { return ''; } } } exports.gcpDetector = new GcpDetector(); //# sourceMappingURL=GcpDetector.js.map