From 95ce5d74854dd2819bba2e630eaffa099caa90d3 Mon Sep 17 00:00:00 2001
From: ensiouel <ensiouel@gmail.com>
Date: Thu, 21 Dec 2023 19:40:18 +0300
Subject: [PATCH] =?UTF-8?q?refactor:=20=D1=82=D0=B5=D0=BF=D0=B5=D1=80?=
 =?UTF-8?q?=D1=8C=20=D1=81=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD=D0=B8=D0=B5=20?=
 =?UTF-8?q?=D0=BC=D0=B5=D1=82=D1=80=D0=B8=D0=BA=20=D0=B2=D0=BE=D0=B7=D0=B2?=
 =?UTF-8?q?=D1=80=D0=B0=D1=89=D0=B0=D0=B5=D1=82=20=D0=BE=D1=88=D0=B8=D0=B1?=
 =?UTF-8?q?=D0=BA=D1=83=20=D0=B2=D1=82=D0=BE=D1=80=D1=8B=D0=BC=20=D0=BF?=
 =?UTF-8?q?=D0=B0=D1=80=D0=B0=D0=BC=D0=B5=D1=82=D1=80=D0=BE=D0=BC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 pkg/metrics/cache.go   | 45 +++++++++++++++++++++++---------------
 pkg/metrics/request.go | 49 ++++++++++++++++++++++++++----------------
 2 files changed, 59 insertions(+), 35 deletions(-)

diff --git a/pkg/metrics/cache.go b/pkg/metrics/cache.go
index d4979e0f..2104a38c 100644
--- a/pkg/metrics/cache.go
+++ b/pkg/metrics/cache.go
@@ -1,7 +1,6 @@
 package metrics
 
 import (
-	"git.perx.ru/perxis/perxis-go/pkg/optional"
 	"go.opentelemetry.io/otel"
 	metricotel "go.opentelemetry.io/otel/metric"
 )
@@ -12,21 +11,33 @@ type CacheMetrics struct {
 	InvalidatesTotal metricotel.Int64Counter
 }
 
-func NewCacheMetrics(subsystem string) *CacheMetrics {
-	meter := otel.Meter(subsystem)
-	metrics := &CacheMetrics{
-		HitsTotal: optional.Must(meter.Int64Counter(
-			"cache_hits_total",
-			metricotel.WithDescription("Количество найденных в кэше значений"),
-		)),
-		MissesTotal: optional.Must(meter.Int64Counter(
-			"cache_misses_total",
-			metricotel.WithDescription("Количество не найденных в кэше значений"),
-		)),
-		InvalidatesTotal: optional.Must(meter.Int64Counter(
-			"cache_invalidates_total",
-			metricotel.WithDescription("Количество инвалидаций кэша"),
-		)),
+func NewCacheMetrics(scope string) (*CacheMetrics, error) {
+	meter := otel.Meter(scope)
+
+	hitsTotal, err := meter.Int64Counter("cache_hits",
+		metricotel.WithDescription("Количество найденных в кэше значений"),
+	)
+	if err != nil {
+		return nil, err
+	}
+
+	missesTotal, err := meter.Int64Counter("cache_misses",
+		metricotel.WithDescription("Количество не найденных в кэше значений"),
+	)
+	if err != nil {
+		return nil, err
 	}
-	return metrics
+
+	invalidatesTotal, err := meter.Int64Counter("cache_invalidates",
+		metricotel.WithDescription("Количество инвалидаций кэша"),
+	)
+	if err != nil {
+		return nil, err
+	}
+
+	return &CacheMetrics{
+		HitsTotal:        hitsTotal,
+		MissesTotal:      missesTotal,
+		InvalidatesTotal: invalidatesTotal,
+	}, nil
 }
diff --git a/pkg/metrics/request.go b/pkg/metrics/request.go
index 98fbc420..fb21e66c 100644
--- a/pkg/metrics/request.go
+++ b/pkg/metrics/request.go
@@ -1,7 +1,6 @@
 package metrics
 
 import (
-	"git.perx.ru/perxis/perxis-go/pkg/optional"
 	"go.opentelemetry.io/otel"
 	metricotel "go.opentelemetry.io/otel/metric"
 )
@@ -13,22 +12,36 @@ type RequestMetrics struct {
 }
 
 // NewRequestMetrics возвращает метрики для подсчета количества удачных/неудачных запросов, а так же длительности ответов.
-func NewRequestMetrics(subsystem string) *RequestMetrics {
-	meter := otel.Meter(subsystem)
-	metrics := &RequestMetrics{
-		Total: optional.Must(meter.Int64Counter(
-			"requests_total",
-			metricotel.WithDescription("Количество запросов"),
-		)),
-		FailedTotal: optional.Must(meter.Int64Counter(
-			"requests_failed_total",
-			metricotel.WithDescription("Количество запросов, вернувших ошибку"),
-		)),
-		DurationMilliseconds: optional.Must(meter.Int64Histogram(
-			"request_duration",
-			metricotel.WithDescription("Длительность обработки запроса"),
-			metricotel.WithUnit("ms"),
-		)),
+//
+// Для RequestMetrics.DurationMilliseconds значения buckets по умолчанию равно []float64{0, 5, 10, 25, 50, 75, 100, 250, 500, 750, 1000, 2500, 5000, 7500, 10000}
+func NewRequestMetrics(scope string) (*RequestMetrics, error) {
+	meter := otel.Meter(scope)
+
+	total, err := meter.Int64Counter("requests",
+		metricotel.WithDescription("Количество запросов"),
+	)
+	if err != nil {
+		return nil, err
+	}
+
+	failedTotal, err := meter.Int64Counter("requests_failed",
+		metricotel.WithDescription("Количество запросов, вернувших ошибку"),
+	)
+	if err != nil {
+		return nil, err
 	}
-	return metrics
+
+	durationMilliseconds, err := meter.Int64Histogram("request_duration",
+		metricotel.WithDescription("Длительность обработки запроса"),
+		metricotel.WithUnit("ms"),
+	)
+	if err != nil {
+		return nil, err
+	}
+
+	return &RequestMetrics{
+		Total:                total,
+		FailedTotal:          failedTotal,
+		DurationMilliseconds: durationMilliseconds,
+	}, nil
 }
-- 
GitLab