@ -8,8 +8,8 @@ import {
import { DataSourceWithBackend } from '@grafana/runtime' ;
import { TraceToLogsData , TraceToLogsOptions } from 'app/core/components/TraceToLogsSettings' ;
import { getDatasourceSrv } from 'app/features/plugins/datasource_srv' ;
import { merge , Observable , throwError } from 'rxjs' ;
import { map } from 'rxjs/operators' ;
import { from , merge , Observable , throwError } from 'rxjs' ;
import { map , mergeMap } from 'rxjs/operators' ;
import { LokiOptions } from '../loki/types' ;
import { transformTrace , transformTraceList } from './resultTransformer' ;
@ -23,19 +23,11 @@ export type TempoQuery = {
} & DataQuery ;
export class TempoDatasource extends DataSourceWithBackend < TempoQuery , TraceToLogsData > {
tracesToLogs : TraceToLogsOptions ;
linkedDatasource : DataSourceApi ;
tracesToLogs? : TraceToLogsOptions ;
constructor ( instanceSettings : DataSourceInstanceSettings < TraceToLogsData > ) {
super ( instanceSettings ) ;
this . tracesToLogs = instanceSettings . jsonData . tracesToLogs || { } ;
if ( this . tracesToLogs . datasourceUid ) {
this . linkDatasource ( ) ;
}
}
async linkDatasource() {
const dsSrv = getDatasourceSrv ( ) ;
this . linkedDatasource = await dsSrv . get ( this . tracesToLogs . datasourceUid ) ;
this . tracesToLogs = instanceSettings . jsonData . tracesToLogs ;
}
query ( options : DataQueryRequest < TempoQuery > ) : Observable < DataQueryResponse > {
@ -47,30 +39,33 @@ export class TempoDatasource extends DataSourceWithBackend<TempoQuery, TraceToLo
) ;
// Run search queries on linked datasource
if ( this . linkedDatasource && searchTargets . length > 0 ) {
// Wrap linked query into a data request based on original request
const linkedRequest : DataQueryRequest = { . . . options , targets : searchTargets.map ( ( t ) = > t . linkedQuery ! ) } ;
// Find trace matchers in derived fields of the linked datasource that's identical to this datasource
const settings : DataSourceInstanceSettings < LokiOptions > = ( this . linkedDatasource as any ) . instanceSettings ;
const traceLinkMatcher : string [ ] =
settings . jsonData . derivedFields
? . filter ( ( field ) = > field . datasourceUid === this . uid && field . matcherRegex )
. map ( ( field ) = > field . matcherRegex ) || [ ] ;
if ( ! traceLinkMatcher || traceLinkMatcher . length === 0 ) {
subQueries . push (
throwError (
'No Loki datasource configured for search. Set up Derived Fields for traces in a Loki datasource settings and link it to this Tempo datasource.'
)
) ;
} else {
subQueries . push (
( this . linkedDatasource . query ( linkedRequest ) as Observable < DataQueryResponse > ) . pipe (
map ( ( response ) = >
response . error ? response : transformTraceList ( response , this . uid , this . name , traceLinkMatcher )
)
)
) ;
}
if ( this . tracesToLogs ? . datasourceUid && searchTargets . length > 0 ) {
const dsSrv = getDatasourceSrv ( ) ;
subQueries . push (
from ( dsSrv . get ( this . tracesToLogs . datasourceUid ) ) . pipe (
mergeMap ( ( linkedDatasource : DataSourceApi ) = > {
// Wrap linked query into a data request based on original request
const linkedRequest : DataQueryRequest = { . . . options , targets : searchTargets.map ( ( t ) = > t . linkedQuery ! ) } ;
// Find trace matchers in derived fields of the linked datasource that's identical to this datasource
const settings : DataSourceInstanceSettings < LokiOptions > = ( linkedDatasource as any ) . instanceSettings ;
const traceLinkMatcher : string [ ] =
settings . jsonData . derivedFields
? . filter ( ( field ) = > field . datasourceUid === this . uid && field . matcherRegex )
. map ( ( field ) = > field . matcherRegex ) || [ ] ;
if ( ! traceLinkMatcher || traceLinkMatcher . length === 0 ) {
return throwError (
'No Loki datasource configured for search. Set up Derived Fields for traces in a Loki datasource settings and link it to this Tempo datasource.'
) ;
} else {
return ( linkedDatasource . query ( linkedRequest ) as Observable < DataQueryResponse > ) . pipe (
map ( ( response ) = >
response . error ? response : transformTraceList ( response , this . uid , this . name , traceLinkMatcher )
)
) ;
}
} )
)
) ;
}
if ( traceTargets . length > 0 ) {