@@ -18,12 +18,37 @@ import { SDK_INFO } from '@opentelemetry/core';
1818import * as assert from 'assert' ;
1919import { IResource , Resource } from '@opentelemetry/resources' ;
2020import {
21+ SEMRESATTRS_CLOUD_ACCOUNT_ID ,
22+ SEMRESATTRS_CLOUD_AVAILABILITY_ZONE ,
23+ SEMRESATTRS_CLOUD_PROVIDER ,
24+ SEMRESATTRS_CLOUD_REGION ,
25+ SEMRESATTRS_CONTAINER_ID ,
26+ SEMRESATTRS_CONTAINER_IMAGE_NAME ,
27+ SEMRESATTRS_CONTAINER_IMAGE_TAG ,
28+ SEMRESATTRS_CONTAINER_NAME ,
29+ SEMRESATTRS_HOST_ID ,
30+ SEMRESATTRS_HOST_IMAGE_ID ,
31+ SEMRESATTRS_HOST_IMAGE_NAME ,
32+ SEMRESATTRS_HOST_IMAGE_VERSION ,
33+ SEMRESATTRS_HOST_NAME ,
34+ SEMRESATTRS_HOST_TYPE ,
35+ SEMRESATTRS_K8S_CLUSTER_NAME ,
36+ SEMRESATTRS_K8S_DEPLOYMENT_NAME ,
37+ SEMRESATTRS_K8S_NAMESPACE_NAME ,
38+ SEMRESATTRS_K8S_POD_NAME ,
39+ SEMRESATTRS_PROCESS_COMMAND ,
40+ SEMRESATTRS_PROCESS_COMMAND_LINE ,
41+ SEMRESATTRS_PROCESS_EXECUTABLE_NAME ,
42+ SEMRESATTRS_PROCESS_PID ,
2143 SEMRESATTRS_SERVICE_INSTANCE_ID ,
44+ SEMRESATTRS_SERVICE_NAME ,
45+ SEMRESATTRS_SERVICE_NAMESPACE ,
46+ SEMRESATTRS_SERVICE_VERSION ,
2247 SEMRESATTRS_TELEMETRY_SDK_LANGUAGE ,
2348 SEMRESATTRS_TELEMETRY_SDK_NAME ,
2449 SEMRESATTRS_TELEMETRY_SDK_VERSION ,
25- SemanticResourceAttributes ,
2650} from '@opentelemetry/semantic-conventions' ;
51+ import * as semconv from '@opentelemetry/semantic-conventions' ;
2752
2853/**
2954 * Test utility method to validate a cloud resource
@@ -43,22 +68,22 @@ export const assertCloudResource = (
4368 assertHasOneLabel ( 'CLOUD' , resource ) ;
4469 if ( validations . provider )
4570 assert . strictEqual (
46- resource . attributes [ SemanticResourceAttributes . CLOUD_PROVIDER ] ,
71+ resource . attributes [ SEMRESATTRS_CLOUD_PROVIDER ] ,
4772 validations . provider
4873 ) ;
4974 if ( validations . accountId )
5075 assert . strictEqual (
51- resource . attributes [ SemanticResourceAttributes . CLOUD_ACCOUNT_ID ] ,
76+ resource . attributes [ SEMRESATTRS_CLOUD_ACCOUNT_ID ] ,
5277 validations . accountId
5378 ) ;
5479 if ( validations . region )
5580 assert . strictEqual (
56- resource . attributes [ SemanticResourceAttributes . CLOUD_REGION ] ,
81+ resource . attributes [ SEMRESATTRS_CLOUD_REGION ] ,
5782 validations . region
5883 ) ;
5984 if ( validations . zone )
6085 assert . strictEqual (
61- resource . attributes [ SemanticResourceAttributes . CLOUD_AVAILABILITY_ZONE ] ,
86+ resource . attributes [ SEMRESATTRS_CLOUD_AVAILABILITY_ZONE ] ,
6287 validations . zone
6388 ) ;
6489} ;
@@ -81,22 +106,22 @@ export const assertContainerResource = (
81106 assertHasOneLabel ( 'CONTAINER' , resource ) ;
82107 if ( validations . name )
83108 assert . strictEqual (
84- resource . attributes [ SemanticResourceAttributes . CONTAINER_NAME ] ,
109+ resource . attributes [ SEMRESATTRS_CONTAINER_NAME ] ,
85110 validations . name
86111 ) ;
87112 if ( validations . id )
88113 assert . strictEqual (
89- resource . attributes [ SemanticResourceAttributes . CONTAINER_ID ] ,
114+ resource . attributes [ SEMRESATTRS_CONTAINER_ID ] ,
90115 validations . id
91116 ) ;
92117 if ( validations . imageName )
93118 assert . strictEqual (
94- resource . attributes [ SemanticResourceAttributes . CONTAINER_IMAGE_NAME ] ,
119+ resource . attributes [ SEMRESATTRS_CONTAINER_IMAGE_NAME ] ,
95120 validations . imageName
96121 ) ;
97122 if ( validations . imageTag )
98123 assert . strictEqual (
99- resource . attributes [ SemanticResourceAttributes . CONTAINER_IMAGE_TAG ] ,
124+ resource . attributes [ SEMRESATTRS_CONTAINER_IMAGE_TAG ] ,
100125 validations . imageTag
101126 ) ;
102127} ;
@@ -122,32 +147,32 @@ export const assertHostResource = (
122147 assertHasOneLabel ( 'HOST' , resource ) ;
123148 if ( validations . id )
124149 assert . strictEqual (
125- resource . attributes [ SemanticResourceAttributes . HOST_ID ] ,
150+ resource . attributes [ SEMRESATTRS_HOST_ID ] ,
126151 validations . id
127152 ) ;
128153 if ( validations . name )
129154 assert . strictEqual (
130- resource . attributes [ SemanticResourceAttributes . HOST_NAME ] ,
155+ resource . attributes [ SEMRESATTRS_HOST_NAME ] ,
131156 validations . name
132157 ) ;
133158 if ( validations . hostType )
134159 assert . strictEqual (
135- resource . attributes [ SemanticResourceAttributes . HOST_TYPE ] ,
160+ resource . attributes [ SEMRESATTRS_HOST_TYPE ] ,
136161 validations . hostType
137162 ) ;
138163 if ( validations . imageName )
139164 assert . strictEqual (
140- resource . attributes [ SemanticResourceAttributes . HOST_IMAGE_NAME ] ,
165+ resource . attributes [ SEMRESATTRS_HOST_IMAGE_NAME ] ,
141166 validations . imageName
142167 ) ;
143168 if ( validations . imageId )
144169 assert . strictEqual (
145- resource . attributes [ SemanticResourceAttributes . HOST_IMAGE_ID ] ,
170+ resource . attributes [ SEMRESATTRS_HOST_IMAGE_ID ] ,
146171 validations . imageId
147172 ) ;
148173 if ( validations . imageVersion )
149174 assert . strictEqual (
150- resource . attributes [ SemanticResourceAttributes . HOST_IMAGE_VERSION ] ,
175+ resource . attributes [ SEMRESATTRS_HOST_IMAGE_VERSION ] ,
151176 validations . imageVersion
152177 ) ;
153178} ;
@@ -170,22 +195,22 @@ export const assertK8sResource = (
170195 assertHasOneLabel ( 'K8S' , resource ) ;
171196 if ( validations . clusterName )
172197 assert . strictEqual (
173- resource . attributes [ SemanticResourceAttributes . K8S_CLUSTER_NAME ] ,
198+ resource . attributes [ SEMRESATTRS_K8S_CLUSTER_NAME ] ,
174199 validations . clusterName
175200 ) ;
176201 if ( validations . namespaceName )
177202 assert . strictEqual (
178- resource . attributes [ SemanticResourceAttributes . K8S_NAMESPACE_NAME ] ,
203+ resource . attributes [ SEMRESATTRS_K8S_NAMESPACE_NAME ] ,
179204 validations . namespaceName
180205 ) ;
181206 if ( validations . podName )
182207 assert . strictEqual (
183- resource . attributes [ SemanticResourceAttributes . K8S_POD_NAME ] ,
208+ resource . attributes [ SEMRESATTRS_K8S_POD_NAME ] ,
184209 validations . podName
185210 ) ;
186211 if ( validations . deploymentName )
187212 assert . strictEqual (
188- resource . attributes [ SemanticResourceAttributes . K8S_DEPLOYMENT_NAME ] ,
213+ resource . attributes [ SEMRESATTRS_K8S_DEPLOYMENT_NAME ] ,
189214 validations . deploymentName
190215 ) ;
191216} ;
@@ -213,17 +238,17 @@ export const assertTelemetrySDKResource = (
213238
214239 if ( validations . name )
215240 assert . strictEqual (
216- resource . attributes [ SemanticResourceAttributes . TELEMETRY_SDK_NAME ] ,
241+ resource . attributes [ SEMRESATTRS_TELEMETRY_SDK_NAME ] ,
217242 validations . name
218243 ) ;
219244 if ( validations . language )
220245 assert . strictEqual (
221- resource . attributes [ SemanticResourceAttributes . TELEMETRY_SDK_LANGUAGE ] ,
246+ resource . attributes [ SEMRESATTRS_TELEMETRY_SDK_LANGUAGE ] ,
222247 validations . language
223248 ) ;
224249 if ( validations . version )
225250 assert . strictEqual (
226- resource . attributes [ SemanticResourceAttributes . TELEMETRY_SDK_VERSION ] ,
251+ resource . attributes [ SEMRESATTRS_TELEMETRY_SDK_VERSION ] ,
227252 validations . version
228253 ) ;
229254} ;
@@ -244,21 +269,21 @@ export const assertServiceResource = (
244269 }
245270) => {
246271 assert . strictEqual (
247- resource . attributes [ SemanticResourceAttributes . SERVICE_NAME ] ,
272+ resource . attributes [ SEMRESATTRS_SERVICE_NAME ] ,
248273 validations . name
249274 ) ;
250275 assert . strictEqual (
251- resource . attributes [ SemanticResourceAttributes . SERVICE_INSTANCE_ID ] ,
276+ resource . attributes [ SEMRESATTRS_SERVICE_INSTANCE_ID ] ,
252277 validations . instanceId
253278 ) ;
254279 if ( validations . namespace )
255280 assert . strictEqual (
256- resource . attributes [ SemanticResourceAttributes . SERVICE_NAMESPACE ] ,
281+ resource . attributes [ SEMRESATTRS_SERVICE_NAMESPACE ] ,
257282 validations . namespace
258283 ) ;
259284 if ( validations . version )
260285 assert . strictEqual (
261- resource . attributes [ SemanticResourceAttributes . SERVICE_VERSION ] ,
286+ resource . attributes [ SEMRESATTRS_SERVICE_VERSION ] ,
262287 validations . version
263288 ) ;
264289} ;
@@ -279,24 +304,24 @@ export const assertProcessResource = (
279304 }
280305) => {
281306 assert . strictEqual (
282- resource . attributes [ SemanticResourceAttributes . PROCESS_PID ] ,
307+ resource . attributes [ SEMRESATTRS_PROCESS_PID ] ,
283308 validations . pid
284309 ) ;
285310 if ( validations . name ) {
286311 assert . strictEqual (
287- resource . attributes [ SemanticResourceAttributes . PROCESS_EXECUTABLE_NAME ] ,
312+ resource . attributes [ SEMRESATTRS_PROCESS_EXECUTABLE_NAME ] ,
288313 validations . name
289314 ) ;
290315 }
291316 if ( validations . command ) {
292317 assert . strictEqual (
293- resource . attributes [ SemanticResourceAttributes . PROCESS_COMMAND ] ,
318+ resource . attributes [ SEMRESATTRS_PROCESS_COMMAND ] ,
294319 validations . command
295320 ) ;
296321 }
297322 if ( validations . commandLine ) {
298323 assert . strictEqual (
299- resource . attributes [ SemanticResourceAttributes . PROCESS_COMMAND_LINE ] ,
324+ resource . attributes [ SEMRESATTRS_PROCESS_COMMAND_LINE ] ,
300325 validations . commandLine
301326 ) ;
302327 }
@@ -311,30 +336,29 @@ export const assertEmptyResource = (resource: Resource) => {
311336 assert . strictEqual ( Object . keys ( resource . attributes ) . length , 0 ) ;
312337} ;
313338
339+ /**
340+ * Assert that the `resource` has at least one known attribute with the given
341+ * `prefix`. By "known", we mean it is an attribute defined in semconv.
342+ */
314343const assertHasOneLabel = ( prefix : string , resource : Resource ) : void => {
315- const hasOne = Object . entries ( SemanticResourceAttributes ) . find (
316- ( [ key , value ] ) => {
317- return (
318- key . startsWith ( prefix ) &&
319- Object . prototype . hasOwnProperty . call ( resource . attributes , value )
320- ) ;
321- }
344+ const semconvModPrefix = `SEMRESATTRS_${ prefix . toUpperCase ( ) } _` ;
345+ const knownAttrs : Set < string > = new Set (
346+ Object . entries ( semconv )
347+ . filter (
348+ ( [ k , v ] ) => typeof v === 'string' && k . startsWith ( semconvModPrefix )
349+ )
350+ . map ( ( [ , v ] ) => v as string )
351+ ) ;
352+ const hasAttrs = Object . keys ( resource . attributes ) . filter ( k =>
353+ knownAttrs . has ( k )
322354 ) ;
323-
324355 assert . ok (
325- hasOne ,
326- 'Must have one node Resource(s) starting with [' +
356+ hasAttrs . length > 0 ,
357+ 'Must have one Resource(s) starting with [' +
327358 prefix +
328359 '] matching the following attributes: ' +
329- Object . entries ( SemanticResourceAttributes )
330- . reduce ( ( result , [ key , value ] ) => {
331- if ( key . startsWith ( prefix ) ) {
332- result . push ( value ) ;
333- }
334- return result ;
335- } )
336- . join ( ', ' ) +
337- JSON . stringify ( Object . keys ( SemanticResourceAttributes ) )
360+ Array . from ( knownAttrs ) . join ( ', ' ) +
361+ JSON . stringify ( Object . keys ( semconv ) )
338362 ) ;
339363} ;
340364
0 commit comments