Jelajahi Sumber

stackdriver: convert most common stackdriver units to grafana units if possible

Erik Sundell 7 tahun lalu
induk
melakukan
251bb09aed

+ 16 - 0
public/app/plugins/datasource/stackdriver/constants.ts

@@ -253,3 +253,19 @@ export const alignmentPeriods = [
   { text: '1d', value: '+86400s' },
   { text: '1w', value: '+604800s' },
 ];
+
+export const stackdriverUnitMappings = {
+  bit: 'bits',
+  By: 'bytes',
+  s: 's',
+  min: 'm',
+  h: 'h',
+  d: 'd',
+  us: 'µs',
+  ms: 'ms',
+  ns: 'ns',
+  percent: 'percent',
+  MiBy: 'mbytes',
+  'By/s': 'Bps',
+  GBy: 'decgbytes',
+};

+ 14 - 0
public/app/plugins/datasource/stackdriver/datasource.ts

@@ -1,3 +1,4 @@
+import { stackdriverUnitMappings } from './constants';
 /** @ngInject */
 export default class StackdriverDatasource {
   id: number;
@@ -85,6 +86,16 @@ export default class StackdriverDatasource {
     return interpolatedGroupBys;
   }
 
+  resolveUnit(targets: any[]) {
+    let unit = 'none';
+    if (targets.length > 0 && targets.every(t => t.unit === targets[0].unit)) {
+      if (stackdriverUnitMappings.hasOwnProperty(targets[0].unit)) {
+        unit = stackdriverUnitMappings[targets[0].unit];
+      }
+    }
+    return unit;
+  }
+
   async query(options) {
     const result = [];
     const data = await this.getTimeSeries(options);
@@ -93,12 +104,15 @@ export default class StackdriverDatasource {
         if (!queryRes.series) {
           return;
         }
+
+        const unit = this.resolveUnit(options.targets);
         queryRes.series.forEach(series => {
           result.push({
             target: series.name,
             datapoints: series.points,
             refId: queryRes.refId,
             meta: queryRes.meta,
+            unit,
           });
         });
       });

+ 4 - 1
public/app/plugins/datasource/stackdriver/query_ctrl.ts

@@ -19,6 +19,7 @@ export class StackdriverQueryCtrl extends QueryCtrl {
       id: string;
       name: string;
     };
+    unit: string;
     metricType: string;
     service: string;
     refId: string;
@@ -47,6 +48,7 @@ export class StackdriverQueryCtrl extends QueryCtrl {
     metricType: this.defaultDropdownValue,
     service: this.defaultServiceValue,
     metric: '',
+    unit: '',
     aggregation: {
       crossSeriesReducer: 'REDUCE_MEAN',
       alignmentPeriod: 'auto',
@@ -221,7 +223,8 @@ export class StackdriverQueryCtrl extends QueryCtrl {
 
   setMetricType() {
     this.target.metricType = this.metricType;
-    const { valueType, metricKind } = this.metricDescriptors.find(m => m.type === this.target.metricType);
+    const { valueType, metricKind, unit } = this.metricDescriptors.find(m => m.type === this.target.metricType);
+    this.target.unit = unit;
     this.target.valueType = valueType;
     this.target.metricKind = metricKind;
     this.$scope.$broadcast('metricTypeChanged');