Skip to content
Snippets Groups Projects
Select Git revision
  • b7dd55d7208b3c99901dc77ef15f63a8e2b5cbb2
  • master default protected
  • bugfix/fix-return-var-in-find
  • feature/upgrade2
  • v1.10.0
  • v1.8.2
  • v1.8.1
  • v1.8.0
  • 1.7.3
  • v1.7.1
  • v1.6.1
  • v1.6.0
  • v1.5.0
  • v1.4.1
  • v1.3.0
  • v1.2.2
  • v1.2.1
  • v1.2.0
  • v1.0.1
  • v1.0.0
  • v0.0.23
  • v0.0.17
  • v0.0.10
  • v0.0.9
24 results

setup.py

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 {