Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(detector-gcp): collect hostname resource attribute from GCP Metadata API #1364

Merged
merged 5 commits into from
Feb 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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