metrics.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package api
  2. import (
  3. "math/rand"
  4. "strconv"
  5. "github.com/grafana/grafana/pkg/api/dtos"
  6. "github.com/grafana/grafana/pkg/metrics"
  7. "github.com/grafana/grafana/pkg/middleware"
  8. )
  9. func GetTestMetrics(c *middleware.Context) {
  10. from := c.QueryInt64("from")
  11. to := c.QueryInt64("to")
  12. maxDataPoints := c.QueryInt64("maxDataPoints")
  13. stepInSeconds := (to - from) / maxDataPoints
  14. result := dtos.MetricQueryResultDto{}
  15. result.Data = make([]dtos.MetricQueryResultDataDto, 1)
  16. for seriesIndex := range result.Data {
  17. points := make([][2]float64, maxDataPoints)
  18. walker := rand.Float64() * 100
  19. time := from
  20. for i := range points {
  21. points[i][0] = walker
  22. points[i][1] = float64(time)
  23. walker += rand.Float64() - 0.5
  24. time += stepInSeconds
  25. }
  26. result.Data[seriesIndex].Target = "test-series-" + strconv.Itoa(seriesIndex)
  27. result.Data[seriesIndex].DataPoints = points
  28. }
  29. c.JSON(200, &result)
  30. }
  31. func GetInternalMetrics(c middleware.Context) Response {
  32. snapshots := metrics.MetricStats.GetSnapshots()
  33. resp := make(map[string]interface{})
  34. for _, m := range snapshots {
  35. metricName := m.Name() + m.StringifyTags()
  36. switch metric := m.(type) {
  37. case metrics.Counter:
  38. resp[metricName] = map[string]interface{}{
  39. "count": metric.Count(),
  40. }
  41. case metrics.Timer:
  42. percentiles := metric.Percentiles([]float64{0.25, 0.75, 0.90, 0.99})
  43. resp[metricName] = map[string]interface{}{
  44. "count": metric.Count(),
  45. "min": metric.Min(),
  46. "max": metric.Max(),
  47. "mean": metric.Mean(),
  48. "std": metric.StdDev(),
  49. "p25": percentiles[0],
  50. "p75": percentiles[1],
  51. "p90": percentiles[2],
  52. "p99": percentiles[3],
  53. }
  54. }
  55. }
  56. return Json(200, resp)
  57. }