Tempo: Add support for ad-hoc filters (#83290)

* Add support for ad-hoc filters

* Add tests

* Update types
This commit is contained in:
Joey 2024-02-26 08:59:13 +00:00 committed by GitHub
parent af4382e4c2
commit 1899afccb5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 71 additions and 0 deletions

View File

@ -1159,6 +1159,59 @@ describe('label values', () => {
});
});
describe('should provide functionality for ad-hoc filters', () => {
let datasource: TempoDatasource;
beforeEach(() => {
datasource = createTempoDatasource();
jest.spyOn(datasource, 'metadataRequest').mockImplementation(
createMetadataRequest({
data: {
scopes: [{ name: 'span', tags: ['label1', 'label2'] }],
tagValues: [
{
type: 'value1',
value: 'value1',
label: 'value1',
},
{
type: 'value2',
value: 'value2',
label: 'value2',
},
],
},
})
);
});
it('for getTagKeys', async () => {
const response = await datasource.getTagKeys();
expect(response).toEqual([{ text: 'span.label1' }, { text: 'span.label2' }]);
});
it('for getTagValues', async () => {
const now = dateTime('2021-04-20T15:55:00Z');
const options = {
key: 'span.label1',
filters: [],
timeRange: {
from: now,
to: now,
raw: {
from: 'now-15m',
to: 'now',
},
},
};
const response = await datasource.getTagValues(options);
expect(response).toEqual([
{ text: { type: 'value1', value: 'value1', label: 'value1' } },
{ text: { type: 'value2', value: 'value2', label: 'value2' } },
]);
});
});
const prometheusMock = (): DataSourceApi => {
return {
query: jest.fn(() =>

View File

@ -11,6 +11,7 @@ import {
DataQueryResponse,
DataQueryResponseData,
DataSourceApi,
DataSourceGetTagValuesOptions,
DataSourceInstanceSettings,
dateTime,
FieldType,
@ -211,6 +212,23 @@ export class TempoDatasource extends DataSourceWithBackend<TempoQuery, TempoJson
);
}
// Allows to retrieve the list of tags for ad-hoc filters
async getTagKeys(): Promise<Array<{ text: string }>> {
await this.languageProvider.fetchTags();
const tags = this.languageProvider.tagsV2 || [];
return tags
.map(({ name, tags }) =>
tags.filter((tag) => tag !== undefined).map((t) => (name !== 'intrinsic' ? `${name}.${t}` : `${t}`))
)
.flat()
.map((tag) => ({ text: tag }));
}
// Allows to retrieve the list of tag values for ad-hoc filters
getTagValues(options: DataSourceGetTagValuesOptions): Promise<Array<{ text: string }>> {
return this.labelValuesQuery(options.key.replace(/^(resource|span)\./, ''));
}
init = async () => {
const response = await lastValueFrom(
this._request('/api/status/buildinfo').pipe(