소스 검색

Merge pull request #6446 from utkarshcmu/graphite_alerting_fix

Fixed intervalFormat for Graphite Alerting
Carl Bergquist 9 년 전
부모
커밋
bb7f03c91e
2개의 변경된 파일77개의 추가작업 그리고 2개의 파일을 삭제
  1. 17 2
      pkg/tsdb/graphite/graphite.go
  2. 60 0
      pkg/tsdb/graphite/graphite_test.go

+ 17 - 2
pkg/tsdb/graphite/graphite.go

@@ -8,6 +8,7 @@ import (
 	"net/http"
 	"net/http"
 	"net/url"
 	"net/url"
 	"path"
 	"path"
+	"regexp"
 	"strings"
 	"strings"
 
 
 	"golang.org/x/net/context/ctxhttp"
 	"golang.org/x/net/context/ctxhttp"
@@ -49,9 +50,9 @@ func (e *GraphiteExecutor) Execute(ctx context.Context, queries tsdb.QuerySlice,
 
 
 	for _, query := range queries {
 	for _, query := range queries {
 		if fullTarget, err := query.Model.Get("targetFull").String(); err == nil {
 		if fullTarget, err := query.Model.Get("targetFull").String(); err == nil {
-			formData["target"] = []string{fullTarget}
+			formData["target"] = []string{fixIntervalFormat(fullTarget)}
 		} else {
 		} else {
-			formData["target"] = []string{query.Model.Get("target").MustString()}
+			formData["target"] = []string{fixIntervalFormat(query.Model.Get("target").MustString())}
 		}
 		}
 	}
 	}
 
 
@@ -141,3 +142,17 @@ func formatTimeRange(input string) string {
 	}
 	}
 	return strings.Replace(strings.Replace(input, "m", "min", -1), "M", "mon", -1)
 	return strings.Replace(strings.Replace(input, "m", "min", -1), "M", "mon", -1)
 }
 }
+
+func fixIntervalFormat(target string) string {
+	rMinute := regexp.MustCompile(`'(\d+)m'`)
+	rMin := regexp.MustCompile("m")
+	target = rMinute.ReplaceAllStringFunc(target, func(m string) string {
+		return rMin.ReplaceAllString(m, "min")
+	})
+	rMonth := regexp.MustCompile(`'(\d+)M'`)
+	rMon := regexp.MustCompile("M")
+	target = rMonth.ReplaceAllStringFunc(target, func(M string) string {
+		return rMon.ReplaceAllString(M, "mon")
+	})
+	return target
+}

+ 60 - 0
pkg/tsdb/graphite/graphite_test.go

@@ -1 +1,61 @@
 package graphite
 package graphite
+
+import (
+	. "github.com/smartystreets/goconvey/convey"
+	"testing"
+)
+
+func TestGraphiteFunctions(t *testing.T) {
+	Convey("Testing Graphite Functions", t, func() {
+
+		Convey("formatting time range for now", func() {
+
+			timeRange := formatTimeRange("now")
+			So(timeRange, ShouldEqual, "now")
+
+		})
+
+		Convey("formatting time range for now-1m", func() {
+
+			timeRange := formatTimeRange("now-1m")
+			So(timeRange, ShouldEqual, "now-1min")
+
+		})
+
+		Convey("formatting time range for now-1M", func() {
+
+			timeRange := formatTimeRange("now-1M")
+			So(timeRange, ShouldEqual, "now-1mon")
+
+		})
+
+		Convey("fix interval format in query for 1m", func() {
+
+			timeRange := fixIntervalFormat("aliasByNode(hitcount(averageSeries(app.grafana.*.dashboards.views.count), '1m'), 4)")
+			So(timeRange, ShouldEqual, "aliasByNode(hitcount(averageSeries(app.grafana.*.dashboards.views.count), '1min'), 4)")
+
+		})
+
+		Convey("fix interval format in query for 1M", func() {
+
+			timeRange := fixIntervalFormat("aliasByNode(hitcount(averageSeries(app.grafana.*.dashboards.views.count), '1M'), 4)")
+			So(timeRange, ShouldEqual, "aliasByNode(hitcount(averageSeries(app.grafana.*.dashboards.views.count), '1mon'), 4)")
+
+		})
+
+		Convey("should not override query for 1M", func() {
+
+			timeRange := fixIntervalFormat("app.grafana.*.dashboards.views.1M.count")
+			So(timeRange, ShouldEqual, "app.grafana.*.dashboards.views.1M.count")
+
+		})
+
+		Convey("should not override query for 1m", func() {
+
+			timeRange := fixIntervalFormat("app.grafana.*.dashboards.views.1m.count")
+			So(timeRange, ShouldEqual, "app.grafana.*.dashboards.views.1m.count")
+
+		})
+
+	})
+}