azuremonitor-datasource_test.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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/tsdb"
  11. . "github.com/smartystreets/goconvey/convey"
  12. )
  13. func TestAzureMonitorDatasource(t *testing.T) {
  14. Convey("AzureMonitorDatasource", t, func() {
  15. datasource := &AzureMonitorDatasource{}
  16. Convey("Parse queries from frontend and build AzureMonitor API queries", func() {
  17. fromStart := time.Date(2018, 3, 15, 13, 0, 0, 0, time.UTC).In(time.Local)
  18. tsdbQuery := &tsdb.TsdbQuery{
  19. TimeRange: &tsdb.TimeRange{
  20. From: fmt.Sprintf("%v", fromStart.Unix()*1000),
  21. To: fmt.Sprintf("%v", fromStart.Add(34*time.Minute).Unix()*1000),
  22. },
  23. Queries: []*tsdb.Query{
  24. {
  25. Model: simplejson.NewFromAny(map[string]interface{}{
  26. "azureMonitor": map[string]interface{}{
  27. "timeGrain": "PT1M",
  28. "aggregation": "Average",
  29. "resourceGroup": "grafanastaging",
  30. "resourceName": "grafana",
  31. "metricDefinition": "Microsoft.Compute/virtualMachines",
  32. "metricName": "Percentage CPU",
  33. "alias": "testalias",
  34. "queryType": "Azure Monitor",
  35. },
  36. }),
  37. RefId: "A",
  38. },
  39. },
  40. }
  41. Convey("and is a normal query", func() {
  42. queries, err := datasource.buildQueries(tsdbQuery.Queries, tsdbQuery.TimeRange)
  43. So(err, ShouldBeNil)
  44. So(len(queries), ShouldEqual, 1)
  45. So(queries[0].RefID, ShouldEqual, "A")
  46. So(queries[0].URL, ShouldEqual, "resourceGroups/grafanastaging/providers/Microsoft.Compute/virtualMachines/grafana/providers/microsoft.insights/metrics")
  47. 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")
  48. So(len(queries[0].Params), ShouldEqual, 5)
  49. So(queries[0].Params["timespan"][0], ShouldEqual, "2018-03-15T13:00:00Z/2018-03-15T13:34:00Z")
  50. So(queries[0].Params["api-version"][0], ShouldEqual, "2018-01-01")
  51. So(queries[0].Params["aggregation"][0], ShouldEqual, "Average")
  52. So(queries[0].Params["metricnames"][0], ShouldEqual, "Percentage CPU")
  53. So(queries[0].Params["interval"][0], ShouldEqual, "PT1M")
  54. So(queries[0].Alias, ShouldEqual, "testalias")
  55. })
  56. Convey("and has a dimension filter", func() {
  57. tsdbQuery.Queries[0].Model = simplejson.NewFromAny(map[string]interface{}{
  58. "azureMonitor": map[string]interface{}{
  59. "timeGrain": "PT1M",
  60. "aggregation": "Average",
  61. "resourceGroup": "grafanastaging",
  62. "resourceName": "grafana",
  63. "metricDefinition": "Microsoft.Compute/virtualMachines",
  64. "metricName": "Percentage CPU",
  65. "alias": "testalias",
  66. "queryType": "Azure Monitor",
  67. "dimension": "blob",
  68. "dimensionFilter": "*",
  69. },
  70. })
  71. queries, err := datasource.buildQueries(tsdbQuery.Queries, tsdbQuery.TimeRange)
  72. So(err, ShouldBeNil)
  73. 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")
  74. })
  75. })
  76. Convey("Parse AzureMonitor API response in the time series format", func() {
  77. Convey("when data from query aggregated as average to one time series", func() {
  78. data, err := loadTestFile("./test-data/1-azure-monitor-response-avg.json")
  79. So(err, ShouldBeNil)
  80. So(data.Interval, ShouldEqual, "PT1M")
  81. res := &tsdb.QueryResult{Meta: simplejson.New(), RefId: "A"}
  82. query := &AzureMonitorQuery{
  83. UrlComponents: map[string]string{
  84. "resourceName": "grafana",
  85. },
  86. Params: url.Values{
  87. "aggregation": {"Average"},
  88. },
  89. }
  90. err = datasource.parseResponse(res, data, query)
  91. So(err, ShouldBeNil)
  92. So(len(res.Series), ShouldEqual, 1)
  93. So(res.Series[0].Name, ShouldEqual, "grafana.Percentage CPU")
  94. So(len(res.Series[0].Points), ShouldEqual, 5)
  95. So(res.Series[0].Points[0][0].Float64, ShouldEqual, 2.0875)
  96. So(res.Series[0].Points[0][1].Float64, ShouldEqual, 1549620780000)
  97. So(res.Series[0].Points[1][0].Float64, ShouldEqual, 2.1525)
  98. So(res.Series[0].Points[1][1].Float64, ShouldEqual, 1549620840000)
  99. So(res.Series[0].Points[2][0].Float64, ShouldEqual, 2.155)
  100. So(res.Series[0].Points[2][1].Float64, ShouldEqual, 1549620900000)
  101. So(res.Series[0].Points[3][0].Float64, ShouldEqual, 3.6925)
  102. So(res.Series[0].Points[3][1].Float64, ShouldEqual, 1549620960000)
  103. So(res.Series[0].Points[4][0].Float64, ShouldEqual, 2.44)
  104. So(res.Series[0].Points[4][1].Float64, ShouldEqual, 1549621020000)
  105. })
  106. Convey("when data from query aggregated as total to one time series", func() {
  107. data, err := loadTestFile("./test-data/2-azure-monitor-response-total.json")
  108. So(err, ShouldBeNil)
  109. res := &tsdb.QueryResult{Meta: simplejson.New(), RefId: "A"}
  110. query := &AzureMonitorQuery{
  111. UrlComponents: map[string]string{
  112. "resourceName": "grafana",
  113. },
  114. Params: url.Values{
  115. "aggregation": {"Total"},
  116. },
  117. }
  118. err = datasource.parseResponse(res, data, query)
  119. So(err, ShouldBeNil)
  120. So(res.Series[0].Points[0][0].Float64, ShouldEqual, 8.26)
  121. So(res.Series[0].Points[0][1].Float64, ShouldEqual, 1549718940000)
  122. })
  123. Convey("when data from query aggregated as maximum to one time series", func() {
  124. data, err := loadTestFile("./test-data/3-azure-monitor-response-maximum.json")
  125. So(err, ShouldBeNil)
  126. res := &tsdb.QueryResult{Meta: simplejson.New(), RefId: "A"}
  127. query := &AzureMonitorQuery{
  128. UrlComponents: map[string]string{
  129. "resourceName": "grafana",
  130. },
  131. Params: url.Values{
  132. "aggregation": {"Maximum"},
  133. },
  134. }
  135. err = datasource.parseResponse(res, data, query)
  136. So(err, ShouldBeNil)
  137. So(res.Series[0].Points[0][0].Float64, ShouldEqual, 3.07)
  138. So(res.Series[0].Points[0][1].Float64, ShouldEqual, 1549722360000)
  139. })
  140. Convey("when data from query aggregated as minimum to one time series", func() {
  141. data, err := loadTestFile("./test-data/4-azure-monitor-response-minimum.json")
  142. So(err, ShouldBeNil)
  143. res := &tsdb.QueryResult{Meta: simplejson.New(), RefId: "A"}
  144. query := &AzureMonitorQuery{
  145. UrlComponents: map[string]string{
  146. "resourceName": "grafana",
  147. },
  148. Params: url.Values{
  149. "aggregation": {"Minimum"},
  150. },
  151. }
  152. err = datasource.parseResponse(res, data, query)
  153. So(err, ShouldBeNil)
  154. So(res.Series[0].Points[0][0].Float64, ShouldEqual, 1.51)
  155. So(res.Series[0].Points[0][1].Float64, ShouldEqual, 1549723380000)
  156. })
  157. Convey("when data from query aggregated as Count to one time series", func() {
  158. data, err := loadTestFile("./test-data/5-azure-monitor-response-count.json")
  159. So(err, ShouldBeNil)
  160. res := &tsdb.QueryResult{Meta: simplejson.New(), RefId: "A"}
  161. query := &AzureMonitorQuery{
  162. UrlComponents: map[string]string{
  163. "resourceName": "grafana",
  164. },
  165. Params: url.Values{
  166. "aggregation": {"Count"},
  167. },
  168. }
  169. err = datasource.parseResponse(res, data, query)
  170. So(err, ShouldBeNil)
  171. So(res.Series[0].Points[0][0].Float64, ShouldEqual, 4)
  172. So(res.Series[0].Points[0][1].Float64, ShouldEqual, 1549723440000)
  173. })
  174. Convey("when data from query aggregated as total and has dimension filter", func() {
  175. data, err := loadTestFile("./test-data/6-azure-monitor-response-multi-dimension.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": {"Average"},
  184. },
  185. }
  186. err = datasource.parseResponse(res, data, query)
  187. So(err, ShouldBeNil)
  188. So(len(res.Series), ShouldEqual, 3)
  189. So(res.Series[0].Name, ShouldEqual, "grafana{blobtype=PageBlob}.Blob Count")
  190. So(res.Series[0].Points[0][0].Float64, ShouldEqual, 3)
  191. So(res.Series[1].Name, ShouldEqual, "grafana{blobtype=BlockBlob}.Blob Count")
  192. So(res.Series[1].Points[0][0].Float64, ShouldEqual, 1)
  193. So(res.Series[2].Name, ShouldEqual, "grafana{blobtype=Azure Data Lake Storage}.Blob Count")
  194. So(res.Series[2].Points[0][0].Float64, ShouldEqual, 0)
  195. })
  196. })
  197. Convey("Find closest allowed interval for auto time grain", func() {
  198. intervals := map[string]int64{
  199. "3m": 180000,
  200. "5m": 300000,
  201. "10m": 600000,
  202. "15m": 900000,
  203. "1d": 86400000,
  204. "2d": 172800000,
  205. }
  206. closest := datasource.findClosestAllowedIntervalMS(intervals["3m"])
  207. So(closest, ShouldEqual, intervals["5m"])
  208. closest = datasource.findClosestAllowedIntervalMS(intervals["10m"])
  209. So(closest, ShouldEqual, intervals["15m"])
  210. closest = datasource.findClosestAllowedIntervalMS(intervals["2d"])
  211. So(closest, ShouldEqual, intervals["1d"])
  212. })
  213. })
  214. }
  215. func loadTestFile(path string) (AzureMonitorResponse, error) {
  216. var data AzureMonitorResponse
  217. jsonBody, err := ioutil.ReadFile(path)
  218. if err != nil {
  219. return data, err
  220. }
  221. err = json.Unmarshal(jsonBody, &data)
  222. return data, err
  223. }