stackdriver_test.go 8.8 KB

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