Skip to content

Commit

Permalink
feat(detector-gcp): collect hostname resource attribute from GCP Meta…
Browse files Browse the repository at this point in the history
…data API (open-telemetry#1364)

* feat: collect hostname resource attribute from GCP Metadata API

* chore: remove unneeded changelog entry

---------

Co-authored-by: Amir Blum <amirgiraffe@gmail.com>
  • Loading branch information
mwear and blumamir authored Feb 8, 2023
1 parent 63e0fc9 commit 33c57cc
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,19 @@ class GcpDetector implements Detector {
return Resource.empty();
}

const [projectId, instanceId, zoneId, clusterName] = await Promise.all([
this._getProjectId(),
this._getInstanceId(),
this._getZone(),
this._getClusterName(),
]);
const [projectId, instanceId, zoneId, clusterName, hostname] =
await Promise.all([
this._getProjectId(),
this._getInstanceId(),
this._getZone(),
this._getClusterName(),
this._getHostname(),
]);

const attributes: ResourceAttributes = {};
attributes[SemanticResourceAttributes.CLOUD_ACCOUNT_ID] = projectId;
attributes[SemanticResourceAttributes.HOST_ID] = instanceId;
attributes[SemanticResourceAttributes.HOST_NAME] = hostname;
attributes[SemanticResourceAttributes.CLOUD_AVAILABILITY_ZONE] = zoneId;
attributes[SemanticResourceAttributes.CLOUD_PROVIDER] =
CloudProviderValues.GCP;
Expand Down Expand Up @@ -125,6 +128,15 @@ class GcpDetector implements Detector {
return '';
}
}

/** Gets hostname from GCP instance metadata. */
private async _getHostname(): Promise<string> {
try {
return await gcpMetadata.instance('hostname');
} catch {
return '';
}
}
}

export const gcpDetector = new GcpDetector();
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const INSTANCE_ID_PATH = BASE_PATH + '/instance/id';
const PROJECT_ID_PATH = BASE_PATH + '/project/project-id';
const ZONE_PATH = BASE_PATH + '/instance/zone';
const CLUSTER_NAME_PATH = BASE_PATH + '/instance/attributes/cluster-name';
const HOSTNAME_PATH = BASE_PATH + '/instance/hostname';

(semver.satisfies(process.version, '>=10') ? describe : describe.skip)(
'gcpDetector',
Expand Down Expand Up @@ -81,7 +82,9 @@ const CLUSTER_NAME_PATH = BASE_PATH + '/instance/attributes/cluster-name';
.get(ZONE_PATH)
.reply(200, () => 'project/zone/my-zone', HEADERS)
.get(CLUSTER_NAME_PATH)
.reply(404);
.reply(404)
.get(HOSTNAME_PATH)
.reply(200, () => 'dev.my-project.local', HEADERS);
const secondaryScope = nock(SECONDARY_HOST_ADDRESS)
.get(INSTANCE_PATH)
.reply(200, {}, HEADERS);
Expand All @@ -94,7 +97,10 @@ const CLUSTER_NAME_PATH = BASE_PATH + '/instance/attributes/cluster-name';
accountId: 'my-project-id',
zone: 'my-zone',
});
assertHostResource(resource, { id: '4520031799277581759' });
assertHostResource(resource, {
id: '4520031799277581759',
name: 'dev.my-project.local',
});
});

it('should populate K8s attributes when KUBERNETES_SERVICE_HOST is set', async () => {
Expand All @@ -112,7 +118,9 @@ const CLUSTER_NAME_PATH = BASE_PATH + '/instance/attributes/cluster-name';
.get(PROJECT_ID_PATH)
.reply(200, () => 'my-project-id', HEADERS)
.get(ZONE_PATH)
.reply(200, () => 'project/zone/my-zone', HEADERS);
.reply(200, () => 'project/zone/my-zone', HEADERS)
.get(HOSTNAME_PATH)
.reply(200, () => 'dev.my-project.local', HEADERS);
const secondaryScope = nock(SECONDARY_HOST_ADDRESS)
.get(INSTANCE_PATH)
.reply(200, {}, HEADERS);
Expand Down Expand Up @@ -144,7 +152,9 @@ const CLUSTER_NAME_PATH = BASE_PATH + '/instance/attributes/cluster-name';
.get(INSTANCE_ID_PATH)
.reply(400, undefined, HEADERS)
.get(CLUSTER_NAME_PATH)
.reply(413);
.reply(413)
.get(HOSTNAME_PATH)
.reply(400, undefined, HEADERS);
const secondaryScope = nock(SECONDARY_HOST_ADDRESS)
.get(INSTANCE_PATH)
.reply(200, {}, HEADERS);
Expand Down

0 comments on commit 33c57cc

Please sign in to comment.