From cd5d84b0f3acdd31a9d845f42efe7cbec53f7ea2 Mon Sep 17 00:00:00 2001 From: Yyee Date: Tue, 9 Nov 2021 21:04:56 +0800 Subject: [PATCH] Prometheus: Add custom query parameters when creating PromLink url (#41213) * Prometheus: Add custom query parameters when creating PromLink url * Fix variable as suggested and add new test --- .../prometheus/components/PromLink.test.tsx | 27 +++++++++++++++++++ .../prometheus/components/PromLink.tsx | 9 +++++++ 2 files changed, 36 insertions(+) diff --git a/public/app/plugins/datasource/prometheus/components/PromLink.test.tsx b/public/app/plugins/datasource/prometheus/components/PromLink.test.tsx index b2a540d645d..4fb1bfae173 100644 --- a/public/app/plugins/datasource/prometheus/components/PromLink.test.tsx +++ b/public/app/plugins/datasource/prometheus/components/PromLink.test.tsx @@ -44,6 +44,18 @@ const getDataSource = (datasourceOverrides?: Partial) => { return (Object.assign(datasource, datasourceOverrides) as unknown) as PrometheusDatasource; }; +const getDataSourceWithCustomQueryParameters = (datasourceOverrides?: Partial) => { + const datasource = { + getPrometheusTime: () => 124, + createQuery: () => ({ expr: 'up', step: 20 }), + directUrl: 'prom3', + getRateIntervalScopedVariable: jest.fn(() => ({ __rate_interval: { text: '60s', value: '60s' } })), + customQueryParameters: new URLSearchParams('g0.foo=1'), + }; + + return (Object.assign(datasource, datasourceOverrides) as unknown) as PrometheusDatasource; +}; + describe('PromLink', () => { it('should show correct link for 1 component', async () => { render( @@ -85,4 +97,19 @@ describe('PromLink', () => { ); expect(screen.getByText('Prometheus')).toHaveAttribute('href', 'about:blank'); }); + it('should add custom query parameters when it is configured', async () => { + render( +
+ +
+ ); + expect(screen.getByText('Prometheus')).toHaveAttribute( + 'href', + 'prom3/graph?g0.foo=1&g0.expr=up&g0.range_input=0s&g0.end_input=undefined&g0.step_input=20&g0.tab=0' + ); + }); }); diff --git a/public/app/plugins/datasource/prometheus/components/PromLink.tsx b/public/app/plugins/datasource/prometheus/components/PromLink.tsx index 19f9eed97de..f50189edb2d 100644 --- a/public/app/plugins/datasource/prometheus/components/PromLink.tsx +++ b/public/app/plugins/datasource/prometheus/components/PromLink.tsx @@ -44,8 +44,17 @@ const PromLink: FC = ({ panelData, query, datasource }) => { scopedVars: enrichedScopedVars, } as DataQueryRequest; + const customQueryParameters: { [key: string]: string } = {}; + if (datasource.customQueryParameters) { + for (const [k, v] of datasource.customQueryParameters) { + customQueryParameters[k] = v; + } + } + const queryOptions = datasource.createQuery(query, options, start, end); + const expr = { + ...customQueryParameters, 'g0.expr': queryOptions.expr, 'g0.range_input': rangeDiff + 's', 'g0.end_input': endTime,