2020-10-13 12:12:49 -05:00
|
|
|
import {
|
2021-03-05 07:28:17 -06:00
|
|
|
DataQuery,
|
2020-10-13 12:12:49 -05:00
|
|
|
DataQueryRequest,
|
|
|
|
DataQueryResponse,
|
2021-05-10 10:12:19 -05:00
|
|
|
DataSourceApi,
|
2021-03-05 07:28:17 -06:00
|
|
|
DataSourceInstanceSettings,
|
2021-08-05 08:13:44 -05:00
|
|
|
LoadingState,
|
2020-10-13 12:12:49 -05:00
|
|
|
} from '@grafana/data';
|
2021-03-05 07:28:17 -06:00
|
|
|
import { DataSourceWithBackend } from '@grafana/runtime';
|
2021-05-10 10:12:19 -05:00
|
|
|
import { TraceToLogsData, TraceToLogsOptions } from 'app/core/components/TraceToLogsSettings';
|
|
|
|
import { getDatasourceSrv } from 'app/features/plugins/datasource_srv';
|
2021-08-05 08:13:44 -05:00
|
|
|
import { from, merge, Observable, of, throwError } from 'rxjs';
|
2021-06-02 09:37:36 -05:00
|
|
|
import { map, mergeMap } from 'rxjs/operators';
|
2021-05-10 10:12:19 -05:00
|
|
|
import { LokiOptions } from '../loki/types';
|
2021-08-05 08:13:44 -05:00
|
|
|
import { transformFromOTLP as transformFromOTEL, transformTrace, transformTraceList } from './resultTransformer';
|
2021-05-10 10:12:19 -05:00
|
|
|
|
2021-08-05 08:13:44 -05:00
|
|
|
export type TempoQueryType = 'search' | 'traceId' | 'upload';
|
2020-10-13 12:12:49 -05:00
|
|
|
|
|
|
|
export type TempoQuery = {
|
|
|
|
query: string;
|
2021-05-10 10:12:19 -05:00
|
|
|
// Query to find list of traces, e.g., via Loki
|
|
|
|
linkedQuery?: DataQuery;
|
|
|
|
queryType: TempoQueryType;
|
2020-10-13 12:12:49 -05:00
|
|
|
} & DataQuery;
|
|
|
|
|
2021-05-10 10:12:19 -05:00
|
|
|
export class TempoDatasource extends DataSourceWithBackend<TempoQuery, TraceToLogsData> {
|
2021-06-02 09:37:36 -05:00
|
|
|
tracesToLogs?: TraceToLogsOptions;
|
2021-08-05 08:13:44 -05:00
|
|
|
uploadedJson?: string | ArrayBuffer | null = null;
|
2021-06-02 09:37:36 -05:00
|
|
|
|
2021-05-10 10:12:19 -05:00
|
|
|
constructor(instanceSettings: DataSourceInstanceSettings<TraceToLogsData>) {
|
2020-10-13 12:12:49 -05:00
|
|
|
super(instanceSettings);
|
2021-06-02 09:37:36 -05:00
|
|
|
this.tracesToLogs = instanceSettings.jsonData.tracesToLogs;
|
2020-10-13 12:12:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
query(options: DataQueryRequest<TempoQuery>): Observable<DataQueryResponse> {
|
2021-05-10 10:12:19 -05:00
|
|
|
const subQueries: Array<Observable<DataQueryResponse>> = [];
|
|
|
|
const filteredTargets = options.targets.filter((target) => !target.hide);
|
|
|
|
const searchTargets = filteredTargets.filter((target) => target.queryType === 'search');
|
2021-08-05 08:13:44 -05:00
|
|
|
const uploadTargets = filteredTargets.filter((target) => target.queryType === 'upload');
|
2021-05-10 10:12:19 -05:00
|
|
|
const traceTargets = filteredTargets.filter(
|
|
|
|
(target) => target.queryType === 'traceId' || target.queryType === undefined
|
|
|
|
);
|
2021-04-06 11:35:00 -05:00
|
|
|
|
2021-05-10 10:12:19 -05:00
|
|
|
// Run search queries on linked datasource
|
2021-06-02 09:37:36 -05:00
|
|
|
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)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
)
|
|
|
|
);
|
2021-05-10 10:12:19 -05:00
|
|
|
}
|
2021-04-06 11:35:00 -05:00
|
|
|
|
2021-08-05 08:13:44 -05:00
|
|
|
if (uploadTargets.length) {
|
|
|
|
if (this.uploadedJson) {
|
|
|
|
const otelTraceData = JSON.parse(this.uploadedJson as string);
|
|
|
|
if (!otelTraceData.batches) {
|
|
|
|
subQueries.push(of({ error: { message: 'JSON is not valid opentelemetry format' }, data: [] }));
|
|
|
|
} else {
|
|
|
|
subQueries.push(of(transformFromOTEL(otelTraceData.batches)));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
subQueries.push(of({ data: [], state: LoadingState.Done }));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-10 10:12:19 -05:00
|
|
|
if (traceTargets.length > 0) {
|
|
|
|
const traceRequest: DataQueryRequest<TempoQuery> = { ...options, targets: traceTargets };
|
|
|
|
subQueries.push(
|
|
|
|
super.query(traceRequest).pipe(
|
|
|
|
map((response) => {
|
|
|
|
if (response.error) {
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
return transformTrace(response);
|
|
|
|
})
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2021-03-22 13:09:15 -05:00
|
|
|
|
2021-05-10 10:12:19 -05:00
|
|
|
return merge(...subQueries);
|
2020-10-13 12:12:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
async testDatasource(): Promise<any> {
|
2021-05-27 07:47:10 -05:00
|
|
|
// to test Tempo we send a dummy traceID and verify Tempo answers with 'trace not found'
|
|
|
|
const response = await super.query({ targets: [{ query: '0' }] } as any).toPromise();
|
2021-03-05 07:28:17 -06:00
|
|
|
|
2021-05-27 07:47:10 -05:00
|
|
|
const errorMessage = response.error?.message;
|
|
|
|
if (
|
|
|
|
errorMessage &&
|
|
|
|
errorMessage.startsWith('failed to get trace') &&
|
|
|
|
errorMessage.endsWith('trace not found in Tempo')
|
|
|
|
) {
|
|
|
|
return { status: 'success', message: 'Data source is working' };
|
2020-10-13 12:12:49 -05:00
|
|
|
}
|
|
|
|
|
2021-05-27 07:47:10 -05:00
|
|
|
return { status: 'error', message: 'Data source is not working' + (errorMessage ? `: ${errorMessage}` : '') };
|
2021-03-04 14:20:26 -06:00
|
|
|
}
|
|
|
|
|
2020-10-13 12:12:49 -05:00
|
|
|
getQueryDisplayText(query: TempoQuery) {
|
|
|
|
return query.query;
|
|
|
|
}
|
|
|
|
}
|