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

Commit d8b56f7

Browse files
authored
Fix: fallback to global resource type when expected labels are missing (#711)
1 parent 57e82f4 commit d8b56f7

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

packages/opencensus-exporter-stackdriver/src/common-utils.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ export async function getDefaultResource(
5151
}
5252
}
5353
});
54+
if (Object.keys(labels).length !== Object.keys(mappings).length) {
55+
return { type: 'global', labels: { project_id: projectId } };
56+
}
5457
return { type, labels };
5558
}
5659

packages/opencensus-exporter-stackdriver/test/test-stackdriver-monitoring-utils.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -566,14 +566,15 @@ describe('Stackdriver Stats Exporter Utils', () => {
566566
process.env.OC_RESOURCE_LABELS =
567567
'k8s.pod.name=pod-xyz-123,' +
568568
'container.name=c1,k8s.namespace.name=default,' +
569-
'cloud.zone=zone1';
569+
'cloud.zone=zone1,k8s.cluster.name=cluster1';
570570
CoreResource.setup();
571571
const monitoredResource = await getDefaultResource('my-project-id');
572572
const { type, labels } = monitoredResource;
573573

574574
assert.strictEqual(type, 'k8s_container');
575-
assert.strictEqual(Object.keys(labels).length, 5);
575+
assert.strictEqual(Object.keys(labels).length, 6);
576576
assert.deepStrictEqual(labels, {
577+
cluster_name: 'cluster1',
577578
container_name: 'c1',
578579
namespace_name: 'default',
579580
pod_name: 'pod-xyz-123',
@@ -598,6 +599,18 @@ describe('Stackdriver Stats Exporter Utils', () => {
598599
});
599600
});
600601

602+
it('should fallback to global type is any of the label is missing', async () => {
603+
process.env.OC_RESOURCE_TYPE = 'cloud.google.com/gce/instance';
604+
process.env.OC_RESOURCE_LABELS = 'cloud.zone=zone1';
605+
CoreResource.setup();
606+
const monitoredResource = await getDefaultResource('my-project-id');
607+
const { type, labels } = monitoredResource;
608+
609+
assert.strictEqual(type, 'global');
610+
assert.strictEqual(Object.keys(labels).length, 1);
611+
assert.deepStrictEqual(labels, { project_id: 'my-project-id' });
612+
});
613+
601614
it('should return a aws MonitoredResource', async () => {
602615
process.env.OC_RESOURCE_TYPE = 'aws.com/ec2/instance';
603616
process.env.OC_RESOURCE_LABELS =

0 commit comments

Comments
 (0)