stackdriver_test.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. package stackdriver
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "math"
  7. "strconv"
  8. "testing"
  9. "time"
  10. "github.com/grafana/grafana/pkg/components/simplejson"
  11. "github.com/grafana/grafana/pkg/tsdb"
  12. . "github.com/smartystreets/goconvey/convey"
  13. )
  14. func TestStackdriver(t *testing.T) {
  15. Convey("Stackdriver", t, func() {
  16. executor := &StackdriverExecutor{}
  17. Convey("Parse queries from frontend and build Stackdriver API queries", func() {
  18. fromStart := time.Date(2018, 3, 15, 13, 0, 0, 0, time.UTC).In(time.Local)
  19. tsdbQuery := &tsdb.TsdbQuery{
  20. TimeRange: &tsdb.TimeRange{
  21. From: fmt.Sprintf("%v", fromStart.Unix()*1000),
  22. To: fmt.Sprintf("%v", fromStart.Add(34*time.Minute).Unix()*1000),
  23. },
  24. Queries: []*tsdb.Query{
  25. {
  26. Model: simplejson.NewFromAny(map[string]interface{}{
  27. "metricType": "a/metric/type",
  28. "view": "FULL",
  29. "aliasBy": "testalias",
  30. "type": "timeSeriesQuery",
  31. }),
  32. RefId: "A",
  33. },
  34. },
  35. }
  36. Convey("and query has no aggregation set", func() {
  37. queries, err := executor.buildQueries(tsdbQuery)
  38. So(err, ShouldBeNil)
  39. So(len(queries), ShouldEqual, 1)
  40. So(queries[0].RefID, ShouldEqual, "A")
  41. So(queries[0].Target, ShouldEqual, "aggregation.alignmentPeriod=%2B60s&aggregation.crossSeriesReducer=REDUCE_NONE&aggregation.perSeriesAligner=ALIGN_MEAN&filter=metric.type%3D%22a%2Fmetric%2Ftype%22&interval.endTime=2018-03-15T13%3A34%3A00Z&interval.startTime=2018-03-15T13%3A00%3A00Z&view=FULL")
  42. So(len(queries[0].Params), ShouldEqual, 7)
  43. So(queries[0].Params["interval.startTime"][0], ShouldEqual, "2018-03-15T13:00:00Z")
  44. So(queries[0].Params["interval.endTime"][0], ShouldEqual, "2018-03-15T13:34:00Z")
  45. So(queries[0].Params["aggregation.perSeriesAligner"][0], ShouldEqual, "ALIGN_MEAN")
  46. So(queries[0].Params["filter"][0], ShouldEqual, "metric.type=\"a/metric/type\"")
  47. So(queries[0].Params["view"][0], ShouldEqual, "FULL")
  48. So(queries[0].AliasBy, ShouldEqual, "testalias")
  49. })
  50. Convey("and query has filters", func() {
  51. tsdbQuery.Queries[0].Model = simplejson.NewFromAny(map[string]interface{}{
  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. "alignmentPeriod": "grafana-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 60000", func() {
  72. tsdbQuery.Queries[0].IntervalMs = 30000
  73. tsdbQuery.Queries[0].Model = simplejson.NewFromAny(map[string]interface{}{
  74. "alignmentPeriod": "grafana-auto",
  75. "filters": []interface{}{"key", "=", "value", "AND", "key2", "=", "value2"},
  76. })
  77. queries, err := executor.buildQueries(tsdbQuery)
  78. So(err, ShouldBeNil)
  79. So(queries[0].Params["aggregation.alignmentPeriod"][0], ShouldEqual, `+60s`)
  80. })
  81. })
  82. Convey("and alignmentPeriod is set to stackdriver-auto", func() {
  83. Convey("and range is two hours", func() {
  84. tsdbQuery.TimeRange.From = "1538033322461"
  85. tsdbQuery.TimeRange.To = "1538040522461"
  86. tsdbQuery.Queries[0].Model = simplejson.NewFromAny(map[string]interface{}{
  87. "target": "target",
  88. "alignmentPeriod": "stackdriver-auto",
  89. })
  90. queries, err := executor.buildQueries(tsdbQuery)
  91. So(err, ShouldBeNil)
  92. So(queries[0].Params["aggregation.alignmentPeriod"][0], ShouldEqual, `+60s`)
  93. })
  94. Convey("and range is 22 hours", func() {
  95. tsdbQuery.TimeRange.From = "1538034524922"
  96. tsdbQuery.TimeRange.To = "1538113724922"
  97. tsdbQuery.Queries[0].Model = simplejson.NewFromAny(map[string]interface{}{
  98. "target": "target",
  99. "alignmentPeriod": "stackdriver-auto",
  100. })
  101. queries, err := executor.buildQueries(tsdbQuery)
  102. So(err, ShouldBeNil)
  103. So(queries[0].Params["aggregation.alignmentPeriod"][0], ShouldEqual, `+60s`)
  104. })
  105. Convey("and range is 23 hours", func() {
  106. tsdbQuery.TimeRange.From = "1538034567985"
  107. tsdbQuery.TimeRange.To = "1538117367985"
  108. tsdbQuery.Queries[0].Model = simplejson.NewFromAny(map[string]interface{}{
  109. "target": "target",
  110. "alignmentPeriod": "stackdriver-auto",
  111. })
  112. queries, err := executor.buildQueries(tsdbQuery)
  113. So(err, ShouldBeNil)
  114. So(queries[0].Params["aggregation.alignmentPeriod"][0], ShouldEqual, `+300s`)
  115. })
  116. Convey("and range is 7 days", func() {
  117. tsdbQuery.TimeRange.From = "1538036324073"
  118. tsdbQuery.TimeRange.To = "1538641124073"
  119. tsdbQuery.Queries[0].Model = simplejson.NewFromAny(map[string]interface{}{
  120. "target": "target",
  121. "alignmentPeriod": "stackdriver-auto",
  122. })
  123. queries, err := executor.buildQueries(tsdbQuery)
  124. So(err, ShouldBeNil)
  125. So(queries[0].Params["aggregation.alignmentPeriod"][0], ShouldEqual, `+3600s`)
  126. })
  127. })
  128. Convey("and alignmentPeriod is set in frontend", func() {
  129. Convey("and alignment period is too big", func() {
  130. tsdbQuery.Queries[0].IntervalMs = 1000
  131. tsdbQuery.Queries[0].Model = simplejson.NewFromAny(map[string]interface{}{
  132. "alignmentPeriod": "+360000s",
  133. })
  134. queries, err := executor.buildQueries(tsdbQuery)
  135. So(err, ShouldBeNil)
  136. So(queries[0].Params["aggregation.alignmentPeriod"][0], ShouldEqual, `+3600s`)
  137. })
  138. Convey("and alignment period is within accepted range", func() {
  139. tsdbQuery.Queries[0].IntervalMs = 1000
  140. tsdbQuery.Queries[0].Model = simplejson.NewFromAny(map[string]interface{}{
  141. "alignmentPeriod": "+600s",
  142. })
  143. queries, err := executor.buildQueries(tsdbQuery)
  144. So(err, ShouldBeNil)
  145. So(queries[0].Params["aggregation.alignmentPeriod"][0], ShouldEqual, `+600s`)
  146. })
  147. })
  148. Convey("and query has aggregation mean set", func() {
  149. tsdbQuery.Queries[0].Model = simplejson.NewFromAny(map[string]interface{}{
  150. "metricType": "a/metric/type",
  151. "crossSeriesReducer": "REDUCE_SUM",
  152. "view": "FULL",
  153. })
  154. queries, err := executor.buildQueries(tsdbQuery)
  155. So(err, ShouldBeNil)
  156. So(len(queries), ShouldEqual, 1)
  157. So(queries[0].RefID, ShouldEqual, "A")
  158. So(queries[0].Target, ShouldEqual, "aggregation.alignmentPeriod=%2B60s&aggregation.crossSeriesReducer=REDUCE_SUM&aggregation.perSeriesAligner=ALIGN_MEAN&filter=metric.type%3D%22a%2Fmetric%2Ftype%22&interval.endTime=2018-03-15T13%3A34%3A00Z&interval.startTime=2018-03-15T13%3A00%3A00Z&view=FULL")
  159. So(len(queries[0].Params), ShouldEqual, 7)
  160. So(queries[0].Params["interval.startTime"][0], ShouldEqual, "2018-03-15T13:00:00Z")
  161. So(queries[0].Params["interval.endTime"][0], ShouldEqual, "2018-03-15T13:34:00Z")
  162. So(queries[0].Params["aggregation.crossSeriesReducer"][0], ShouldEqual, "REDUCE_SUM")
  163. So(queries[0].Params["aggregation.perSeriesAligner"][0], ShouldEqual, "ALIGN_MEAN")
  164. So(queries[0].Params["aggregation.alignmentPeriod"][0], ShouldEqual, "+60s")
  165. So(queries[0].Params["filter"][0], ShouldEqual, "metric.type=\"a/metric/type\"")
  166. So(queries[0].Params["view"][0], ShouldEqual, "FULL")
  167. })
  168. Convey("and query has group bys", func() {
  169. tsdbQuery.Queries[0].Model = simplejson.NewFromAny(map[string]interface{}{
  170. "metricType": "a/metric/type",
  171. "crossSeriesReducer": "REDUCE_NONE",
  172. "groupBys": []interface{}{"metric.label.group1", "metric.label.group2"},
  173. "view": "FULL",
  174. })
  175. queries, err := executor.buildQueries(tsdbQuery)
  176. So(err, ShouldBeNil)
  177. So(len(queries), ShouldEqual, 1)
  178. So(queries[0].RefID, ShouldEqual, "A")
  179. So(queries[0].Target, ShouldEqual, "aggregation.alignmentPeriod=%2B60s&aggregation.crossSeriesReducer=REDUCE_NONE&aggregation.groupByFields=metric.label.group1&aggregation.groupByFields=metric.label.group2&aggregation.perSeriesAligner=ALIGN_MEAN&filter=metric.type%3D%22a%2Fmetric%2Ftype%22&interval.endTime=2018-03-15T13%3A34%3A00Z&interval.startTime=2018-03-15T13%3A00%3A00Z&view=FULL")
  180. So(len(queries[0].Params), ShouldEqual, 8)
  181. So(queries[0].Params["interval.startTime"][0], ShouldEqual, "2018-03-15T13:00:00Z")
  182. So(queries[0].Params["interval.endTime"][0], ShouldEqual, "2018-03-15T13:34:00Z")
  183. So(queries[0].Params["aggregation.perSeriesAligner"][0], ShouldEqual, "ALIGN_MEAN")
  184. So(queries[0].Params["aggregation.groupByFields"][0], ShouldEqual, "metric.label.group1")
  185. So(queries[0].Params["aggregation.groupByFields"][1], ShouldEqual, "metric.label.group2")
  186. So(queries[0].Params["filter"][0], ShouldEqual, "metric.type=\"a/metric/type\"")
  187. So(queries[0].Params["view"][0], ShouldEqual, "FULL")
  188. })
  189. })
  190. Convey("Parse stackdriver response in the time series format", func() {
  191. Convey("when data from query aggregated to one time series", func() {
  192. data, err := loadTestFile("./test-data/1-series-response-agg-one-metric.json")
  193. So(err, ShouldBeNil)
  194. So(len(data.TimeSeries), ShouldEqual, 1)
  195. res := &tsdb.QueryResult{Meta: simplejson.New(), RefId: "A"}
  196. query := &StackdriverQuery{}
  197. err = executor.parseResponse(res, data, query)
  198. So(err, ShouldBeNil)
  199. So(len(res.Series), ShouldEqual, 1)
  200. So(res.Series[0].Name, ShouldEqual, "serviceruntime.googleapis.com/api/request_count")
  201. So(len(res.Series[0].Points), ShouldEqual, 3)
  202. Convey("timestamps should be in ascending order", func() {
  203. So(res.Series[0].Points[0][0].Float64, ShouldEqual, 0.05)
  204. So(res.Series[0].Points[0][1].Float64, ShouldEqual, int64(1536670020000))
  205. So(res.Series[0].Points[1][0].Float64, ShouldEqual, 1.05)
  206. So(res.Series[0].Points[1][1].Float64, ShouldEqual, int64(1536670080000))
  207. So(res.Series[0].Points[2][0].Float64, ShouldEqual, 1.0666666666667)
  208. So(res.Series[0].Points[2][1].Float64, ShouldEqual, int64(1536670260000))
  209. })
  210. })
  211. Convey("when data from query with no aggregation", func() {
  212. data, err := loadTestFile("./test-data/2-series-response-no-agg.json")
  213. So(err, ShouldBeNil)
  214. So(len(data.TimeSeries), ShouldEqual, 3)
  215. res := &tsdb.QueryResult{Meta: simplejson.New(), RefId: "A"}
  216. query := &StackdriverQuery{}
  217. err = executor.parseResponse(res, data, query)
  218. So(err, ShouldBeNil)
  219. Convey("Should add labels to metric name", func() {
  220. So(len(res.Series), ShouldEqual, 3)
  221. So(res.Series[0].Name, ShouldEqual, "compute.googleapis.com/instance/cpu/usage_time collector-asia-east-1")
  222. So(res.Series[1].Name, ShouldEqual, "compute.googleapis.com/instance/cpu/usage_time collector-europe-west-1")
  223. So(res.Series[2].Name, ShouldEqual, "compute.googleapis.com/instance/cpu/usage_time collector-us-east-1")
  224. })
  225. Convey("Should parse to time series", func() {
  226. So(len(res.Series[0].Points), ShouldEqual, 3)
  227. So(res.Series[0].Points[0][0].Float64, ShouldEqual, 9.8566497180145)
  228. So(res.Series[0].Points[1][0].Float64, ShouldEqual, 9.7323568146676)
  229. So(res.Series[0].Points[2][0].Float64, ShouldEqual, 9.7730520330369)
  230. })
  231. Convey("Should add meta for labels to the response", func() {
  232. metricLabels := res.Meta.Get("metricLabels").Interface().(map[string][]string)
  233. So(metricLabels, ShouldNotBeNil)
  234. So(len(metricLabels["instance_name"]), ShouldEqual, 3)
  235. So(metricLabels["instance_name"][0], ShouldEqual, "collector-asia-east-1")
  236. So(metricLabels["instance_name"][1], ShouldEqual, "collector-europe-west-1")
  237. So(metricLabels["instance_name"][2], ShouldEqual, "collector-us-east-1")
  238. resourceLabels := res.Meta.Get("resourceLabels").Interface().(map[string][]string)
  239. So(resourceLabels, ShouldNotBeNil)
  240. So(len(resourceLabels["zone"]), ShouldEqual, 3)
  241. So(resourceLabels["zone"][0], ShouldEqual, "asia-east1-a")
  242. So(resourceLabels["zone"][1], ShouldEqual, "europe-west1-b")
  243. So(resourceLabels["zone"][2], ShouldEqual, "us-east1-b")
  244. So(len(resourceLabels["project_id"]), ShouldEqual, 1)
  245. So(resourceLabels["project_id"][0], ShouldEqual, "grafana-prod")
  246. })
  247. })
  248. Convey("when data from query with no aggregation and group bys", func() {
  249. data, err := loadTestFile("./test-data/2-series-response-no-agg.json")
  250. So(err, ShouldBeNil)
  251. So(len(data.TimeSeries), ShouldEqual, 3)
  252. res := &tsdb.QueryResult{Meta: simplejson.New(), RefId: "A"}
  253. query := &StackdriverQuery{GroupBys: []string{"metric.label.instance_name", "resource.label.zone"}}
  254. err = executor.parseResponse(res, data, query)
  255. So(err, ShouldBeNil)
  256. Convey("Should add instance name and zone labels to metric name", func() {
  257. So(len(res.Series), ShouldEqual, 3)
  258. So(res.Series[0].Name, ShouldEqual, "compute.googleapis.com/instance/cpu/usage_time collector-asia-east-1 asia-east1-a")
  259. So(res.Series[1].Name, ShouldEqual, "compute.googleapis.com/instance/cpu/usage_time collector-europe-west-1 europe-west1-b")
  260. So(res.Series[2].Name, ShouldEqual, "compute.googleapis.com/instance/cpu/usage_time collector-us-east-1 us-east1-b")
  261. })
  262. })
  263. Convey("when data from query with no aggregation and alias by", func() {
  264. data, err := loadTestFile("./test-data/2-series-response-no-agg.json")
  265. So(err, ShouldBeNil)
  266. So(len(data.TimeSeries), ShouldEqual, 3)
  267. res := &tsdb.QueryResult{Meta: simplejson.New(), RefId: "A"}
  268. Convey("and the alias pattern is for metric type, a metric label and a resource label", func() {
  269. query := &StackdriverQuery{AliasBy: "{{metric.type}} - {{metric.label.instance_name}} - {{resource.label.zone}}", GroupBys: []string{"metric.label.instance_name", "resource.label.zone"}}
  270. err = executor.parseResponse(res, data, query)
  271. So(err, ShouldBeNil)
  272. Convey("Should use alias by formatting and only show instance name", func() {
  273. So(len(res.Series), ShouldEqual, 3)
  274. So(res.Series[0].Name, ShouldEqual, "compute.googleapis.com/instance/cpu/usage_time - collector-asia-east-1 - asia-east1-a")
  275. So(res.Series[1].Name, ShouldEqual, "compute.googleapis.com/instance/cpu/usage_time - collector-europe-west-1 - europe-west1-b")
  276. So(res.Series[2].Name, ShouldEqual, "compute.googleapis.com/instance/cpu/usage_time - collector-us-east-1 - us-east1-b")
  277. })
  278. })
  279. Convey("and the alias pattern is for metric name", func() {
  280. query := &StackdriverQuery{AliasBy: "metric {{metric.name}} service {{metric.service}}", GroupBys: []string{"metric.label.instance_name", "resource.label.zone"}}
  281. err = executor.parseResponse(res, data, query)
  282. So(err, ShouldBeNil)
  283. Convey("Should use alias by formatting and only show instance name", func() {
  284. So(len(res.Series), ShouldEqual, 3)
  285. So(res.Series[0].Name, ShouldEqual, "metric instance/cpu/usage_time service compute")
  286. So(res.Series[1].Name, ShouldEqual, "metric instance/cpu/usage_time service compute")
  287. So(res.Series[2].Name, ShouldEqual, "metric instance/cpu/usage_time service compute")
  288. })
  289. })
  290. })
  291. Convey("when data from query is distribution with exponential bounds", func() {
  292. data, err := loadTestFile("./test-data/3-series-response-distribution-exponential.json")
  293. So(err, ShouldBeNil)
  294. So(len(data.TimeSeries), ShouldEqual, 1)
  295. res := &tsdb.QueryResult{Meta: simplejson.New(), RefId: "A"}
  296. query := &StackdriverQuery{AliasBy: "{{bucket}}"}
  297. err = executor.parseResponse(res, data, query)
  298. So(err, ShouldBeNil)
  299. So(len(res.Series), ShouldEqual, 11)
  300. for i := 0; i < 11; i++ {
  301. if i == 0 {
  302. So(res.Series[i].Name, ShouldEqual, "0")
  303. } else {
  304. So(res.Series[i].Name, ShouldEqual, strconv.FormatInt(int64(math.Pow(float64(2), float64(i-1))), 10))
  305. }
  306. So(len(res.Series[i].Points), ShouldEqual, 3)
  307. }
  308. Convey("timestamps should be in ascending order", func() {
  309. So(res.Series[0].Points[0][1].Float64, ShouldEqual, int64(1536668940000))
  310. So(res.Series[0].Points[1][1].Float64, ShouldEqual, int64(1536669000000))
  311. So(res.Series[0].Points[2][1].Float64, ShouldEqual, int64(1536669060000))
  312. })
  313. Convey("bucket bounds should be correct", func() {
  314. So(res.Series[0].Name, ShouldEqual, "0")
  315. So(res.Series[1].Name, ShouldEqual, "1")
  316. So(res.Series[2].Name, ShouldEqual, "2")
  317. So(res.Series[3].Name, ShouldEqual, "4")
  318. So(res.Series[4].Name, ShouldEqual, "8")
  319. })
  320. Convey("value should be correct", func() {
  321. So(res.Series[8].Points[0][0].Float64, ShouldEqual, 1)
  322. So(res.Series[9].Points[0][0].Float64, ShouldEqual, 1)
  323. So(res.Series[10].Points[0][0].Float64, ShouldEqual, 1)
  324. So(res.Series[8].Points[1][0].Float64, ShouldEqual, 0)
  325. So(res.Series[9].Points[1][0].Float64, ShouldEqual, 0)
  326. So(res.Series[10].Points[1][0].Float64, ShouldEqual, 1)
  327. So(res.Series[8].Points[2][0].Float64, ShouldEqual, 0)
  328. So(res.Series[9].Points[2][0].Float64, ShouldEqual, 1)
  329. So(res.Series[10].Points[2][0].Float64, ShouldEqual, 0)
  330. })
  331. })
  332. Convey("when data from query is distribution with explicit bounds", func() {
  333. data, err := loadTestFile("./test-data/4-series-response-distribution-explicit.json")
  334. So(err, ShouldBeNil)
  335. So(len(data.TimeSeries), ShouldEqual, 1)
  336. res := &tsdb.QueryResult{Meta: simplejson.New(), RefId: "A"}
  337. query := &StackdriverQuery{AliasBy: "{{bucket}}"}
  338. err = executor.parseResponse(res, data, query)
  339. So(err, ShouldBeNil)
  340. So(len(res.Series), ShouldEqual, 33)
  341. for i := 0; i < 33; i++ {
  342. if i == 0 {
  343. So(res.Series[i].Name, ShouldEqual, "0")
  344. }
  345. So(len(res.Series[i].Points), ShouldEqual, 2)
  346. }
  347. Convey("timestamps should be in ascending order", func() {
  348. So(res.Series[0].Points[0][1].Float64, ShouldEqual, int64(1550859086000))
  349. So(res.Series[0].Points[1][1].Float64, ShouldEqual, int64(1550859146000))
  350. })
  351. Convey("bucket bounds should be correct", func() {
  352. So(res.Series[0].Name, ShouldEqual, "0")
  353. So(res.Series[1].Name, ShouldEqual, "0.01")
  354. So(res.Series[2].Name, ShouldEqual, "0.05")
  355. So(res.Series[3].Name, ShouldEqual, "0.1")
  356. })
  357. Convey("value should be correct", func() {
  358. So(res.Series[8].Points[0][0].Float64, ShouldEqual, 381)
  359. So(res.Series[9].Points[0][0].Float64, ShouldEqual, 212)
  360. So(res.Series[10].Points[0][0].Float64, ShouldEqual, 56)
  361. So(res.Series[8].Points[1][0].Float64, ShouldEqual, 375)
  362. So(res.Series[9].Points[1][0].Float64, ShouldEqual, 213)
  363. So(res.Series[10].Points[1][0].Float64, ShouldEqual, 56)
  364. })
  365. })
  366. })
  367. Convey("when interpolating filter wildcards", func() {
  368. Convey("and wildcard is used in the beginning and the end of the word", func() {
  369. Convey("and theres no wildcard in the middle of the word", func() {
  370. value := interpolateFilterWildcards("*-central1*")
  371. So(value, ShouldEqual, `has_substring("-central1")`)
  372. })
  373. Convey("and there is a wildcard in the middle of the word", func() {
  374. value := interpolateFilterWildcards("*-cent*ral1*")
  375. So(value, ShouldNotStartWith, `has_substring`)
  376. })
  377. })
  378. Convey("and wildcard is used in the beginning of the word", func() {
  379. Convey("and there is not a wildcard elsewhere in the word", func() {
  380. value := interpolateFilterWildcards("*-central1")
  381. So(value, ShouldEqual, `ends_with("-central1")`)
  382. })
  383. Convey("and there is a wildcard elsewhere in the word", func() {
  384. value := interpolateFilterWildcards("*-cent*al1")
  385. So(value, ShouldNotStartWith, `ends_with`)
  386. })
  387. })
  388. Convey("and wildcard is used at the end of the word", func() {
  389. Convey("and there is not a wildcard elsewhere in the word", func() {
  390. value := interpolateFilterWildcards("us-central*")
  391. So(value, ShouldEqual, `starts_with("us-central")`)
  392. })
  393. Convey("and there is a wildcard elsewhere in the word", func() {
  394. value := interpolateFilterWildcards("*us-central*")
  395. So(value, ShouldNotStartWith, `starts_with`)
  396. })
  397. })
  398. Convey("and wildcard is used in the middle of the word", func() {
  399. Convey("and there is only one wildcard", func() {
  400. value := interpolateFilterWildcards("us-ce*tral1-b")
  401. So(value, ShouldEqual, `monitoring.regex.full_match("^us\\-ce.*tral1\\-b$")`)
  402. })
  403. Convey("and there is more than one wildcard", func() {
  404. value := interpolateFilterWildcards("us-ce*tra*1-b")
  405. So(value, ShouldEqual, `monitoring.regex.full_match("^us\\-ce.*tra.*1\\-b$")`)
  406. })
  407. })
  408. Convey("and wildcard is used in the middle of the word and in the beginning of the word", func() {
  409. value := interpolateFilterWildcards("*s-ce*tral1-b")
  410. So(value, ShouldEqual, `monitoring.regex.full_match("^.*s\\-ce.*tral1\\-b$")`)
  411. })
  412. Convey("and wildcard is used in the middle of the word and in the ending of the word", func() {
  413. value := interpolateFilterWildcards("us-ce*tral1-*")
  414. So(value, ShouldEqual, `monitoring.regex.full_match("^us\\-ce.*tral1\\-.*$")`)
  415. })
  416. Convey("and no wildcard is used", func() {
  417. value := interpolateFilterWildcards("us-central1-a}")
  418. So(value, ShouldEqual, `us-central1-a}`)
  419. })
  420. })
  421. Convey("when building filter string", func() {
  422. Convey("and theres no regex operator", func() {
  423. Convey("and there are wildcards in a filter value", func() {
  424. filterParts := []interface{}{"zone", "=", "*-central1*"}
  425. value := buildFilterString("somemetrictype", filterParts)
  426. So(value, ShouldEqual, `metric.type="somemetrictype" zone=has_substring("-central1")`)
  427. })
  428. Convey("and there are no wildcards in any filter value", func() {
  429. filterParts := []interface{}{"zone", "!=", "us-central1-a"}
  430. value := buildFilterString("somemetrictype", filterParts)
  431. So(value, ShouldEqual, `metric.type="somemetrictype" zone!="us-central1-a"`)
  432. })
  433. })
  434. Convey("and there is a regex operator", func() {
  435. filterParts := []interface{}{"zone", "=~", "us-central1-a~"}
  436. value := buildFilterString("somemetrictype", filterParts)
  437. Convey("it should remove the ~ character from the operator that belongs to the value", func() {
  438. So(value, ShouldNotContainSubstring, `=~`)
  439. So(value, ShouldContainSubstring, `zone=`)
  440. })
  441. Convey("it should insert monitoring.regex.full_match before filter value", func() {
  442. So(value, ShouldContainSubstring, `zone=monitoring.regex.full_match("us-central1-a~")`)
  443. })
  444. })
  445. })
  446. })
  447. }
  448. func loadTestFile(path string) (StackdriverResponse, error) {
  449. var data StackdriverResponse
  450. jsonBody, err := ioutil.ReadFile(path)
  451. if err != nil {
  452. return data, err
  453. }
  454. err = json.Unmarshal(jsonBody, &data)
  455. return data, err
  456. }