azuremonitor_test.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 TestAzureMonitor(t *testing.T) {
  14. Convey("AzureMonitor", t, func() {
  15. executor := &AzureMonitorExecutor{}
  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 := executor.buildQueries(tsdbQuery)
  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. })
  57. Convey("Parse AzureMonitor API response in the time series format", func() {
  58. Convey("when data from query aggregated as average to one time series", func() {
  59. data, err := loadTestFile("./test-data/1-azure-monitor-response-avg.json")
  60. So(err, ShouldBeNil)
  61. So(data.Interval, ShouldEqual, "PT1M")
  62. res := &tsdb.QueryResult{Meta: simplejson.New(), RefId: "A"}
  63. query := &AzureMonitorQuery{
  64. UrlComponents: map[string]string{
  65. "resourceName": "grafana",
  66. },
  67. Params: url.Values{
  68. "aggregation": {"Average"},
  69. },
  70. }
  71. err = executor.parseResponse(res, data, query)
  72. So(err, ShouldBeNil)
  73. So(len(res.Series), ShouldEqual, 1)
  74. So(res.Series[0].Name, ShouldEqual, "grafana.Percentage CPU")
  75. So(len(res.Series[0].Points), ShouldEqual, 5)
  76. So(res.Series[0].Points[0][0].Float64, ShouldEqual, 2.0875)
  77. So(res.Series[0].Points[0][1].Float64, ShouldEqual, 1549620780000)
  78. So(res.Series[0].Points[1][0].Float64, ShouldEqual, 2.1525)
  79. So(res.Series[0].Points[1][1].Float64, ShouldEqual, 1549620840000)
  80. So(res.Series[0].Points[2][0].Float64, ShouldEqual, 2.155)
  81. So(res.Series[0].Points[2][1].Float64, ShouldEqual, 1549620900000)
  82. So(res.Series[0].Points[3][0].Float64, ShouldEqual, 3.6925)
  83. So(res.Series[0].Points[3][1].Float64, ShouldEqual, 1549620960000)
  84. So(res.Series[0].Points[4][0].Float64, ShouldEqual, 2.44)
  85. So(res.Series[0].Points[4][1].Float64, ShouldEqual, 1549621020000)
  86. })
  87. Convey("when data from query aggregated as total to one time series", func() {
  88. data, err := loadTestFile("./test-data/2-azure-monitor-response-total.json")
  89. So(err, ShouldBeNil)
  90. res := &tsdb.QueryResult{Meta: simplejson.New(), RefId: "A"}
  91. query := &AzureMonitorQuery{
  92. UrlComponents: map[string]string{
  93. "resourceName": "grafana",
  94. },
  95. Params: url.Values{
  96. "aggregation": {"Total"},
  97. },
  98. }
  99. err = executor.parseResponse(res, data, query)
  100. So(err, ShouldBeNil)
  101. So(res.Series[0].Points[0][0].Float64, ShouldEqual, 8.26)
  102. So(res.Series[0].Points[0][1].Float64, ShouldEqual, 1549718940000)
  103. })
  104. Convey("when data from query aggregated as maximum to one time series", func() {
  105. data, err := loadTestFile("./test-data/3-azure-monitor-response-maximum.json")
  106. So(err, ShouldBeNil)
  107. res := &tsdb.QueryResult{Meta: simplejson.New(), RefId: "A"}
  108. query := &AzureMonitorQuery{
  109. UrlComponents: map[string]string{
  110. "resourceName": "grafana",
  111. },
  112. Params: url.Values{
  113. "aggregation": {"Maximum"},
  114. },
  115. }
  116. err = executor.parseResponse(res, data, query)
  117. So(err, ShouldBeNil)
  118. So(res.Series[0].Points[0][0].Float64, ShouldEqual, 3.07)
  119. So(res.Series[0].Points[0][1].Float64, ShouldEqual, 1549722360000)
  120. })
  121. Convey("when data from query aggregated as minimum to one time series", func() {
  122. data, err := loadTestFile("./test-data/4-azure-monitor-response-minimum.json")
  123. So(err, ShouldBeNil)
  124. res := &tsdb.QueryResult{Meta: simplejson.New(), RefId: "A"}
  125. query := &AzureMonitorQuery{
  126. UrlComponents: map[string]string{
  127. "resourceName": "grafana",
  128. },
  129. Params: url.Values{
  130. "aggregation": {"Minimum"},
  131. },
  132. }
  133. err = executor.parseResponse(res, data, query)
  134. So(err, ShouldBeNil)
  135. So(res.Series[0].Points[0][0].Float64, ShouldEqual, 1.51)
  136. So(res.Series[0].Points[0][1].Float64, ShouldEqual, 1549723380000)
  137. })
  138. Convey("when data from query aggregated as Count to one time series", func() {
  139. data, err := loadTestFile("./test-data/5-azure-monitor-response-count.json")
  140. So(err, ShouldBeNil)
  141. res := &tsdb.QueryResult{Meta: simplejson.New(), RefId: "A"}
  142. query := &AzureMonitorQuery{
  143. UrlComponents: map[string]string{
  144. "resourceName": "grafana",
  145. },
  146. Params: url.Values{
  147. "aggregation": {"Count"},
  148. },
  149. }
  150. err = executor.parseResponse(res, data, query)
  151. So(err, ShouldBeNil)
  152. So(res.Series[0].Points[0][0].Float64, ShouldEqual, 4)
  153. So(res.Series[0].Points[0][1].Float64, ShouldEqual, 1549723440000)
  154. })
  155. Convey("when data from query aggregated as total and has dimension filter", func() {
  156. data, err := loadTestFile("./test-data/6-azure-monitor-response-multi-dimension.json")
  157. So(err, ShouldBeNil)
  158. res := &tsdb.QueryResult{Meta: simplejson.New(), RefId: "A"}
  159. query := &AzureMonitorQuery{
  160. UrlComponents: map[string]string{
  161. "resourceName": "grafana",
  162. },
  163. Params: url.Values{
  164. "aggregation": {"Average"},
  165. },
  166. }
  167. err = executor.parseResponse(res, data, query)
  168. So(err, ShouldBeNil)
  169. So(len(res.Series), ShouldEqual, 3)
  170. So(res.Series[0].Name, ShouldEqual, "grafana{blobtype=PageBlob}.Blob Count")
  171. So(res.Series[0].Points[0][0].Float64, ShouldEqual, 3)
  172. So(res.Series[1].Name, ShouldEqual, "grafana{blobtype=BlockBlob}.Blob Count")
  173. So(res.Series[1].Points[0][0].Float64, ShouldEqual, 1)
  174. So(res.Series[2].Name, ShouldEqual, "grafana{blobtype=Azure Data Lake Storage}.Blob Count")
  175. So(res.Series[2].Points[0][0].Float64, ShouldEqual, 0)
  176. })
  177. })
  178. })
  179. }
  180. func loadTestFile(path string) (AzureMonitorResponse, error) {
  181. var data AzureMonitorResponse
  182. jsonBody, err := ioutil.ReadFile(path)
  183. if err != nil {
  184. return data, err
  185. }
  186. err = json.Unmarshal(jsonBody, &data)
  187. return data, err
  188. }