Commit 9959bbfd by Daniel Lee Committed by GitHub

Merge pull request #15260 from grafana/15182-stackdriver-filter

Changes default interpolation for Stackdriver filter to be regex. Fixes #15182
parents 6dd1a8e6 1ecd70e2
...@@ -10,21 +10,21 @@ import { Alignments } from './Alignments'; ...@@ -10,21 +10,21 @@ import { Alignments } from './Alignments';
import { AlignmentPeriods } from './AlignmentPeriods'; import { AlignmentPeriods } from './AlignmentPeriods';
import { AliasBy } from './AliasBy'; import { AliasBy } from './AliasBy';
import { Help } from './Help'; import { Help } from './Help';
import { Target, MetricDescriptor } from '../types'; import { StackdriverQuery, MetricDescriptor } from '../types';
import { getAlignmentPickerData } from '../functions'; import { getAlignmentPickerData } from '../functions';
import StackdriverDatasource from '../datasource'; import StackdriverDatasource from '../datasource';
import { SelectOptionItem } from '@grafana/ui'; import { SelectOptionItem } from '@grafana/ui';
export interface Props { export interface Props {
onQueryChange: (target: Target) => void; onQueryChange: (target: StackdriverQuery) => void;
onExecuteQuery: () => void; onExecuteQuery: () => void;
target: Target; target: StackdriverQuery;
events: any; events: any;
datasource: StackdriverDatasource; datasource: StackdriverDatasource;
templateSrv: TemplateSrv; templateSrv: TemplateSrv;
} }
interface State extends Target { interface State extends StackdriverQuery {
alignOptions: SelectOptionItem[]; alignOptions: SelectOptionItem[];
lastQuery: string; lastQuery: string;
lastQueryError: string; lastQueryError: string;
......
...@@ -2,9 +2,10 @@ import { stackdriverUnitMappings } from './constants'; ...@@ -2,9 +2,10 @@ import { stackdriverUnitMappings } from './constants';
import appEvents from 'app/core/app_events'; import appEvents from 'app/core/app_events';
import _ from 'lodash'; import _ from 'lodash';
import StackdriverMetricFindQuery from './StackdriverMetricFindQuery'; import StackdriverMetricFindQuery from './StackdriverMetricFindQuery';
import { MetricDescriptor } from './types'; import { StackdriverQuery, MetricDescriptor } from './types';
import { DataSourceApi, DataQueryOptions } from '@grafana/ui/src/types';
export default class StackdriverDatasource { export default class StackdriverDatasource implements DataSourceApi<StackdriverQuery> {
id: number; id: number;
url: string; url: string;
baseUrl: string; baseUrl: string;
...@@ -39,9 +40,7 @@ export default class StackdriverDatasource { ...@@ -39,9 +40,7 @@ export default class StackdriverDatasource {
alignmentPeriod: this.templateSrv.replace(t.alignmentPeriod, options.scopedVars || {}), alignmentPeriod: this.templateSrv.replace(t.alignmentPeriod, options.scopedVars || {}),
groupBys: this.interpolateGroupBys(t.groupBys, options.scopedVars), groupBys: this.interpolateGroupBys(t.groupBys, options.scopedVars),
view: t.view || 'FULL', view: t.view || 'FULL',
filters: (t.filters || []).map(f => { filters: this.interpolateFilters(t.filters, options.scopedVars),
return this.templateSrv.replace(f, options.scopedVars || {});
}),
aliasBy: this.templateSrv.replace(t.aliasBy, options.scopedVars || {}), aliasBy: this.templateSrv.replace(t.aliasBy, options.scopedVars || {}),
type: 'timeSeriesQuery', type: 'timeSeriesQuery',
}; };
...@@ -63,7 +62,13 @@ export default class StackdriverDatasource { ...@@ -63,7 +62,13 @@ export default class StackdriverDatasource {
} }
} }
async getLabels(metricType, refId) { interpolateFilters(filters: string[], scopedVars: object) {
return (filters || []).map(f => {
return this.templateSrv.replace(f, scopedVars || {}, 'regex');
});
}
async getLabels(metricType: string, refId: string) {
const response = await this.getTimeSeries({ const response = await this.getTimeSeries({
targets: [ targets: [
{ {
...@@ -103,7 +108,7 @@ export default class StackdriverDatasource { ...@@ -103,7 +108,7 @@ export default class StackdriverDatasource {
return unit; return unit;
} }
async query(options) { async query(options: DataQueryOptions<StackdriverQuery>) {
const result = []; const result = [];
const data = await this.getTimeSeries(options); const data = await this.getTimeSeries(options);
if (data.results) { if (data.results) {
......
import _ from 'lodash'; import _ from 'lodash';
import { QueryCtrl } from 'app/plugins/sdk'; import { QueryCtrl } from 'app/plugins/sdk';
import { Target } from './types'; import { StackdriverQuery } from './types';
import { TemplateSrv } from 'app/features/templating/template_srv'; import { TemplateSrv } from 'app/features/templating/template_srv';
export class StackdriverQueryCtrl extends QueryCtrl { export class StackdriverQueryCtrl extends QueryCtrl {
...@@ -16,7 +16,7 @@ export class StackdriverQueryCtrl extends QueryCtrl { ...@@ -16,7 +16,7 @@ export class StackdriverQueryCtrl extends QueryCtrl {
this.onExecuteQuery = this.onExecuteQuery.bind(this); this.onExecuteQuery = this.onExecuteQuery.bind(this);
} }
onQueryChange(target: Target) { onQueryChange(target: StackdriverQuery) {
Object.assign(this.target, target); Object.assign(this.target, target);
} }
......
import StackdriverDataSource from '../datasource'; import StackdriverDataSource from '../datasource';
import { metricDescriptors } from './testData'; import { metricDescriptors } from './testData';
import moment from 'moment'; import moment from 'moment';
import { TemplateSrvStub } from 'test/specs/helpers'; import { TemplateSrv } from 'app/features/templating/template_srv';
import { CustomVariable } from 'app/features/templating/all';
describe('StackdriverDataSource', () => { describe('StackdriverDataSource', () => {
const instanceSettings = { const instanceSettings = {
...@@ -9,7 +10,7 @@ describe('StackdriverDataSource', () => { ...@@ -9,7 +10,7 @@ describe('StackdriverDataSource', () => {
defaultProject: 'testproject', defaultProject: 'testproject',
}, },
}; };
const templateSrv = new TemplateSrvStub(); const templateSrv = new TemplateSrv();
const timeSrv = {}; const timeSrv = {};
describe('when performing testDataSource', () => { describe('when performing testDataSource', () => {
...@@ -154,15 +155,41 @@ describe('StackdriverDataSource', () => { ...@@ -154,15 +155,41 @@ describe('StackdriverDataSource', () => {
}); });
}); });
describe('when interpolating a template variable for the filter', () => {
let interpolated;
describe('and is single value variable', () => {
beforeEach(() => {
const filterTemplateSrv = initTemplateSrv('filtervalue1');
const ds = new StackdriverDataSource(instanceSettings, {}, filterTemplateSrv, timeSrv);
interpolated = ds.interpolateFilters(['resource.label.zone', '=~', '${test}'], {});
});
it('should replace the variable with the value', () => {
expect(interpolated.length).toBe(3);
expect(interpolated[2]).toBe('filtervalue1');
});
});
describe('and is multi value variable', () => {
beforeEach(() => {
const filterTemplateSrv = initTemplateSrv(['filtervalue1', 'filtervalue2'], true);
const ds = new StackdriverDataSource(instanceSettings, {}, filterTemplateSrv, timeSrv);
interpolated = ds.interpolateFilters(['resource.label.zone', '=~', '[[test]]'], {});
});
it('should replace the variable with a regex expression', () => {
expect(interpolated[2]).toBe('(filtervalue1|filtervalue2)');
});
});
});
describe('when interpolating a template variable for group bys', () => { describe('when interpolating a template variable for group bys', () => {
let interpolated; let interpolated;
describe('and is single value variable', () => { describe('and is single value variable', () => {
beforeEach(() => { beforeEach(() => {
templateSrv.data = { const groupByTemplateSrv = initTemplateSrv('groupby1');
test: 'groupby1', const ds = new StackdriverDataSource(instanceSettings, {}, groupByTemplateSrv, timeSrv);
};
const ds = new StackdriverDataSource(instanceSettings, {}, templateSrv, timeSrv);
interpolated = ds.interpolateGroupBys(['[[test]]'], {}); interpolated = ds.interpolateGroupBys(['[[test]]'], {});
}); });
...@@ -174,10 +201,8 @@ describe('StackdriverDataSource', () => { ...@@ -174,10 +201,8 @@ describe('StackdriverDataSource', () => {
describe('and is multi value variable', () => { describe('and is multi value variable', () => {
beforeEach(() => { beforeEach(() => {
templateSrv.data = { const groupByTemplateSrv = initTemplateSrv(['groupby1', 'groupby2'], true);
test: 'groupby1,groupby2', const ds = new StackdriverDataSource(instanceSettings, {}, groupByTemplateSrv, timeSrv);
};
const ds = new StackdriverDataSource(instanceSettings, {}, templateSrv, timeSrv);
interpolated = ds.interpolateGroupBys(['[[test]]'], {}); interpolated = ds.interpolateGroupBys(['[[test]]'], {});
}); });
...@@ -241,3 +266,19 @@ describe('StackdriverDataSource', () => { ...@@ -241,3 +266,19 @@ describe('StackdriverDataSource', () => {
}); });
}); });
}); });
function initTemplateSrv(values: any, multi = false) {
const templateSrv = new TemplateSrv();
templateSrv.init([
new CustomVariable(
{
name: 'test',
current: {
value: values,
},
multi: multi,
},
{}
),
]);
return templateSrv;
}
import { DataQuery } from '@grafana/ui/src/types';
export enum MetricFindQueryTypes { export enum MetricFindQueryTypes {
Services = 'services', Services = 'services',
MetricTypes = 'metricTypes', MetricTypes = 'metricTypes',
...@@ -20,20 +22,22 @@ export interface VariableQueryData { ...@@ -20,20 +22,22 @@ export interface VariableQueryData {
services: Array<{ value: string; name: string }>; services: Array<{ value: string; name: string }>;
} }
export interface Target { export interface StackdriverQuery extends DataQuery {
defaultProject: string; defaultProject?: string;
unit: string; unit?: string;
metricType: string; metricType: string;
service: string; service?: string;
refId: string; refId: string;
crossSeriesReducer: string; crossSeriesReducer: string;
alignmentPeriod: string; alignmentPeriod?: string;
perSeriesAligner: string; perSeriesAligner: string;
groupBys: string[]; groupBys?: string[];
filters: string[]; filters?: string[];
aliasBy: string; aliasBy?: string;
metricKind: string; metricKind: string;
valueType: string; valueType: string;
datasourceId?: number;
view?: string;
} }
export interface AnnotationTarget { export interface AnnotationTarget {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment