mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Prometheus: Fix ad-hoc filters (#66521)
* refactor label names back to getTagKeys to fulfill contract with parent class required for ad-hoc functionality
This commit is contained in:
@@ -72,7 +72,7 @@ export const PromVariableQueryEditor = ({ onChange, query, datasource }: Props)
|
||||
return;
|
||||
}
|
||||
|
||||
datasource.getLabelNames().then((labelNames: Array<{ text: string }>) => {
|
||||
datasource.getTagKeys().then((labelNames: Array<{ text: string }>) => {
|
||||
setLabelOptions(labelNames.map(({ text }) => ({ label: text, value: text })));
|
||||
});
|
||||
}, [datasource, qryType]);
|
||||
|
||||
@@ -94,6 +94,16 @@ describe('PrometheusDatasource', () => {
|
||||
ds = new PrometheusDatasource(instanceSettings, templateSrvStub, timeSrvStub);
|
||||
});
|
||||
|
||||
// Some functions are required by the parent datasource class to provide functionality such as ad-hoc filters, which requires the definition of the getTagKeys, and getTagValues functions
|
||||
describe('Datasource contract', () => {
|
||||
it('has function called getTagKeys', () => {
|
||||
expect(Object.getOwnPropertyNames(Object.getPrototypeOf(ds))).toContain('getTagKeys');
|
||||
});
|
||||
it('has function called getTagValues', () => {
|
||||
expect(Object.getOwnPropertyNames(Object.getPrototypeOf(ds))).toContain('getTagValues');
|
||||
});
|
||||
});
|
||||
|
||||
describe('Query', () => {
|
||||
it('returns empty array when no queries', async () => {
|
||||
await expect(ds.query(createDataRequest([]))).toEmitValuesWith((response) => {
|
||||
@@ -167,7 +177,7 @@ describe('PrometheusDatasource', () => {
|
||||
).rejects.toMatchObject({
|
||||
message: expect.stringMatching('Browser access'),
|
||||
});
|
||||
await expect(directDs.getLabelNames()).rejects.toMatchObject({
|
||||
await expect(directDs.getTagKeys()).rejects.toMatchObject({
|
||||
message: expect.stringMatching('Browser access'),
|
||||
});
|
||||
await expect(directDs.getTagValues()).rejects.toMatchObject({
|
||||
|
||||
@@ -934,10 +934,11 @@ export class PrometheusDatasource
|
||||
);
|
||||
}
|
||||
|
||||
// By implementing getTagKeys and getTagValues we add ad-hoc filters functionality
|
||||
// this is used to get label keys, a.k.a label names
|
||||
// it is used in metric_find_query.ts
|
||||
// and in Tempo here grafana/public/app/plugins/datasource/tempo/QueryEditor/ServiceGraphSection.tsx
|
||||
async getLabelNames(options?: any) {
|
||||
async getTagKeys(options?: any) {
|
||||
if (options?.series) {
|
||||
// Get tags for the provided series only
|
||||
const seriesLabels: Array<Record<string, string[]>> = await Promise.all(
|
||||
@@ -956,6 +957,7 @@ export class PrometheusDatasource
|
||||
}
|
||||
}
|
||||
|
||||
// By implementing getTagKeys and getTagValues we add ad-hoc filters functionality
|
||||
async getTagValues(options: { key?: string } = {}) {
|
||||
const params = this.getTimeRangeParams();
|
||||
const result = await this.metadataRequest(`/api/v1/label/${options.key}/values`, params);
|
||||
|
||||
@@ -25,7 +25,7 @@ export default class PrometheusMetricFindQuery {
|
||||
const queryResultRegex = /^query_result\((.+)\)\s*$/;
|
||||
const labelNamesQuery = this.query.match(labelNamesRegex);
|
||||
if (labelNamesQuery) {
|
||||
return this.datasource.getLabelNames();
|
||||
return this.datasource.getTagKeys();
|
||||
}
|
||||
|
||||
const labelValuesQuery = this.query.match(labelValuesRegex);
|
||||
|
||||
Reference in New Issue
Block a user