2021-04-26 13:52:50 +02:00
|
|
|
package metrics
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
|
2021-09-30 16:23:30 +02:00
|
|
|
"github.com/imgproxy/imgproxy/v3/metrics/datadog"
|
|
|
|
"github.com/imgproxy/imgproxy/v3/metrics/newrelic"
|
|
|
|
"github.com/imgproxy/imgproxy/v3/metrics/prometheus"
|
2021-04-26 13:52:50 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func Init() error {
|
|
|
|
prometheus.Init()
|
|
|
|
|
|
|
|
if err := newrelic.Init(); err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
datadog.Init()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func Stop() {
|
|
|
|
datadog.Stop()
|
|
|
|
}
|
|
|
|
|
2022-01-13 20:40:14 +02:00
|
|
|
func Enabled() bool {
|
|
|
|
return prometheus.Enabled() ||
|
|
|
|
newrelic.Enabled() ||
|
|
|
|
datadog.Enabled()
|
|
|
|
}
|
|
|
|
|
2021-04-26 13:52:50 +02:00
|
|
|
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)
|
2022-07-20 11:49:05 +02:00
|
|
|
newrelic.SendError(ctx, errType, err)
|
|
|
|
datadog.SendError(ctx, errType, err)
|
2021-04-26 13:52:50 +02:00
|
|
|
}
|