metrics.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package metrics
  2. import (
  3. "context"
  4. "net/http"
  5. "time"
  6. "github.com/imgproxy/imgproxy/v3/metrics/datadog"
  7. "github.com/imgproxy/imgproxy/v3/metrics/newrelic"
  8. "github.com/imgproxy/imgproxy/v3/metrics/prometheus"
  9. )
  10. func Init() error {
  11. prometheus.Init()
  12. if err := newrelic.Init(); err != nil {
  13. return nil
  14. }
  15. datadog.Init()
  16. return nil
  17. }
  18. func Stop() {
  19. datadog.Stop()
  20. }
  21. func StartRequest(ctx context.Context, rw http.ResponseWriter, r *http.Request) (context.Context, context.CancelFunc, http.ResponseWriter) {
  22. promCancel := prometheus.StartRequest()
  23. ctx, nrCancel, rw := newrelic.StartTransaction(ctx, rw, r)
  24. ctx, ddCancel, rw := datadog.StartRootSpan(ctx, rw, r)
  25. cancel := func() {
  26. promCancel()
  27. nrCancel()
  28. ddCancel()
  29. }
  30. return ctx, cancel, rw
  31. }
  32. func StartDownloadingSegment(ctx context.Context) context.CancelFunc {
  33. promCancel := prometheus.StartDownloadingSegment()
  34. nrCancel := newrelic.StartSegment(ctx, "Downloading image")
  35. ddCancel := datadog.StartSpan(ctx, "downloading_image")
  36. cancel := func() {
  37. promCancel()
  38. nrCancel()
  39. ddCancel()
  40. }
  41. return cancel
  42. }
  43. func StartProcessingSegment(ctx context.Context) context.CancelFunc {
  44. promCancel := prometheus.StartProcessingSegment()
  45. nrCancel := newrelic.StartSegment(ctx, "Processing image")
  46. ddCancel := datadog.StartSpan(ctx, "processing_image")
  47. cancel := func() {
  48. promCancel()
  49. nrCancel()
  50. ddCancel()
  51. }
  52. return cancel
  53. }
  54. func SendError(ctx context.Context, errType string, err error) {
  55. prometheus.IncrementErrorsTotal(errType)
  56. newrelic.SendError(ctx, err)
  57. datadog.SendError(ctx, err)
  58. }
  59. func SendTimeout(ctx context.Context, d time.Duration) {
  60. prometheus.IncrementErrorsTotal("timeout")
  61. newrelic.SendTimeout(ctx, d)
  62. datadog.SendTimeout(ctx, d)
  63. }