stackdriver_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. "aliasBy": "testalias",
  29. "type": "timeSeriesQuery",
  30. }),
  31. RefId: "A",
  32. },
  33. },
  34. }
  35. Convey("and query has no aggregation set", func() {
  36. queries, err := executor.buildQueries(tsdbQuery)
  37. So(err, ShouldBeNil)
  38. So(len(queries), ShouldEqual, 1)
  39. So(queries[0].RefID, ShouldEqual, "A")
  40. So(queries[0].Target, ShouldEqual, "target")
  41. So(len(queries[0].Params), ShouldEqual, 7)
  42. So(queries[0].Params["interval.startTime"][0], ShouldEqual, "2018-03-15T13:00:00Z")
  43. So(queries[0].Params["interval.endTime"][0], ShouldEqual, "2018-03-15T13:34:00Z")
  44. So(queries[0].Params["aggregation.perSeriesAligner"][0], ShouldEqual, "ALIGN_MEAN")
  45. So(queries[0].Params["filter"][0], ShouldEqual, "metric.type=\"a/metric/type\"")
  46. So(queries[0].Params["view"][0], ShouldEqual, "FULL")
  47. So(queries[0].AliasBy, ShouldEqual, "testalias")
  48. })
  49. Convey("and query has filters", func() {
  50. tsdbQuery.Queries[0].Model = simplejson.NewFromAny(map[string]interface{}{
  51. "target": "target",
  52. "metricType": "a/metric/type",
  53. "filters": []interface{}{"key", "=", "value", "AND", "key2", "=", "value2"},
  54. })
  55. queries, err := executor.buildQueries(tsdbQuery)
  56. So(err, ShouldBeNil)
  57. So(len(queries), ShouldEqual, 1)
  58. So(queries[0].Params["filter"][0], ShouldEqual, `metric.type="a/metric/type" key="value" key2="value2"`)
  59. })
  60. Convey("and alignmentPeriod is set to grafana-auto", func() {
  61. Convey("and IntervalMs is larger than 60000", func() {
  62. tsdbQuery.Queries[0].IntervalMs = 1000000
  63. tsdbQuery.Queries[0].Model = simplejson.NewFromAny(map[string]interface{}{
  64. "target": "target",
  65. "alignmentPeriod": "grafana-auto",
  66. "filters": []interface{}{"key", "=", "value", "AND", "key2", "=", "value2"},
  67. })
  68. queries, err := executor.buildQueries(tsdbQuery)
  69. So(err, ShouldBeNil)
  70. So(queries[0].Params["aggregation.alignmentPeriod"][0], ShouldEqual, `+1000s`)
  71. })
  72. Convey("and IntervalMs is less than 60000", func() {
  73. tsdbQuery.Queries[0].IntervalMs = 30000
  74. tsdbQuery.Queries[0].Model = simplejson.NewFromAny(map[string]interface{}{
  75. "target": "target",
  76. "alignmentPeriod": "grafana-auto",
  77. "filters": []interface{}{"key", "=", "value", "AND", "key2", "=", "value2"},
  78. })
  79. queries, err := executor.buildQueries(tsdbQuery)
  80. So(err, ShouldBeNil)
  81. So(queries[0].Params["aggregation.alignmentPeriod"][0], ShouldEqual, `+60s`)
  82. })
  83. })
  84. Convey("and alignmentPeriod is set in frontend", func() {
  85. Convey("and alignment period is too big", func() {
  86. tsdbQuery.Queries[0].IntervalMs = 1000
  87. tsdbQuery.Queries[0].Model = simplejson.NewFromAny(map[string]interface{}{
  88. "alignmentPeriod": "+360000s",
  89. })
  90. queries, err := executor.buildQueries(tsdbQuery)
  91. So(err, ShouldBeNil)
  92. So(queries[0].Params["aggregation.alignmentPeriod"][0], ShouldEqual, `+3600s`)
  93. })
  94. Convey("and alignment period is within accepted range", func() {
  95. tsdbQuery.Queries[0].IntervalMs = 1000
  96. tsdbQuery.Queries[0].Model = simplejson.NewFromAny(map[string]interface{}{
  97. "alignmentPeriod": "+600s",
  98. })
  99. queries, err := executor.buildQueries(tsdbQuery)
  100. So(err, ShouldBeNil)
  101. So(queries[0].Params["aggregation.alignmentPeriod"][0], ShouldEqual, `+600s`)
  102. })
  103. })
  104. Convey("and query has aggregation mean set", func() {
  105. tsdbQuery.Queries[0].Model = simplejson.NewFromAny(map[string]interface{}{
  106. "target": "target",
  107. "metricType": "a/metric/type",
  108. "primaryAggregation": "REDUCE_MEAN",
  109. "view": "FULL",
  110. })
  111. queries, err := executor.buildQueries(tsdbQuery)
  112. So(err, ShouldBeNil)
  113. So(len(queries), ShouldEqual, 1)
  114. So(queries[0].RefID, ShouldEqual, "A")
  115. So(queries[0].Target, ShouldEqual, "target")
  116. So(len(queries[0].Params), ShouldEqual, 7)
  117. So(queries[0].Params["interval.startTime"][0], ShouldEqual, "2018-03-15T13:00:00Z")
  118. So(queries[0].Params["interval.endTime"][0], ShouldEqual, "2018-03-15T13:34:00Z")
  119. So(queries[0].Params["aggregation.crossSeriesReducer"][0], ShouldEqual, "REDUCE_MEAN")
  120. So(queries[0].Params["aggregation.perSeriesAligner"][0], ShouldEqual, "ALIGN_MEAN")
  121. So(queries[0].Params["aggregation.alignmentPeriod"][0], ShouldEqual, "+60s")
  122. So(queries[0].Params["filter"][0], ShouldEqual, "metric.type=\"a/metric/type\"")
  123. So(queries[0].Params["view"][0], ShouldEqual, "FULL")
  124. })
  125. Convey("and query has group bys", func() {
  126. tsdbQuery.Queries[0].Model = simplejson.NewFromAny(map[string]interface{}{
  127. "target": "target",
  128. "metricType": "a/metric/type",
  129. "primaryAggregation": "REDUCE_NONE",
  130. "groupBys": []interface{}{"metric.label.group1", "metric.label.group2"},
  131. "view": "FULL",
  132. })
  133. queries, err := executor.buildQueries(tsdbQuery)
  134. So(err, ShouldBeNil)
  135. So(len(queries), ShouldEqual, 1)
  136. So(queries[0].RefID, ShouldEqual, "A")
  137. So(queries[0].Target, ShouldEqual, "target")
  138. So(len(queries[0].Params), ShouldEqual, 8)
  139. So(queries[0].Params["interval.startTime"][0], ShouldEqual, "2018-03-15T13:00:00Z")
  140. So(queries[0].Params["interval.endTime"][0], ShouldEqual, "2018-03-15T13:34:00Z")
  141. So(queries[0].Params["aggregation.perSeriesAligner"][0], ShouldEqual, "ALIGN_MEAN")
  142. So(queries[0].Params["aggregation.groupByFields"][0], ShouldEqual, "metric.label.group1")
  143. So(queries[0].Params["aggregation.groupByFields"][1], ShouldEqual, "metric.label.group2")
  144. So(queries[0].Params["filter"][0], ShouldEqual, "metric.type=\"a/metric/type\"")
  145. So(queries[0].Params["view"][0], ShouldEqual, "FULL")
  146. })
  147. })
  148. Convey("Parse stackdriver response in the time series format", func() {
  149. Convey("when data from query aggregated to one time series", func() {
  150. data, err := loadTestFile("./test-data/1-series-response-agg-one-metric.json")
  151. So(err, ShouldBeNil)
  152. So(len(data.TimeSeries), ShouldEqual, 1)
  153. res := &tsdb.QueryResult{Meta: simplejson.New(), RefId: "A"}
  154. query := &StackdriverQuery{}
  155. err = executor.parseResponse(res, data, query)
  156. So(err, ShouldBeNil)
  157. So(len(res.Series), ShouldEqual, 1)
  158. So(res.Series[0].Name, ShouldEqual, "serviceruntime.googleapis.com/api/request_count")
  159. So(len(res.Series[0].Points), ShouldEqual, 3)
  160. Convey("timestamps should be in ascending order", func() {
  161. So(res.Series[0].Points[0][0].Float64, ShouldEqual, 0.05)
  162. So(res.Series[0].Points[0][1].Float64, ShouldEqual, 1536670020000)
  163. So(res.Series[0].Points[1][0].Float64, ShouldEqual, 1.05)
  164. So(res.Series[0].Points[1][1].Float64, ShouldEqual, 1536670080000)
  165. So(res.Series[0].Points[2][0].Float64, ShouldEqual, 1.0666666666667)
  166. So(res.Series[0].Points[2][1].Float64, ShouldEqual, 1536670260000)
  167. })
  168. })
  169. Convey("when data from query with no aggregation", func() {
  170. data, err := loadTestFile("./test-data/2-series-response-no-agg.json")
  171. So(err, ShouldBeNil)
  172. So(len(data.TimeSeries), ShouldEqual, 3)
  173. res := &tsdb.QueryResult{Meta: simplejson.New(), RefId: "A"}
  174. query := &StackdriverQuery{}
  175. err = executor.parseResponse(res, data, query)
  176. So(err, ShouldBeNil)
  177. Convey("Should add labels to metric name", func() {
  178. So(len(res.Series), ShouldEqual, 3)
  179. So(res.Series[0].Name, ShouldEqual, "compute.googleapis.com/instance/cpu/usage_time collector-asia-east-1")
  180. So(res.Series[1].Name, ShouldEqual, "compute.googleapis.com/instance/cpu/usage_time collector-europe-west-1")
  181. So(res.Series[2].Name, ShouldEqual, "compute.googleapis.com/instance/cpu/usage_time collector-us-east-1")
  182. })
  183. Convey("Should parse to time series", func() {
  184. So(len(res.Series[0].Points), ShouldEqual, 3)
  185. So(res.Series[0].Points[0][0].Float64, ShouldEqual, 9.8566497180145)
  186. So(res.Series[0].Points[1][0].Float64, ShouldEqual, 9.7323568146676)
  187. So(res.Series[0].Points[2][0].Float64, ShouldEqual, 9.7730520330369)
  188. })
  189. Convey("Should add meta for labels to the response", func() {
  190. metricLabels := res.Meta.Get("metricLabels").Interface().(map[string][]string)
  191. So(metricLabels, ShouldNotBeNil)
  192. So(len(metricLabels["instance_name"]), ShouldEqual, 3)
  193. So(metricLabels["instance_name"][0], ShouldEqual, "collector-asia-east-1")
  194. So(metricLabels["instance_name"][1], ShouldEqual, "collector-europe-west-1")
  195. So(metricLabels["instance_name"][2], ShouldEqual, "collector-us-east-1")
  196. resourceLabels := res.Meta.Get("resourceLabels").Interface().(map[string][]string)
  197. So(resourceLabels, ShouldNotBeNil)
  198. So(len(resourceLabels["zone"]), ShouldEqual, 3)
  199. So(resourceLabels["zone"][0], ShouldEqual, "asia-east1-a")
  200. So(resourceLabels["zone"][1], ShouldEqual, "europe-west1-b")
  201. So(resourceLabels["zone"][2], ShouldEqual, "us-east1-b")
  202. So(len(resourceLabels["project_id"]), ShouldEqual, 1)
  203. So(resourceLabels["project_id"][0], ShouldEqual, "grafana-prod")
  204. })
  205. })
  206. Convey("when data from query with no aggregation and group bys", func() {
  207. data, err := loadTestFile("./test-data/2-series-response-no-agg.json")
  208. So(err, ShouldBeNil)
  209. So(len(data.TimeSeries), ShouldEqual, 3)
  210. res := &tsdb.QueryResult{Meta: simplejson.New(), RefId: "A"}
  211. query := &StackdriverQuery{GroupBys: []string{"metric.label.instance_name", "resource.label.zone"}}
  212. err = executor.parseResponse(res, data, query)
  213. So(err, ShouldBeNil)
  214. Convey("Should add instance name and zone labels to metric name", func() {
  215. So(len(res.Series), ShouldEqual, 3)
  216. So(res.Series[0].Name, ShouldEqual, "compute.googleapis.com/instance/cpu/usage_time collector-asia-east-1 asia-east1-a")
  217. So(res.Series[1].Name, ShouldEqual, "compute.googleapis.com/instance/cpu/usage_time collector-europe-west-1 europe-west1-b")
  218. So(res.Series[2].Name, ShouldEqual, "compute.googleapis.com/instance/cpu/usage_time collector-us-east-1 us-east1-b")
  219. })
  220. })
  221. Convey("when data from query with no aggregation and alias by", func() {
  222. data, err := loadTestFile("./test-data/2-series-response-no-agg.json")
  223. So(err, ShouldBeNil)
  224. So(len(data.TimeSeries), ShouldEqual, 3)
  225. res := &tsdb.QueryResult{Meta: simplejson.New(), RefId: "A"}
  226. Convey("and the alias pattern is for metric type, a metric label and a resource label", func() {
  227. query := &StackdriverQuery{AliasBy: "{{metric.type}} - {{metric.label.instance_name}} - {{resource.label.zone}}", GroupBys: []string{"metric.label.instance_name", "resource.label.zone"}}
  228. err = executor.parseResponse(res, data, query)
  229. So(err, ShouldBeNil)
  230. Convey("Should use alias by formatting and only show instance name", func() {
  231. So(len(res.Series), ShouldEqual, 3)
  232. So(res.Series[0].Name, ShouldEqual, "compute.googleapis.com/instance/cpu/usage_time - collector-asia-east-1 - asia-east1-a")
  233. So(res.Series[1].Name, ShouldEqual, "compute.googleapis.com/instance/cpu/usage_time - collector-europe-west-1 - europe-west1-b")
  234. So(res.Series[2].Name, ShouldEqual, "compute.googleapis.com/instance/cpu/usage_time - collector-us-east-1 - us-east1-b")
  235. })
  236. })
  237. Convey("and the alias pattern is for metric name", func() {
  238. query := &StackdriverQuery{AliasBy: "metric {{metric.name}} service {{metric.service}} category {{metric.category}}", GroupBys: []string{"metric.label.instance_name", "resource.label.zone"}}
  239. err = executor.parseResponse(res, data, query)
  240. So(err, ShouldBeNil)
  241. Convey("Should use alias by formatting and only show instance name", func() {
  242. So(len(res.Series), ShouldEqual, 3)
  243. So(res.Series[0].Name, ShouldEqual, "metric cpu/usage_time service compute category instance")
  244. So(res.Series[1].Name, ShouldEqual, "metric cpu/usage_time service compute category instance")
  245. So(res.Series[2].Name, ShouldEqual, "metric cpu/usage_time service compute category instance")
  246. })
  247. })
  248. })
  249. })
  250. })
  251. }
  252. func loadTestFile(path string) (StackdriverResponse, error) {
  253. var data StackdriverResponse
  254. jsonBody, err := ioutil.ReadFile(path)
  255. if err != nil {
  256. return data, err
  257. }
  258. err = json.Unmarshal(jsonBody, &data)
  259. if err != nil {
  260. return data, err
  261. }
  262. return data, nil
  263. }