stackdriver_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 query from frontend", 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. }),
  28. RefId: "A",
  29. },
  30. },
  31. }
  32. queries, err := executor.parseQueries(tsdbQuery)
  33. So(err, ShouldBeNil)
  34. So(len(queries), ShouldEqual, 1)
  35. So(queries[0].RefID, ShouldEqual, "A")
  36. So(queries[0].Target, ShouldEqual, "target")
  37. So(len(queries[0].Params), ShouldEqual, 4)
  38. So(queries[0].Params["interval.startTime"][0], ShouldEqual, "2018-03-15T13:00:00Z")
  39. So(queries[0].Params["interval.endTime"][0], ShouldEqual, "2018-03-15T13:34:00Z")
  40. So(queries[0].Params["aggregation.perSeriesAligner"][0], ShouldEqual, "ALIGN_NONE")
  41. So(queries[0].Params["filter"][0], ShouldEqual, "a/metric/type")
  42. })
  43. Convey("Parse stackdriver response in the time series format", func() {
  44. Convey("when data from query aggregated to one time series", func() {
  45. var data StackDriverResponse
  46. jsonBody, err := ioutil.ReadFile("./test-data/1-series-response-agg-one-metric.json")
  47. So(err, ShouldBeNil)
  48. err = json.Unmarshal(jsonBody, &data)
  49. So(err, ShouldBeNil)
  50. So(len(data.TimeSeries), ShouldEqual, 1)
  51. res := &tsdb.QueryResult{Meta: simplejson.New(), RefId: "A"}
  52. err = executor.parseResponse(res, data)
  53. So(err, ShouldBeNil)
  54. So(len(res.Series), ShouldEqual, 1)
  55. So(res.Series[0].Name, ShouldEqual, "serviceruntime.googleapis.com/api/request_count")
  56. So(len(res.Series[0].Points), ShouldEqual, 3)
  57. So(res.Series[0].Points[0][0].Float64, ShouldEqual, 1.0666666666667)
  58. So(res.Series[0].Points[1][0].Float64, ShouldEqual, 1.05)
  59. So(res.Series[0].Points[2][0].Float64, ShouldEqual, 0.05)
  60. })
  61. Convey("when data from query with no aggregation", func() {
  62. var data StackDriverResponse
  63. jsonBody, err := ioutil.ReadFile("./test-data/2-series-response-no-agg.json")
  64. So(err, ShouldBeNil)
  65. err = json.Unmarshal(jsonBody, &data)
  66. So(err, ShouldBeNil)
  67. So(len(data.TimeSeries), ShouldEqual, 3)
  68. res := &tsdb.QueryResult{Meta: simplejson.New(), RefId: "A"}
  69. err = executor.parseResponse(res, data)
  70. So(err, ShouldBeNil)
  71. Convey("Should add labels to metric name", func() {
  72. So(len(res.Series), ShouldEqual, 3)
  73. So(res.Series[0].Name, ShouldEqual, "compute.googleapis.com/instance/cpu/usage_time collector-asia-east-1")
  74. So(res.Series[1].Name, ShouldEqual, "compute.googleapis.com/instance/cpu/usage_time collector-europe-west-1")
  75. So(res.Series[2].Name, ShouldEqual, "compute.googleapis.com/instance/cpu/usage_time collector-us-east-1")
  76. So(len(res.Series[0].Points), ShouldEqual, 3)
  77. So(res.Series[0].Points[0][0].Float64, ShouldEqual, 9.7730520330369)
  78. So(res.Series[0].Points[1][0].Float64, ShouldEqual, 9.7323568146676)
  79. So(res.Series[0].Points[2][0].Float64, ShouldEqual, 9.8566497180145)
  80. })
  81. })
  82. })
  83. })
  84. }