metrics.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package metrics
  2. import (
  3. "context"
  4. "net/http"
  5. "github.com/imgproxy/imgproxy/v3/metrics/cloudwatch"
  6. "github.com/imgproxy/imgproxy/v3/metrics/datadog"
  7. "github.com/imgproxy/imgproxy/v3/metrics/newrelic"
  8. "github.com/imgproxy/imgproxy/v3/metrics/otel"
  9. "github.com/imgproxy/imgproxy/v3/metrics/prometheus"
  10. )
  11. func Init() error {
  12. prometheus.Init()
  13. if err := newrelic.Init(); err != nil {
  14. return nil
  15. }
  16. datadog.Init()
  17. if err := otel.Init(); err != nil {
  18. return err
  19. }
  20. if err := cloudwatch.Init(); err != nil {
  21. return err
  22. }
  23. return nil
  24. }
  25. func Stop() {
  26. newrelic.Stop()
  27. datadog.Stop()
  28. otel.Stop()
  29. cloudwatch.Stop()
  30. }
  31. func Enabled() bool {
  32. return prometheus.Enabled() ||
  33. newrelic.Enabled() ||
  34. datadog.Enabled() ||
  35. otel.Enabled() ||
  36. cloudwatch.Enabled()
  37. }
  38. func StartRequest(ctx context.Context, rw http.ResponseWriter, r *http.Request) (context.Context, context.CancelFunc, http.ResponseWriter) {
  39. promCancel, rw := prometheus.StartRequest(rw)
  40. ctx, nrCancel, rw := newrelic.StartTransaction(ctx, rw, r)
  41. ctx, ddCancel, rw := datadog.StartRootSpan(ctx, rw, r)
  42. ctx, otelCancel, rw := otel.StartRootSpan(ctx, rw, r)
  43. cancel := func() {
  44. promCancel()
  45. nrCancel()
  46. ddCancel()
  47. otelCancel()
  48. }
  49. return ctx, cancel, rw
  50. }
  51. func StartQueueSegment(ctx context.Context) context.CancelFunc {
  52. promCancel := prometheus.StartQueueSegment()
  53. nrCancel := newrelic.StartSegment(ctx, "Queue")
  54. ddCancel := datadog.StartSpan(ctx, "queue")
  55. otelCancel := otel.StartSpan(ctx, "queue")
  56. cancel := func() {
  57. promCancel()
  58. nrCancel()
  59. ddCancel()
  60. otelCancel()
  61. }
  62. return cancel
  63. }
  64. func StartDownloadingSegment(ctx context.Context) context.CancelFunc {
  65. promCancel := prometheus.StartDownloadingSegment()
  66. nrCancel := newrelic.StartSegment(ctx, "Downloading image")
  67. ddCancel := datadog.StartSpan(ctx, "downloading_image")
  68. otelCancel := otel.StartSpan(ctx, "downloading_image")
  69. cancel := func() {
  70. promCancel()
  71. nrCancel()
  72. ddCancel()
  73. otelCancel()
  74. }
  75. return cancel
  76. }
  77. func StartProcessingSegment(ctx context.Context) context.CancelFunc {
  78. promCancel := prometheus.StartProcessingSegment()
  79. nrCancel := newrelic.StartSegment(ctx, "Processing image")
  80. ddCancel := datadog.StartSpan(ctx, "processing_image")
  81. otelCancel := otel.StartSpan(ctx, "processing_image")
  82. cancel := func() {
  83. promCancel()
  84. nrCancel()
  85. ddCancel()
  86. otelCancel()
  87. }
  88. return cancel
  89. }
  90. func StartStreamingSegment(ctx context.Context) context.CancelFunc {
  91. promCancel := prometheus.StartStreamingSegment()
  92. nrCancel := newrelic.StartSegment(ctx, "Streaming image")
  93. ddCancel := datadog.StartSpan(ctx, "streaming_image")
  94. otelCancel := otel.StartSpan(ctx, "streaming_image")
  95. cancel := func() {
  96. promCancel()
  97. nrCancel()
  98. ddCancel()
  99. otelCancel()
  100. }
  101. return cancel
  102. }
  103. func SendError(ctx context.Context, errType string, err error) {
  104. prometheus.IncrementErrorsTotal(errType)
  105. newrelic.SendError(ctx, errType, err)
  106. datadog.SendError(ctx, errType, err)
  107. otel.SendError(ctx, errType, err)
  108. }
  109. func ObserveBufferSize(t string, size int) {
  110. prometheus.ObserveBufferSize(t, size)
  111. newrelic.ObserveBufferSize(t, size)
  112. datadog.ObserveBufferSize(t, size)
  113. otel.ObserveBufferSize(t, size)
  114. cloudwatch.ObserveBufferSize(t, size)
  115. }
  116. func SetBufferDefaultSize(t string, size int) {
  117. prometheus.SetBufferDefaultSize(t, size)
  118. newrelic.SetBufferDefaultSize(t, size)
  119. datadog.SetBufferDefaultSize(t, size)
  120. otel.SetBufferDefaultSize(t, size)
  121. cloudwatch.SetBufferDefaultSize(t, size)
  122. }
  123. func SetBufferMaxSize(t string, size int) {
  124. prometheus.SetBufferMaxSize(t, size)
  125. newrelic.SetBufferMaxSize(t, size)
  126. datadog.SetBufferMaxSize(t, size)
  127. otel.SetBufferMaxSize(t, size)
  128. cloudwatch.SetBufferMaxSize(t, size)
  129. }