Browse Source

stackdriver: making sure we dont pass too big alignmentPeriods to the stackdriver api

Erik Sundell 7 năm trước cách đây
mục cha
commit
edf38f040d

+ 6 - 0
pkg/tsdb/stackdriver/stackdriver.go

@@ -163,6 +163,12 @@ func setAggParams(params *url.Values, query *tsdb.Query) {
 		alignmentPeriod = "+" + strconv.Itoa(alignmentPeriodValue) + "s"
 	}
 
+	re := regexp.MustCompile("[0-9]+")
+	aa, err := strconv.ParseInt(re.FindString(alignmentPeriod), 10, 64)
+	if err != nil || aa > 3600 {
+		alignmentPeriod = "+3600s"
+	}
+
 	params.Add("aggregation.crossSeriesReducer", primaryAggregation)
 	params.Add("aggregation.perSeriesAligner", perSeriesAligner)
 	params.Add("aggregation.alignmentPeriod", alignmentPeriod)

+ 24 - 0
pkg/tsdb/stackdriver/stackdriver_test.go

@@ -91,6 +91,30 @@ func TestStackdriver(t *testing.T) {
 				})
 			})
 
+			Convey("and alignmentPeriod is set in frontend", func() {
+				Convey("and alignment period is too big", func() {
+					tsdbQuery.Queries[0].IntervalMs = 1000
+					tsdbQuery.Queries[0].Model = simplejson.NewFromAny(map[string]interface{}{
+						"alignmentPeriod": "+360000s",
+					})
+
+					queries, err := executor.buildQueries(tsdbQuery)
+					So(err, ShouldBeNil)
+					So(queries[0].Params["aggregation.alignmentPeriod"][0], ShouldEqual, `+3600s`)
+				})
+
+				Convey("and alignment period is within accepted range", func() {
+					tsdbQuery.Queries[0].IntervalMs = 1000
+					tsdbQuery.Queries[0].Model = simplejson.NewFromAny(map[string]interface{}{
+						"alignmentPeriod": "+600s",
+					})
+
+					queries, err := executor.buildQueries(tsdbQuery)
+					So(err, ShouldBeNil)
+					So(queries[0].Params["aggregation.alignmentPeriod"][0], ShouldEqual, `+600s`)
+				})
+			})
+
 			Convey("and query has aggregation mean set", func() {
 				tsdbQuery.Queries[0].Model = simplejson.NewFromAny(map[string]interface{}{
 					"target":             "target",