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

Commit

Permalink
Fix: fallback to global resource type when expected labels are missing (
Browse files Browse the repository at this point in the history
  • Loading branch information
mayurkale22 authored Nov 22, 2019
1 parent 57e82f4 commit d8b56f7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
3 changes: 3 additions & 0 deletions packages/opencensus-exporter-stackdriver/src/common-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ export async function getDefaultResource(
}
}
});
if (Object.keys(labels).length !== Object.keys(mappings).length) {
return { type: 'global', labels: { project_id: projectId } };
}
return { type, labels };
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -566,14 +566,15 @@ describe('Stackdriver Stats Exporter Utils', () => {
process.env.OC_RESOURCE_LABELS =
'k8s.pod.name=pod-xyz-123,' +
'container.name=c1,k8s.namespace.name=default,' +
'cloud.zone=zone1';
'cloud.zone=zone1,k8s.cluster.name=cluster1';
CoreResource.setup();
const monitoredResource = await getDefaultResource('my-project-id');
const { type, labels } = monitoredResource;

assert.strictEqual(type, 'k8s_container');
assert.strictEqual(Object.keys(labels).length, 5);
assert.strictEqual(Object.keys(labels).length, 6);
assert.deepStrictEqual(labels, {
cluster_name: 'cluster1',
container_name: 'c1',
namespace_name: 'default',
pod_name: 'pod-xyz-123',
Expand All @@ -598,6 +599,18 @@ describe('Stackdriver Stats Exporter Utils', () => {
});
});

it('should fallback to global type is any of the label is missing', async () => {
process.env.OC_RESOURCE_TYPE = 'cloud.google.com/gce/instance';
process.env.OC_RESOURCE_LABELS = 'cloud.zone=zone1';
CoreResource.setup();
const monitoredResource = await getDefaultResource('my-project-id');
const { type, labels } = monitoredResource;

assert.strictEqual(type, 'global');
assert.strictEqual(Object.keys(labels).length, 1);
assert.deepStrictEqual(labels, { project_id: 'my-project-id' });
});

it('should return a aws MonitoredResource', async () => {
process.env.OC_RESOURCE_TYPE = 'aws.com/ec2/instance';
process.env.OC_RESOURCE_LABELS =
Expand Down

0 comments on commit d8b56f7

Please sign in to comment.