azuremonitor-datasource_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. package azuremonitor
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "net/url"
  7. "testing"
  8. "time"
  9. "github.com/grafana/grafana/pkg/components/simplejson"
  10. "github.com/grafana/grafana/pkg/models"
  11. "github.com/grafana/grafana/pkg/tsdb"
  12. . "github.com/smartystreets/goconvey/convey"
  13. )
  14. func TestAzureMonitorDatasource(t *testing.T) {
  15. Convey("AzureMonitorDatasource", t, func() {
  16. datasource := &AzureMonitorDatasource{}
  17. Convey("Parse queries from frontend and build AzureMonitor 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. DataSource: &models.DataSource{
  27. JsonData: simplejson.NewFromAny(map[string]interface{}{
  28. "subscriptionId": "default-subscription",
  29. }),
  30. },
  31. Model: simplejson.NewFromAny(map[string]interface{}{
  32. "subscription": "12345678-aaaa-bbbb-cccc-123456789abc",
  33. "azureMonitor": map[string]interface{}{
  34. "timeGrain": "PT1M",
  35. "aggregation": "Average",
  36. "resourceGroup": "grafanastaging",
  37. "resourceName": "grafana",
  38. "metricDefinition": "Microsoft.Compute/virtualMachines",
  39. "metricName": "Percentage CPU",
  40. "alias": "testalias",
  41. "queryType": "Azure Monitor",
  42. },
  43. }),
  44. RefId: "A",
  45. },
  46. },
  47. }
  48. Convey("and is a normal query", func() {
  49. queries, err := datasource.buildQueries(tsdbQuery.Queries, tsdbQuery.TimeRange)
  50. So(err, ShouldBeNil)
  51. So(len(queries), ShouldEqual, 1)
  52. So(queries[0].RefID, ShouldEqual, "A")
  53. So(queries[0].URL, ShouldEqual, "12345678-aaaa-bbbb-cccc-123456789abc/resourceGroups/grafanastaging/providers/Microsoft.Compute/virtualMachines/grafana/providers/microsoft.insights/metrics")
  54. So(queries[0].Target, ShouldEqual, "aggregation=Average&api-version=2018-01-01&interval=PT1M&metricnames=Percentage+CPU&timespan=2018-03-15T13%3A00%3A00Z%2F2018-03-15T13%3A34%3A00Z")
  55. So(len(queries[0].Params), ShouldEqual, 5)
  56. So(queries[0].Params["timespan"][0], ShouldEqual, "2018-03-15T13:00:00Z/2018-03-15T13:34:00Z")
  57. So(queries[0].Params["api-version"][0], ShouldEqual, "2018-01-01")
  58. So(queries[0].Params["aggregation"][0], ShouldEqual, "Average")
  59. So(queries[0].Params["metricnames"][0], ShouldEqual, "Percentage CPU")
  60. So(queries[0].Params["interval"][0], ShouldEqual, "PT1M")
  61. So(queries[0].Alias, ShouldEqual, "testalias")
  62. })
  63. Convey("and has a time grain set to auto", func() {
  64. tsdbQuery.Queries[0].Model = simplejson.NewFromAny(map[string]interface{}{
  65. "azureMonitor": map[string]interface{}{
  66. "timeGrain": "auto",
  67. "aggregation": "Average",
  68. "resourceGroup": "grafanastaging",
  69. "resourceName": "grafana",
  70. "metricDefinition": "Microsoft.Compute/virtualMachines",
  71. "metricName": "Percentage CPU",
  72. "alias": "testalias",
  73. "queryType": "Azure Monitor",
  74. },
  75. })
  76. tsdbQuery.Queries[0].IntervalMs = 400000
  77. queries, err := datasource.buildQueries(tsdbQuery.Queries, tsdbQuery.TimeRange)
  78. So(err, ShouldBeNil)
  79. So(queries[0].Params["interval"][0], ShouldEqual, "PT15M")
  80. })
  81. Convey("and has a time grain set to auto and the metric has a limited list of allowed time grains", func() {
  82. tsdbQuery.Queries[0].Model = simplejson.NewFromAny(map[string]interface{}{
  83. "azureMonitor": map[string]interface{}{
  84. "timeGrain": "auto",
  85. "aggregation": "Average",
  86. "resourceGroup": "grafanastaging",
  87. "resourceName": "grafana",
  88. "metricDefinition": "Microsoft.Compute/virtualMachines",
  89. "metricName": "Percentage CPU",
  90. "alias": "testalias",
  91. "queryType": "Azure Monitor",
  92. "allowedTimeGrainsMs": []interface{}{"auto", json.Number("60000"), json.Number("300000")},
  93. },
  94. })
  95. tsdbQuery.Queries[0].IntervalMs = 400000
  96. queries, err := datasource.buildQueries(tsdbQuery.Queries, tsdbQuery.TimeRange)
  97. So(err, ShouldBeNil)
  98. So(queries[0].Params["interval"][0], ShouldEqual, "PT5M")
  99. })
  100. Convey("and has a dimension filter", func() {
  101. tsdbQuery.Queries[0].Model = simplejson.NewFromAny(map[string]interface{}{
  102. "azureMonitor": map[string]interface{}{
  103. "timeGrain": "PT1M",
  104. "aggregation": "Average",
  105. "resourceGroup": "grafanastaging",
  106. "resourceName": "grafana",
  107. "metricDefinition": "Microsoft.Compute/virtualMachines",
  108. "metricName": "Percentage CPU",
  109. "alias": "testalias",
  110. "queryType": "Azure Monitor",
  111. "dimension": "blob",
  112. "dimensionFilter": "*",
  113. },
  114. })
  115. queries, err := datasource.buildQueries(tsdbQuery.Queries, tsdbQuery.TimeRange)
  116. So(err, ShouldBeNil)
  117. So(queries[0].Target, ShouldEqual, "%24filter=blob+eq+%27%2A%27&aggregation=Average&api-version=2018-01-01&interval=PT1M&metricnames=Percentage+CPU&timespan=2018-03-15T13%3A00%3A00Z%2F2018-03-15T13%3A34%3A00Z")
  118. })
  119. Convey("and has a dimension filter set to None", func() {
  120. tsdbQuery.Queries[0].Model = simplejson.NewFromAny(map[string]interface{}{
  121. "azureMonitor": map[string]interface{}{
  122. "timeGrain": "PT1M",
  123. "aggregation": "Average",
  124. "resourceGroup": "grafanastaging",
  125. "resourceName": "grafana",
  126. "metricDefinition": "Microsoft.Compute/virtualMachines",
  127. "metricName": "Percentage CPU",
  128. "alias": "testalias",
  129. "queryType": "Azure Monitor",
  130. "dimension": "None",
  131. "dimensionFilter": "*",
  132. },
  133. })
  134. queries, err := datasource.buildQueries(tsdbQuery.Queries, tsdbQuery.TimeRange)
  135. So(err, ShouldBeNil)
  136. So(queries[0].Target, ShouldEqual, "aggregation=Average&api-version=2018-01-01&interval=PT1M&metricnames=Percentage+CPU&timespan=2018-03-15T13%3A00%3A00Z%2F2018-03-15T13%3A34%3A00Z")
  137. })
  138. })
  139. Convey("Parse AzureMonitor API response in the time series format", func() {
  140. Convey("when data from query aggregated as average to one time series", func() {
  141. data, err := loadTestFile("./test-data/1-azure-monitor-response-avg.json")
  142. So(err, ShouldBeNil)
  143. So(data.Interval, ShouldEqual, "PT1M")
  144. res := &tsdb.QueryResult{Meta: simplejson.New(), RefId: "A"}
  145. query := &AzureMonitorQuery{
  146. UrlComponents: map[string]string{
  147. "resourceName": "grafana",
  148. },
  149. Params: url.Values{
  150. "aggregation": {"Average"},
  151. },
  152. }
  153. err = datasource.parseResponse(res, data, query)
  154. So(err, ShouldBeNil)
  155. So(len(res.Series), ShouldEqual, 1)
  156. So(res.Series[0].Name, ShouldEqual, "grafana.Percentage CPU")
  157. So(len(res.Series[0].Points), ShouldEqual, 5)
  158. So(res.Series[0].Points[0][0].Float64, ShouldEqual, 2.0875)
  159. So(res.Series[0].Points[0][1].Float64, ShouldEqual, int64(1549620780000))
  160. So(res.Series[0].Points[1][0].Float64, ShouldEqual, 2.1525)
  161. So(res.Series[0].Points[1][1].Float64, ShouldEqual, int64(1549620840000))
  162. So(res.Series[0].Points[2][0].Float64, ShouldEqual, 2.155)
  163. So(res.Series[0].Points[2][1].Float64, ShouldEqual, int64(1549620900000))
  164. So(res.Series[0].Points[3][0].Float64, ShouldEqual, 3.6925)
  165. So(res.Series[0].Points[3][1].Float64, ShouldEqual, int64(1549620960000))
  166. So(res.Series[0].Points[4][0].Float64, ShouldEqual, 2.44)
  167. So(res.Series[0].Points[4][1].Float64, ShouldEqual, int64(1549621020000))
  168. })
  169. Convey("when data from query aggregated as total to one time series", func() {
  170. data, err := loadTestFile("./test-data/2-azure-monitor-response-total.json")
  171. So(err, ShouldBeNil)
  172. res := &tsdb.QueryResult{Meta: simplejson.New(), RefId: "A"}
  173. query := &AzureMonitorQuery{
  174. UrlComponents: map[string]string{
  175. "resourceName": "grafana",
  176. },
  177. Params: url.Values{
  178. "aggregation": {"Total"},
  179. },
  180. }
  181. err = datasource.parseResponse(res, data, query)
  182. So(err, ShouldBeNil)
  183. So(res.Series[0].Points[0][0].Float64, ShouldEqual, 8.26)
  184. So(res.Series[0].Points[0][1].Float64, ShouldEqual, int64(1549718940000))
  185. })
  186. Convey("when data from query aggregated as maximum to one time series", func() {
  187. data, err := loadTestFile("./test-data/3-azure-monitor-response-maximum.json")
  188. So(err, ShouldBeNil)
  189. res := &tsdb.QueryResult{Meta: simplejson.New(), RefId: "A"}
  190. query := &AzureMonitorQuery{
  191. UrlComponents: map[string]string{
  192. "resourceName": "grafana",
  193. },
  194. Params: url.Values{
  195. "aggregation": {"Maximum"},
  196. },
  197. }
  198. err = datasource.parseResponse(res, data, query)
  199. So(err, ShouldBeNil)
  200. So(res.Series[0].Points[0][0].Float64, ShouldEqual, 3.07)
  201. So(res.Series[0].Points[0][1].Float64, ShouldEqual, int64(1549722360000))
  202. })
  203. Convey("when data from query aggregated as minimum to one time series", func() {
  204. data, err := loadTestFile("./test-data/4-azure-monitor-response-minimum.json")
  205. So(err, ShouldBeNil)
  206. res := &tsdb.QueryResult{Meta: simplejson.New(), RefId: "A"}
  207. query := &AzureMonitorQuery{
  208. UrlComponents: map[string]string{
  209. "resourceName": "grafana",
  210. },
  211. Params: url.Values{
  212. "aggregation": {"Minimum"},
  213. },
  214. }
  215. err = datasource.parseResponse(res, data, query)
  216. So(err, ShouldBeNil)
  217. So(res.Series[0].Points[0][0].Float64, ShouldEqual, 1.51)
  218. So(res.Series[0].Points[0][1].Float64, ShouldEqual, int64(1549723380000))
  219. })
  220. Convey("when data from query aggregated as Count to one time series", func() {
  221. data, err := loadTestFile("./test-data/5-azure-monitor-response-count.json")
  222. So(err, ShouldBeNil)
  223. res := &tsdb.QueryResult{Meta: simplejson.New(), RefId: "A"}
  224. query := &AzureMonitorQuery{
  225. UrlComponents: map[string]string{
  226. "resourceName": "grafana",
  227. },
  228. Params: url.Values{
  229. "aggregation": {"Count"},
  230. },
  231. }
  232. err = datasource.parseResponse(res, data, query)
  233. So(err, ShouldBeNil)
  234. So(res.Series[0].Points[0][0].Float64, ShouldEqual, 4)
  235. So(res.Series[0].Points[0][1].Float64, ShouldEqual, int64(1549723440000))
  236. })
  237. Convey("when data from query aggregated as total and has dimension filter", func() {
  238. data, err := loadTestFile("./test-data/6-azure-monitor-response-multi-dimension.json")
  239. So(err, ShouldBeNil)
  240. res := &tsdb.QueryResult{Meta: simplejson.New(), RefId: "A"}
  241. query := &AzureMonitorQuery{
  242. UrlComponents: map[string]string{
  243. "resourceName": "grafana",
  244. },
  245. Params: url.Values{
  246. "aggregation": {"Average"},
  247. },
  248. }
  249. err = datasource.parseResponse(res, data, query)
  250. So(err, ShouldBeNil)
  251. So(len(res.Series), ShouldEqual, 3)
  252. So(res.Series[0].Name, ShouldEqual, "grafana{blobtype=PageBlob}.Blob Count")
  253. So(res.Series[0].Points[0][0].Float64, ShouldEqual, 3)
  254. So(res.Series[1].Name, ShouldEqual, "grafana{blobtype=BlockBlob}.Blob Count")
  255. So(res.Series[1].Points[0][0].Float64, ShouldEqual, 1)
  256. So(res.Series[2].Name, ShouldEqual, "grafana{blobtype=Azure Data Lake Storage}.Blob Count")
  257. So(res.Series[2].Points[0][0].Float64, ShouldEqual, 0)
  258. })
  259. Convey("when data from query has alias patterns", func() {
  260. data, err := loadTestFile("./test-data/2-azure-monitor-response-total.json")
  261. So(err, ShouldBeNil)
  262. res := &tsdb.QueryResult{Meta: simplejson.New(), RefId: "A"}
  263. query := &AzureMonitorQuery{
  264. Alias: "custom {{resourcegroup}} {{namespace}} {{resourceName}} {{metric}}",
  265. UrlComponents: map[string]string{
  266. "resourceName": "grafana",
  267. },
  268. Params: url.Values{
  269. "aggregation": {"Total"},
  270. },
  271. }
  272. err = datasource.parseResponse(res, data, query)
  273. So(err, ShouldBeNil)
  274. So(res.Series[0].Name, ShouldEqual, "custom grafanastaging Microsoft.Compute/virtualMachines grafana Percentage CPU")
  275. })
  276. Convey("when data has dimension filters and alias patterns", func() {
  277. data, err := loadTestFile("./test-data/6-azure-monitor-response-multi-dimension.json")
  278. So(err, ShouldBeNil)
  279. res := &tsdb.QueryResult{Meta: simplejson.New(), RefId: "A"}
  280. query := &AzureMonitorQuery{
  281. Alias: "{{dimensionname}}={{DimensionValue}}",
  282. UrlComponents: map[string]string{
  283. "resourceName": "grafana",
  284. },
  285. Params: url.Values{
  286. "aggregation": {"Average"},
  287. },
  288. }
  289. err = datasource.parseResponse(res, data, query)
  290. So(err, ShouldBeNil)
  291. So(res.Series[0].Name, ShouldEqual, "blobtype=PageBlob")
  292. So(res.Series[1].Name, ShouldEqual, "blobtype=BlockBlob")
  293. So(res.Series[2].Name, ShouldEqual, "blobtype=Azure Data Lake Storage")
  294. })
  295. })
  296. Convey("Find closest allowed interval for auto time grain", func() {
  297. intervals := map[string]int64{
  298. "3m": 180000,
  299. "5m": 300000,
  300. "10m": 600000,
  301. "15m": 900000,
  302. "1d": 86400000,
  303. "2d": 172800000,
  304. }
  305. closest := datasource.findClosestAllowedIntervalMS(intervals["3m"], []int64{})
  306. So(closest, ShouldEqual, intervals["5m"])
  307. closest = datasource.findClosestAllowedIntervalMS(intervals["10m"], []int64{})
  308. So(closest, ShouldEqual, intervals["15m"])
  309. closest = datasource.findClosestAllowedIntervalMS(intervals["2d"], []int64{})
  310. So(closest, ShouldEqual, intervals["1d"])
  311. closest = datasource.findClosestAllowedIntervalMS(intervals["3m"], []int64{intervals["1d"]})
  312. So(closest, ShouldEqual, intervals["1d"])
  313. })
  314. })
  315. }
  316. func loadTestFile(path string) (AzureMonitorResponse, error) {
  317. var data AzureMonitorResponse
  318. jsonBody, err := ioutil.ReadFile(path)
  319. if err != nil {
  320. return data, err
  321. }
  322. err = json.Unmarshal(jsonBody, &data)
  323. return data, err
  324. }