mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
stackdriver: WIP - implement stackdriver style auto alignment period. also return the used alignment period and display it in the query editor
This commit is contained in:
@@ -116,6 +116,8 @@ func (e *StackdriverExecutor) buildQueries(tsdbQuery *tsdb.TsdbQuery) ([]*Stackd
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
durationSeconds := int(endTime.Sub(startTime).Seconds())
|
||||||
|
|
||||||
for _, query := range tsdbQuery.Queries {
|
for _, query := range tsdbQuery.Queries {
|
||||||
var target string
|
var target string
|
||||||
|
|
||||||
@@ -145,7 +147,7 @@ func (e *StackdriverExecutor) buildQueries(tsdbQuery *tsdb.TsdbQuery) ([]*Stackd
|
|||||||
params.Add("interval.endTime", endTime.UTC().Format(time.RFC3339))
|
params.Add("interval.endTime", endTime.UTC().Format(time.RFC3339))
|
||||||
params.Add("filter", strings.Trim(fmt.Sprintf(`metric.type="%s" %s`, metricType, filterString), " "))
|
params.Add("filter", strings.Trim(fmt.Sprintf(`metric.type="%s" %s`, metricType, filterString), " "))
|
||||||
params.Add("view", query.Model.Get("view").MustString())
|
params.Add("view", query.Model.Get("view").MustString())
|
||||||
setAggParams(¶ms, query)
|
setAggParams(¶ms, query, durationSeconds)
|
||||||
|
|
||||||
if setting.Env == setting.DEV {
|
if setting.Env == setting.DEV {
|
||||||
slog.Debug("Stackdriver request", "params", params)
|
slog.Debug("Stackdriver request", "params", params)
|
||||||
@@ -171,7 +173,7 @@ func (e *StackdriverExecutor) buildQueries(tsdbQuery *tsdb.TsdbQuery) ([]*Stackd
|
|||||||
return stackdriverQueries, nil
|
return stackdriverQueries, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func setAggParams(params *url.Values, query *tsdb.Query) {
|
func setAggParams(params *url.Values, query *tsdb.Query, durationSeconds int) {
|
||||||
primaryAggregation := query.Model.Get("primaryAggregation").MustString()
|
primaryAggregation := query.Model.Get("primaryAggregation").MustString()
|
||||||
perSeriesAligner := query.Model.Get("perSeriesAligner").MustString()
|
perSeriesAligner := query.Model.Get("perSeriesAligner").MustString()
|
||||||
alignmentPeriod := query.Model.Get("alignmentPeriod").MustString()
|
alignmentPeriod := query.Model.Get("alignmentPeriod").MustString()
|
||||||
@@ -185,10 +187,21 @@ func setAggParams(params *url.Values, query *tsdb.Query) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if alignmentPeriod == "grafana-auto" || alignmentPeriod == "" {
|
if alignmentPeriod == "grafana-auto" || alignmentPeriod == "" {
|
||||||
alignmentPeriodValue := int(math.Max(float64(query.IntervalMs), 60.0))
|
alignmentPeriodValue := int(math.Max(float64(query.IntervalMs)/1000, 60.0))
|
||||||
alignmentPeriod = "+" + strconv.Itoa(alignmentPeriodValue) + "s"
|
alignmentPeriod = "+" + strconv.Itoa(alignmentPeriodValue) + "s"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if alignmentPeriod == "stackdriver-auto" {
|
||||||
|
alignmentPeriodValue := int(math.Max(float64(durationSeconds), 60.0))
|
||||||
|
if alignmentPeriodValue <= 60*60*5 {
|
||||||
|
alignmentPeriod = "+60s"
|
||||||
|
} else if alignmentPeriodValue <= 60*60*23 {
|
||||||
|
alignmentPeriod = "+300s"
|
||||||
|
} else {
|
||||||
|
alignmentPeriod = "+3600s"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
re := regexp.MustCompile("[0-9]+")
|
re := regexp.MustCompile("[0-9]+")
|
||||||
seconds, err := strconv.ParseInt(re.FindString(alignmentPeriod), 10, 64)
|
seconds, err := strconv.ParseInt(re.FindString(alignmentPeriod), 10, 64)
|
||||||
if err != nil || seconds > 3600 {
|
if err != nil || seconds > 3600 {
|
||||||
@@ -218,6 +231,15 @@ func (e *StackdriverExecutor) executeQuery(ctx context.Context, query *Stackdriv
|
|||||||
|
|
||||||
req.URL.RawQuery = query.Params.Encode()
|
req.URL.RawQuery = query.Params.Encode()
|
||||||
queryResult.Meta.Set("rawQuery", req.URL.RawQuery)
|
queryResult.Meta.Set("rawQuery", req.URL.RawQuery)
|
||||||
|
alignmentPeriod, ok := req.URL.Query()["aggregation.alignmentPeriod"]
|
||||||
|
|
||||||
|
if ok {
|
||||||
|
re := regexp.MustCompile("[0-9]+")
|
||||||
|
seconds, err := strconv.ParseInt(re.FindString(alignmentPeriod[0]), 10, 64)
|
||||||
|
if err == nil {
|
||||||
|
queryResult.Meta.Set("alignmentPeriod", seconds)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
span, ctx := opentracing.StartSpanFromContext(ctx, "stackdriver query")
|
span, ctx := opentracing.StartSpanFromContext(ctx, "stackdriver query")
|
||||||
span.SetTag("target", query.Target)
|
span.SetTag("target", query.Target)
|
||||||
|
|||||||
@@ -28,15 +28,19 @@
|
|||||||
<div class="gf-form-label gf-form-label--grow"></div>
|
<div class="gf-form-label gf-form-label--grow"></div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="gf-form offset-width-9">
|
</div>
|
||||||
<label class="gf-form-label query-keyword width-12">Alignment Period</label>
|
<div class="gf-form-inline">
|
||||||
|
<div class="gf-form">
|
||||||
|
<label class="gf-form-label query-keyword width-9">Alignment Period</label>
|
||||||
<div class="gf-form-select-wrapper gf-form-select-wrapper--caret-indent">
|
<div class="gf-form-select-wrapper gf-form-select-wrapper--caret-indent">
|
||||||
<select class="gf-form-input width-14" ng-model="target.aggregation.alignmentPeriod" ng-options="f.value as f.text for f in alignmentPeriods"
|
<select class="gf-form-input width-12" ng-model="target.aggregation.alignmentPeriod" ng-options="f.value as f.text for f in alignmentPeriods"
|
||||||
ng-change="refresh()"></select>
|
ng-change="refresh()"></select>
|
||||||
</div>
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
<div class="gf-form gf-form--grow">
|
<div class="gf-form gf-form--grow">
|
||||||
<div class="gf-form-label gf-form-label--grow"></div>
|
<label ng-if="alignmentPeriod" class="gf-form-label gf-form-label--grow">
|
||||||
</div>
|
{{formatAlignmentText()}}
|
||||||
|
</label>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -41,7 +41,7 @@
|
|||||||
<div class="gf-form-label gf-form-label--grow"></div>
|
<div class="gf-form-label gf-form-label--grow"></div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<stackdriver-aggregation target="ctrl.target" refresh="ctrl.refresh()"></stackdriver-aggregation>
|
<stackdriver-aggregation target="ctrl.target" alignment-period="ctrl.lastQueryMeta.alignmentPeriod" refresh="ctrl.refresh()"></stackdriver-aggregation>
|
||||||
<div class="gf-form-inline">
|
<div class="gf-form-inline">
|
||||||
<div class="gf-form">
|
<div class="gf-form">
|
||||||
<span class="gf-form-label query-keyword width-9">Alias By</span>
|
<span class="gf-form-label query-keyword width-9">Alias By</span>
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
import angular from 'angular';
|
import angular from 'angular';
|
||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
import * as options from './constants';
|
import * as options from './constants';
|
||||||
|
import * as options from './constants';
|
||||||
|
import kbn from 'app/core/utils/kbn';
|
||||||
|
|
||||||
export class StackdriverAggregation {
|
export class StackdriverAggregation {
|
||||||
constructor() {
|
constructor() {
|
||||||
@@ -10,6 +12,7 @@ export class StackdriverAggregation {
|
|||||||
restrict: 'E',
|
restrict: 'E',
|
||||||
scope: {
|
scope: {
|
||||||
target: '=',
|
target: '=',
|
||||||
|
alignmentPeriod: '<',
|
||||||
refresh: '&',
|
refresh: '&',
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
@@ -24,6 +27,7 @@ export class StackdriverAggregationCtrl {
|
|||||||
$scope.alignmentPeriods = options.alignmentPeriods;
|
$scope.alignmentPeriods = options.alignmentPeriods;
|
||||||
$scope.onAlignmentChange = this.onAlignmentChange.bind(this);
|
$scope.onAlignmentChange = this.onAlignmentChange.bind(this);
|
||||||
$scope.onAggregationChange = this.onAggregationChange.bind(this);
|
$scope.onAggregationChange = this.onAggregationChange.bind(this);
|
||||||
|
$scope.formatAlignmentText = this.formatAlignmentText.bind(this);
|
||||||
$scope.$on('metricTypeChanged', this.setAlignOptions.bind(this));
|
$scope.$on('metricTypeChanged', this.setAlignOptions.bind(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -77,6 +81,13 @@ export class StackdriverAggregationCtrl {
|
|||||||
this.$scope.target.aggregation.crossSeriesReducer = newValue ? newValue.value : '';
|
this.$scope.target.aggregation.crossSeriesReducer = newValue ? newValue.value : '';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
formatAlignmentText() {
|
||||||
|
const selectedAlignment = this.$scope.alignOptions.find(
|
||||||
|
ap => ap.value === this.$scope.target.aggregation.perSeriesAligner
|
||||||
|
);
|
||||||
|
return `${kbn.secondsToHms(this.$scope.alignmentPeriod)} interval (${selectedAlignment.text})`;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
angular.module('grafana.controllers').directive('stackdriverAggregation', StackdriverAggregation);
|
angular.module('grafana.controllers').directive('stackdriverAggregation', StackdriverAggregation);
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import { FilterSegments, DefaultRemoveFilterValue } from './filter_segments';
|
|||||||
import './query_aggregation_ctrl';
|
import './query_aggregation_ctrl';
|
||||||
|
|
||||||
export interface QueryMeta {
|
export interface QueryMeta {
|
||||||
|
alignmentPeriod: string;
|
||||||
rawQuery: string;
|
rawQuery: string;
|
||||||
rawQueryString: string;
|
rawQueryString: string;
|
||||||
metricLabels: { [key: string]: string[] };
|
metricLabels: { [key: string]: string[] };
|
||||||
|
|||||||
Reference in New Issue
Block a user