diff --git a/public/app/plugins/datasource/prometheus/components/VariableQueryEditor.tsx b/public/app/plugins/datasource/prometheus/components/VariableQueryEditor.tsx index c2b064d5e76..167b1f9608e 100644 --- a/public/app/plugins/datasource/prometheus/components/VariableQueryEditor.tsx +++ b/public/app/plugins/datasource/prometheus/components/VariableQueryEditor.tsx @@ -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]); diff --git a/public/app/plugins/datasource/prometheus/datasource.test.ts b/public/app/plugins/datasource/prometheus/datasource.test.ts index 7c41947e10b..c7fb6b5f2a5 100644 --- a/public/app/plugins/datasource/prometheus/datasource.test.ts +++ b/public/app/plugins/datasource/prometheus/datasource.test.ts @@ -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({ diff --git a/public/app/plugins/datasource/prometheus/datasource.tsx b/public/app/plugins/datasource/prometheus/datasource.tsx index b212d59baf1..e4c03719d26 100644 --- a/public/app/plugins/datasource/prometheus/datasource.tsx +++ b/public/app/plugins/datasource/prometheus/datasource.tsx @@ -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> = 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); diff --git a/public/app/plugins/datasource/prometheus/metric_find_query.ts b/public/app/plugins/datasource/prometheus/metric_find_query.ts index 7ce5f1f50d8..a0225c60b5a 100644 --- a/public/app/plugins/datasource/prometheus/metric_find_query.ts +++ b/public/app/plugins/datasource/prometheus/metric_find_query.ts @@ -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); diff --git a/public/app/plugins/datasource/tempo/QueryEditor/ServiceGraphSection.tsx b/public/app/plugins/datasource/tempo/QueryEditor/ServiceGraphSection.tsx index 1c5785dec69..edaa5ea1d8c 100644 --- a/public/app/plugins/datasource/tempo/QueryEditor/ServiceGraphSection.tsx +++ b/public/app/plugins/datasource/tempo/QueryEditor/ServiceGraphSection.tsx @@ -29,7 +29,7 @@ export function ServiceGraphSection({ const [hasKeys, setHasKeys] = useState(undefined); useEffect(() => { async function fn(ds: PrometheusDatasource) { - const keys = await ds.getLabelNames({ + const keys = await ds.getTagKeys({ series: [ 'traces_service_graph_request_server_seconds_sum', 'traces_service_graph_request_total',