/*
 * Copyright The OpenTelemetry Authors
 * Copyright 2022 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/gce.go
 */
import { diag } from '@opentelemetry/api';
import * as metadata from 'gcp-metadata';
const MACHINE_TYPE_METADATA_ATTR = 'machine-type';
const ID_METADATA_ATTR = 'id';
const HOST_NAME_METADATA_ATTR = 'name';
const ZONE_METADATA_ATTR = 'zone';
export async function onGce() {
    try {
        await metadata.instance(MACHINE_TYPE_METADATA_ATTR);
        return true;
    }
    catch (err) {
        diag.debug('Could not fetch metadata attribute %s, assuming not on GCE. Error was %s', MACHINE_TYPE_METADATA_ATTR, err);
        return false;
    }
}
/**
 * The machine type of the instance on which this program is running. Check that {@link
 * onGce()} is true before calling this, or it may throw exceptions.
 */
export async function hostType() {
    return metadata.instance(MACHINE_TYPE_METADATA_ATTR);
}
/**
 * The instance ID of the instance on which this program is running. Check that {@link onGce()}
 * is true before calling this, or it may throw exceptions.
 */
export async function hostId() {
    // May be a bignumber.js BigNumber which can just be converted with toString(). See
    // https://github.com/googleapis/gcp-metadata#take-care-with-large-number-valued-properties
    const id = await metadata.instance(ID_METADATA_ATTR);
    return id.toString();
}
/**
 * The instance ID of the instance on which this program is running. Check that {@link onGce()}
 * is true before calling this, or it may throw exceptions.
 */
export async function hostName() {
    return metadata.instance(HOST_NAME_METADATA_ATTR);
}
/**
 * The zone and region in which this program is running. Check that {@link onGce()} is true
 * before calling this, or it may throw exceptions.
 */
export async function availabilityZoneAndRegion() {
    const fullZone = await metadata.instance(ZONE_METADATA_ATTR);
    // Format described in
    // https://cloud.google.com/compute/docs/metadata/default-metadata-values#vm_instance_metadata
    const re = /projects\/\d+\/zones\/(?<zone>(?<region>\w+-\w+)-\w+)/;
    const { zone, region } = fullZone.match(re)?.groups ?? {};
    if (!zone || !region) {
        throw new Error(`zone was not in the expected format: projects/PROJECT_NUM/zones/COUNTRY-REGION-ZONE. Got ${fullZone}`);
    }
    return { zone, region };
}
//# sourceMappingURL=gce.js.map