diff --git a/pkg/clients/middleware/metrics_middleware.go b/pkg/clients/middleware/metrics_middleware.go index b96bac0685734880625dda7566588bc93d2345c1..f3e6207ae34a8eb12d5c684a60be392c9d813968 100644 --- a/pkg/clients/middleware/metrics_middleware.go +++ b/pkg/clients/middleware/metrics_middleware.go @@ -8,6 +8,7 @@ package middleware import ( "context" + "strings" "git.perx.ru/perxis/perxis-go/pkg/clients" "git.perx.ru/perxis/perxis-go/pkg/metrics" @@ -16,6 +17,7 @@ import ( // metricsMiddleware implements clients.Clients that is instrumented with metrics type metricsMiddleware struct { + serviceName string requestMetrics *metrics.RequestMetrics next clients.Clients } @@ -24,6 +26,7 @@ type metricsMiddleware struct { func MetricsMiddleware(requestMetrics *metrics.RequestMetrics) Middleware { return func(next clients.Clients) clients.Clients { return &metricsMiddleware{ + serviceName: strings.ToLower("Clients"), requestMetrics: requestMetrics, next: next, } @@ -32,91 +35,98 @@ func MetricsMiddleware(requestMetrics *metrics.RequestMetrics) Middleware { // Create implements clients.Clients func (m metricsMiddleware) Create(ctx context.Context, client *clients.Client) (created *clients.Client, err error) { - m.requestMetrics.Total.WithLabelValues("Create").Inc() - timer := prometheus.NewTimer(m.requestMetrics.DurationSeconds.WithLabelValues("Create")) - defer func() { - timer.ObserveDuration() - if err != nil { - m.requestMetrics.FailedTotal.WithLabelValues("Create").Inc() - } - }() - return m.next.Create(ctx, client) + labels := prometheus.Labels{"service": m.serviceName, "method": "Create"} + timer := prometheus.NewTimer(m.requestMetrics.DurationSeconds.With(labels)) + defer timer.ObserveDuration() + m.requestMetrics.Total.With(labels).Inc() + created, err = m.next.Create(ctx, client) + if err != nil { + m.requestMetrics.FailedTotal.With(labels).Inc() + return + } + return } // Delete implements clients.Clients func (m metricsMiddleware) Delete(ctx context.Context, spaceId string, id string) (err error) { - m.requestMetrics.Total.WithLabelValues("Delete").Inc() - timer := prometheus.NewTimer(m.requestMetrics.DurationSeconds.WithLabelValues("Delete")) - defer func() { - timer.ObserveDuration() - if err != nil { - m.requestMetrics.FailedTotal.WithLabelValues("Delete").Inc() - } - }() - return m.next.Delete(ctx, spaceId, id) + labels := prometheus.Labels{"service": m.serviceName, "method": "Delete"} + timer := prometheus.NewTimer(m.requestMetrics.DurationSeconds.With(labels)) + defer timer.ObserveDuration() + m.requestMetrics.Total.With(labels).Inc() + err = m.next.Delete(ctx, spaceId, id) + if err != nil { + m.requestMetrics.FailedTotal.With(labels).Inc() + return + } + return } // Enable implements clients.Clients func (m metricsMiddleware) Enable(ctx context.Context, spaceId string, id string, enable bool) (err error) { - m.requestMetrics.Total.WithLabelValues("Enable").Inc() - timer := prometheus.NewTimer(m.requestMetrics.DurationSeconds.WithLabelValues("Enable")) - defer func() { - timer.ObserveDuration() - if err != nil { - m.requestMetrics.FailedTotal.WithLabelValues("Enable").Inc() - } - }() - return m.next.Enable(ctx, spaceId, id, enable) + labels := prometheus.Labels{"service": m.serviceName, "method": "Enable"} + timer := prometheus.NewTimer(m.requestMetrics.DurationSeconds.With(labels)) + defer timer.ObserveDuration() + m.requestMetrics.Total.With(labels).Inc() + err = m.next.Enable(ctx, spaceId, id, enable) + if err != nil { + m.requestMetrics.FailedTotal.With(labels).Inc() + return + } + return } // Get implements clients.Clients func (m metricsMiddleware) Get(ctx context.Context, spaceId string, id string) (client *clients.Client, err error) { - m.requestMetrics.Total.WithLabelValues("Get").Inc() - timer := prometheus.NewTimer(m.requestMetrics.DurationSeconds.WithLabelValues("Get")) - defer func() { - timer.ObserveDuration() - if err != nil { - m.requestMetrics.FailedTotal.WithLabelValues("Get").Inc() - } - }() - return m.next.Get(ctx, spaceId, id) + labels := prometheus.Labels{"service": m.serviceName, "method": "Get"} + timer := prometheus.NewTimer(m.requestMetrics.DurationSeconds.With(labels)) + defer timer.ObserveDuration() + m.requestMetrics.Total.With(labels).Inc() + client, err = m.next.Get(ctx, spaceId, id) + if err != nil { + m.requestMetrics.FailedTotal.With(labels).Inc() + return + } + return } // GetBy implements clients.Clients func (m metricsMiddleware) GetBy(ctx context.Context, spaceId string, params *clients.GetByParams) (client *clients.Client, err error) { - m.requestMetrics.Total.WithLabelValues("GetBy").Inc() - timer := prometheus.NewTimer(m.requestMetrics.DurationSeconds.WithLabelValues("GetBy")) - defer func() { - timer.ObserveDuration() - if err != nil { - m.requestMetrics.FailedTotal.WithLabelValues("GetBy").Inc() - } - }() - return m.next.GetBy(ctx, spaceId, params) + labels := prometheus.Labels{"service": m.serviceName, "method": "GetBy"} + timer := prometheus.NewTimer(m.requestMetrics.DurationSeconds.With(labels)) + defer timer.ObserveDuration() + m.requestMetrics.Total.With(labels).Inc() + client, err = m.next.GetBy(ctx, spaceId, params) + if err != nil { + m.requestMetrics.FailedTotal.With(labels).Inc() + return + } + return } // List implements clients.Clients func (m metricsMiddleware) List(ctx context.Context, spaceId string) (clients []*clients.Client, err error) { - m.requestMetrics.Total.WithLabelValues("List").Inc() - timer := prometheus.NewTimer(m.requestMetrics.DurationSeconds.WithLabelValues("List")) - defer func() { - timer.ObserveDuration() - if err != nil { - m.requestMetrics.FailedTotal.WithLabelValues("List").Inc() - } - }() - return m.next.List(ctx, spaceId) + labels := prometheus.Labels{"service": m.serviceName, "method": "List"} + timer := prometheus.NewTimer(m.requestMetrics.DurationSeconds.With(labels)) + defer timer.ObserveDuration() + m.requestMetrics.Total.With(labels).Inc() + clients, err = m.next.List(ctx, spaceId) + if err != nil { + m.requestMetrics.FailedTotal.With(labels).Inc() + return + } + return } // Update implements clients.Clients func (m metricsMiddleware) Update(ctx context.Context, client *clients.Client) (err error) { - m.requestMetrics.Total.WithLabelValues("Update").Inc() - timer := prometheus.NewTimer(m.requestMetrics.DurationSeconds.WithLabelValues("Update")) - defer func() { - timer.ObserveDuration() - if err != nil { - m.requestMetrics.FailedTotal.WithLabelValues("Update").Inc() - } - }() - return m.next.Update(ctx, client) + labels := prometheus.Labels{"service": m.serviceName, "method": "Update"} + timer := prometheus.NewTimer(m.requestMetrics.DurationSeconds.With(labels)) + defer timer.ObserveDuration() + m.requestMetrics.Total.With(labels).Inc() + err = m.next.Update(ctx, client) + if err != nil { + m.requestMetrics.FailedTotal.With(labels).Inc() + return + } + return }