mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
stackdriver: adds support for primary aggregations
WIP: Hardcoded values for the aligner and alignment period. Need to set the aligment period to the closest min interval and research the aligner more.
This commit is contained in:
parent
0b5783563e
commit
f4fe26c659
@ -59,7 +59,7 @@ func (e *StackdriverExecutor) Query(ctx context.Context, dsInfo *models.DataSour
|
|||||||
Results: make(map[string]*tsdb.QueryResult),
|
Results: make(map[string]*tsdb.QueryResult),
|
||||||
}
|
}
|
||||||
|
|
||||||
queries, err := e.parseQueries(tsdbQuery)
|
queries, err := e.buildQueries(tsdbQuery)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -75,7 +75,7 @@ func (e *StackdriverExecutor) Query(ctx context.Context, dsInfo *models.DataSour
|
|||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *StackdriverExecutor) parseQueries(tsdbQuery *tsdb.TsdbQuery) ([]*StackdriverQuery, error) {
|
func (e *StackdriverExecutor) buildQueries(tsdbQuery *tsdb.TsdbQuery) ([]*StackdriverQuery, error) {
|
||||||
stackdriverQueries := []*StackdriverQuery{}
|
stackdriverQueries := []*StackdriverQuery{}
|
||||||
|
|
||||||
startTime, err := tsdbQuery.TimeRange.ParseFrom()
|
startTime, err := tsdbQuery.TimeRange.ParseFrom()
|
||||||
@ -102,8 +102,8 @@ func (e *StackdriverExecutor) parseQueries(tsdbQuery *tsdb.TsdbQuery) ([]*Stackd
|
|||||||
params := url.Values{}
|
params := url.Values{}
|
||||||
params.Add("interval.startTime", startTime.UTC().Format(time.RFC3339))
|
params.Add("interval.startTime", startTime.UTC().Format(time.RFC3339))
|
||||||
params.Add("interval.endTime", endTime.UTC().Format(time.RFC3339))
|
params.Add("interval.endTime", endTime.UTC().Format(time.RFC3339))
|
||||||
params.Add("aggregation.perSeriesAligner", "ALIGN_NONE")
|
params.Add("filter", "metric.type=\""+metricType+"\"")
|
||||||
params.Add("filter", metricType)
|
setAggParams(¶ms, query)
|
||||||
|
|
||||||
if setting.Env == setting.DEV {
|
if setting.Env == setting.DEV {
|
||||||
slog.Debug("Stackdriver request", "params", params)
|
slog.Debug("Stackdriver request", "params", params)
|
||||||
@ -119,6 +119,22 @@ func (e *StackdriverExecutor) parseQueries(tsdbQuery *tsdb.TsdbQuery) ([]*Stackd
|
|||||||
return stackdriverQueries, nil
|
return stackdriverQueries, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func setAggParams(params *url.Values, query *tsdb.Query) {
|
||||||
|
primaryAggregation := query.Model.Get("primaryAggregation").MustString()
|
||||||
|
if primaryAggregation == "" {
|
||||||
|
primaryAggregation = "REDUCE_NONE"
|
||||||
|
}
|
||||||
|
|
||||||
|
if primaryAggregation == "REDUCE_NONE" {
|
||||||
|
params.Add("aggregation.perSeriesAligner", "ALIGN_NONE")
|
||||||
|
} else {
|
||||||
|
params.Add("aggregation.crossSeriesReducer", primaryAggregation)
|
||||||
|
params.Add("aggregation.perSeriesAligner", "ALIGN_MEAN")
|
||||||
|
params.Add("aggregation.alignmentPeriod", "+60s")
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func (e *StackdriverExecutor) executeQuery(ctx context.Context, query *StackdriverQuery, tsdbQuery *tsdb.TsdbQuery) (*tsdb.QueryResult, error) {
|
func (e *StackdriverExecutor) executeQuery(ctx context.Context, query *StackdriverQuery, tsdbQuery *tsdb.TsdbQuery) (*tsdb.QueryResult, error) {
|
||||||
queryResult := &tsdb.QueryResult{Meta: simplejson.New(), RefId: query.RefID}
|
queryResult := &tsdb.QueryResult{Meta: simplejson.New(), RefId: query.RefID}
|
||||||
|
|
||||||
|
@ -17,7 +17,7 @@ func TestStackdriver(t *testing.T) {
|
|||||||
Convey("Stackdriver", t, func() {
|
Convey("Stackdriver", t, func() {
|
||||||
executor := &StackdriverExecutor{}
|
executor := &StackdriverExecutor{}
|
||||||
|
|
||||||
Convey("Parse query from frontend", func() {
|
Convey("Parse queries from frontend and build Stackdriver API queries", func() {
|
||||||
fromStart := time.Date(2018, 3, 15, 13, 0, 0, 0, time.UTC).In(time.Local)
|
fromStart := time.Date(2018, 3, 15, 13, 0, 0, 0, time.UTC).In(time.Local)
|
||||||
tsdbQuery := &tsdb.TsdbQuery{
|
tsdbQuery := &tsdb.TsdbQuery{
|
||||||
TimeRange: &tsdb.TimeRange{
|
TimeRange: &tsdb.TimeRange{
|
||||||
@ -34,7 +34,9 @@ func TestStackdriver(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
queries, err := executor.parseQueries(tsdbQuery)
|
|
||||||
|
Convey("and query has no aggregation set", func() {
|
||||||
|
queries, err := executor.buildQueries(tsdbQuery)
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
|
|
||||||
So(len(queries), ShouldEqual, 1)
|
So(len(queries), ShouldEqual, 1)
|
||||||
@ -44,7 +46,31 @@ func TestStackdriver(t *testing.T) {
|
|||||||
So(queries[0].Params["interval.startTime"][0], ShouldEqual, "2018-03-15T13:00:00Z")
|
So(queries[0].Params["interval.startTime"][0], ShouldEqual, "2018-03-15T13:00:00Z")
|
||||||
So(queries[0].Params["interval.endTime"][0], ShouldEqual, "2018-03-15T13:34:00Z")
|
So(queries[0].Params["interval.endTime"][0], ShouldEqual, "2018-03-15T13:34:00Z")
|
||||||
So(queries[0].Params["aggregation.perSeriesAligner"][0], ShouldEqual, "ALIGN_NONE")
|
So(queries[0].Params["aggregation.perSeriesAligner"][0], ShouldEqual, "ALIGN_NONE")
|
||||||
So(queries[0].Params["filter"][0], ShouldEqual, "a/metric/type")
|
So(queries[0].Params["filter"][0], ShouldEqual, "metric.type=\"a/metric/type\"")
|
||||||
|
})
|
||||||
|
|
||||||
|
Convey("and query has aggregation mean set", func() {
|
||||||
|
tsdbQuery.Queries[0].Model = simplejson.NewFromAny(map[string]interface{}{
|
||||||
|
"target": "target",
|
||||||
|
"metricType": "a/metric/type",
|
||||||
|
"primaryAggregation": "REDUCE_MEAN",
|
||||||
|
})
|
||||||
|
|
||||||
|
queries, err := executor.buildQueries(tsdbQuery)
|
||||||
|
So(err, ShouldBeNil)
|
||||||
|
|
||||||
|
So(len(queries), ShouldEqual, 1)
|
||||||
|
So(queries[0].RefID, ShouldEqual, "A")
|
||||||
|
So(queries[0].Target, ShouldEqual, "target")
|
||||||
|
So(len(queries[0].Params), ShouldEqual, 6)
|
||||||
|
So(queries[0].Params["interval.startTime"][0], ShouldEqual, "2018-03-15T13:00:00Z")
|
||||||
|
So(queries[0].Params["interval.endTime"][0], ShouldEqual, "2018-03-15T13:34:00Z")
|
||||||
|
So(queries[0].Params["aggregation.crossSeriesReducer"][0], ShouldEqual, "REDUCE_MEAN")
|
||||||
|
So(queries[0].Params["aggregation.perSeriesAligner"][0], ShouldEqual, "ALIGN_MEAN")
|
||||||
|
So(queries[0].Params["aggregation.alignmentPeriod"][0], ShouldEqual, "+60s")
|
||||||
|
So(queries[0].Params["filter"][0], ShouldEqual, "metric.type=\"a/metric/type\"")
|
||||||
|
})
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
Convey("Parse stackdriver response in the time series format", func() {
|
Convey("Parse stackdriver response in the time series format", func() {
|
||||||
|
@ -15,7 +15,8 @@ export default class StackdriverDatasource {
|
|||||||
const queries = options.targets.filter(target => !target.hide).map(t => ({
|
const queries = options.targets.filter(target => !target.hide).map(t => ({
|
||||||
refId: t.refId,
|
refId: t.refId,
|
||||||
datasourceId: this.id,
|
datasourceId: this.id,
|
||||||
metricType: `metric.type="${t.metricType}"`,
|
metricType: t.metricType,
|
||||||
|
primaryAggregation: t.aggregation,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
const result = [];
|
const result = [];
|
||||||
@ -32,6 +33,9 @@ export default class StackdriverDatasource {
|
|||||||
|
|
||||||
if (data.results) {
|
if (data.results) {
|
||||||
Object['values'](data.results).forEach(queryRes => {
|
Object['values'](data.results).forEach(queryRes => {
|
||||||
|
if (!queryRes.series) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
queryRes.series.forEach(series => {
|
queryRes.series.forEach(series => {
|
||||||
result.push({
|
result.push({
|
||||||
target: series.name,
|
target: series.name,
|
||||||
|
@ -1,11 +1,4 @@
|
|||||||
<query-editor-row query-ctrl="ctrl" has-text-edit-mode="true">
|
<query-editor-row query-ctrl="ctrl" has-text-edit-mode="true">
|
||||||
<div class="gf-form-inline">
|
|
||||||
<div class="gf-form">
|
|
||||||
<span class="gf-form-label width-9">Project</span>
|
|
||||||
<input class="gf-form-input" disabled type="text" ng-model='ctrl.target.project.name' get-options="ctrl.getProjects()"
|
|
||||||
css-class="min-width-12" />
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<div class="gf-form-inline">
|
<div class="gf-form-inline">
|
||||||
<div class="gf-form">
|
<div class="gf-form">
|
||||||
<span class="gf-form-label width-9">Metric Type</span>
|
<span class="gf-form-label width-9">Metric Type</span>
|
||||||
@ -17,6 +10,24 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="gf-form-inline">
|
<div class="gf-form-inline">
|
||||||
|
<div class="gf-form gf-form--grow">
|
||||||
|
<label class="gf-form-label query-keyword width-9">Aggregation</label>
|
||||||
|
<div class="gf-form-select-wrapper gf-form-select-wrapper--caret-indent">
|
||||||
|
<select class="gf-form-input width-11" ng-model="ctrl.target.aggregation" ng-options="f.value as f.text for f in ctrl.aggOptions"
|
||||||
|
ng-change="ctrl.refresh()"></select>
|
||||||
|
</div>
|
||||||
|
<div class="gf-form-label gf-form-label--grow"></div>
|
||||||
|
</div>
|
||||||
|
<div class="gf-form gf-form--grow">
|
||||||
|
<div class="gf-form-label gf-form-label--grow"></div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="gf-form-inline">
|
||||||
|
<div class="gf-form">
|
||||||
|
<span class="gf-form-label width-9">Project</span>
|
||||||
|
<input class="gf-form-input" disabled type="text" ng-model='ctrl.target.project.name' get-options="ctrl.getProjects()"
|
||||||
|
css-class="min-width-12" />
|
||||||
|
</div>
|
||||||
<div class="gf-form">
|
<div class="gf-form">
|
||||||
<label class="gf-form-label query-keyword" ng-click="ctrl.showHelp = !ctrl.showHelp">
|
<label class="gf-form-label query-keyword" ng-click="ctrl.showHelp = !ctrl.showHelp">
|
||||||
Show Help
|
Show Help
|
||||||
|
@ -23,9 +23,24 @@ export class StackdriverQueryCtrl extends QueryCtrl {
|
|||||||
id: 'default',
|
id: 'default',
|
||||||
name: 'loading project...',
|
name: 'loading project...',
|
||||||
},
|
},
|
||||||
// metricType: this.defaultDropdownValue,
|
metricType: this.defaultDropdownValue,
|
||||||
|
aggregation: 'REDUCE_MEAN',
|
||||||
};
|
};
|
||||||
|
|
||||||
|
aggOptions = [
|
||||||
|
{ text: 'none', value: 'REDUCE_NONE' },
|
||||||
|
{ text: 'mean', value: 'REDUCE_MEAN' },
|
||||||
|
{ text: 'min', value: 'REDUCE_MIN' },
|
||||||
|
{ text: 'max', value: 'REDUCE_MAX' },
|
||||||
|
{ text: 'sum', value: 'REDUCE_SUM' },
|
||||||
|
{ text: 'std. dev.', value: 'REDUCE_STDDEV' },
|
||||||
|
{ text: 'count', value: 'REDUCE_COUNT' },
|
||||||
|
{ text: '99th percentile', value: 'REDUCE_PERCENTILE_99' },
|
||||||
|
{ text: '95th percentile', value: 'REDUCE_PERCENTILE_95' },
|
||||||
|
{ text: '50th percentile', value: 'REDUCE_PERCENTILE_50' },
|
||||||
|
{ text: '5th percentile', value: 'REDUCE_PERCENTILE_05' },
|
||||||
|
];
|
||||||
|
|
||||||
showHelp: boolean;
|
showHelp: boolean;
|
||||||
showLastQuery: boolean;
|
showLastQuery: boolean;
|
||||||
lastQueryMeta: QueryMeta;
|
lastQueryMeta: QueryMeta;
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import StackdriverDataSource from '../datasource';
|
import StackdriverDataSource from '../datasource';
|
||||||
import { metricDescriptors } from './testData';
|
import { metricDescriptors } from './testData';
|
||||||
|
import moment from 'moment';
|
||||||
|
|
||||||
describe('StackdriverDataSource', () => {
|
describe('StackdriverDataSource', () => {
|
||||||
describe('when performing testDataSource', () => {
|
describe('when performing testDataSource', () => {
|
||||||
@ -93,4 +94,51 @@ describe('StackdriverDataSource', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('When performing query', () => {
|
||||||
|
const options = {
|
||||||
|
range: {
|
||||||
|
from: moment.utc('2017-08-22T20:00:00Z'),
|
||||||
|
to: moment.utc('2017-08-22T23:59:00Z'),
|
||||||
|
},
|
||||||
|
rangeRaw: {
|
||||||
|
from: 'now-4h',
|
||||||
|
to: 'now',
|
||||||
|
},
|
||||||
|
targets: [
|
||||||
|
{
|
||||||
|
refId: 'A',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
};
|
||||||
|
|
||||||
|
describe('and no time series data is returned', () => {
|
||||||
|
let ds;
|
||||||
|
const response = {
|
||||||
|
results: {
|
||||||
|
A: {
|
||||||
|
refId: 'A',
|
||||||
|
meta: {
|
||||||
|
rawQuery: 'arawquerystring',
|
||||||
|
},
|
||||||
|
series: null,
|
||||||
|
tables: null,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
const backendSrv = {
|
||||||
|
datasourceRequest: async () => Promise.resolve({ status: 200, data: response }),
|
||||||
|
};
|
||||||
|
ds = new StackdriverDataSource({}, backendSrv);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return a list of datapoints', () => {
|
||||||
|
return ds.query(options).then(results => {
|
||||||
|
expect(results.data.length).toBe(0);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user