Skip to content

Commit ae16321

Browse files
committed
chore(gcp-resource-detector): unit test to prevent a regression and ensure previously set attributes are not overwritten with blank values.
When attributes have already been set by other detectors, such as the EnvDetector, when the GcpDetector is run, it should not overwrite an existing attribute with a blank value if the source environment variable doesn't exist. Prior to commit 143a1f4, the behavior was as follows: - OTEL_RESOURCE_ATTRIBUTES = 'k8s.namespace.name=my-namespace' - NAMESPACE is undefined - EnvDetector runs and adds 'k8s.namespace.name=my-namespace' to attributes list - GcpDetector runs and sets 'k8s.namespace.name=' (blank) The GcpDetector should only set 'k8s.namespace.name' if the source environment variable is defined.
1 parent d579630 commit ae16321

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

detectors/node/opentelemetry-resource-detector-gcp/test/detectors/GcpDetector.test.ts

+40
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,46 @@ describe('gcpDetector', () => {
176176
});
177177
});
178178

179+
it('should return resource and undefined for non-available kubernetes attributes', async () => {
180+
process.env.KUBERNETES_SERVICE_HOST = 'my-host';
181+
process.env.HOSTNAME = 'my-hostname';
182+
process.env.CONTAINER_NAME = 'my-container-name';
183+
const scope = nock(HOST_ADDRESS)
184+
.get(INSTANCE_PATH)
185+
.reply(200, {}, HEADERS)
186+
.get(INSTANCE_ID_PATH)
187+
.reply(200, () => '4520031799277581759', HEADERS)
188+
.get(CLUSTER_NAME_PATH)
189+
.reply(200, () => 'my-cluster', HEADERS)
190+
.get(PROJECT_ID_PATH)
191+
.reply(200, () => 'my-project-id', HEADERS)
192+
.get(ZONE_PATH)
193+
.reply(200, () => 'project/zone/my-zone', HEADERS)
194+
.get(HOSTNAME_PATH)
195+
.reply(200, () => 'dev.my-project.local', HEADERS);
196+
const secondaryScope = nock(SECONDARY_HOST_ADDRESS)
197+
.get(INSTANCE_PATH)
198+
.reply(200, {}, HEADERS);
199+
200+
const resource = gcpDetector.detect();
201+
await resource.waitForAsyncAttributes?.();
202+
203+
secondaryScope.done();
204+
scope.done();
205+
206+
assertCloudResource(resource, {
207+
provider: 'gcp',
208+
accountId: 'my-project-id',
209+
zone: 'my-zone',
210+
});
211+
assertK8sResource(resource, {
212+
clusterName: 'my-cluster',
213+
podName: 'my-hostname',
214+
namespaceName: undefined,
215+
});
216+
assertContainerResource(resource, { name: 'my-container-name' });
217+
});
218+
179219
it('returns empty resource if not detected', async () => {
180220
const resource = detectResources({ detectors: [gcpDetector] });
181221
await resource.waitForAsyncAttributes?.();

0 commit comments

Comments
 (0)