metrics.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package metrics
  2. import (
  3. "context"
  4. "net/http"
  5. "github.com/imgproxy/imgproxy/v3/metrics/datadog"
  6. "github.com/imgproxy/imgproxy/v3/metrics/newrelic"
  7. "github.com/imgproxy/imgproxy/v3/metrics/prometheus"
  8. )
  9. func Init() error {
  10. prometheus.Init()
  11. if err := newrelic.Init(); err != nil {
  12. return nil
  13. }
  14. datadog.Init()
  15. return nil
  16. }
  17. func Stop() {
  18. newrelic.Stop()
  19. datadog.Stop()
  20. }
  21. func Enabled() bool {
  22. return prometheus.Enabled() ||
  23. newrelic.Enabled() ||
  24. datadog.Enabled()
  25. }
  26. func StartRequest(ctx context.Context, rw http.ResponseWriter, r *http.Request) (context.Context, context.CancelFunc, http.ResponseWriter) {
  27. promCancel := prometheus.StartRequest()
  28. ctx, nrCancel, rw := newrelic.StartTransaction(ctx, rw, r)
  29. ctx, ddCancel, rw := datadog.StartRootSpan(ctx, rw, r)
  30. cancel := func() {
  31. promCancel()
  32. nrCancel()
  33. ddCancel()
  34. }
  35. return ctx, cancel, rw
  36. }
  37. func StartQueueSegment(ctx context.Context) context.CancelFunc {
  38. promCancel := prometheus.StartQueueSegment()
  39. nrCancel := newrelic.StartSegment(ctx, "Queue")
  40. ddCancel := datadog.StartSpan(ctx, "queue")
  41. cancel := func() {
  42. promCancel()
  43. nrCancel()
  44. ddCancel()
  45. }
  46. return cancel
  47. }
  48. func StartDownloadingSegment(ctx context.Context) context.CancelFunc {
  49. promCancel := prometheus.StartDownloadingSegment()
  50. nrCancel := newrelic.StartSegment(ctx, "Downloading image")
  51. ddCancel := datadog.StartSpan(ctx, "downloading_image")
  52. cancel := func() {
  53. promCancel()
  54. nrCancel()
  55. ddCancel()
  56. }
  57. return cancel
  58. }
  59. func StartProcessingSegment(ctx context.Context) context.CancelFunc {
  60. promCancel := prometheus.StartProcessingSegment()
  61. nrCancel := newrelic.StartSegment(ctx, "Processing image")
  62. ddCancel := datadog.StartSpan(ctx, "processing_image")
  63. cancel := func() {
  64. promCancel()
  65. nrCancel()
  66. ddCancel()
  67. }
  68. return cancel
  69. }
  70. func SendError(ctx context.Context, errType string, err error) {
  71. prometheus.IncrementErrorsTotal(errType)
  72. newrelic.SendError(ctx, errType, err)
  73. datadog.SendError(ctx, errType, err)
  74. }
  75. func ObserveBufferSize(t string, size int) {
  76. prometheus.ObserveBufferSize(t, size)
  77. newrelic.ObserveBufferSize(t, size)
  78. datadog.ObserveBufferSize(t, size)
  79. }
  80. func SetBufferDefaultSize(t string, size int) {
  81. prometheus.SetBufferDefaultSize(t, size)
  82. newrelic.SetBufferDefaultSize(t, size)
  83. datadog.SetBufferDefaultSize(t, size)
  84. }
  85. func SetBufferMaxSize(t string, size int) {
  86. prometheus.SetBufferMaxSize(t, size)
  87. newrelic.SetBufferMaxSize(t, size)
  88. datadog.SetBufferMaxSize(t, size)
  89. }