Tempo: Add time range to tempo search query behind a feature flag (#43811)

This commit is contained in:
Connor Lindsey 2022-01-10 07:38:40 -07:00 committed by GitHub
parent 3d6e9076c9
commit ef12028a68
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 52 additions and 6 deletions

View File

@ -43,6 +43,7 @@ export interface FeatureToggles {
accesscontrol: boolean;
tempoServiceGraph: boolean;
tempoSearch: boolean;
tempoBackendSearch: boolean;
recordedQueries: boolean;
newNavigation: boolean;
fullRangeLogsVolume: boolean;

View File

@ -66,6 +66,7 @@ export class GrafanaBootConfig implements GrafanaConfig {
trimDefaults: false,
tempoServiceGraph: false,
tempoSearch: false,
tempoBackendSearch: false,
recordedQueries: false,
newNavigation: false,
fullRangeLogsVolume: false,

View File

@ -136,8 +136,15 @@ class TempoQueryFieldComponent extends React.PureComponent<Props, State> {
{query.queryType === 'nativeSearch' && (
<p style={{ maxWidth: '65ch' }}>
<Badge icon="rocket" text="Beta" color="blue" />
&nbsp;Tempo search is currently in beta and is designed to return recent traces only. It ignores the time
range picker. We are actively working on full backend search. Look for improvements in the near future!
{config.featureToggles.tempoBackendSearch ? (
<>&nbsp;Tempo search is currently in beta.</>
) : (
<>
&nbsp;Tempo search is currently in beta and is designed to return recent traces only. It ignores the
time range picker. We are actively working on full backend search. Look for improvements in the near
future!
</>
)}
</p>
)}
{query.queryType === 'search' && (

View File

@ -178,6 +178,24 @@ describe('Tempo data source', () => {
});
});
it('should include time range if provided', () => {
const ds = new TempoDatasource(defaultSettings);
const tempoQuery: TempoQuery = {
queryType: 'search',
refId: 'A',
query: '',
search: '',
};
const timeRange = { startTime: 0, endTime: 1000 };
const builtQuery = ds.buildSearchQuery(tempoQuery, timeRange);
expect(builtQuery).toStrictEqual({
tags: '',
limit: DEFAULT_LIMIT,
start: timeRange.startTime,
end: timeRange.endTime,
});
});
it('formats native search query history correctly', () => {
const ds = new TempoDatasource(defaultSettings);
const tempoQuery: TempoQuery = {

View File

@ -11,7 +11,7 @@ import {
LoadingState,
} from '@grafana/data';
import { TraceToLogsOptions } from 'app/core/components/TraceToLogsSettings';
import { BackendSrvRequest, DataSourceWithBackend, getBackendSrv } from '@grafana/runtime';
import { config, BackendSrvRequest, DataSourceWithBackend, getBackendSrv } from '@grafana/runtime';
import { serializeParams } from 'app/core/utils/fetch';
import { getDatasourceSrv } from 'app/features/plugins/datasource_srv';
import { identity, pick, pickBy, groupBy, startCase } from 'lodash';
@ -55,6 +55,15 @@ export interface TempoQuery extends DataQuery {
serviceMapQuery?: string;
}
interface SearchQueryParams {
minDuration?: string;
maxDuration?: string;
limit?: number;
tags: string;
start?: number;
end?: number;
}
export const DEFAULT_LIMIT = 20;
export class TempoDatasource extends DataSourceWithBackend<TempoQuery, TempoJsonData> {
@ -116,7 +125,10 @@ export class TempoDatasource extends DataSourceWithBackend<TempoQuery, TempoJson
if (targets.nativeSearch?.length) {
try {
const searchQuery = this.buildSearchQuery(targets.nativeSearch[0]);
const timeRange = config.featureToggles.tempoBackendSearch
? { startTime: options.range.from.unix(), endTime: options.range.to.unix() }
: undefined;
const searchQuery = this.buildSearchQuery(targets.nativeSearch[0], timeRange);
subQueries.push(
this._request('/api/search', searchQuery).pipe(
map((response) => {
@ -222,7 +234,7 @@ export class TempoDatasource extends DataSourceWithBackend<TempoQuery, TempoJson
return query.query;
}
buildSearchQuery(query: TempoQuery) {
buildSearchQuery(query: TempoQuery, timeRange?: { startTime: number; endTime?: number }): SearchQueryParams {
let tags = query.search ?? '';
let tempoQuery = pick(query, ['minDuration', 'maxDuration', 'limit']);
@ -259,7 +271,14 @@ export class TempoDatasource extends DataSourceWithBackend<TempoQuery, TempoJson
throw new Error('Please enter a valid limit.');
}
return { tags, ...tempoQuery };
let searchQuery: SearchQueryParams = { tags, ...tempoQuery };
if (timeRange) {
searchQuery.start = timeRange.startTime;
searchQuery.end = timeRange.endTime;
}
return searchQuery;
}
async getServiceGraphLabels() {