import { SDK_INFO } from '@opentelemetry/core' ; import * as assert from 'assert' ; import { Resource } from '@opentelemetry/resources' ; import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions' ; param resource param validations export const assertCloudResource = ( resource , validations ) => { assertHasOneLabel ( 'CLOUD' , resource ) ; if ( validations . provider ) assert . strictEqual ( resource . attributes [ SemanticResourceAttributes . CLOUD_PROVIDER ] , validations . provider ) ; if ( validations . accountId ) assert . strictEqual ( resource . attributes [ SemanticResourceAttributes . CLOUD_ACCOUNT_ID ] , validations . accountId ) ; if ( validations . region ) assert . strictEqual ( resource . attributes [ SemanticResourceAttributes . CLOUD_REGION ] , validations . region ) ; if ( validations . zone ) assert . strictEqual ( resource . attributes [ SemanticResourceAttributes . CLOUD_AVAILABILITY_ZONE ] , validations . zone ) ; } ; param resource param validations export const assertContainerResource = ( resource , validations ) => { assertHasOneLabel ( 'CONTAINER' , resource ) ; if ( validations . name ) assert . strictEqual ( resource . attributes [ SemanticResourceAttributes . CONTAINER_NAME ] , validations . name ) ; if ( validations . id ) assert . strictEqual ( resource . attributes [ SemanticResourceAttributes . CONTAINER_ID ] , validations . id ) ; if ( validations . imageName ) assert . strictEqual ( resource . attributes [ SemanticResourceAttributes . CONTAINER_IMAGE_NAME ] , validations . imageName ) ; if ( validations . imageTag ) assert . strictEqual ( resource . attributes [ SemanticResourceAttributes . CONTAINER_IMAGE_TAG ] , validations . imageTag ) ; } ; param resource param validations export const assertHostResource = ( resource , validations ) => { assertHasOneLabel ( 'HOST' , resource ) ; if ( validations . id ) assert . strictEqual ( resource . attributes [ SemanticResourceAttributes . HOST_ID ] , validations . id ) ; if ( validations . name ) assert . strictEqual ( resource . attributes [ SemanticResourceAttributes . HOST_NAME ] , validations . name ) ; if ( validations . hostType ) assert . strictEqual ( resource . attributes [ SemanticResourceAttributes . HOST_TYPE ] , validations . hostType ) ; if ( validations . imageName ) assert . strictEqual ( resource . attributes [ SemanticResourceAttributes . HOST_IMAGE_NAME ] , validations . imageName ) ; if ( validations . imageId ) assert . strictEqual ( resource . attributes [ SemanticResourceAttributes . HOST_IMAGE_ID ] , validations . imageId ) ; if ( validations . imageVersion ) assert . strictEqual ( resource . attributes [ SemanticResourceAttributes . HOST_IMAGE_VERSION ] , validations . imageVersion ) ; } ; param resource param validations export const assertK8sResource = ( resource , validations ) => { assertHasOneLabel ( 'K8S' , resource ) ; if ( validations . clusterName ) assert . strictEqual ( resource . attributes [ SemanticResourceAttributes . K8S_CLUSTER_NAME ] , validations . clusterName ) ; if ( validations . namespaceName ) assert . strictEqual ( resource . attributes [ SemanticResourceAttributes . K8S_NAMESPACE_NAME ] , validations . namespaceName ) ; if ( validations . podName ) assert . strictEqual ( resource . attributes [ SemanticResourceAttributes . K8S_POD_NAME ] , validations . podName ) ; if ( validations . deploymentName ) assert . strictEqual ( resource . attributes [ SemanticResourceAttributes . K8S_DEPLOYMENT_NAME ] , validations . deploymentName ) ; } ; param resource param validations export const assertTelemetrySDKResource = ( resource , validations ) => { const defaults = { name : SDK_INFO . NAME , language : SDK_INFO . LANGUAGE , version : SDK_INFO . VERSION , } ; validations = { ... defaults , ... validations } ; if ( validations . name ) assert . strictEqual ( resource . attributes [ SemanticResourceAttributes . TELEMETRY_SDK_NAME ] , validations . name ) ; if ( validations . language ) assert . strictEqual ( resource . attributes [ SemanticResourceAttributes . TELEMETRY_SDK_LANGUAGE ] , validations . language ) ; if ( validations . version ) assert . strictEqual ( resource . attributes [ SemanticResourceAttributes . TELEMETRY_SDK_VERSION ] , validations . version ) ; } ; param resource param validations export const assertServiceResource = ( resource , validations ) => { assert . strictEqual ( resource . attributes [ SemanticResourceAttributes . SERVICE_NAME ] , validations . name ) ; assert . strictEqual ( resource . attributes [ SemanticResourceAttributes . SERVICE_INSTANCE_ID ] , validations . instanceId ) ; if ( validations . namespace ) assert . strictEqual ( resource . attributes [ SemanticResourceAttributes . SERVICE_NAMESPACE ] , validations . namespace ) ; if ( validations . version ) assert . strictEqual ( resource . attributes [ SemanticResourceAttributes . SERVICE_VERSION ] , validations . version ) ; } ; param resource param validations export const assertProcessResource = ( resource , validations ) => { assert . strictEqual ( resource . attributes [ SemanticResourceAttributes . PROCESS_PID ] , validations . pid ) ; if ( validations . name ) { assert . strictEqual ( resource . attributes [ SemanticResourceAttributes . PROCESS_EXECUTABLE_NAME ] , validations . name ) ; } if ( validations . command ) { assert . strictEqual ( resource . attributes [ SemanticResourceAttributes . PROCESS_COMMAND ] , validations . command ) ; } if ( validations . commandLine ) { assert . strictEqual ( resource . attributes [ SemanticResourceAttributes . PROCESS_COMMAND_LINE ] , validations . commandLine ) ; } } ; param resource export const assertEmptyResource = ( resource ) => { assert . strictEqual ( Object . keys ( resource . attributes ) . length , 0 ) ; } ; const assertHasOneLabel = ( prefix , resource ) : void => { const hasOne = Object . entries ( SemanticResourceAttributes ) . find ( ( [ key , value ] ) => { return ( key . startsWith ( prefix ) && Object . prototype . hasOwnProperty . call ( resource . attributes , value ) ) ; } ) ; assert . ok ( hasOne , 'Resource must have one of the following attributes: ' + Object . entries ( SemanticResourceAttributes ) . reduce ( ( result , [ key , value ] ) => { if ( key . startsWith ( prefix ) ) { result . push ( value ) ; } return result ; } ) . join ( ', ' ) ) ; } ;