import { Span , SpanStatusCode , SpanAttributes } from '@opentelemetry/api' ; import { AttributeNames } from './enums/AttributeNames' ; import { AddressFamily } from './enums/AddressFamily' ; import * as dns from 'dns' ; import { IgnoreMatcher } from './types' ; param err param span param nodeVersion export const setError = ( err , span , nodeVersion ) => { const { code , message , name } = err ; const attributes = { [ AttributeNames . DNS_ERROR_MESSAGE ] : message , [ AttributeNames . DNS_ERROR_NAME ] : name , } as SpanAttributes ; if ( nodeVersion . startsWith ( 'v12' ) ) { attributes [ AttributeNames . DNS_ERROR_CODE ] = code ! ; } span . setAttributes ( attributes ) ; span . setStatus ( { code : SpanStatusCode . ERROR , message , } ) ; } ; param family param index export const getFamilyAttribute = ( family , index ? ) : string => { return index ? `peer[${ index }].ipv${ family }` : `peer.ipv${ family }` ; } ; param funcName param service export const getOperationName = ( funcName , service ? ) : string => { return service ? `dns.${ service }/${ funcName }` : `dns.${ funcName }` ; } ; export const setLookupAttributes = ( span , address , family ? ) => { const attributes = { } as SpanAttributes ; const isObject = typeof address === 'object' ; let addresses = address ; if ( ! isObject ) { addresses = [ { address , family } as dns . LookupAddress ] ; } else if ( ! ( addresses instanceof Array ) ) { addresses = [ { address : ( address as dns . LookupAddress ) . address , family : ( address as dns . LookupAddress ) . family , } as dns . LookupAddress , ] ; } addresses . forEach ( ( _ , i ) => { const peerAttrFormat = getFamilyAttribute ( _ . family , i ) ; attributes [ peerAttrFormat ] = _ . address ; } ) ; span . setAttributes ( attributes ) ; } ; param constant param obj param pattern export const satisfiesPattern = < T > ( constant , pattern ) : boolean => { if ( typeof pattern === 'string' ) { return pattern === constant ; } else if ( pattern instanceof RegExp ) { return pattern . test ( constant ) ; } else if ( typeof pattern === 'function' ) { return pattern ( constant ) ; } else { throw new TypeError ( 'Pattern is in unsupported datatype' ) ; } } ; param constant param list param onException export const isIgnored = ( constant , list ? , onException ? ) : boolean => { if ( ! list ) { return false ; } try { for ( const pattern of list ) { if ( satisfiesPattern ( constant , pattern ) ) { return true ; } } } catch ( e ) { if ( onException ) { onException ( e ) ; } } return false ; } ;