metrics.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. datadog.Stop()
  19. }
  20. func Enabled() bool {
  21. return prometheus.Enabled() ||
  22. newrelic.Enabled() ||
  23. datadog.Enabled()
  24. }
  25. func StartRequest(ctx context.Context, rw http.ResponseWriter, r *http.Request) (context.Context, context.CancelFunc, http.ResponseWriter) {
  26. promCancel := prometheus.StartRequest()
  27. ctx, nrCancel, rw := newrelic.StartTransaction(ctx, rw, r)
  28. ctx, ddCancel, rw := datadog.StartRootSpan(ctx, rw, r)
  29. cancel := func() {
  30. promCancel()
  31. nrCancel()
  32. ddCancel()
  33. }
  34. return ctx, cancel, rw
  35. }
  36. func StartDownloadingSegment(ctx context.Context) context.CancelFunc {
  37. promCancel := prometheus.StartDownloadingSegment()
  38. nrCancel := newrelic.StartSegment(ctx, "Downloading image")
  39. ddCancel := datadog.StartSpan(ctx, "downloading_image")
  40. cancel := func() {
  41. promCancel()
  42. nrCancel()
  43. ddCancel()
  44. }
  45. return cancel
  46. }
  47. func StartProcessingSegment(ctx context.Context) context.CancelFunc {
  48. promCancel := prometheus.StartProcessingSegment()
  49. nrCancel := newrelic.StartSegment(ctx, "Processing image")
  50. ddCancel := datadog.StartSpan(ctx, "processing_image")
  51. cancel := func() {
  52. promCancel()
  53. nrCancel()
  54. ddCancel()
  55. }
  56. return cancel
  57. }
  58. func SendError(ctx context.Context, errType string, err error) {
  59. prometheus.IncrementErrorsTotal(errType)
  60. newrelic.SendError(ctx, errType, err)
  61. datadog.SendError(ctx, errType, err)
  62. }
  63. func ObserveBufferSize(t string, size int) {
  64. prometheus.ObserveBufferSize(t, size)
  65. datadog.ObserveBufferSize(t, size)
  66. }
  67. func SetBufferDefaultSize(t string, size int) {
  68. prometheus.SetBufferDefaultSize(t, size)
  69. datadog.SetBufferDefaultSize(t, size)
  70. }
  71. func SetBufferMaxSize(t string, size int) {
  72. prometheus.SetBufferMaxSize(t, size)
  73. datadog.SetBufferMaxSize(t, size)
  74. }