diff --git a/packages/opencensus-exporter-stackdriver/src/common-utils.ts b/packages/opencensus-exporter-stackdriver/src/common-utils.ts index a057c7260..d2864f775 100644 --- a/packages/opencensus-exporter-stackdriver/src/common-utils.ts +++ b/packages/opencensus-exporter-stackdriver/src/common-utils.ts @@ -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 }; } diff --git a/packages/opencensus-exporter-stackdriver/test/test-stackdriver-monitoring-utils.ts b/packages/opencensus-exporter-stackdriver/test/test-stackdriver-monitoring-utils.ts index 8feae4e88..bf98482d2 100644 --- a/packages/opencensus-exporter-stackdriver/test/test-stackdriver-monitoring-utils.ts +++ b/packages/opencensus-exporter-stackdriver/test/test-stackdriver-monitoring-utils.ts @@ -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', @@ -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 =