Skip to content

Commit 8b32219

Browse files
authored
refactor(detector-gcp): change implementation to DetectorSync interface (#2335)
1 parent 18a5731 commit 8b32219

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

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

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@
1717
import * as gcpMetadata from 'gcp-metadata';
1818
import { diag } from '@opentelemetry/api';
1919
import {
20-
Detector,
20+
DetectorSync,
2121
ResourceDetectionConfig,
2222
Resource,
2323
ResourceAttributes,
24+
IResource,
2425
} from '@opentelemetry/resources';
2526
import { getEnv } from '@opentelemetry/core';
2627
import {
@@ -41,19 +42,21 @@ import {
4142
* Cloud Platform and return a {@link Resource} populated with metadata about
4243
* the instance. Returns an empty Resource if detection fails.
4344
*/
44-
class GcpDetector implements Detector {
45+
class GcpDetector implements DetectorSync {
46+
detect(_config?: ResourceDetectionConfig): IResource {
47+
return new Resource({}, this._getAttribures());
48+
}
49+
4550
/**
4651
* Attempts to connect and obtain instance configuration data from the GCP metadata service.
47-
* If the connection is successful it returns a promise containing a {@link Resource}
48-
* populated with instance metadata. Returns a promise containing an
49-
* empty {@link Resource} if the connection or parsing of the metadata fails.
50-
*
51-
* @param config The resource detection config
52+
* If the connection is successful it returns a promise containing a {@link ResourceAttributes}
53+
* object with instance metadata. Returns a promise containing an
54+
* empty {@link ResourceAttributes} if the connection or parsing of the metadata fails.
5255
*/
53-
async detect(_config?: ResourceDetectionConfig): Promise<Resource> {
56+
private async _getAttribures(): Promise<ResourceAttributes> {
5457
if (!(await gcpMetadata.isAvailable())) {
5558
diag.debug('GcpDetector failed: GCP Metadata unavailable.');
56-
return Resource.empty();
59+
return {};
5760
}
5861

5962
const [projectId, instanceId, zoneId, clusterName, hostname] =
@@ -75,7 +78,7 @@ class GcpDetector implements Detector {
7578
if (getEnv().KUBERNETES_SERVICE_HOST)
7679
this._addK8sAttributes(attributes, clusterName);
7780

78-
return new Resource(attributes);
81+
return attributes;
7982
}
8083

8184
/** Add resource attributes for K8s */

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import {
3131
assertContainerResource,
3232
assertEmptyResource,
3333
} from '@opentelemetry/contrib-test-utils';
34-
import { Resource } from '@opentelemetry/resources';
3534

3635
const HEADERS = {
3736
[HEADER_NAME.toLowerCase()]: HEADER_VALUE,
@@ -85,7 +84,10 @@ describe('gcpDetector', () => {
8584
const secondaryScope = nock(SECONDARY_HOST_ADDRESS)
8685
.get(INSTANCE_PATH)
8786
.reply(200, {}, HEADERS);
88-
const resource: Resource = await gcpDetector.detect();
87+
88+
const resource = gcpDetector.detect();
89+
await resource.waitForAsyncAttributes?.();
90+
8991
secondaryScope.done();
9092
scope.done();
9193

@@ -121,7 +123,10 @@ describe('gcpDetector', () => {
121123
const secondaryScope = nock(SECONDARY_HOST_ADDRESS)
122124
.get(INSTANCE_PATH)
123125
.reply(200, {}, HEADERS);
124-
const resource = await gcpDetector.detect();
126+
127+
const resource = gcpDetector.detect();
128+
await resource.waitForAsyncAttributes?.();
129+
125130
secondaryScope.done();
126131
scope.done();
127132

@@ -155,7 +160,10 @@ describe('gcpDetector', () => {
155160
const secondaryScope = nock(SECONDARY_HOST_ADDRESS)
156161
.get(INSTANCE_PATH)
157162
.reply(200, {}, HEADERS);
158-
const resource = await gcpDetector.detect();
163+
164+
const resource = gcpDetector.detect();
165+
await resource.waitForAsyncAttributes?.();
166+
159167
secondaryScope.done();
160168
scope.done();
161169

@@ -167,7 +175,8 @@ describe('gcpDetector', () => {
167175
});
168176

169177
it('returns empty resource if not detected', async () => {
170-
const resource = await gcpDetector.detect();
178+
const resource = gcpDetector.detect();
179+
await resource.waitForAsyncAttributes?.();
171180
assertEmptyResource(resource);
172181
});
173182
});

0 commit comments

Comments
 (0)