123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- package metrics
- import (
- "context"
- "net/http"
- "time"
- "github.com/imgproxy/imgproxy/v3/metrics/datadog"
- "github.com/imgproxy/imgproxy/v3/metrics/newrelic"
- "github.com/imgproxy/imgproxy/v3/metrics/prometheus"
- )
- func Init() error {
- prometheus.Init()
- if err := newrelic.Init(); err != nil {
- return nil
- }
- datadog.Init()
- return nil
- }
- func Stop() {
- datadog.Stop()
- }
- func StartRequest(ctx context.Context, rw http.ResponseWriter, r *http.Request) (context.Context, context.CancelFunc, http.ResponseWriter) {
- promCancel := prometheus.StartRequest()
- ctx, nrCancel, rw := newrelic.StartTransaction(ctx, rw, r)
- ctx, ddCancel, rw := datadog.StartRootSpan(ctx, rw, r)
- cancel := func() {
- promCancel()
- nrCancel()
- ddCancel()
- }
- return ctx, cancel, rw
- }
- func StartDownloadingSegment(ctx context.Context) context.CancelFunc {
- promCancel := prometheus.StartDownloadingSegment()
- nrCancel := newrelic.StartSegment(ctx, "Downloading image")
- ddCancel := datadog.StartSpan(ctx, "downloading_image")
- cancel := func() {
- promCancel()
- nrCancel()
- ddCancel()
- }
- return cancel
- }
- func StartProcessingSegment(ctx context.Context) context.CancelFunc {
- promCancel := prometheus.StartProcessingSegment()
- nrCancel := newrelic.StartSegment(ctx, "Processing image")
- ddCancel := datadog.StartSpan(ctx, "processing_image")
- cancel := func() {
- promCancel()
- nrCancel()
- ddCancel()
- }
- return cancel
- }
- func SendError(ctx context.Context, errType string, err error) {
- prometheus.IncrementErrorsTotal(errType)
- newrelic.SendError(ctx, err)
- datadog.SendError(ctx, err)
- }
- func SendTimeout(ctx context.Context, d time.Duration) {
- prometheus.IncrementErrorsTotal("timeout")
- newrelic.SendTimeout(ctx, d)
- datadog.SendTimeout(ctx, d)
- }
|