Skip to content
This repository has been archived by the owner on Oct 3, 2023. It is now read-only.

fix(resources): GKE pod name label #728

Merged
merged 1 commit into from Feb 19, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 3 additions & 12 deletions packages/opencensus-resource-util/src/resource-utils.ts
Expand Up @@ -104,18 +104,18 @@ export async function getComputerEngineResource(): Promise<Resource> {
/** Returns Resource for GCP GKE container. */
export async function getKubernetesEngineResource(): Promise<Resource> {
if (Object.keys(gkeResourceLabels).length === 0) {
const [projectId, zoneId, clusterName, hostname] = await Promise.all([
const [projectId, zoneId, clusterName] = await Promise.all([
getProjectId(),
getZone(),
getClusterName(),
getHostname(),
]);
gkeResourceLabels[CLOUD_RESOURCE.ACCOUNT_ID_KEY] = projectId;
gkeResourceLabels[CLOUD_RESOURCE.ZONE_KEY] = zoneId;
gkeResourceLabels[K8S_RESOURCE.CLUSTER_NAME_KEY] = clusterName;
gkeResourceLabels[K8S_RESOURCE.NAMESPACE_NAME_KEY] =
process.env.NAMESPACE || '';
gkeResourceLabels[K8S_RESOURCE.POD_NAME_KEY] = hostname;
gkeResourceLabels[K8S_RESOURCE.POD_NAME_KEY] =
process.env.HOSTNAME || os.hostname();
gkeResourceLabels[CONTAINER_RESOURCE.NAME_KEY] =
process.env.CONTAINER_NAME || '';
}
Expand Down Expand Up @@ -206,15 +206,6 @@ async function getClusterName() {
}
}

/** Gets hostname from GCP instance metadata. */
async function getHostname() {
try {
return await gcpMetadata.instance('hostname');
} catch (ignore) {
return os.hostname();
}
}

export function clear() {
resourceType = undefined;
gkeResourceLabels = {};
Expand Down
16 changes: 6 additions & 10 deletions packages/opencensus-resource-util/test/test-detect-resource.ts
Expand Up @@ -40,7 +40,6 @@ const INSTANCE_PATH = BASE_PATH + '/instance';
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 HOSTNAME_PATH = BASE_PATH + '/instance/hostname';
const CLUSTER_NAME_PATH = BASE_PATH + '/instance/attributes/cluster-name';
const mockedAwsResponse = {
instanceId: 'my-instance-id',
Expand All @@ -65,20 +64,20 @@ describe('detectResource', () => {
delete process.env.CONTAINER_NAME;
delete process.env.OC_RESOURCE_TYPE;
delete process.env.OC_RESOURCE_LABELS;
delete process.env.HOSTNAME;
CoreResource.setup();
});

it('should return GCP_GKE_CONTAINER resource when KUBERNETES_SERVICE_HOST is set', async () => {
process.env.KUBERNETES_SERVICE_HOST = 'my-host';
process.env.HOSTNAME = 'my-hostname';
const scope = nock(HOST_ADDRESS)
.get(CLUSTER_NAME_PATH)
.reply(200, () => 'my-cluster', HEADERS)
.get(PROJECT_ID_PATH)
.reply(200, () => 'my-project-id', HEADERS)
.get(ZONE_PATH)
.reply(200, () => 'project/zone/my-zone', HEADERS)
.get(HOSTNAME_PATH)
.reply(200, () => 'my-hostname', HEADERS);
.reply(200, () => 'project/zone/my-zone', HEADERS);
const { type, labels } = await resource.detectResource();
scope.done();

Expand All @@ -95,16 +94,15 @@ describe('detectResource', () => {
it('should return GCP_GKE_CONTAINER resource when KUBERNETES_SERVICE_HOST, NAMESPACE and CONTAINER_NAME is set', async () => {
process.env.KUBERNETES_SERVICE_HOST = 'my-host';
process.env.NAMESPACE = 'my-namespace';
process.env.HOSTNAME = 'my-hostname';
process.env.CONTAINER_NAME = 'my-container-name';
const scope = nock(HOST_ADDRESS)
.get(CLUSTER_NAME_PATH)
.reply(200, () => 'my-cluster', HEADERS)
.get(PROJECT_ID_PATH)
.reply(200, () => 'my-project-id', HEADERS)
.get(ZONE_PATH)
.reply(200, () => 'project/zone/my-zone', HEADERS)
.get(HOSTNAME_PATH)
.reply(200, () => 'my-hostname', HEADERS);
.reply(200, () => 'project/zone/my-zone', HEADERS);
const { type, labels } = await resource.detectResource();
scope.done();

Expand Down Expand Up @@ -139,9 +137,7 @@ describe('detectResource', () => {
.get(PROJECT_ID_PATH)
.reply(200, () => 'my-project-id', HEADERS)
.get(ZONE_PATH)
.reply(200, () => 'project/zone/my-zone', HEADERS)
.get(HOSTNAME_PATH)
.reply(200, () => 'my-hostname', HEADERS);
.reply(200, () => 'project/zone/my-zone', HEADERS);
const { type, labels } = await resource.detectResource();
scope.done();

Expand Down