From 422371c2ed4f47555e0565766d31fa10d13a6ac5 Mon Sep 17 00:00:00 2001 From: Andreas Gerstmayr Date: Thu, 17 Jun 2021 17:58:57 +0200 Subject: [PATCH] dashboards: add pmproxy URL and hostspec variables to the PCP Vector Host Overview dashboard --- CHANGELOG.md | 2 + src/datasources/bpftrace/datasource.test.ts | 10 +- .../lib/pmapi/data_processor.test.ts | 6 +- src/datasources/lib/pmapi/datasource_base.ts | 55 +++++++-- src/datasources/lib/specs/fixtures/grafana.ts | 10 +- .../pcp-vector-host-overview.jsonnet | 107 ++++++++++-------- src/datasources/vector/datasource.test.ts | 93 ++++++++++++--- 7 files changed, 196 insertions(+), 87 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7bf432d4..83746a82 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## 3.0.4 (unreleased) +- **dashboards**: PCP Vector Host Overview: add pmproxy URL and hostspec variables +- **vector, bpftrace**: use `pcp://127.0.0.1` as default hostspec (no functional change) - **chore**: update dependencies - **test**: replace convey with testify for the Go tests diff --git a/src/datasources/bpftrace/datasource.test.ts b/src/datasources/bpftrace/datasource.test.ts index 6cb24367..d112a644 100644 --- a/src/datasources/bpftrace/datasource.test.ts +++ b/src/datasources/bpftrace/datasource.test.ts @@ -31,7 +31,7 @@ describe('PCP BPFtrace', () => { }); it('should register a script and return the data', async () => { - const queries = [ + const targets = [ { refId: 'A', expr: '...cpuwalk...', @@ -39,7 +39,7 @@ describe('PCP BPFtrace', () => { }, ]; - let response = await datasource.query(grafana.dataQueryRequest(queries)); + let response = await datasource.query(grafana.dataQueryRequest({ targets })); expect(response).toEqual({ data: [] }); mockNextResponses([ @@ -57,7 +57,7 @@ describe('PCP BPFtrace', () => { mockNextResponses([fetchResponse2]); await datasource.poller.poll(); - response = await datasource.query(grafana.dataQueryRequest(queries)); + response = await datasource.query(grafana.dataQueryRequest({ targets })); expect({ fields: response.data[0].fields }).toMatchInlineSnapshot( { fields: [ @@ -245,14 +245,14 @@ describe('PCP BPFtrace', () => { Array [ Object { "params": Object { - "hostspec": "127.0.0.1", + "hostspec": "pcp://127.0.0.1", "polltimeout": 11, }, "url": "http://localhost:1234/pmapi/context", }, Object { "params": Object { - "hostspec": "127.0.0.1", + "hostspec": "pcp://127.0.0.1", "polltimeout": 30, }, "url": "http://localhost:1234/pmapi/context", diff --git a/src/datasources/lib/pmapi/data_processor.test.ts b/src/datasources/lib/pmapi/data_processor.test.ts index 63a8ee0a..d6c8f0bd 100644 --- a/src/datasources/lib/pmapi/data_processor.test.ts +++ b/src/datasources/lib/pmapi/data_processor.test.ts @@ -5,7 +5,7 @@ import { processQueries } from './data_processor'; describe('data processor', () => { it('should create a dataframe and handle missing frames and backward counters', () => { const target = poller.target({ query: { expr: 'disk.dev.read' } }); - const dataQueryRequest = grafana.dataQueryRequest([target.query]); // request data between 10-20s + const dataQueryRequest = grafana.dataQueryRequest({ targets: [target.query] }); // request data between 10-20s const values = [ { @@ -266,7 +266,7 @@ describe('data processor', () => { }; const endpoint = poller.endpoint({ metrics: [metricA, metricB], targets: [targetA, targetB] }); - const dataQueryRequest = grafana.dataQueryRequest([targetA.query, targetB.query]); + const dataQueryRequest = grafana.dataQueryRequest({ targets: [targetA.query, targetB.query] }); const result = processQueries( dataQueryRequest, @@ -359,7 +359,7 @@ describe('data processor', () => { }; const endpoint = poller.endpoint({ metrics: [metric], targets: [target] }); - const dataQueryRequest = grafana.dataQueryRequest([target.query]); + const dataQueryRequest = grafana.dataQueryRequest({ targets: [target.query] }); const result = processQueries(dataQueryRequest, [{ endpoint, query: target.query, metrics: [metric] }], 1); expect(result[0].fields).toMatchInlineSnapshot(` Array [ diff --git a/src/datasources/lib/pmapi/datasource_base.ts b/src/datasources/lib/pmapi/datasource_base.ts index 52925606..2c571c5e 100644 --- a/src/datasources/lib/pmapi/datasource_base.ts +++ b/src/datasources/lib/pmapi/datasource_base.ts @@ -19,7 +19,9 @@ import { MinimalPmapiQuery, PmapiDefaultOptions, PmapiOptions, PmapiQuery } from const log = getLogger('datasource_base'); export abstract class DataSourceBase extends DataSourceApi { + /** URL as specified in the datasource settings page (can be undefined) */ url?: string; + /** hostspec as specified in the datasource settings page, or default hostspec */ hostspec: string; retentionTimeMs: number; pmApiService: PmApiService; @@ -33,8 +35,19 @@ export abstract class DataSourceBase { diff --git a/src/datasources/lib/specs/fixtures/grafana.ts b/src/datasources/lib/specs/fixtures/grafana.ts index 7e72bdd0..46e02a7e 100644 --- a/src/datasources/lib/specs/fixtures/grafana.ts +++ b/src/datasources/lib/specs/fixtures/grafana.ts @@ -1,5 +1,7 @@ import { DataQuery, DataQueryRequest, dateTimeParse, TimeRange } from '@grafana/data'; import { FetchResponse } from '@grafana/runtime'; +import { defaultsDeep } from 'lodash'; +import { DeepPartial } from 'utility-types'; export function timeRange(): TimeRange { return { @@ -12,8 +14,8 @@ export function timeRange(): TimeRange { }; } -export function dataQueryRequest(queries: Q[] = []): DataQueryRequest { - return { +export function dataQueryRequest(props?: DeepPartial>): DataQueryRequest { + return defaultsDeep({}, props, { app: 'dashboard', dashboardId: 1, panelId: 2, @@ -24,8 +26,8 @@ export function dataQueryRequest(queries: Q[] = []): DataQu timezone: '', range: timeRange(), scopedVars: {}, - targets: queries, - }; + targets: [], + }); } export function response(data: T): FetchResponse { diff --git a/src/datasources/vector/dashboards/pcp-vector-host-overview.jsonnet b/src/datasources/vector/dashboards/pcp-vector-host-overview.jsonnet index eb5489fa..6208aae3 100644 --- a/src/datasources/vector/dashboards/pcp-vector-host-overview.jsonnet +++ b/src/datasources/vector/dashboards/pcp-vector-host-overview.jsonnet @@ -19,6 +19,21 @@ grafana.dashboard.new( hide='value', ) ) +.addTemplate( + grafana.template.text( + 'url', + label='URL', + ) + { + description: 'overwrite pmproxy URL (example: http://127.0.0.1:44322)' + } +) +.addTemplate( + grafana.template.text( + 'hostspec', + ) + { + description: 'overwrite PCP host specification (example: pcp://127.0.0.1:44321)' + } +) .addPanel( grafana.row.new( @@ -39,8 +54,8 @@ grafana.dashboard.new( stack=true, ) .addTargets([ - { expr: 'kernel.cpu.util.user', format: 'time_series', legendFormat: '$metric' }, - { expr: 'kernel.cpu.util.sys', format: 'time_series', legendFormat: '$metric' }, + { expr: 'kernel.cpu.util.user', format: 'time_series', legendFormat: '$metric', url: '$url', hostspec: '$hostspec' }, + { expr: 'kernel.cpu.util.sys', format: 'time_series', legendFormat: '$metric', url: '$url', hostspec: '$hostspec' }, ]), gridPos={ x: 0, y: 1, @@ -60,7 +75,7 @@ grafana.dashboard.new( legend_values=true, ) .addTargets([ - { expr: 'kernel.all.load', format: 'time_series', legendFormat: '$instance' }, + { expr: 'kernel.all.load', format: 'time_series', legendFormat: '$instance', url: '$url', hostspec: '$hostspec' }, ]), gridPos={ x: 12, y: 1, @@ -77,9 +92,9 @@ grafana.dashboard.new( stack=true, ) .addTargets([ - { expr: 'mem.util.free', format: 'time_series', legendFormat: '$metric' }, - { expr: 'mem.util.cached', format: 'time_series', legendFormat: '$metric' }, - { expr: 'mem.physmem', format: 'time_series', legendFormat: '$metric' }, + { expr: 'mem.util.free', format: 'time_series', legendFormat: '$metric', url: '$url', hostspec: '$hostspec' }, + { expr: 'mem.util.cached', format: 'time_series', legendFormat: '$metric', url: '$url', hostspec: '$hostspec' }, + { expr: 'mem.physmem', format: 'time_series', legendFormat: '$metric', url: '$url', hostspec: '$hostspec' }, ]) .addSeriesOverride({ "alias": "/physmem/", @@ -101,7 +116,7 @@ grafana.dashboard.new( min=0, ) .addTargets([ - { expr: 'disk.dev.util', format: 'time_series', legendFormat: '$instance' }, + { expr: 'disk.dev.util', format: 'time_series', legendFormat: '$instance', url: '$url', hostspec: '$hostspec' }, ]), gridPos={ x: 12, y: 8, @@ -129,7 +144,7 @@ grafana.dashboard.new( max=1, ) .addTargets([ - { expr: 'kernel.percpu.cpu.user', format: 'time_series', legendFormat: '$instance' }, + { expr: 'kernel.percpu.cpu.user', format: 'time_series', legendFormat: '$instance', url: '$url', hostspec: '$hostspec' }, ]), gridPos={ x: 0, y: 16, @@ -146,7 +161,7 @@ grafana.dashboard.new( max=1, ) .addTargets([ - { expr: 'kernel.percpu.cpu.sys', format: 'time_series', legendFormat: '$instance' }, + { expr: 'kernel.percpu.cpu.sys', format: 'time_series', legendFormat: '$instance', url: '$url', hostspec: '$hostspec' }, ]), gridPos={ x: 12, y: 16, @@ -163,7 +178,7 @@ grafana.dashboard.new( legend_show=false, ) .addTargets([ - { expr: 'kernel.cpu.util.user', format: 'time_series', legendFormat: '$metric' }, + { expr: 'kernel.cpu.util.user', format: 'time_series', legendFormat: '$metric', url: '$url', hostspec: '$hostspec' }, ]), gridPos={ x: 0, y: 23, @@ -180,7 +195,7 @@ grafana.dashboard.new( legend_show=false, ) .addTargets([ - { expr: 'kernel.cpu.util.sys', format: 'time_series', legendFormat: '$metric' }, + { expr: 'kernel.cpu.util.sys', format: 'time_series', legendFormat: '$metric', url: '$url', hostspec: '$hostspec' }, ]), gridPos={ x: 6, y: 23, @@ -197,7 +212,7 @@ grafana.dashboard.new( legend_show=false, ) .addTargets([ - { expr: 'kernel.cpu.util.intr', format: 'time_series', legendFormat: '$metric' }, + { expr: 'kernel.cpu.util.intr', format: 'time_series', legendFormat: '$metric', url: '$url', hostspec: '$hostspec' }, ]), gridPos={ x: 12, y: 23, @@ -214,7 +229,7 @@ grafana.dashboard.new( legend_show=false, ) .addTargets([ - { expr: 'kernel.cpu.util.wait', format: 'time_series', legendFormat: '$metric' }, + { expr: 'kernel.cpu.util.wait', format: 'time_series', legendFormat: '$metric', url: '$url', hostspec: '$hostspec' }, ]), gridPos={ x: 18, y: 23, @@ -242,7 +257,7 @@ grafana.dashboard.new( legend_show=false, ) .addTargets([ - { expr: 'kernel.all.pswitch', format: 'time_series', legendFormat: '$metric' }, + { expr: 'kernel.all.pswitch', format: 'time_series', legendFormat: '$metric', url: '$url', hostspec: '$hostspec' }, ]), gridPos={ x: 0, y: 31, @@ -260,7 +275,7 @@ grafana.dashboard.new( staircase=true, ) .addTargets([ - { expr: 'kernel.all.runnable', format: 'time_series' , legendFormat: '$metric'}, + { expr: 'kernel.all.runnable', format: 'time_series' , legendFormat: '$metric', url: '$url', hostspec: '$hostspec'}, ]), gridPos={ x: 12, y: 31, @@ -288,7 +303,7 @@ grafana.dashboard.new( legend_show=false, ) .addTargets([ - { expr: 'mem.util.used', format: 'time_series', legendFormat: '$metric' }, + { expr: 'mem.util.used', format: 'time_series', legendFormat: '$metric', url: '$url', hostspec: '$hostspec' }, ]), gridPos={ x: 0, y: 39, @@ -305,7 +320,7 @@ grafana.dashboard.new( legend_show=false, ) .addTargets([ - { expr: 'mem.util.cached', format: 'time_series', legendFormat: '$metric' }, + { expr: 'mem.util.cached', format: 'time_series', legendFormat: '$metric', url: '$url', hostspec: '$hostspec' }, ]), gridPos={ x: 8, y: 39, @@ -322,7 +337,7 @@ grafana.dashboard.new( legend_show=false, ) .addTargets([ - { expr: 'mem.util.free', format: 'time_series', legendFormat: '$metric' }, + { expr: 'mem.util.free', format: 'time_series', legendFormat: '$metric', url: '$url', hostspec: '$hostspec' }, ]), gridPos={ x: 16, y: 39, @@ -339,7 +354,7 @@ grafana.dashboard.new( legend_show=false, ) .addTargets([ - { expr: 'mem.vmstat.pgfault', format: 'time_series', legendFormat: '$metric' }, + { expr: 'mem.vmstat.pgfault', format: 'time_series', legendFormat: '$metric', url: '$url', hostspec: '$hostspec' }, ]), gridPos={ x: 0, y: 46, @@ -356,7 +371,7 @@ grafana.dashboard.new( legend_show=false, ) .addTargets([ - { expr: 'mem.vmstat.pgmajfault', format: 'time_series', legendFormat: '$metric' }, + { expr: 'mem.vmstat.pgmajfault', format: 'time_series', legendFormat: '$metric', url: '$url', hostspec: '$hostspec' }, ]), gridPos={ x: 12, y: 46, @@ -382,7 +397,7 @@ grafana.dashboard.new( format='Bps', ) .addTargets([ - { expr: 'network.interface.in.bytes', format: 'time_series', legendFormat: '$instance' }, + { expr: 'network.interface.in.bytes', format: 'time_series', legendFormat: '$instance', url: '$url', hostspec: '$hostspec' }, ]), gridPos={ x: 0, y: 54, @@ -397,7 +412,7 @@ grafana.dashboard.new( format='Bps', ) .addTargets([ - { expr: 'network.interface.out.bytes', format: 'time_series', legendFormat: '$instance' }, + { expr: 'network.interface.out.bytes', format: 'time_series', legendFormat: '$instance', url: '$url', hostspec: '$hostspec' }, ]), gridPos={ x: 12, y: 54, @@ -414,7 +429,7 @@ grafana.dashboard.new( decimals=0, ) .addTargets([ - { expr: 'network.interface.in.drops', format: 'time_series', legendFormat: '$instance' }, + { expr: 'network.interface.in.drops', format: 'time_series', legendFormat: '$instance', url: '$url', hostspec: '$hostspec' }, ]), gridPos={ x: 0, y: 61, @@ -431,7 +446,7 @@ grafana.dashboard.new( decimals=0, ) .addTargets([ - { expr: 'network.interface.out.drops', format: 'time_series', legendFormat: '$instance' }, + { expr: 'network.interface.out.drops', format: 'time_series', legendFormat: '$instance', url: '$url', hostspec: '$hostspec' }, ]), gridPos={ x: 12, y: 61, @@ -448,7 +463,7 @@ grafana.dashboard.new( decimals=0, ) .addTargets([ - { expr: 'network.interface.in.packets', format: 'time_series', legendFormat: '$instance' }, + { expr: 'network.interface.in.packets', format: 'time_series', legendFormat: '$instance', url: '$url', hostspec: '$hostspec' }, ]), gridPos={ x: 0, y: 68, @@ -465,7 +480,7 @@ grafana.dashboard.new( decimals=0, ) .addTargets([ - { expr: 'network.interface.out.packets', format: 'time_series', legendFormat: '$instance' }, + { expr: 'network.interface.out.packets', format: 'time_series', legendFormat: '$instance', url: '$url', hostspec: '$hostspec' }, ]), gridPos={ x: 12, y: 68, @@ -493,9 +508,9 @@ grafana.dashboard.new( stack=true, ) .addTargets([ - { expr: 'network.tcpconn.time_wait', format: 'time_series', legendFormat: '$metric' }, - { expr: 'network.tcpconn.established', format: 'time_series', legendFormat: '$metric' }, - { expr: 'network.tcpconn.close_wait', format: 'time_series', legendFormat: '$metric' }, + { expr: 'network.tcpconn.time_wait', format: 'time_series', legendFormat: '$metric', url: '$url', hostspec: '$hostspec' }, + { expr: 'network.tcpconn.established', format: 'time_series', legendFormat: '$metric', url: '$url', hostspec: '$hostspec' }, + { expr: 'network.tcpconn.close_wait', format: 'time_series', legendFormat: '$metric', url: '$url', hostspec: '$hostspec' }, ]), gridPos={ x: 0, y: 76, @@ -512,7 +527,7 @@ grafana.dashboard.new( legend_show=false, ) .addTargets([ - { expr: 'network.tcp.timeouts', format: 'time_series', legendFormat: '$metric' }, + { expr: 'network.tcp.timeouts', format: 'time_series', legendFormat: '$metric', url: '$url', hostspec: '$hostspec' }, ]), gridPos={ x: 0, y: 83, @@ -529,7 +544,7 @@ grafana.dashboard.new( legend_show=false, ) .addTargets([ - { expr: 'network.tcpconn.close_wait', format: 'time_series', legendFormat: '$metric' }, + { expr: 'network.tcpconn.close_wait', format: 'time_series', legendFormat: '$metric', url: '$url', hostspec: '$hostspec' }, ]), gridPos={ x: 6, y: 83, @@ -546,7 +561,7 @@ grafana.dashboard.new( legend_show=false, ) .addTargets([ - { expr: 'network.tcpconn.time_wait', format: 'time_series', legendFormat: '$metric' }, + { expr: 'network.tcpconn.time_wait', format: 'time_series', legendFormat: '$metric', url: '$url', hostspec: '$hostspec' }, ]), gridPos={ x: 12, y: 83, @@ -563,7 +578,7 @@ grafana.dashboard.new( legend_show=false, ) .addTargets([ - { expr: 'network.tcpconn.established', format: 'time_series', legendFormat: '$metric' }, + { expr: 'network.tcpconn.established', format: 'time_series', legendFormat: '$metric', url: '$url', hostspec: '$hostspec' }, ]), gridPos={ x: 18, y: 83, @@ -580,8 +595,8 @@ grafana.dashboard.new( decimals=0, ) .addTargets([ - { expr: 'network.tcp.listendrops', format: 'time_series', legendFormat: '$metric' }, - { expr: 'network.tcp.listenoverflows', format: 'time_series', legendFormat: '$metric' }, + { expr: 'network.tcp.listendrops', format: 'time_series', legendFormat: '$metric', url: '$url', hostspec: '$hostspec' }, + { expr: 'network.tcp.listenoverflows', format: 'time_series', legendFormat: '$metric', url: '$url', hostspec: '$hostspec' }, ]), gridPos={ x: 0, y: 90, @@ -598,10 +613,10 @@ grafana.dashboard.new( decimals=0, ) .addTargets([ - { expr: 'network.tcp.retranssegs', format: 'time_series', legendFormat: '$metric0' }, - { expr: 'network.tcp.fastretrans', format: 'time_series', legendFormat: '$metric0' }, - { expr: 'network.tcp.slowstartretrans', format: 'time_series', legendFormat: '$metric0' }, - { expr: 'network.tcp.synretrans', format: 'time_series', legendFormat: '$metric0' }, + { expr: 'network.tcp.retranssegs', format: 'time_series', legendFormat: '$metric0', url: '$url', hostspec: '$hostspec' }, + { expr: 'network.tcp.fastretrans', format: 'time_series', legendFormat: '$metric0', url: '$url', hostspec: '$hostspec' }, + { expr: 'network.tcp.slowstartretrans', format: 'time_series', legendFormat: '$metric0', url: '$url', hostspec: '$hostspec' }, + { expr: 'network.tcp.synretrans', format: 'time_series', legendFormat: '$metric0', url: '$url', hostspec: '$hostspec' }, ]), gridPos={ x: 12, y: 90, @@ -628,8 +643,8 @@ grafana.dashboard.new( min=0, ) .addTargets([ - { expr: 'disk.dev.read_rawactive', legendFormat: 'read $instance', format: 'time_series' }, - { expr: 'disk.dev.write_rawactive', legendFormat: 'write $instance', format: 'time_series' }, + { expr: 'disk.dev.read_rawactive', format: 'time_series', legendFormat: 'read $instance', url: '$url', hostspec: '$hostspec' }, + { expr: 'disk.dev.write_rawactive', format: 'time_series', legendFormat: 'write $instance', url: '$url', hostspec: '$hostspec' }, ]), gridPos={ x: 0, y: 98, @@ -645,8 +660,8 @@ grafana.dashboard.new( min=0, ) .addTargets([ - { expr: 'disk.dev.read', legendFormat: 'read $instance', format: 'time_series' }, - { expr: 'disk.dev.write', legendFormat: 'write $instance', format: 'time_series' }, + { expr: 'disk.dev.read', format: 'time_series', legendFormat: 'read $instance', url: '$url', hostspec: '$hostspec' }, + { expr: 'disk.dev.write', format: 'time_series', legendFormat: 'write $instance', url: '$url', hostspec: '$hostspec' }, ]), gridPos={ x: 12, y: 98, @@ -662,8 +677,8 @@ grafana.dashboard.new( min=0, ) .addTargets([ - { expr: 'disk.dev.read_bytes', legendFormat: 'read $instance', format: 'time_series' }, - { expr: 'disk.dev.write_bytes', legendFormat: 'write $instance', format: 'time_series' }, + { expr: 'disk.dev.read_bytes', format: 'time_series', legendFormat: 'read $instance', url: '$url', hostspec: '$hostspec' }, + { expr: 'disk.dev.write_bytes', format: 'time_series', legendFormat: 'write $instance', url: '$url', hostspec: '$hostspec' }, ]), gridPos={ x: 0, y: 105, @@ -680,7 +695,7 @@ grafana.dashboard.new( max=1, ) .addTargets([ - { expr: 'disk.dev.avactive', format: 'time_series', legendFormat: '$instance' }, + { expr: 'disk.dev.avactive', format: 'time_series', legendFormat: '$instance', url: '$url', hostspec: '$hostspec' }, ]), gridPos={ x: 12, y: 105, diff --git a/src/datasources/vector/datasource.test.ts b/src/datasources/vector/datasource.test.ts index 8e816971..58e9ca89 100644 --- a/src/datasources/vector/datasource.test.ts +++ b/src/datasources/vector/datasource.test.ts @@ -12,7 +12,7 @@ jest.mock('@grafana/runtime', () => ({ ...jest.requireActual('@grafana/runtime'), getBackendSrv: () => backendSrvMock, getTemplateSrv: () => ({ - replace: (x: string) => x, + replace: (x: string) => x.replace('$empty_dashboard_var', ''), }), })); @@ -33,8 +33,8 @@ describe('PCP Vector', () => { }); it('should poll disk.dev.read, perform rate conversion and return the result', async () => { - const queries = [ds.query()]; - let response = await datasource.query(grafana.dataQueryRequest(queries)); + const targets = [ds.query()]; + let response = await datasource.query(grafana.dataQueryRequest({ targets })); expect(response).toEqual({ data: [] }); mockNextResponses([ @@ -57,7 +57,7 @@ describe('PCP Vector', () => { ]); await datasource.poller.poll(); - response = await datasource.query(grafana.dataQueryRequest(queries)); + response = await datasource.query(grafana.dataQueryRequest({ targets })); expect({ fields: response.data[0].fields }).toMatchInlineSnapshot( { fields: [{}, { config: { custom: expect.anything() } }, { config: { custom: expect.anything() } }], @@ -123,7 +123,7 @@ describe('PCP Vector', () => { Array [ Object { "params": Object { - "hostspec": "127.0.0.1", + "hostspec": "pcp://127.0.0.1", "polltimeout": 11, }, "url": "http://localhost:1234/pmapi/context", @@ -267,8 +267,8 @@ describe('PCP Vector', () => { }); it.skip('redisBackfill hook should use panel url', async () => { - const queries = [ds.query({ expr: 'kernel.all.sysfork', url: 'http://panel_url:1234' })]; - let response = await datasource.query(grafana.dataQueryRequest(queries)); + const targets = [ds.query({ expr: 'kernel.all.sysfork', url: 'http://panel_url:1234' })]; + let response = await datasource.query(grafana.dataQueryRequest({ targets })); expect(response).toEqual({ data: [] }); mockNextResponses([ @@ -281,7 +281,7 @@ describe('PCP Vector', () => { ]); await datasource.poller.poll(); - response = await datasource.query(grafana.dataQueryRequest(queries)); + response = await datasource.query(grafana.dataQueryRequest({ targets })); expect({ fields: response.data[0].fields }).toMatchInlineSnapshot( { fields: [{}, { config: { custom: expect.anything() } }] }, @@ -325,7 +325,7 @@ describe('PCP Vector', () => { Array [ Object { "params": Object { - "hostspec": "127.0.0.1", + "hostspec": "pcp://127.0.0.1", "polltimeout": 11, }, "url": "http://panel_url:1234/pmapi/context", @@ -380,7 +380,7 @@ describe('PCP Vector: overridden url and hostspec', () => { const datasource = new PCPVectorDataSource(instanceSettings as any); const targets = [{ refId: 'A', expr: 'mem.util.free', format: TargetFormat.TimeSeries }]; - let response = await datasource.query(grafana.dataQueryRequest(targets)); + let response = await datasource.query(grafana.dataQueryRequest({ targets })); expect(response).toEqual({ data: [] }); mockNextResponses([ @@ -391,7 +391,7 @@ describe('PCP Vector: overridden url and hostspec', () => { ]); await datasource.poller.poll(); - response = await datasource.query(grafana.dataQueryRequest(targets)); + response = await datasource.query(grafana.dataQueryRequest({ targets })); expect(response).toMatchObject({ data: [{ fields: [{ values: { buffer: [10000] } }, { values: { buffer: [1000] } }] }], }); @@ -434,7 +434,7 @@ describe('PCP Vector: overridden url and hostspec', () => { { refId: 'A', expr: 'mem.util.free', format: TargetFormat.TimeSeries, url: 'http://panel_host:8080' }, ]; - let response = await datasource.query(grafana.dataQueryRequest(targets)); + let response = await datasource.query(grafana.dataQueryRequest({ targets })); expect(response).toEqual({ data: [] }); mockNextResponses([ @@ -445,7 +445,7 @@ describe('PCP Vector: overridden url and hostspec', () => { ]); await datasource.poller.poll(); - response = await datasource.query(grafana.dataQueryRequest(targets)); + response = await datasource.query(grafana.dataQueryRequest({ targets })); expect(response).toMatchObject({ data: [{ fields: [{ values: { buffer: [10000] } }, { values: { buffer: [1000] } }] }], }); @@ -493,7 +493,7 @@ describe('PCP Vector: overridden url and hostspec', () => { }, ]; - let response = await datasource.query(grafana.dataQueryRequest(targets)); + let response = await datasource.query(grafana.dataQueryRequest({ targets })); expect(response).toEqual({ data: [] }); mockNextResponses([ @@ -504,7 +504,7 @@ describe('PCP Vector: overridden url and hostspec', () => { ]); await datasource.poller.poll(); - response = await datasource.query(grafana.dataQueryRequest(targets)); + response = await datasource.query(grafana.dataQueryRequest({ targets })); expect(response).toMatchObject({ data: [{ fields: [{ values: { buffer: [10000] } }, { values: { buffer: [1000] } }] }], }); @@ -553,7 +553,7 @@ describe('PCP Vector: overridden url and hostspec', () => { }, ]; - let response = await datasource.query(grafana.dataQueryRequest(targets)); + let response = await datasource.query(grafana.dataQueryRequest({ targets })); expect(response).toEqual({ data: [] }); mockNextResponses([ @@ -564,7 +564,7 @@ describe('PCP Vector: overridden url and hostspec', () => { ]); await datasource.poller.poll(); - response = await datasource.query(grafana.dataQueryRequest(targets)); + response = await datasource.query(grafana.dataQueryRequest({ targets })); expect(response).toMatchObject({ data: [{ fields: [{ values: { buffer: [10000] } }, { values: { buffer: [1000] } }] }], }); @@ -594,4 +594,63 @@ describe('PCP Vector: overridden url and hostspec', () => { ] `); }); + + it('should use datasource url if overwritten url is blank', async () => { + const instanceSettings = { + url: 'http://settings_host:1234', + jsonData: { + hostspec: 'pcp://settings_hostspec:4321', + }, + }; + const datasource = new PCPVectorDataSource(instanceSettings as any); + const targets = [ + { + refId: 'A', + expr: 'mem.util.free', + format: TargetFormat.TimeSeries, + url: '$empty_dashboard_var', + }, + ]; + + let response = await datasource.query(grafana.dataQueryRequest({ targets })); + expect(response).toEqual({ data: [] }); + + mockNextResponses([ + pmapi.context(), + //pmseries.ping(false), + pmapi.metric(['mem.util.free']), + pmapi.fetch('mem.util.free', 10, [[null, 1000]]), + ]); + await datasource.poller.poll(); + + response = await datasource.query(grafana.dataQueryRequest({ targets })); + expect(response).toMatchObject({ + data: [{ fields: [{ values: { buffer: [10000] } }, { values: { buffer: [1000] } }] }], + }); + expect(backendSrvMock.fetch.mock.calls.map(([{ url, params }]) => ({ url, params }))).toMatchInlineSnapshot(` + Array [ + Object { + "params": Object { + "hostspec": "pcp://settings_hostspec:4321", + "polltimeout": 11, + }, + "url": "http://settings_host:1234/pmapi/context", + }, + Object { + "params": Object { + "context": 123, + "names": "mem.util.free", + }, + "url": "http://settings_host:1234/pmapi/metric", + }, + Object { + "params": Object { + "context": 123, + "names": "mem.util.free", + }, + "url": "http://settings_host:1234/pmapi/fetch", + }, + ] + `); + }); });