From 404bff1dba71a892c1feafdfa95ebc035892ca1b Mon Sep 17 00:00:00 2001
From: ensiouel <ensiouel@gmail.com>
Date: Wed, 20 Dec 2023 19:34:44 +0300
Subject: [PATCH] =?UTF-8?q?refactor:=20=D1=81=D0=B3=D0=B5=D0=BD=D0=B5?=
 =?UTF-8?q?=D1=80=D0=B8=D1=80=D0=BE=D0=B2=D0=B0=D0=BD=20metrics=20middlewa?=
 =?UTF-8?q?re=20=D0=B4=D0=BB=D1=8F=20clients?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 pkg/clients/middleware/metrics_middleware.go | 136 ++++++++++---------
 1 file changed, 73 insertions(+), 63 deletions(-)

diff --git a/pkg/clients/middleware/metrics_middleware.go b/pkg/clients/middleware/metrics_middleware.go
index b96bac06..f3e6207a 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
 }
-- 
GitLab