Skip to content
Snippets Groups Projects
Commit 90c95329 authored by ensiouel's avatar ensiouel
Browse files

cache.MetricsMiddleware переименован в cache.TelemetryMiddleware и больше не...

cache.MetricsMiddleware переименован в cache.TelemetryMiddleware и больше не принимает *metrics.CacheMetrics, а генерирует его внутри
parent c44ce410
No related branches found
No related tags found
No related merge requests found
...@@ -8,26 +8,31 @@ import ( ...@@ -8,26 +8,31 @@ import (
otelmetric "go.opentelemetry.io/otel/metric" otelmetric "go.opentelemetry.io/otel/metric"
) )
type metricsMiddleware struct { type telemetryMiddleware struct {
next Cache next Cache
cacheMetrics *metrics.CacheMetrics cacheMetrics *metrics.CacheMetrics
attributes otelmetric.MeasurementOption attributes otelmetric.MeasurementOption
} }
// MetricsMiddleware возвращает обертку над кэшем, которая используется для отслеживания количества хитов и промахов в кэше. // TelemetryMiddleware возвращает обертку над кэшем, которая используется для отслеживания количества хитов и промахов в кэше.
func MetricsMiddleware(next Cache, cacheMetrics *metrics.CacheMetrics, keyValues ...attribute.KeyValue) Cache { func TelemetryMiddleware(next Cache, scope string, keyValues ...attribute.KeyValue) Cache {
return &metricsMiddleware{ cacheMetrics, err := metrics.NewCacheMetrics(scope)
if err != nil {
panic(err)
}
return &telemetryMiddleware{
next: next, next: next,
cacheMetrics: cacheMetrics, cacheMetrics: cacheMetrics,
attributes: otelmetric.WithAttributes(keyValues...), attributes: otelmetric.WithAttributes(keyValues...),
} }
} }
func (c *metricsMiddleware) Set(key, value any) error { func (c *telemetryMiddleware) Set(key, value any) error {
return c.next.Set(key, value) return c.next.Set(key, value)
} }
func (c *metricsMiddleware) Get(key any) (any, error) { func (c *telemetryMiddleware) Get(key any) (any, error) {
value, err := c.next.Get(key) value, err := c.next.Get(key)
if err != nil { if err != nil {
c.cacheMetrics.MissesTotal.Add(context.TODO(), 1, c.attributes) c.cacheMetrics.MissesTotal.Add(context.TODO(), 1, c.attributes)
...@@ -37,7 +42,7 @@ func (c *metricsMiddleware) Get(key any) (any, error) { ...@@ -37,7 +42,7 @@ func (c *metricsMiddleware) Get(key any) (any, error) {
return value, nil return value, nil
} }
func (c *metricsMiddleware) Remove(key any) error { func (c *telemetryMiddleware) Remove(key any) error {
c.cacheMetrics.InvalidatesTotal.Add(context.TODO(), 1, c.attributes) c.cacheMetrics.InvalidatesTotal.Add(context.TODO(), 1, c.attributes)
return c.next.Remove(key) return c.next.Remove(key)
} }
...@@ -12,32 +12,29 @@ type CacheMetrics struct { ...@@ -12,32 +12,29 @@ type CacheMetrics struct {
} }
func NewCacheMetrics(scope string) (*CacheMetrics, error) { func NewCacheMetrics(scope string) (*CacheMetrics, error) {
meter := otel.Meter(scope) var (
meter = otel.Meter(scope)
cacheMetrics *CacheMetrics
err error
)
hitsTotal, err := meter.Int64Counter("cache_hits", if cacheMetrics.HitsTotal, err = meter.Int64Counter("cache_hits",
otelmetric.WithDescription("Количество найденных в кэше значений"), otelmetric.WithDescription("Количество найденных в кэше значений"),
) ); err != nil {
if err != nil {
return nil, err return nil, err
} }
missesTotal, err := meter.Int64Counter("cache_misses", if cacheMetrics.MissesTotal, err = meter.Int64Counter("cache_misses",
otelmetric.WithDescription("Количество не найденных в кэше значений"), otelmetric.WithDescription("Количество не найденных в кэше значений"),
) ); err != nil {
if err != nil {
return nil, err return nil, err
} }
invalidatesTotal, err := meter.Int64Counter("cache_invalidates", if cacheMetrics.InvalidatesTotal, err = meter.Int64Counter("cache_invalidates",
otelmetric.WithDescription("Количество инвалидаций кэша"), otelmetric.WithDescription("Количество инвалидаций кэша"),
) ); err != nil {
if err != nil {
return nil, err return nil, err
} }
return &CacheMetrics{ return cacheMetrics, nil
HitsTotal: hitsTotal,
MissesTotal: missesTotal,
InvalidatesTotal: invalidatesTotal,
}, nil
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment