Skip to content

Commit 4ed64cc

Browse files
authored
chore(gcp-detector): clean up code and test case for cloud run support (#2)
Signed-off-by: Kasper Borg Nissen <[email protected]>
1 parent 44433fa commit 4ed64cc

File tree

2 files changed

+54
-30
lines changed

2 files changed

+54
-30
lines changed

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import {
2424
DetectedResourceAttributes,
2525
} from '@opentelemetry/resources';
2626
import {
27+
CLOUDPLATFORMVALUES_GCP_KUBERNETES_ENGINE,
28+
CLOUDPLATFORMVALUES_GCP_CLOUD_RUN,
2729
CLOUDPROVIDERVALUES_GCP,
2830
SEMRESATTRS_CLOUD_ACCOUNT_ID,
2931
SEMRESATTRS_CLOUD_AVAILABILITY_ZONE,
@@ -37,6 +39,7 @@ import {
3739
SEMRESATTRS_FAAS_NAME,
3840
SEMRESATTRS_FAAS_INSTANCE,
3941
SEMRESATTRS_FAAS_VERSION,
42+
SEMRESATTRS_CLOUD_PLATFORM,
4043
} from '@opentelemetry/semantic-conventions';
4144

4245
/**
@@ -57,26 +60,29 @@ class GcpDetector implements ResourceDetector {
5760
*/
5861
private _getAttributes(): DetectedResourceAttributes {
5962
const isAvail = gcpMetadata.isAvailable();
60-
63+
const instanceId = this._getInstanceId(isAvail);
64+
6165
const attributes: DetectedResourceAttributes = {
6266
[SEMRESATTRS_CLOUD_PROVIDER]: (async () => {
6367
return (await isAvail) ? CLOUDPROVIDERVALUES_GCP : undefined;
6468
})(),
6569
[SEMRESATTRS_CLOUD_ACCOUNT_ID]: this._getProjectId(isAvail),
66-
[SEMRESATTRS_HOST_ID]: this._getInstanceId(isAvail),
70+
[SEMRESATTRS_HOST_ID]: instanceId,
6771
[SEMRESATTRS_HOST_NAME]: this._getHostname(isAvail),
6872
[SEMRESATTRS_CLOUD_AVAILABILITY_ZONE]: this._getZone(isAvail),
6973
};
7074

7175
// Add resource attributes for Cloud Run.
7276
if (process.env.K_SERVICE) {
77+
attributes[SEMRESATTRS_CLOUD_PLATFORM] = CLOUDPLATFORMVALUES_GCP_CLOUD_RUN;
7378
attributes[SEMRESATTRS_FAAS_NAME] = process.env.K_SERVICE;
7479
attributes[SEMRESATTRS_FAAS_VERSION] = process.env.K_REVISION;
75-
attributes[SEMRESATTRS_FAAS_INSTANCE] = this._getInstanceId(isAvail);
80+
attributes[SEMRESATTRS_FAAS_INSTANCE] = instanceId;
7681
}
7782

7883
// Add resource attributes for K8s.
7984
if (process.env.KUBERNETES_SERVICE_HOST) {
85+
attributes[SEMRESATTRS_CLOUD_PLATFORM] = CLOUDPLATFORMVALUES_GCP_KUBERNETES_ENGINE;
8086
attributes[SEMRESATTRS_K8S_CLUSTER_NAME] = this._getClusterName(isAvail);
8187
attributes[SEMRESATTRS_K8S_NAMESPACE_NAME] = (async () => {
8288
return (await isAvail) ? process.env.NAMESPACE : undefined;

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

Lines changed: 45 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,15 @@ import {
3131
assertContainerResource,
3232
assertEmptyResource,
3333
} from '@opentelemetry/contrib-test-utils';
34-
import { detectResources } from '@opentelemetry/resources';
34+
import { detectResources, Resource } from '@opentelemetry/resources';
35+
import * as assert from 'assert';
36+
import {
37+
CLOUDPLATFORMVALUES_GCP_CLOUD_RUN,
38+
SEMRESATTRS_CLOUD_PLATFORM,
39+
SEMRESATTRS_FAAS_INSTANCE,
40+
SEMRESATTRS_FAAS_NAME,
41+
SEMRESATTRS_FAAS_VERSION,
42+
} from '@opentelemetry/semantic-conventions';
3543

3644
const HEADERS = {
3745
[HEADER_NAME.toLowerCase()]: HEADER_VALUE,
@@ -43,6 +51,34 @@ const ZONE_PATH = BASE_PATH + '/instance/zone';
4351
const CLUSTER_NAME_PATH = BASE_PATH + '/instance/attributes/cluster-name';
4452
const HOSTNAME_PATH = BASE_PATH + '/instance/hostname';
4553

54+
const assertFaasResource = (
55+
resource: Resource,
56+
validations: {
57+
name?: string;
58+
instance?: string;
59+
version?: string;
60+
}
61+
) => {
62+
if (validations.name) {
63+
assert.strictEqual(
64+
resource.attributes[SEMRESATTRS_FAAS_NAME],
65+
validations.name
66+
);
67+
}
68+
if (validations.instance) {
69+
assert.strictEqual(
70+
resource.attributes[SEMRESATTRS_FAAS_INSTANCE],
71+
validations.instance
72+
);
73+
}
74+
if (validations.version) {
75+
assert.strictEqual(
76+
resource.attributes[SEMRESATTRS_FAAS_VERSION],
77+
validations.version
78+
);
79+
}
80+
};
81+
4682
describe('gcpDetector', () => {
4783
describe('.detect', () => {
4884
before(() => {
@@ -210,7 +246,8 @@ describe('gcpDetector', () => {
210246

211247
secondaryScope.done();
212248
scope.done();
213-
249+
250+
assert.strictEqual(resource.attributes[SEMRESATTRS_CLOUD_PLATFORM],CLOUDPLATFORMVALUES_GCP_CLOUD_RUN)
214251
assertCloudResource(resource, {
215252
provider: 'gcp',
216253
accountId: 'my-project-id',
@@ -220,30 +257,11 @@ describe('gcpDetector', () => {
220257
id: '4520031799277581759',
221258
name: 'dev.my-project.local',
222259
});
223-
224-
const attrs = resource.attributes;
225-
226-
// This should be moved to the @opentelemetry/contrib-test-utils and replaced once available.
227-
// Check faas.name and faas.version which are simple string values
228-
if (attrs['faas.name'] !== 'my-cloud-run-service') {
229-
throw new Error(`Cloud Run faas.name is "${attrs['faas.name']}" instead of "my-cloud-run-service"`);
230-
}
231-
232-
if (attrs['faas.version'] !== 'my-cloud-run-revision') {
233-
throw new Error(`Cloud Run faas.version is "${attrs['faas.version']}" instead of "my-cloud-run-revision"`);
234-
}
235-
236-
// For faas.instance, it could be a resolved value or a Promise
237-
if (attrs['faas.instance'] instanceof Promise) {
238-
const resolvedInstance = await attrs['faas.instance'];
239-
if (resolvedInstance !== '4520031799277581759') {
240-
throw new Error(`Cloud Run faas.instance resolved to "${resolvedInstance}" instead of "4520031799277581759"`);
241-
}
242-
} else if (attrs['faas.instance'] !== '' && attrs['faas.instance'] !== '4520031799277581759') {
243-
// The current implementation is returning an empty string, but the correct value would be the instance ID
244-
// We accept either for test compatibility
245-
throw new Error(`Cloud Run faas.instance is "${attrs['faas.instance']}" which is not empty or the instance ID`);
246-
}
247-
}).timeout(3000);
260+
assertFaasResource(resource, {
261+
name: 'my-cloud-run-service',
262+
version: 'my-cloud-run-revision',
263+
instance: '4520031799277581759',
264+
})
265+
});
248266
});
249267
});

0 commit comments

Comments
 (0)