Kaynağa Gözat

stackdriver: fixes #15182

For the filter expression, a better default is to
interpolate multi variables as a regex rather than
the default globbing pattern.

Also, uses the real TemplateSrv class rather than
stubbing it in the tests.
Daniel Lee 6 yıl önce
ebeveyn
işleme
1ecd70e2dd

+ 8 - 4
public/app/plugins/datasource/stackdriver/datasource.ts

@@ -40,9 +40,7 @@ export default class StackdriverDatasource implements DataSourceApi<StackdriverQ
           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 => {
-            return this.templateSrv.replace(f, options.scopedVars || {});
-          }),
+          filters: this.interpolateFilters(t.filters, options.scopedVars),
           aliasBy: this.templateSrv.replace(t.aliasBy, options.scopedVars || {}),
           aliasBy: this.templateSrv.replace(t.aliasBy, options.scopedVars || {}),
           type: 'timeSeriesQuery',
           type: 'timeSeriesQuery',
         };
         };
@@ -64,7 +62,13 @@ export default class StackdriverDatasource implements DataSourceApi<StackdriverQ
     }
     }
   }
   }
 
 
-  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: [
         {
         {

+ 51 - 10
public/app/plugins/datasource/stackdriver/specs/datasource.test.ts

@@ -1,7 +1,8 @@
 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', () => {
       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', () => {
     });
     });
   });
   });
 
 
+  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 = {
-          test: 'groupby1',
-        };
-        const ds = new StackdriverDataSource(instanceSettings, {}, templateSrv, timeSrv);
+        const groupByTemplateSrv = initTemplateSrv('groupby1');
+        const ds = new StackdriverDataSource(instanceSettings, {}, groupByTemplateSrv, timeSrv);
         interpolated = ds.interpolateGroupBys(['[[test]]'], {});
         interpolated = ds.interpolateGroupBys(['[[test]]'], {});
       });
       });
 
 
@@ -174,10 +201,8 @@ describe('StackdriverDataSource', () => {
 
 
     describe('and is multi value variable', () => {
     describe('and is multi value variable', () => {
       beforeEach(() => {
       beforeEach(() => {
-        templateSrv.data = {
-          test: 'groupby1,groupby2',
-        };
-        const ds = new StackdriverDataSource(instanceSettings, {}, templateSrv, timeSrv);
+        const groupByTemplateSrv = initTemplateSrv(['groupby1', 'groupby2'], true);
+        const ds = new StackdriverDataSource(instanceSettings, {}, groupByTemplateSrv, timeSrv);
         interpolated = ds.interpolateGroupBys(['[[test]]'], {});
         interpolated = ds.interpolateGroupBys(['[[test]]'], {});
       });
       });
 
 
@@ -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;
+}

+ 5 - 5
public/app/plugins/datasource/stackdriver/types.ts

@@ -30,14 +30,14 @@ export interface StackdriverQuery extends DataQuery {
   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;
-  valueType?: string;
-  datasourceId: number;
-  view: string;
+  metricKind: string;
+  valueType: string;
+  datasourceId?: number;
+  view?: string;
 }
 }
 
 
 export interface AnnotationTarget {
 export interface AnnotationTarget {