import { errors } from '@elastic/elasticsearch' ; import { ElasticsearchClient , Logger } from 'kibana/server' ; import { rangeQuery } from '../../../../../observability/server' ; import { environmentQuery } from '../../../../common/utils/environment_query' ; import { unwrapEsResponse , WrappedElasticsearchClientError , } from '../../../../../observability/server' ; import { ESSearchResponse } from '../../../../../../../src/core/types/elasticsearch' ; import { Annotation as ESAnnotation } from '../../../../../observability/common/annotations' ; import { ScopedAnnotationsClient } from '../../../../../observability/server' ; import { Annotation , AnnotationType } from '../../../../common/annotations' ; import { SERVICE_NAME } from '../../../../common/elasticsearch_fieldnames' ; import { withApmSpan } from '../../../utils/with_apm_span' ; export function getStoredAnnotations ( { serviceName , environment , client , annotationsClient , logger , start , end , } ) { return withApmSpan ( 'get_stored_annotations' , async ( ) => { const body = { size : 50 , query : { bool : { filter : [ { term : { 'annotation.type' : 'deployment' } } , { term : { tags : 'apm' } } , { term : { [ SERVICE_NAME ] : serviceName } } , ... rangeQuery ( start , end ) , ... environmentQuery ( environment ) , ] , } , } , } ; try { const response = ( await unwrapEsResponse ( client . search ( { index : annotationsClient . index , body , } ) ) ) as any ; return response . hits . hits . map ( ( hit ) => { return { type : AnnotationType . VERSION , id : hit . _id as string , '@timestamp' : new Date ( hit . _source [ '@timestamp' ] ) . getTime ( ) , text : hit . _source . message , } ; } ) ; } catch ( error ) { if ( error instanceof WrappedElasticsearchClientError && error . originalError instanceof errors . ResponseError ) { const type = error . originalError . body . error . type ; if ( type === 'index_not_found_exception' ) { return [ ] ; } if ( type === 'security_exception' ) { logger . warn ( `Unable to get stored annotations due to a security exception. Please make sure that the user has 'indices:data/read/search' permissions for ${ annotationsClient . index }` ) ; return [ ] ; } } throw error ; } } ) ; }