stackdriver_test.go 13 KB

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