Skip to content
Snippets Groups Projects
Select Git revision
  • 93beed6e6a766d566cc581d415ac23935ed6a13f
  • 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

collections.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 {
    		m.requestMetrics.FailedTotal.WithLabelValues("Archive").Inc()
    		return
    	}
    	return
    }
    
    // Create implements items.Items
    func (m metricsMiddleware) Create(ctx context.Context, item *items.Item, opts ...*items.CreateOptions) (created *items.Item, err error) {
    	timer := prometheus.NewTimer(m.requestMetrics.DurationSeconds.WithLabelValues("Create"))
    	defer timer.ObserveDuration()
    	m.requestMetrics.Total.WithLabelValues("Create").Inc()
    	created, err = m.next.Create(ctx, item, opts...)
    	if err != nil {
    		m.requestMetrics.FailedTotal.WithLabelValues("Create").Inc()
    		return
    	}
    	return
    }
    
    // Delete implements items.Items
    func (m metricsMiddleware) Delete(ctx context.Context, item *items.Item, options ...*items.DeleteOptions) (err error) {
    	timer := prometheus.NewTimer(m.requestMetrics.DurationSeconds.WithLabelValues("Delete"))
    	defer timer.ObserveDuration()
    	m.requestMetrics.Total.WithLabelValues("Delete").Inc()
    	err = m.next.Delete(ctx, item, options...)
    	if err != nil {
    		m.requestMetrics.FailedTotal.WithLabelValues("Delete").Inc()
    		return
    	}
    	return
    }
    
    // Find implements items.Items
    func (m metricsMiddleware) Find(ctx context.Context, spaceId string, envId string, collectionId string, filter *items.Filter, options ...*items.FindOptions) (items []*items.Item, total int, err error) {
    	timer := prometheus.NewTimer(m.requestMetrics.DurationSeconds.WithLabelValues("Find"))
    	defer timer.ObserveDuration()
    	m.requestMetrics.Total.WithLabelValues("Find").Inc()
    	items, total, err = m.next.Find(ctx, spaceId, envId, collectionId, filter, options...)
    	if err != nil {
    		m.requestMetrics.FailedTotal.WithLabelValues("Find").Inc()
    		return
    	}
    	return
    }
    
    // FindArchived implements items.Items
    func (m metricsMiddleware) FindArchived(ctx context.Context, spaceId string, envId string, collectionId string, filter *items.Filter, options ...*items.FindArchivedOptions) (items []*items.Item, total int, err error) {
    	timer := prometheus.NewTimer(m.requestMetrics.DurationSeconds.WithLabelValues("FindArchived"))
    	defer timer.ObserveDuration()
    	m.requestMetrics.Total.WithLabelValues("FindArchived").Inc()
    	items, total, err = m.next.FindArchived(ctx, spaceId, envId, collectionId, filter, options...)
    	if err != nil {
    		m.requestMetrics.FailedTotal.WithLabelValues("FindArchived").Inc()
    		return
    	}
    	return
    }
    
    // FindPublished implements items.Items
    func (m metricsMiddleware) FindPublished(ctx context.Context, spaceId string, envId string, collectionId string, filter *items.Filter, options ...*items.FindPublishedOptions) (items []*items.Item, total int, err error) {
    	timer := prometheus.NewTimer(m.requestMetrics.DurationSeconds.WithLabelValues("FindPublished"))
    	defer timer.ObserveDuration()
    	m.requestMetrics.Total.WithLabelValues("FindPublished").Inc()
    	items, total, err = m.next.FindPublished(ctx, spaceId, envId, collectionId, filter, options...)
    	if err != nil {
    		m.requestMetrics.FailedTotal.WithLabelValues("FindPublished").Inc()
    		return
    	}
    	return
    }
    
    // Get implements items.Items
    func (m metricsMiddleware) Get(ctx context.Context, spaceId string, envId string, collectionId string, itemId string, options ...*items.GetOptions) (item *items.Item, err error) {
    	timer := prometheus.NewTimer(m.requestMetrics.DurationSeconds.WithLabelValues("Get"))
    	defer timer.ObserveDuration()
    	m.requestMetrics.Total.WithLabelValues("Get").Inc()
    	item, err = m.next.Get(ctx, spaceId, envId, collectionId, itemId, options...)
    	if err != nil {
    		m.requestMetrics.FailedTotal.WithLabelValues("Get").Inc()
    		return
    	}
    	return
    }
    
    // GetPublished implements items.Items
    func (m metricsMiddleware) GetPublished(ctx context.Context, spaceId string, envId string, collectionId string, itemId string, options ...*items.GetPublishedOptions) (item *items.Item, err error) {
    	timer := prometheus.NewTimer(m.requestMetrics.DurationSeconds.WithLabelValues("GetPublished"))
    	defer timer.ObserveDuration()
    	m.requestMetrics.Total.WithLabelValues("GetPublished").Inc()
    	item, err = m.next.GetPublished(ctx, spaceId, envId, collectionId, itemId, options...)
    	if err != nil {
    		m.requestMetrics.FailedTotal.WithLabelValues("GetPublished").Inc()
    		return
    	}
    	return
    }
    
    // GetRevision implements items.Items
    func (m metricsMiddleware) GetRevision(ctx context.Context, spaceId string, envId string, collectionId string, itemId string, revisionId string, options ...*items.GetRevisionOptions) (item *items.Item, err error) {
    	timer := prometheus.NewTimer(m.requestMetrics.DurationSeconds.WithLabelValues("GetRevision"))
    	defer timer.ObserveDuration()
    	m.requestMetrics.Total.WithLabelValues("GetRevision").Inc()
    	item, err = m.next.GetRevision(ctx, spaceId, envId, collectionId, itemId, revisionId, options...)
    	if err != nil {
    		m.requestMetrics.FailedTotal.WithLabelValues("GetRevision").Inc()
    		return
    	}
    	return
    }
    
    // Introspect implements items.Items
    func (m metricsMiddleware) Introspect(ctx context.Context, item *items.Item, opts ...*items.IntrospectOptions) (itm *items.Item, sch *schema.Schema, err error) {
    	timer := prometheus.NewTimer(m.requestMetrics.DurationSeconds.WithLabelValues("Introspect"))
    	defer timer.ObserveDuration()
    	m.requestMetrics.Total.WithLabelValues("Introspect").Inc()
    	itm, sch, err = m.next.Introspect(ctx, item, opts...)
    	if err != nil {
    		m.requestMetrics.FailedTotal.WithLabelValues("Introspect").Inc()
    		return
    	}
    	return
    }
    
    // ListRevisions implements items.Items
    func (m metricsMiddleware) ListRevisions(ctx context.Context, spaceId string, envId string, collectionId string, itemId string, options ...*items.ListRevisionsOptions) (items []*items.Item, err error) {
    	timer := prometheus.NewTimer(m.requestMetrics.DurationSeconds.WithLabelValues("ListRevisions"))
    	defer timer.ObserveDuration()
    	m.requestMetrics.Total.WithLabelValues("ListRevisions").Inc()
    	items, err = m.next.ListRevisions(ctx, spaceId, envId, collectionId, itemId, options...)
    	if err != nil {
    		m.requestMetrics.FailedTotal.WithLabelValues("ListRevisions").Inc()
    		return
    	}
    	return
    }
    
    // Publish implements items.Items
    func (m metricsMiddleware) Publish(ctx context.Context, item *items.Item, options ...*items.PublishOptions) (err error) {
    	timer := prometheus.NewTimer(m.requestMetrics.DurationSeconds.WithLabelValues("Publish"))
    	defer timer.ObserveDuration()
    	m.requestMetrics.Total.WithLabelValues("Publish").Inc()
    	err = m.next.Publish(ctx, item, options...)
    	if err != nil {
    		m.requestMetrics.FailedTotal.WithLabelValues("Publish").Inc()
    		return
    	}
    	return
    }
    
    // Unarchive implements items.Items
    func (m metricsMiddleware) Unarchive(ctx context.Context, item *items.Item, options ...*items.UnarchiveOptions) (err error) {
    	timer := prometheus.NewTimer(m.requestMetrics.DurationSeconds.WithLabelValues("Unarchive"))
    	defer timer.ObserveDuration()
    	m.requestMetrics.Total.WithLabelValues("Unarchive").Inc()
    	err = m.next.Unarchive(ctx, item, options...)
    	if err != nil {
    		m.requestMetrics.FailedTotal.WithLabelValues("Unarchive").Inc()
    		return
    	}
    	return
    }
    
    // Undelete implements items.Items
    func (m metricsMiddleware) Undelete(ctx context.Context, item *items.Item, options ...*items.UndeleteOptions) (err error) {
    	timer := prometheus.NewTimer(m.requestMetrics.DurationSeconds.WithLabelValues("Undelete"))
    	defer timer.ObserveDuration()
    	m.requestMetrics.Total.WithLabelValues("Undelete").Inc()
    	err = m.next.Undelete(ctx, item, options...)
    	if err != nil {
    		m.requestMetrics.FailedTotal.WithLabelValues("Undelete").Inc()
    		return
    	}
    	return
    }
    
    // Unpublish implements items.Items
    func (m metricsMiddleware) Unpublish(ctx context.Context, item *items.Item, options ...*items.UnpublishOptions) (err error) {
    	timer := prometheus.NewTimer(m.requestMetrics.DurationSeconds.WithLabelValues("Unpublish"))
    	defer timer.ObserveDuration()
    	m.requestMetrics.Total.WithLabelValues("Unpublish").Inc()
    	err = m.next.Unpublish(ctx, item, options...)
    	if err != nil {
    		m.requestMetrics.FailedTotal.WithLabelValues("Unpublish").Inc()
    		return
    	}
    	return
    }
    
    // Update implements items.Items
    func (m metricsMiddleware) Update(ctx context.Context, item *items.Item, options ...*items.UpdateOptions) (err error) {
    	timer := prometheus.NewTimer(m.requestMetrics.DurationSeconds.WithLabelValues("Update"))
    	defer timer.ObserveDuration()
    	m.requestMetrics.Total.WithLabelValues("Update").Inc()
    	err = m.next.Update(ctx, item, options...)
    	if err != nil {
    		m.requestMetrics.FailedTotal.WithLabelValues("Update").Inc()
    		return
    	}
    	return
    }