@@ -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
3644const HEADERS = {
3745 [ HEADER_NAME . toLowerCase ( ) ] : HEADER_VALUE ,
@@ -43,6 +51,34 @@ const ZONE_PATH = BASE_PATH + '/instance/zone';
4351const CLUSTER_NAME_PATH = BASE_PATH + '/instance/attributes/cluster-name' ;
4452const 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+
4682describe ( '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