Skip to content

Commit 22b3ad3

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 4b51e60 commit 22b3ad3

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,46 @@ describe('gcpDetector', () => {
174174
});
175175
});
176176

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

0 commit comments

Comments
 (0)