stackdriver_test.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 filters", func() {
  47. tsdbQuery.Queries[0].Model = simplejson.NewFromAny(map[string]interface{}{
  48. "target": "target",
  49. "metricType": "a/metric/type",
  50. "filters": []interface{}{"key", "=", "value", "AND", "key2", "=", "value2"},
  51. })
  52. queries, err := executor.buildQueries(tsdbQuery)
  53. So(err, ShouldBeNil)
  54. So(len(queries), ShouldEqual, 1)
  55. So(queries[0].Params["filter"][0], ShouldEqual, `metric.type="a/metric/type" key="value" key2="value2"`)
  56. })
  57. Convey("and query has aggregation mean set", func() {
  58. tsdbQuery.Queries[0].Model = simplejson.NewFromAny(map[string]interface{}{
  59. "target": "target",
  60. "metricType": "a/metric/type",
  61. "primaryAggregation": "REDUCE_MEAN",
  62. "view": "FULL",
  63. })
  64. queries, err := executor.buildQueries(tsdbQuery)
  65. So(err, ShouldBeNil)
  66. So(len(queries), ShouldEqual, 1)
  67. So(queries[0].RefID, ShouldEqual, "A")
  68. So(queries[0].Target, ShouldEqual, "target")
  69. So(len(queries[0].Params), ShouldEqual, 7)
  70. So(queries[0].Params["interval.startTime"][0], ShouldEqual, "2018-03-15T13:00:00Z")
  71. So(queries[0].Params["interval.endTime"][0], ShouldEqual, "2018-03-15T13:34:00Z")
  72. So(queries[0].Params["aggregation.crossSeriesReducer"][0], ShouldEqual, "REDUCE_MEAN")
  73. So(queries[0].Params["aggregation.perSeriesAligner"][0], ShouldEqual, "ALIGN_MEAN")
  74. So(queries[0].Params["aggregation.alignmentPeriod"][0], ShouldEqual, "+60s")
  75. So(queries[0].Params["filter"][0], ShouldEqual, "metric.type=\"a/metric/type\"")
  76. So(queries[0].Params["view"][0], ShouldEqual, "FULL")
  77. })
  78. Convey("and query has group bys", func() {
  79. tsdbQuery.Queries[0].Model = simplejson.NewFromAny(map[string]interface{}{
  80. "target": "target",
  81. "metricType": "a/metric/type",
  82. "primaryAggregation": "REDUCE_NONE",
  83. "groupBys": []interface{}{"metric.label.group1", "metric.label.group2"},
  84. "view": "FULL",
  85. })
  86. queries, err := executor.buildQueries(tsdbQuery)
  87. So(err, ShouldBeNil)
  88. So(len(queries), ShouldEqual, 1)
  89. So(queries[0].RefID, ShouldEqual, "A")
  90. So(queries[0].Target, ShouldEqual, "target")
  91. So(len(queries[0].Params), ShouldEqual, 6)
  92. So(queries[0].Params["interval.startTime"][0], ShouldEqual, "2018-03-15T13:00:00Z")
  93. So(queries[0].Params["interval.endTime"][0], ShouldEqual, "2018-03-15T13:34:00Z")
  94. So(queries[0].Params["aggregation.perSeriesAligner"][0], ShouldEqual, "ALIGN_NONE")
  95. So(queries[0].Params["aggregation.groupByFields"][0], ShouldEqual, "metric.label.group1")
  96. So(queries[0].Params["aggregation.groupByFields"][1], ShouldEqual, "metric.label.group2")
  97. So(queries[0].Params["filter"][0], ShouldEqual, "metric.type=\"a/metric/type\"")
  98. So(queries[0].Params["view"][0], ShouldEqual, "FULL")
  99. })
  100. })
  101. Convey("Parse stackdriver response in the time series format", func() {
  102. Convey("when data from query aggregated to one time series", func() {
  103. var data StackDriverResponse
  104. jsonBody, err := ioutil.ReadFile("./test-data/1-series-response-agg-one-metric.json")
  105. So(err, ShouldBeNil)
  106. err = json.Unmarshal(jsonBody, &data)
  107. So(err, ShouldBeNil)
  108. So(len(data.TimeSeries), ShouldEqual, 1)
  109. res := &tsdb.QueryResult{Meta: simplejson.New(), RefId: "A"}
  110. err = executor.parseResponse(res, data)
  111. So(err, ShouldBeNil)
  112. So(len(res.Series), ShouldEqual, 1)
  113. So(res.Series[0].Name, ShouldEqual, "serviceruntime.googleapis.com/api/request_count")
  114. So(len(res.Series[0].Points), ShouldEqual, 3)
  115. Convey("timestamps should be in ascending order", func() {
  116. So(res.Series[0].Points[0][0].Float64, ShouldEqual, 0.05)
  117. So(res.Series[0].Points[0][1].Float64, ShouldEqual, 1536670020000)
  118. So(res.Series[0].Points[1][0].Float64, ShouldEqual, 1.05)
  119. So(res.Series[0].Points[1][1].Float64, ShouldEqual, 1536670080000)
  120. So(res.Series[0].Points[2][0].Float64, ShouldEqual, 1.0666666666667)
  121. So(res.Series[0].Points[2][1].Float64, ShouldEqual, 1536670260000)
  122. })
  123. })
  124. Convey("when data from query with no aggregation", func() {
  125. var data StackDriverResponse
  126. jsonBody, err := ioutil.ReadFile("./test-data/2-series-response-no-agg.json")
  127. So(err, ShouldBeNil)
  128. err = json.Unmarshal(jsonBody, &data)
  129. So(err, ShouldBeNil)
  130. So(len(data.TimeSeries), ShouldEqual, 3)
  131. res := &tsdb.QueryResult{Meta: simplejson.New(), RefId: "A"}
  132. err = executor.parseResponse(res, data)
  133. So(err, ShouldBeNil)
  134. Convey("Should add labels to metric name", func() {
  135. So(len(res.Series), ShouldEqual, 3)
  136. So(res.Series[0].Name, ShouldEqual, "compute.googleapis.com/instance/cpu/usage_time collector-asia-east-1")
  137. So(res.Series[1].Name, ShouldEqual, "compute.googleapis.com/instance/cpu/usage_time collector-europe-west-1")
  138. So(res.Series[2].Name, ShouldEqual, "compute.googleapis.com/instance/cpu/usage_time collector-us-east-1")
  139. So(len(res.Series[0].Points), ShouldEqual, 3)
  140. So(res.Series[0].Points[0][0].Float64, ShouldEqual, 9.8566497180145)
  141. So(res.Series[0].Points[1][0].Float64, ShouldEqual, 9.7323568146676)
  142. So(res.Series[0].Points[2][0].Float64, ShouldEqual, 9.7730520330369)
  143. })
  144. Convey("Should add meta for labels to the response", func() {
  145. metricLabels := res.Meta.Get("metricLabels").Interface().(map[string][]string)
  146. So(metricLabels, ShouldNotBeNil)
  147. So(len(metricLabels["instance_name"]), ShouldEqual, 3)
  148. So(metricLabels["instance_name"][0], ShouldEqual, "collector-asia-east-1")
  149. So(metricLabels["instance_name"][1], ShouldEqual, "collector-europe-west-1")
  150. So(metricLabels["instance_name"][2], ShouldEqual, "collector-us-east-1")
  151. resourceLabels := res.Meta.Get("resourceLabels").Interface().(map[string][]string)
  152. So(resourceLabels, ShouldNotBeNil)
  153. So(len(resourceLabels["zone"]), ShouldEqual, 3)
  154. So(resourceLabels["zone"][0], ShouldEqual, "asia-east1-a")
  155. So(resourceLabels["zone"][1], ShouldEqual, "europe-west1-b")
  156. So(resourceLabels["zone"][2], ShouldEqual, "us-east1-b")
  157. So(len(resourceLabels["project_id"]), ShouldEqual, 1)
  158. So(resourceLabels["project_id"][0], ShouldEqual, "grafana-prod")
  159. })
  160. })
  161. })
  162. })
  163. }