azuremonitor-datasource_test.go 14 KB

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