Skip to content
Snippets Groups Projects
Select Git revision
  • 1290c5526a7ad1ac1acf99ca3f0650faecd16f46
  • master default protected
  • refactor/PRXS-3053-Files
  • feature/PRXS-3143-3235-ReferenceOptions
  • feature/PRXS-3421-ImplementNewRefAPI
  • feature/PRXS-3143-LimitReferenceFields
  • feature/PRXS-3234-FeaturePruneIdents
  • feature/3149-LocaleCodeAsID-Feature
  • feature/PRXS-3383-CollectionsRankSortAPI
  • PRXS-3421-RecursiveReferences
  • feature/PRXS-3383-CollectionsSort
  • feature/3109-SerializeFeature
  • release/0.33
  • feature/3109-RecoverySchema
  • feature/3109-feature
  • fix/PRXS-3369-ValidateFields
  • refactor/PRXS-3306-MovePkgGroup1
  • refactor/6-pkg-refactor-expr
  • fix/PRXS-3360-TemplateBuilderPatch
  • feature/3293-MongoV2
  • feature/3272-GoVersionUp
  • v0.33.1
  • v0.32.0
  • v0.31.1
  • v0.31.0
  • v0.30.0
  • v0.29.0
  • v0.28.0
  • v0.27.0-alpha.1+16
  • v0.27.0-alpha.1+15
  • v0.27.0-alpha.1+14
  • v0.27.0-alpha.1+13
  • v0.27.0-alpha.1+12
  • v0.27.0-alpha.1+11
  • v0.27.0-alpha.1+10
  • v0.27.0-alpha.1+9
  • v0.27.0-alpha.1+8
  • v0.27.0-alpha.1+7
  • v0.27.0-alpha.1+6
  • v0.27.0-alpha.1+5
  • v0.27.0-alpha.1+4
41 results

metrics_middleware.go

Blame
  • 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 {