prometheus_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package prometheus
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/grafana/grafana/pkg/models"
  6. "github.com/grafana/grafana/pkg/tsdb"
  7. "github.com/grafana/grafana/pkg/components/simplejson"
  8. p "github.com/prometheus/common/model"
  9. . "github.com/smartystreets/goconvey/convey"
  10. )
  11. func TestPrometheus(t *testing.T) {
  12. Convey("Prometheus", t, func() {
  13. dsInfo := &models.DataSource{
  14. JsonData: simplejson.New(),
  15. }
  16. Convey("converting metric name", func() {
  17. metric := map[p.LabelName]p.LabelValue{
  18. p.LabelName("app"): p.LabelValue("backend"),
  19. p.LabelName("device"): p.LabelValue("mobile"),
  20. }
  21. query := &PrometheusQuery{
  22. LegendFormat: "legend {{app}} {{ device }} {{broken}}",
  23. }
  24. So(formatLegend(metric, query), ShouldEqual, "legend backend mobile {{broken}}")
  25. })
  26. Convey("build full serie name", func() {
  27. metric := map[p.LabelName]p.LabelValue{
  28. p.LabelName(p.MetricNameLabel): p.LabelValue("http_request_total"),
  29. p.LabelName("app"): p.LabelValue("backend"),
  30. p.LabelName("device"): p.LabelValue("mobile"),
  31. }
  32. query := &PrometheusQuery{
  33. LegendFormat: "",
  34. }
  35. So(formatLegend(metric, query), ShouldEqual, `http_request_total{app="backend", device="mobile"}`)
  36. })
  37. Convey("parsing query model with step", func() {
  38. json := `{
  39. "expr": "go_goroutines",
  40. "format": "time_series",
  41. "refId": "A"
  42. }`
  43. jsonModel, _ := simplejson.NewJson([]byte(json))
  44. queryContext := &tsdb.TsdbQuery{}
  45. queryModels := []*tsdb.Query{
  46. {Model: jsonModel},
  47. }
  48. Convey("with 48h time range", func() {
  49. queryContext.TimeRange = tsdb.NewTimeRange("12h", "now")
  50. model, err := parseQuery(dsInfo, queryModels, queryContext)
  51. So(err, ShouldBeNil)
  52. So(model.Step, ShouldEqual, time.Second*30)
  53. })
  54. })
  55. Convey("parsing query model without step parameter", func() {
  56. json := `{
  57. "expr": "go_goroutines",
  58. "format": "time_series",
  59. "intervalFactor": 1,
  60. "refId": "A"
  61. }`
  62. jsonModel, _ := simplejson.NewJson([]byte(json))
  63. queryContext := &tsdb.TsdbQuery{}
  64. queryModels := []*tsdb.Query{
  65. {Model: jsonModel},
  66. }
  67. Convey("with 48h time range", func() {
  68. queryContext.TimeRange = tsdb.NewTimeRange("48h", "now")
  69. model, err := parseQuery(dsInfo, queryModels, queryContext)
  70. So(err, ShouldBeNil)
  71. So(model.Step, ShouldEqual, time.Minute*2)
  72. })
  73. Convey("with 1h time range", func() {
  74. queryContext.TimeRange = tsdb.NewTimeRange("1h", "now")
  75. model, err := parseQuery(dsInfo, queryModels, queryContext)
  76. So(err, ShouldBeNil)
  77. So(model.Step, ShouldEqual, time.Second*15)
  78. })
  79. })
  80. Convey("parsing query model with intervalFactor", func() {
  81. Convey("high intervalFactor", func() {
  82. json := `{
  83. "expr": "go_goroutines",
  84. "format": "time_series",
  85. "intervalFactor": 10,
  86. "refId": "A"
  87. }`
  88. jsonModel, _ := simplejson.NewJson([]byte(json))
  89. queryContext := &tsdb.TsdbQuery{}
  90. queryModels := []*tsdb.Query{
  91. {Model: jsonModel},
  92. }
  93. Convey("with 48h time range", func() {
  94. queryContext.TimeRange = tsdb.NewTimeRange("48h", "now")
  95. model, err := parseQuery(dsInfo, queryModels, queryContext)
  96. So(err, ShouldBeNil)
  97. So(model.Step, ShouldEqual, time.Minute*20)
  98. })
  99. })
  100. Convey("low intervalFactor", func() {
  101. json := `{
  102. "expr": "go_goroutines",
  103. "format": "time_series",
  104. "intervalFactor": 1,
  105. "refId": "A"
  106. }`
  107. jsonModel, _ := simplejson.NewJson([]byte(json))
  108. queryContext := &tsdb.TsdbQuery{}
  109. queryModels := []*tsdb.Query{
  110. {Model: jsonModel},
  111. }
  112. Convey("with 48h time range", func() {
  113. queryContext.TimeRange = tsdb.NewTimeRange("48h", "now")
  114. model, err := parseQuery(dsInfo, queryModels, queryContext)
  115. So(err, ShouldBeNil)
  116. So(model.Step, ShouldEqual, time.Minute*2)
  117. })
  118. })
  119. })
  120. })
  121. }