Select Git revision
metrics_middleware.go
metrics_middleware.go 11.33 KiB
// Code generated by gowrap. DO NOT EDIT.
// template: ../../../assets/templates/middleware/metrics
// gowrap: http://github.com/hexdigest/gowrap
package middleware
//go:generate gowrap gen -p git.perx.ru/perxis/perxis-go/pkg/items -i Items -t ../../../assets/templates/middleware/metrics -o metrics_middleware.go -l ""
import (
"context"
"strings"
"git.perx.ru/perxis/perxis-go/pkg/items"
"git.perx.ru/perxis/perxis-go/pkg/metrics"
"git.perx.ru/perxis/perxis-go/pkg/schema"
"github.com/prometheus/client_golang/prometheus"
)
// metricsMiddleware implements items.Items that is instrumented with metrics
type metricsMiddleware struct {
serviceName string
requestMetrics *metrics.RequestMetrics
next items.Items
}
// MetricsMiddleware instruments an implementation of the metricsMiddleware with metrics
func MetricsMiddleware(requestMetrics *metrics.RequestMetrics) Middleware {
return func(next items.Items) items.Items {
return &metricsMiddleware{
serviceName: strings.ToLower("Items"), // эта строка для генерации по шаблону для всех сервисов, там ServiceName с заглавной буквы
requestMetrics: requestMetrics,
next: next,
}
}
}
// Aggregate implements items.Items
func (m metricsMiddleware) Aggregate(ctx context.Context, spaceId string, envId string, collectionId string, filter *items.Filter, options ...*items.AggregateOptions) (result map[string]interface{}, err error) {
timer := prometheus.NewTimer(m.requestMetrics.DurationSeconds.WithLabelValues("Aggregate"))
defer timer.ObserveDuration()
m.requestMetrics.Total.With(prometheus.Labels{"service": m.serviceName, "method": "Aggregate"}).Inc() // тут как больше нравится. Мне нравится более явно
// m.requestMetrics.Total.WithLabelValues(m.serviceName, "Aggregate").Inc()
result, err = m.next.Aggregate(ctx, spaceId, envId, collectionId, filter, options...)
if err != nil {
m.requestMetrics.FailedTotal.WithLabelValues("Aggregate").Inc()
return
}
return
}
// AggregatePublished implements items.Items
func (m metricsMiddleware) AggregatePublished(ctx context.Context, spaceId string, envId string, collectionId string, filter *items.Filter, options ...*items.AggregatePublishedOptions) (result map[string]interface{}, err error) {
timer := prometheus.NewTimer(m.requestMetrics.DurationSeconds.WithLabelValues("AggregatePublished"))
defer timer.ObserveDuration()
m.requestMetrics.Total.WithLabelValues("AggregatePublished").Inc()
result, err = m.next.AggregatePublished(ctx, spaceId, envId, collectionId, filter, options...)
if err != nil {
m.requestMetrics.FailedTotal.WithLabelValues("AggregatePublished").Inc()
return
}
return
}
// Archive implements items.Items
func (m metricsMiddleware) Archive(ctx context.Context, item *items.Item, options ...*items.ArchiveOptions) (err error) {
timer := prometheus.NewTimer(m.requestMetrics.DurationSeconds.WithLabelValues("Archive"))
defer timer.ObserveDuration()
m.requestMetrics.Total.WithLabelValues("Archive").Inc()
err = m.next.Archive(ctx, item, options...)
if err != nil {