azuremonitor-datasource_test.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 dimension filter", func() {
  64. tsdbQuery.Queries[0].Model = simplejson.NewFromAny(map[string]interface{}{
  65. "azureMonitor": map[string]interface{}{
  66. "timeGrain": "PT1M",
  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. "dimension": "blob",
  75. "dimensionFilter": "*",
  76. },
  77. })
  78. queries, err := datasource.buildQueries(tsdbQuery.Queries, tsdbQuery.TimeRange)
  79. So(err, ShouldBeNil)
  80. 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")
  81. })
  82. })
  83. Convey("Parse AzureMonitor API response in the time series format", func() {
  84. Convey("when data from query aggregated as average to one time series", func() {
  85. data, err := loadTestFile("./test-data/1-azure-monitor-response-avg.json")
  86. So(err, ShouldBeNil)
  87. So(data.Interval, ShouldEqual, "PT1M")
  88. res := &tsdb.QueryResult{Meta: simplejson.New(), RefId: "A"}
  89. query := &AzureMonitorQuery{
  90. UrlComponents: map[string]string{
  91. "resourceName": "grafana",
  92. },
  93. Params: url.Values{
  94. "aggregation": {"Average"},
  95. },
  96. }
  97. err = datasource.parseResponse(res, data, query)
  98. So(err, ShouldBeNil)
  99. So(len(res.Series), ShouldEqual, 1)
  100. So(res.Series[0].Name, ShouldEqual, "grafana.Percentage CPU")
  101. So(len(res.Series[0].Points), ShouldEqual, 5)
  102. So(res.Series[0].Points[0][0].Float64, ShouldEqual, 2.0875)
  103. So(res.Series[0].Points[0][1].Float64, ShouldEqual, int64(1549620780000))
  104. So(res.Series[0].Points[1][0].Float64, ShouldEqual, 2.1525)
  105. So(res.Series[0].Points[1][1].Float64, ShouldEqual, int64(1549620840000))
  106. So(res.Series[0].Points[2][0].Float64, ShouldEqual, 2.155)
  107. So(res.Series[0].Points[2][1].Float64, ShouldEqual, int64(1549620900000))
  108. So(res.Series[0].Points[3][0].Float64, ShouldEqual, 3.6925)
  109. So(res.Series[0].Points[3][1].Float64, ShouldEqual, int64(1549620960000))
  110. So(res.Series[0].Points[4][0].Float64, ShouldEqual, 2.44)
  111. So(res.Series[0].Points[4][1].Float64, ShouldEqual, int64(1549621020000))
  112. })
  113. Convey("when data from query aggregated as total to one time series", func() {
  114. data, err := loadTestFile("./test-data/2-azure-monitor-response-total.json")
  115. So(err, ShouldBeNil)
  116. res := &tsdb.QueryResult{Meta: simplejson.New(), RefId: "A"}
  117. query := &AzureMonitorQuery{
  118. UrlComponents: map[string]string{
  119. "resourceName": "grafana",
  120. },
  121. Params: url.Values{
  122. "aggregation": {"Total"},
  123. },
  124. }
  125. err = datasource.parseResponse(res, data, query)
  126. So(err, ShouldBeNil)
  127. So(res.Series[0].Points[0][0].Float64, ShouldEqual, 8.26)
  128. So(res.Series[0].Points[0][1].Float64, ShouldEqual, int64(1549718940000))
  129. })
  130. Convey("when data from query aggregated as maximum to one time series", func() {
  131. data, err := loadTestFile("./test-data/3-azure-monitor-response-maximum.json")
  132. So(err, ShouldBeNil)
  133. res := &tsdb.QueryResult{Meta: simplejson.New(), RefId: "A"}
  134. query := &AzureMonitorQuery{
  135. UrlComponents: map[string]string{
  136. "resourceName": "grafana",
  137. },
  138. Params: url.Values{
  139. "aggregation": {"Maximum"},
  140. },
  141. }
  142. err = datasource.parseResponse(res, data, query)
  143. So(err, ShouldBeNil)
  144. So(res.Series[0].Points[0][0].Float64, ShouldEqual, 3.07)
  145. So(res.Series[0].Points[0][1].Float64, ShouldEqual, int64(1549722360000))
  146. })
  147. Convey("when data from query aggregated as minimum to one time series", func() {
  148. data, err := loadTestFile("./test-data/4-azure-monitor-response-minimum.json")
  149. So(err, ShouldBeNil)
  150. res := &tsdb.QueryResult{Meta: simplejson.New(), RefId: "A"}
  151. query := &AzureMonitorQuery{
  152. UrlComponents: map[string]string{
  153. "resourceName": "grafana",
  154. },
  155. Params: url.Values{
  156. "aggregation": {"Minimum"},
  157. },
  158. }
  159. err = datasource.parseResponse(res, data, query)
  160. So(err, ShouldBeNil)
  161. So(res.Series[0].Points[0][0].Float64, ShouldEqual, 1.51)
  162. So(res.Series[0].Points[0][1].Float64, ShouldEqual, int64(1549723380000))
  163. })
  164. Convey("when data from query aggregated as Count to one time series", func() {
  165. data, err := loadTestFile("./test-data/5-azure-monitor-response-count.json")
  166. So(err, ShouldBeNil)
  167. res := &tsdb.QueryResult{Meta: simplejson.New(), RefId: "A"}
  168. query := &AzureMonitorQuery{
  169. UrlComponents: map[string]string{
  170. "resourceName": "grafana",
  171. },
  172. Params: url.Values{
  173. "aggregation": {"Count"},
  174. },
  175. }
  176. err = datasource.parseResponse(res, data, query)
  177. So(err, ShouldBeNil)
  178. So(res.Series[0].Points[0][0].Float64, ShouldEqual, 4)
  179. So(res.Series[0].Points[0][1].Float64, ShouldEqual, int64(1549723440000))
  180. })
  181. Convey("when data from query aggregated as total and has dimension filter", func() {
  182. data, err := loadTestFile("./test-data/6-azure-monitor-response-multi-dimension.json")
  183. So(err, ShouldBeNil)
  184. res := &tsdb.QueryResult{Meta: simplejson.New(), RefId: "A"}
  185. query := &AzureMonitorQuery{
  186. UrlComponents: map[string]string{
  187. "resourceName": "grafana",
  188. },
  189. Params: url.Values{
  190. "aggregation": {"Average"},
  191. },
  192. }
  193. err = datasource.parseResponse(res, data, query)
  194. So(err, ShouldBeNil)
  195. So(len(res.Series), ShouldEqual, 3)
  196. So(res.Series[0].Name, ShouldEqual, "grafana{blobtype=PageBlob}.Blob Count")
  197. So(res.Series[0].Points[0][0].Float64, ShouldEqual, 3)
  198. So(res.Series[1].Name, ShouldEqual, "grafana{blobtype=BlockBlob}.Blob Count")
  199. So(res.Series[1].Points[0][0].Float64, ShouldEqual, 1)
  200. So(res.Series[2].Name, ShouldEqual, "grafana{blobtype=Azure Data Lake Storage}.Blob Count")
  201. So(res.Series[2].Points[0][0].Float64, ShouldEqual, 0)
  202. })
  203. })
  204. Convey("Find closest allowed interval for auto time grain", func() {
  205. intervals := map[string]int64{
  206. "3m": 180000,
  207. "5m": 300000,
  208. "10m": 600000,
  209. "15m": 900000,
  210. "1d": 86400000,
  211. "2d": 172800000,
  212. }
  213. closest := datasource.findClosestAllowedIntervalMS(intervals["3m"])
  214. So(closest, ShouldEqual, intervals["5m"])
  215. closest = datasource.findClosestAllowedIntervalMS(intervals["10m"])
  216. So(closest, ShouldEqual, intervals["15m"])
  217. closest = datasource.findClosestAllowedIntervalMS(intervals["2d"])
  218. So(closest, ShouldEqual, intervals["1d"])
  219. })
  220. })
  221. }
  222. func loadTestFile(path string) (AzureMonitorResponse, error) {
  223. var data AzureMonitorResponse
  224. jsonBody, err := ioutil.ReadFile(path)
  225. if err != nil {
  226. return data, err
  227. }
  228. err = json.Unmarshal(jsonBody, &data)
  229. return data, err
  230. }