diff --git a/pkg/metrics/cache.go b/pkg/metrics/cache.go index d4979e0f8ea12ea9f00dbacf6691fa73f904db9a..2104a38c1153a91bf0f4c62811da5e711c70ff08 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 98fbc42012d0f7245c2156c3d3c83c51aaa5ba99..fb21e66c300c12b9d90fc3870b86de550625f61e 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 }