1
0
mirror of https://github.com/imgproxy/imgproxy.git synced 2024-12-09 09:56:01 +02:00
imgproxy/metrics/metrics.go

159 lines
3.6 KiB
Go
Raw Normal View History

2021-04-26 13:52:50 +02:00
package metrics
import (
"context"
"net/http"
2022-12-04 17:01:37 +02:00
"github.com/imgproxy/imgproxy/v3/metrics/cloudwatch"
2021-09-30 16:23:30 +02:00
"github.com/imgproxy/imgproxy/v3/metrics/datadog"
"github.com/imgproxy/imgproxy/v3/metrics/newrelic"
2022-10-06 11:08:23 +02:00
"github.com/imgproxy/imgproxy/v3/metrics/otel"
2021-09-30 16:23:30 +02:00
"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()
2022-10-06 11:08:23 +02:00
if err := otel.Init(); err != nil {
return err
}
2022-12-04 17:01:37 +02:00
if err := cloudwatch.Init(); err != nil {
return err
}
2021-04-26 13:52:50 +02:00
return nil
}
func Stop() {
2022-07-07 15:13:30 +02:00
newrelic.Stop()
2021-04-26 13:52:50 +02:00
datadog.Stop()
2022-10-06 11:08:23 +02:00
otel.Stop()
2022-12-04 17:01:37 +02:00
cloudwatch.Stop()
2021-04-26 13:52:50 +02:00
}
func Enabled() bool {
return prometheus.Enabled() ||
newrelic.Enabled() ||
2022-10-06 11:08:23 +02:00
datadog.Enabled() ||
2022-12-04 17:01:37 +02:00
otel.Enabled() ||
cloudwatch.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)
2022-10-06 11:08:23 +02:00
ctx, otelCancel, rw := otel.StartRootSpan(ctx, rw, r)
2021-04-26 13:52:50 +02:00
cancel := func() {
promCancel()
nrCancel()
ddCancel()
2022-10-06 11:08:23 +02:00
otelCancel()
2021-04-26 13:52:50 +02:00
}
return ctx, cancel, rw
}
func StartQueueSegment(ctx context.Context) context.CancelFunc {
promCancel := prometheus.StartQueueSegment()
nrCancel := newrelic.StartSegment(ctx, "Queue")
ddCancel := datadog.StartSpan(ctx, "queue")
2022-10-06 11:08:23 +02:00
otelCancel := otel.StartSpan(ctx, "queue")
cancel := func() {
promCancel()
nrCancel()
ddCancel()
2022-10-06 11:08:23 +02:00
otelCancel()
}
return cancel
}
2021-04-26 13:52:50 +02:00
func StartDownloadingSegment(ctx context.Context) context.CancelFunc {
promCancel := prometheus.StartDownloadingSegment()
nrCancel := newrelic.StartSegment(ctx, "Downloading image")
ddCancel := datadog.StartSpan(ctx, "downloading_image")
2022-10-06 11:08:23 +02:00
otelCancel := otel.StartSpan(ctx, "downloading_image")
2021-04-26 13:52:50 +02:00
cancel := func() {
promCancel()
nrCancel()
ddCancel()
2022-10-06 11:08:23 +02:00
otelCancel()
2021-04-26 13:52:50 +02:00
}
return cancel
}
func StartProcessingSegment(ctx context.Context) context.CancelFunc {
promCancel := prometheus.StartProcessingSegment()
nrCancel := newrelic.StartSegment(ctx, "Processing image")
ddCancel := datadog.StartSpan(ctx, "processing_image")
2022-10-06 11:08:23 +02:00
otelCancel := otel.StartSpan(ctx, "processing_image")
2021-04-26 13:52:50 +02:00
cancel := func() {
promCancel()
nrCancel()
ddCancel()
2022-10-06 11:08:23 +02:00
otelCancel()
2021-04-26 13:52:50 +02:00
}
return cancel
}
2022-09-07 12:50:21 +02:00
func StartStreamingSegment(ctx context.Context) context.CancelFunc {
promCancel := prometheus.StartStreamingSegment()
nrCancel := newrelic.StartSegment(ctx, "Streaming image")
ddCancel := datadog.StartSpan(ctx, "streaming_image")
2022-10-06 11:08:23 +02:00
otelCancel := otel.StartSpan(ctx, "streaming_image")
2022-09-07 12:50:21 +02:00
cancel := func() {
promCancel()
nrCancel()
ddCancel()
2022-10-06 11:08:23 +02:00
otelCancel()
2022-09-07 12:50:21 +02:00
}
return cancel
}
2021-04-26 13:52:50 +02:00
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)
2022-10-06 11:08:23 +02:00
otel.SendError(ctx, errType, err)
2021-04-26 13:52:50 +02:00
}
2022-07-07 10:17:27 +02:00
func ObserveBufferSize(t string, size int) {
prometheus.ObserveBufferSize(t, size)
2022-07-07 15:13:30 +02:00
newrelic.ObserveBufferSize(t, size)
2022-07-07 10:17:27 +02:00
datadog.ObserveBufferSize(t, size)
2023-05-01 17:05:25 +02:00
otel.ObserveBufferSize(t, size)
2022-12-04 17:01:37 +02:00
cloudwatch.ObserveBufferSize(t, size)
2022-07-07 10:17:27 +02:00
}
func SetBufferDefaultSize(t string, size int) {
prometheus.SetBufferDefaultSize(t, size)
2022-07-07 15:13:30 +02:00
newrelic.SetBufferDefaultSize(t, size)
2022-07-07 10:17:27 +02:00
datadog.SetBufferDefaultSize(t, size)
2023-05-01 17:05:25 +02:00
otel.SetBufferDefaultSize(t, size)
2022-12-04 17:01:37 +02:00
cloudwatch.SetBufferDefaultSize(t, size)
2022-07-07 10:17:27 +02:00
}
func SetBufferMaxSize(t string, size int) {
prometheus.SetBufferMaxSize(t, size)
2022-07-07 15:13:30 +02:00
newrelic.SetBufferMaxSize(t, size)
2022-07-07 10:17:27 +02:00
datadog.SetBufferMaxSize(t, size)
2023-05-01 17:05:25 +02:00
otel.SetBufferMaxSize(t, size)
2022-12-04 17:01:37 +02:00
cloudwatch.SetBufferMaxSize(t, size)
2022-07-07 10:17:27 +02:00
}