stackdriver_test.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package stackdriver
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "testing"
  7. "time"
  8. "github.com/grafana/grafana/pkg/components/simplejson"
  9. "github.com/grafana/grafana/pkg/tsdb"
  10. . "github.com/smartystreets/goconvey/convey"
  11. )
  12. func TestStackdriver(t *testing.T) {
  13. Convey("Stackdriver", t, func() {
  14. executor := &StackdriverExecutor{}
  15. Convey("Parse queries from frontend and build Stackdriver API queries", func() {
  16. fromStart := time.Date(2018, 3, 15, 13, 0, 0, 0, time.UTC).In(time.Local)
  17. tsdbQuery := &tsdb.TsdbQuery{
  18. TimeRange: &tsdb.TimeRange{
  19. From: fmt.Sprintf("%v", fromStart.Unix()*1000),
  20. To: fmt.Sprintf("%v", fromStart.Add(34*time.Minute).Unix()*1000),
  21. },
  22. Queries: []*tsdb.Query{
  23. {
  24. Model: simplejson.NewFromAny(map[string]interface{}{
  25. "target": "target",
  26. "metricType": "a/metric/type",
  27. "view": "FULL",
  28. }),
  29. RefId: "A",
  30. },
  31. },
  32. }
  33. Convey("and query has no aggregation set", func() {
  34. queries, err := executor.buildQueries(tsdbQuery)
  35. So(err, ShouldBeNil)
  36. So(len(queries), ShouldEqual, 1)
  37. So(queries[0].RefID, ShouldEqual, "A")
  38. So(queries[0].Target, ShouldEqual, "target")
  39. So(len(queries[0].Params), ShouldEqual, 5)
  40. So(queries[0].Params["interval.startTime"][0], ShouldEqual, "2018-03-15T13:00:00Z")
  41. So(queries[0].Params["interval.endTime"][0], ShouldEqual, "2018-03-15T13:34:00Z")
  42. So(queries[0].Params["aggregation.perSeriesAligner"][0], ShouldEqual, "ALIGN_NONE")
  43. So(queries[0].Params["filter"][0], ShouldEqual, "metric.type=\"a/metric/type\"")
  44. So(queries[0].Params["view"][0], ShouldEqual, "FULL")
  45. })
  46. Convey("and query has aggregation mean set", func() {
  47. tsdbQuery.Queries[0].Model = simplejson.NewFromAny(map[string]interface{}{
  48. "target": "target",
  49. "metricType": "a/metric/type",
  50. "primaryAggregation": "REDUCE_MEAN",
  51. "view": "FULL",
  52. })
  53. queries, err := executor.buildQueries(tsdbQuery)
  54. So(err, ShouldBeNil)
  55. So(len(queries), ShouldEqual, 1)
  56. So(queries[0].RefID, ShouldEqual, "A")
  57. So(queries[0].Target, ShouldEqual, "target")
  58. So(len(queries[0].Params), ShouldEqual, 7)
  59. So(queries[0].Params["interval.startTime"][0], ShouldEqual, "2018-03-15T13:00:00Z")
  60. So(queries[0].Params["interval.endTime"][0], ShouldEqual, "2018-03-15T13:34:00Z")
  61. So(queries[0].Params["aggregation.crossSeriesReducer"][0], ShouldEqual, "REDUCE_MEAN")
  62. So(queries[0].Params["aggregation.perSeriesAligner"][0], ShouldEqual, "ALIGN_MEAN")
  63. So(queries[0].Params["aggregation.alignmentPeriod"][0], ShouldEqual, "+60s")
  64. So(queries[0].Params["filter"][0], ShouldEqual, "metric.type=\"a/metric/type\"")
  65. So(queries[0].Params["view"][0], ShouldEqual, "FULL")
  66. })
  67. Convey("and query has group bys", func() {
  68. tsdbQuery.Queries[0].Model = simplejson.NewFromAny(map[string]interface{}{
  69. "target": "target",
  70. "metricType": "a/metric/type",
  71. "primaryAggregation": "REDUCE_NONE",
  72. "groupBys": []interface{}{"metric.label.group1", "metric.label.group2"},
  73. "view": "FULL",
  74. })
  75. queries, err := executor.buildQueries(tsdbQuery)
  76. So(err, ShouldBeNil)
  77. So(len(queries), ShouldEqual, 1)
  78. So(queries[0].RefID, ShouldEqual, "A")
  79. So(queries[0].Target, ShouldEqual, "target")
  80. So(len(queries[0].Params), ShouldEqual, 6)
  81. So(queries[0].Params["interval.startTime"][0], ShouldEqual, "2018-03-15T13:00:00Z")
  82. So(queries[0].Params["interval.endTime"][0], ShouldEqual, "2018-03-15T13:34:00Z")
  83. So(queries[0].Params["aggregation.perSeriesAligner"][0], ShouldEqual, "ALIGN_NONE")
  84. So(queries[0].Params["aggregation.groupByFields"][0], ShouldEqual, "metric.label.group1")
  85. So(queries[0].Params["aggregation.groupByFields"][1], ShouldEqual, "metric.label.group2")
  86. So(queries[0].Params["filter"][0], ShouldEqual, "metric.type=\"a/metric/type\"")
  87. So(queries[0].Params["view"][0], ShouldEqual, "FULL")
  88. })
  89. })
  90. Convey("Parse stackdriver response in the time series format", func() {
  91. Convey("when data from query aggregated to one time series", func() {
  92. var data StackDriverResponse
  93. jsonBody, err := ioutil.ReadFile("./test-data/1-series-response-agg-one-metric.json")
  94. So(err, ShouldBeNil)
  95. err = json.Unmarshal(jsonBody, &data)
  96. So(err, ShouldBeNil)
  97. So(len(data.TimeSeries), ShouldEqual, 1)
  98. res := &tsdb.QueryResult{Meta: simplejson.New(), RefId: "A"}
  99. err = executor.parseResponse(res, data)
  100. So(err, ShouldBeNil)
  101. So(len(res.Series), ShouldEqual, 1)
  102. So(res.Series[0].Name, ShouldEqual, "serviceruntime.googleapis.com/api/request_count")
  103. So(len(res.Series[0].Points), ShouldEqual, 3)
  104. Convey("timestamps should be in ascending order", func() {
  105. So(res.Series[0].Points[0][0].Float64, ShouldEqual, 0.05)
  106. So(res.Series[0].Points[0][1].Float64, ShouldEqual, 1536670020000)
  107. So(res.Series[0].Points[1][0].Float64, ShouldEqual, 1.05)
  108. So(res.Series[0].Points[1][1].Float64, ShouldEqual, 1536670080000)
  109. So(res.Series[0].Points[2][0].Float64, ShouldEqual, 1.0666666666667)
  110. So(res.Series[0].Points[2][1].Float64, ShouldEqual, 1536670260000)
  111. })
  112. })
  113. Convey("when data from query with no aggregation", func() {
  114. var data StackDriverResponse
  115. jsonBody, err := ioutil.ReadFile("./test-data/2-series-response-no-agg.json")
  116. So(err, ShouldBeNil)
  117. err = json.Unmarshal(jsonBody, &data)
  118. So(err, ShouldBeNil)
  119. So(len(data.TimeSeries), ShouldEqual, 3)
  120. res := &tsdb.QueryResult{Meta: simplejson.New(), RefId: "A"}
  121. err = executor.parseResponse(res, data)
  122. So(err, ShouldBeNil)
  123. Convey("Should add labels to metric name", func() {
  124. So(len(res.Series), ShouldEqual, 3)
  125. So(res.Series[0].Name, ShouldEqual, "compute.googleapis.com/instance/cpu/usage_time collector-asia-east-1")
  126. So(res.Series[1].Name, ShouldEqual, "compute.googleapis.com/instance/cpu/usage_time collector-europe-west-1")
  127. So(res.Series[2].Name, ShouldEqual, "compute.googleapis.com/instance/cpu/usage_time collector-us-east-1")
  128. So(len(res.Series[0].Points), ShouldEqual, 3)
  129. So(res.Series[0].Points[0][0].Float64, ShouldEqual, 9.8566497180145)
  130. So(res.Series[0].Points[1][0].Float64, ShouldEqual, 9.7323568146676)
  131. So(res.Series[0].Points[2][0].Float64, ShouldEqual, 9.7730520330369)
  132. })
  133. Convey("Should add meta for labels to the response", func() {
  134. metricLabels := res.Meta.Get("metricLabels").Interface().(map[string][]string)
  135. So(metricLabels, ShouldNotBeNil)
  136. So(len(metricLabels["instance_name"]), ShouldEqual, 3)
  137. So(metricLabels["instance_name"][0], ShouldEqual, "collector-asia-east-1")
  138. So(metricLabels["instance_name"][1], ShouldEqual, "collector-europe-west-1")
  139. So(metricLabels["instance_name"][2], ShouldEqual, "collector-us-east-1")
  140. resourceLabels := res.Meta.Get("resourceLabels").Interface().(map[string][]string)
  141. So(resourceLabels, ShouldNotBeNil)
  142. So(len(resourceLabels["zone"]), ShouldEqual, 3)
  143. So(resourceLabels["zone"][0], ShouldEqual, "asia-east1-a")
  144. So(resourceLabels["zone"][1], ShouldEqual, "europe-west1-b")
  145. So(resourceLabels["zone"][2], ShouldEqual, "us-east1-b")
  146. So(len(resourceLabels["project_id"]), ShouldEqual, 1)
  147. So(resourceLabels["project_id"][0], ShouldEqual, "grafana-prod")
  148. })
  149. })
  150. })
  151. })
  152. }