@opentelemetry/resource-detector-gcp
Version:
OpenTelemetry SDK resource detector for GCP
76 lines • 2.59 kB
JavaScript
/*
* Copyright The OpenTelemetry Authors
* Copyright 2023 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
/**
* Implementation in this file copied from
* https://github.com/GoogleCloudPlatform/opentelemetry-operations-go/blob/v1.8.0/detectors/gcp/app_engine.go
*/
import * as metadata from 'gcp-metadata';
import * as gce from './gce';
import * as faas from './faas';
const GAE_SERVICE_ENV = 'GAE_SERVICE';
const GAE_VERSION_ENV = 'GAE_VERSION';
const GAE_INSTANCE_ENV = 'GAE_INSTANCE';
const GAE_ENV = 'GAE_ENV';
const GAE_STANDARD = 'standard';
const ZONE_METADATA_ATTR = 'zone';
export async function onAppEngineStandard() {
return process.env[GAE_ENV] === GAE_STANDARD;
}
export async function onAppEngine() {
return process.env[GAE_SERVICE_ENV] !== undefined;
}
/**
* The service name of the app engine service. Check that {@link onAppEngine()} is true before
* calling this, or it may throw exceptions.
*/
export async function serviceName() {
return lookupEnv(GAE_SERVICE_ENV);
}
/**
* The service version of the app engine service. Check that {@link onAppEngine()} is true
* before calling this, or it may throw exceptions.
*/
export async function serviceVersion() {
return lookupEnv(GAE_VERSION_ENV);
}
/**
* The service instance of the app engine service. Check that {@link onAppEngine()} is true
* before calling this, or it may throw exceptions.
*/
export async function serviceInstance() {
return lookupEnv(GAE_INSTANCE_ENV);
}
/**
* The zone and region in which this program is running. Check that {@link onAppEngine()} is
* true before calling this, or it may throw exceptions.
*/
export async function flexAvailabilityZoneAndRegion() {
return await gce.availabilityZoneAndRegion();
}
/**
* The zone the app engine service is running in. Check that {@link onAppEngineStandard()} is
* true before calling this, or it may throw exceptions.
*/
export async function standardAvailabilityZone() {
const zone = await metadata.instance(ZONE_METADATA_ATTR);
// zone is of the form "projects/233510669999/zones/us15"
return zone.slice(zone.lastIndexOf('/') + 1);
}
/**
* The region the app engine service is running in. Check that {@link onAppEngineStandard()} is
* true before calling this, or it may throw exceptions.
*/
export async function standardCloudRegion() {
return await faas.faasCloudRegion();
}
function lookupEnv(key) {
const val = process.env[key];
if (val === undefined) {
throw new Error(`Environment variable ${key} not found`);
}
return val;
}
//# sourceMappingURL=gae.js.map