diff --git a/packages/opencensus-core/src/common/monitored-resource/gcp-metadata-config.ts b/packages/opencensus-core/src/common/monitored-resource/gcp-metadata-config.ts index 828144b41..97b55a5cc 100644 --- a/packages/opencensus-core/src/common/monitored-resource/gcp-metadata-config.ts +++ b/packages/opencensus-core/src/common/monitored-resource/gcp-metadata-config.ts @@ -33,6 +33,7 @@ export class GcpMetadataConfig { static metadata: Record = {}; static readonly logger: loggerTypes.Logger = logger.logger(); static promise: Promise>; + static readonly ID_KEY = 'instance/id'; /** * Initializes metadata service once and load gcp metadata into map. @@ -41,35 +42,36 @@ export class GcpMetadataConfig { if (GcpMetadataConfig.runned) { return GcpMetadataConfig.promise; } + let attributes: Record; + if (GcpMetadataConfig.KUBERNETES_SERVICE_HOST in process.env) { + attributes = monitoredResourceAttributes.GKE; + } else { + attributes = monitoredResourceAttributes.GCE; + } GcpMetadataConfig.promise = - GcpMetadataConfig.getAttribute('instance_id').then(id => { + GcpMetadataConfig.getAttribute('instance/id').then(id => { if (isString(id)) { GcpMetadataConfig.metadata['instance_id'] = id; GcpMetadataConfig.runned = true; - return GcpMetadataConfig.getAttributes(); + return GcpMetadataConfig.getAttributes(attributes); } return Promise.resolve(GcpMetadataConfig.metadata); }); return GcpMetadataConfig.promise; } - private static getAttributes() { - let attributes; - if (GcpMetadataConfig.KUBERNETES_SERVICE_HOST in process.env) { - attributes = monitoredResourceAttributes.GKE; - } else { - attributes = monitoredResourceAttributes.GCE; - } + private static getAttributes(attributes: Record) { const promises: Array> = []; Object.keys(attributes) .filter(key => key !== 'instance_id') .forEach(key => { - promises.push(GcpMetadataConfig.getAttribute(key).then(value => { - if (value) { - GcpMetadataConfig.metadata[key] = value; - } - return value; - })); + promises.push( + GcpMetadataConfig.getAttribute(attributes[key]).then(value => { + if (value) { + GcpMetadataConfig.metadata[key] = value; + } + return value; + })); }); return Promise.all(promises) .then(() => GcpMetadataConfig.metadata) @@ -80,10 +82,10 @@ export class GcpMetadataConfig { * Fetches the requested instance metadata entry. * @param name Attribute name relative to the computeMetadata/v1 prefix */ - static getAttribute(name: string): Promise { + static getAttribute(attribute: string): Promise { const options = { host: GcpMetadataConfig.URL, - path: '/' + monitoredResourceAttributes.GKE[name], + path: '/' + attribute, port: GcpMetadataConfig.PORT, headers: GcpMetadataConfig.HEADER }; diff --git a/packages/opencensus-core/src/common/monitored-resource/types.ts b/packages/opencensus-core/src/common/monitored-resource/types.ts index d4dbdc786..56511c32e 100644 --- a/packages/opencensus-core/src/common/monitored-resource/types.ts +++ b/packages/opencensus-core/src/common/monitored-resource/types.ts @@ -64,8 +64,7 @@ export const monitoredResourceAttributes: Record< export type MonitoredResourceType = MonitoredResources.GCP_GKE_CONTAINER| MonitoredResources.GCP_GCE_INSTANCE|MonitoredResources.AWS_EC2_INSTANCE; -export type MonitoredResourceMetadata = - Promise|string>|Record|string; +export type MonitoredResourceMetadata = Record|string; export interface MonitoredResource { readonly type: MonitoredResourceType;