stackdriver_test.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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 within accepted range", func() {
  130. tsdbQuery.Queries[0].IntervalMs = 1000
  131. tsdbQuery.Queries[0].Model = simplejson.NewFromAny(map[string]interface{}{
  132. "alignmentPeriod": "+600s",
  133. })
  134. queries, err := executor.buildQueries(tsdbQuery)
  135. So(err, ShouldBeNil)
  136. So(queries[0].Params["aggregation.alignmentPeriod"][0], ShouldEqual, `+600s`)
  137. })
  138. })
  139. Convey("and query has aggregation mean set", func() {
  140. tsdbQuery.Queries[0].Model = simplejson.NewFromAny(map[string]interface{}{
  141. "metricType": "a/metric/type",
  142. "crossSeriesReducer": "REDUCE_SUM",
  143. "view": "FULL",
  144. })
  145. queries, err := executor.buildQueries(tsdbQuery)
  146. So(err, ShouldBeNil)
  147. So(len(queries), ShouldEqual, 1)
  148. So(queries[0].RefID, ShouldEqual, "A")
  149. 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")
  150. So(len(queries[0].Params), ShouldEqual, 7)
  151. So(queries[0].Params["interval.startTime"][0], ShouldEqual, "2018-03-15T13:00:00Z")
  152. So(queries[0].Params["interval.endTime"][0], ShouldEqual, "2018-03-15T13:34:00Z")
  153. So(queries[0].Params["aggregation.crossSeriesReducer"][0], ShouldEqual, "REDUCE_SUM")
  154. So(queries[0].Params["aggregation.perSeriesAligner"][0], ShouldEqual, "ALIGN_MEAN")
  155. So(queries[0].Params["aggregation.alignmentPeriod"][0], ShouldEqual, "+60s")
  156. So(queries[0].Params["filter"][0], ShouldEqual, "metric.type=\"a/metric/type\"")
  157. So(queries[0].Params["view"][0], ShouldEqual, "FULL")
  158. })
  159. Convey("and query has group bys", func() {
  160. tsdbQuery.Queries[0].Model = simplejson.NewFromAny(map[string]interface{}{
  161. "metricType": "a/metric/type",
  162. "crossSeriesReducer": "REDUCE_NONE",
  163. "groupBys": []interface{}{"metric.label.group1", "metric.label.group2"},
  164. "view": "FULL",
  165. })
  166. queries, err := executor.buildQueries(tsdbQuery)
  167. So(err, ShouldBeNil)
  168. So(len(queries), ShouldEqual, 1)
  169. So(queries[0].RefID, ShouldEqual, "A")
  170. 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")
  171. So(len(queries[0].Params), ShouldEqual, 8)
  172. So(queries[0].Params["interval.startTime"][0], ShouldEqual, "2018-03-15T13:00:00Z")
  173. So(queries[0].Params["interval.endTime"][0], ShouldEqual, "2018-03-15T13:34:00Z")
  174. So(queries[0].Params["aggregation.perSeriesAligner"][0], ShouldEqual, "ALIGN_MEAN")
  175. So(queries[0].Params["aggregation.groupByFields"][0], ShouldEqual, "metric.label.group1")
  176. So(queries[0].Params["aggregation.groupByFields"][1], ShouldEqual, "metric.label.group2")
  177. So(queries[0].Params["filter"][0], ShouldEqual, "metric.type=\"a/metric/type\"")
  178. So(queries[0].Params["view"][0], ShouldEqual, "FULL")
  179. })
  180. })
  181. Convey("Parse stackdriver response in the time series format", func() {
  182. Convey("when data from query aggregated to one time series", func() {
  183. data, err := loadTestFile("./test-data/1-series-response-agg-one-metric.json")
  184. So(err, ShouldBeNil)
  185. So(len(data.TimeSeries), ShouldEqual, 1)
  186. res := &tsdb.QueryResult{Meta: simplejson.New(), RefId: "A"}
  187. query := &StackdriverQuery{}
  188. err = executor.parseResponse(res, data, query)
  189. So(err, ShouldBeNil)
  190. So(len(res.Series), ShouldEqual, 1)
  191. So(res.Series[0].Name, ShouldEqual, "serviceruntime.googleapis.com/api/request_count")
  192. So(len(res.Series[0].Points), ShouldEqual, 3)
  193. Convey("timestamps should be in ascending order", func() {
  194. So(res.Series[0].Points[0][0].Float64, ShouldEqual, 0.05)
  195. So(res.Series[0].Points[0][1].Float64, ShouldEqual, int64(1536670020000))
  196. So(res.Series[0].Points[1][0].Float64, ShouldEqual, 1.05)
  197. So(res.Series[0].Points[1][1].Float64, ShouldEqual, int64(1536670080000))
  198. So(res.Series[0].Points[2][0].Float64, ShouldEqual, 1.0666666666667)
  199. So(res.Series[0].Points[2][1].Float64, ShouldEqual, int64(1536670260000))
  200. })
  201. })
  202. Convey("when data from query with no aggregation", func() {
  203. data, err := loadTestFile("./test-data/2-series-response-no-agg.json")
  204. So(err, ShouldBeNil)
  205. So(len(data.TimeSeries), ShouldEqual, 3)
  206. res := &tsdb.QueryResult{Meta: simplejson.New(), RefId: "A"}
  207. query := &StackdriverQuery{}
  208. err = executor.parseResponse(res, data, query)
  209. So(err, ShouldBeNil)
  210. Convey("Should add labels to metric name", func() {
  211. So(len(res.Series), ShouldEqual, 3)
  212. So(res.Series[0].Name, ShouldEqual, "compute.googleapis.com/instance/cpu/usage_time collector-asia-east-1")
  213. So(res.Series[1].Name, ShouldEqual, "compute.googleapis.com/instance/cpu/usage_time collector-europe-west-1")
  214. So(res.Series[2].Name, ShouldEqual, "compute.googleapis.com/instance/cpu/usage_time collector-us-east-1")
  215. })
  216. Convey("Should parse to time series", func() {
  217. So(len(res.Series[0].Points), ShouldEqual, 3)
  218. So(res.Series[0].Points[0][0].Float64, ShouldEqual, 9.8566497180145)
  219. So(res.Series[0].Points[1][0].Float64, ShouldEqual, 9.7323568146676)
  220. So(res.Series[0].Points[2][0].Float64, ShouldEqual, 9.7730520330369)
  221. })
  222. Convey("Should add meta for labels to the response", func() {
  223. metricLabels := res.Meta.Get("metricLabels").Interface().(map[string][]string)
  224. So(metricLabels, ShouldNotBeNil)
  225. So(len(metricLabels["instance_name"]), ShouldEqual, 3)
  226. So(metricLabels["instance_name"][0], ShouldEqual, "collector-asia-east-1")
  227. So(metricLabels["instance_name"][1], ShouldEqual, "collector-europe-west-1")
  228. So(metricLabels["instance_name"][2], ShouldEqual, "collector-us-east-1")
  229. resourceLabels := res.Meta.Get("resourceLabels").Interface().(map[string][]string)
  230. So(resourceLabels, ShouldNotBeNil)
  231. So(len(resourceLabels["zone"]), ShouldEqual, 3)
  232. So(resourceLabels["zone"][0], ShouldEqual, "asia-east1-a")
  233. So(resourceLabels["zone"][1], ShouldEqual, "europe-west1-b")
  234. So(resourceLabels["zone"][2], ShouldEqual, "us-east1-b")
  235. So(len(resourceLabels["project_id"]), ShouldEqual, 1)
  236. So(resourceLabels["project_id"][0], ShouldEqual, "grafana-prod")
  237. })
  238. })
  239. Convey("when data from query with no aggregation and group bys", func() {
  240. data, err := loadTestFile("./test-data/2-series-response-no-agg.json")
  241. So(err, ShouldBeNil)
  242. So(len(data.TimeSeries), ShouldEqual, 3)
  243. res := &tsdb.QueryResult{Meta: simplejson.New(), RefId: "A"}
  244. query := &StackdriverQuery{GroupBys: []string{"metric.label.instance_name", "resource.label.zone"}}
  245. err = executor.parseResponse(res, data, query)
  246. So(err, ShouldBeNil)
  247. Convey("Should add instance name and zone labels to metric name", func() {
  248. So(len(res.Series), ShouldEqual, 3)
  249. So(res.Series[0].Name, ShouldEqual, "compute.googleapis.com/instance/cpu/usage_time collector-asia-east-1 asia-east1-a")
  250. So(res.Series[1].Name, ShouldEqual, "compute.googleapis.com/instance/cpu/usage_time collector-europe-west-1 europe-west1-b")
  251. So(res.Series[2].Name, ShouldEqual, "compute.googleapis.com/instance/cpu/usage_time collector-us-east-1 us-east1-b")
  252. })
  253. })
  254. Convey("when data from query with no aggregation and alias by", func() {
  255. data, err := loadTestFile("./test-data/2-series-response-no-agg.json")
  256. So(err, ShouldBeNil)
  257. So(len(data.TimeSeries), ShouldEqual, 3)
  258. res := &tsdb.QueryResult{Meta: simplejson.New(), RefId: "A"}
  259. Convey("and the alias pattern is for metric type, a metric label and a resource label", func() {
  260. query := &StackdriverQuery{AliasBy: "{{metric.type}} - {{metric.label.instance_name}} - {{resource.label.zone}}", GroupBys: []string{"metric.label.instance_name", "resource.label.zone"}}
  261. err = executor.parseResponse(res, data, query)
  262. So(err, ShouldBeNil)
  263. Convey("Should use alias by formatting and only show instance name", func() {
  264. So(len(res.Series), ShouldEqual, 3)
  265. So(res.Series[0].Name, ShouldEqual, "compute.googleapis.com/instance/cpu/usage_time - collector-asia-east-1 - asia-east1-a")
  266. So(res.Series[1].Name, ShouldEqual, "compute.googleapis.com/instance/cpu/usage_time - collector-europe-west-1 - europe-west1-b")
  267. So(res.Series[2].Name, ShouldEqual, "compute.googleapis.com/instance/cpu/usage_time - collector-us-east-1 - us-east1-b")
  268. })
  269. })
  270. Convey("and the alias pattern is for metric name", func() {
  271. query := &StackdriverQuery{AliasBy: "metric {{metric.name}} service {{metric.service}}", GroupBys: []string{"metric.label.instance_name", "resource.label.zone"}}
  272. err = executor.parseResponse(res, data, query)
  273. So(err, ShouldBeNil)
  274. Convey("Should use alias by formatting and only show instance name", func() {
  275. So(len(res.Series), ShouldEqual, 3)
  276. So(res.Series[0].Name, ShouldEqual, "metric instance/cpu/usage_time service compute")
  277. So(res.Series[1].Name, ShouldEqual, "metric instance/cpu/usage_time service compute")
  278. So(res.Series[2].Name, ShouldEqual, "metric instance/cpu/usage_time service compute")
  279. })
  280. })
  281. })
  282. Convey("when data from query is distribution with exponential bounds", func() {
  283. data, err := loadTestFile("./test-data/3-series-response-distribution-exponential.json")
  284. So(err, ShouldBeNil)
  285. So(len(data.TimeSeries), ShouldEqual, 1)
  286. res := &tsdb.QueryResult{Meta: simplejson.New(), RefId: "A"}
  287. query := &StackdriverQuery{AliasBy: "{{bucket}}"}
  288. err = executor.parseResponse(res, data, query)
  289. So(err, ShouldBeNil)
  290. So(len(res.Series), ShouldEqual, 11)
  291. for i := 0; i < 11; i++ {
  292. if i == 0 {
  293. So(res.Series[i].Name, ShouldEqual, "0")
  294. } else {
  295. So(res.Series[i].Name, ShouldEqual, strconv.FormatInt(int64(math.Pow(float64(2), float64(i-1))), 10))
  296. }
  297. So(len(res.Series[i].Points), ShouldEqual, 3)
  298. }
  299. Convey("timestamps should be in ascending order", func() {
  300. So(res.Series[0].Points[0][1].Float64, ShouldEqual, int64(1536668940000))
  301. So(res.Series[0].Points[1][1].Float64, ShouldEqual, int64(1536669000000))
  302. So(res.Series[0].Points[2][1].Float64, ShouldEqual, int64(1536669060000))
  303. })
  304. Convey("bucket bounds should be correct", func() {
  305. So(res.Series[0].Name, ShouldEqual, "0")
  306. So(res.Series[1].Name, ShouldEqual, "1")
  307. So(res.Series[2].Name, ShouldEqual, "2")
  308. So(res.Series[3].Name, ShouldEqual, "4")
  309. So(res.Series[4].Name, ShouldEqual, "8")
  310. })
  311. Convey("value should be correct", func() {
  312. So(res.Series[8].Points[0][0].Float64, ShouldEqual, 1)
  313. So(res.Series[9].Points[0][0].Float64, ShouldEqual, 1)
  314. So(res.Series[10].Points[0][0].Float64, ShouldEqual, 1)
  315. So(res.Series[8].Points[1][0].Float64, ShouldEqual, 0)
  316. So(res.Series[9].Points[1][0].Float64, ShouldEqual, 0)
  317. So(res.Series[10].Points[1][0].Float64, ShouldEqual, 1)
  318. So(res.Series[8].Points[2][0].Float64, ShouldEqual, 0)
  319. So(res.Series[9].Points[2][0].Float64, ShouldEqual, 1)
  320. So(res.Series[10].Points[2][0].Float64, ShouldEqual, 0)
  321. })
  322. })
  323. Convey("when data from query is distribution with explicit bounds", func() {
  324. data, err := loadTestFile("./test-data/4-series-response-distribution-explicit.json")
  325. So(err, ShouldBeNil)
  326. So(len(data.TimeSeries), ShouldEqual, 1)
  327. res := &tsdb.QueryResult{Meta: simplejson.New(), RefId: "A"}
  328. query := &StackdriverQuery{AliasBy: "{{bucket}}"}
  329. err = executor.parseResponse(res, data, query)
  330. So(err, ShouldBeNil)
  331. So(len(res.Series), ShouldEqual, 33)
  332. for i := 0; i < 33; i++ {
  333. if i == 0 {
  334. So(res.Series[i].Name, ShouldEqual, "0")
  335. }
  336. So(len(res.Series[i].Points), ShouldEqual, 2)
  337. }
  338. Convey("timestamps should be in ascending order", func() {
  339. So(res.Series[0].Points[0][1].Float64, ShouldEqual, int64(1550859086000))
  340. So(res.Series[0].Points[1][1].Float64, ShouldEqual, int64(1550859146000))
  341. })
  342. Convey("bucket bounds should be correct", func() {
  343. So(res.Series[0].Name, ShouldEqual, "0")
  344. So(res.Series[1].Name, ShouldEqual, "0.01")
  345. So(res.Series[2].Name, ShouldEqual, "0.05")
  346. So(res.Series[3].Name, ShouldEqual, "0.1")
  347. })
  348. Convey("value should be correct", func() {
  349. So(res.Series[8].Points[0][0].Float64, ShouldEqual, 381)
  350. So(res.Series[9].Points[0][0].Float64, ShouldEqual, 212)
  351. So(res.Series[10].Points[0][0].Float64, ShouldEqual, 56)
  352. So(res.Series[8].Points[1][0].Float64, ShouldEqual, 375)
  353. So(res.Series[9].Points[1][0].Float64, ShouldEqual, 213)
  354. So(res.Series[10].Points[1][0].Float64, ShouldEqual, 56)
  355. })
  356. })
  357. })
  358. Convey("when interpolating filter wildcards", func() {
  359. Convey("and wildcard is used in the beginning and the end of the word", func() {
  360. Convey("and theres no wildcard in the middle of the word", func() {
  361. value := interpolateFilterWildcards("*-central1*")
  362. So(value, ShouldEqual, `has_substring("-central1")`)
  363. })
  364. Convey("and there is a wildcard in the middle of the word", func() {
  365. value := interpolateFilterWildcards("*-cent*ral1*")
  366. So(value, ShouldNotStartWith, `has_substring`)
  367. })
  368. })
  369. Convey("and wildcard is used in the beginning of the word", func() {
  370. Convey("and there is not a wildcard elsewhere in the word", func() {
  371. value := interpolateFilterWildcards("*-central1")
  372. So(value, ShouldEqual, `ends_with("-central1")`)
  373. })
  374. Convey("and there is a wildcard elsewhere in the word", func() {
  375. value := interpolateFilterWildcards("*-cent*al1")
  376. So(value, ShouldNotStartWith, `ends_with`)
  377. })
  378. })
  379. Convey("and wildcard is used at the end of the word", func() {
  380. Convey("and there is not a wildcard elsewhere in the word", func() {
  381. value := interpolateFilterWildcards("us-central*")
  382. So(value, ShouldEqual, `starts_with("us-central")`)
  383. })
  384. Convey("and there is a wildcard elsewhere in the word", func() {
  385. value := interpolateFilterWildcards("*us-central*")
  386. So(value, ShouldNotStartWith, `starts_with`)
  387. })
  388. })
  389. Convey("and wildcard is used in the middle of the word", func() {
  390. Convey("and there is only one wildcard", func() {
  391. value := interpolateFilterWildcards("us-ce*tral1-b")
  392. So(value, ShouldEqual, `monitoring.regex.full_match("^us\\-ce.*tral1\\-b$")`)
  393. })
  394. Convey("and there is more than one wildcard", func() {
  395. value := interpolateFilterWildcards("us-ce*tra*1-b")
  396. So(value, ShouldEqual, `monitoring.regex.full_match("^us\\-ce.*tra.*1\\-b$")`)
  397. })
  398. })
  399. Convey("and wildcard is used in the middle of the word and in the beginning of the word", func() {
  400. value := interpolateFilterWildcards("*s-ce*tral1-b")
  401. So(value, ShouldEqual, `monitoring.regex.full_match("^.*s\\-ce.*tral1\\-b$")`)
  402. })
  403. Convey("and wildcard is used in the middle of the word and in the ending of the word", func() {
  404. value := interpolateFilterWildcards("us-ce*tral1-*")
  405. So(value, ShouldEqual, `monitoring.regex.full_match("^us\\-ce.*tral1\\-.*$")`)
  406. })
  407. Convey("and no wildcard is used", func() {
  408. value := interpolateFilterWildcards("us-central1-a}")
  409. So(value, ShouldEqual, `us-central1-a}`)
  410. })
  411. })
  412. Convey("when building filter string", func() {
  413. Convey("and theres no regex operator", func() {
  414. Convey("and there are wildcards in a filter value", func() {
  415. filterParts := []interface{}{"zone", "=", "*-central1*"}
  416. value := buildFilterString("somemetrictype", filterParts)
  417. So(value, ShouldEqual, `metric.type="somemetrictype" zone=has_substring("-central1")`)
  418. })
  419. Convey("and there are no wildcards in any filter value", func() {
  420. filterParts := []interface{}{"zone", "!=", "us-central1-a"}
  421. value := buildFilterString("somemetrictype", filterParts)
  422. So(value, ShouldEqual, `metric.type="somemetrictype" zone!="us-central1-a"`)
  423. })
  424. })
  425. Convey("and there is a regex operator", func() {
  426. filterParts := []interface{}{"zone", "=~", "us-central1-a~"}
  427. value := buildFilterString("somemetrictype", filterParts)
  428. Convey("it should remove the ~ character from the operator that belongs to the value", func() {
  429. So(value, ShouldNotContainSubstring, `=~`)
  430. So(value, ShouldContainSubstring, `zone=`)
  431. })
  432. Convey("it should insert monitoring.regex.full_match before filter value", func() {
  433. So(value, ShouldContainSubstring, `zone=monitoring.regex.full_match("us-central1-a~")`)
  434. })
  435. })
  436. })
  437. })
  438. }
  439. func loadTestFile(path string) (StackdriverResponse, error) {
  440. var data StackdriverResponse
  441. jsonBody, err := ioutil.ReadFile(path)
  442. if err != nil {
  443. return data, err
  444. }
  445. err = json.Unmarshal(jsonBody, &data)
  446. return data, err
  447. }