Skip to content
Snippets Groups Projects
Select Git revision
  • f26dbe0d14f12238555fd617996bed873685acdb
  • master default protected
  • feature/PRXS-3383-CollectionsSort
  • feature/2781-SpacesLoggingMiddleware
  • feature/PRXS-3421-ImplementNewRefAPI
  • feature/PRXS-3143-3235-ReferenceOptions
  • feature/PRXS-3143-LimitReferenceFields
  • feature/PRXS-3234-FeaturePruneIdents
  • PRXS-3421-RecursiveReferences
  • 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
  • feature/PRXS-3218-HideTemplateActions
  • feature/PRXS-3234-PruneIdents
  • 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

telemetry_middleware.go

Blame
  • telemetry_middleware.go 4.57 KiB
    // Code generated by gowrap. DO NOT EDIT.
    // template: ..\..\..\assets\templates\middleware\telemetry
    // gowrap: http://github.com/hexdigest/gowrap
    
    package middleware
    
    //go:generate gowrap gen -p git.perx.ru/perxis/perxis-go/pkg/locales -i Locales -t ..\..\..\assets\templates\middleware\telemetry -o telemetry_middleware.go -l ""
    
    // source template: https://github.com/hexdigest/gowrap/blob/master/templates/opentelemetry
    
    import (
    	"context"
    	"time"
    
    	"git.perx.ru/perxis/perxis-go/pkg/locales"
    	"git.perx.ru/perxis/perxis-go/pkg/telemetry/metrics"
    	"go.opentelemetry.io/otel"
    	"go.opentelemetry.io/otel/attribute"
    	otelmetric "go.opentelemetry.io/otel/metric"
    	"go.opentelemetry.io/otel/trace"
    )
    
    // telemetryMiddleware implements locales.Locales interface instrumented with opentracing spans
    type telemetryMiddleware struct {
    	locales.Locales
    	_instance      string
    	requestMetrics *metrics.RequestMetrics
    	_spanDecorator func(span trace.Span, params, results map[string]interface{})
    }
    
    // TelemetryMiddleware returns telemetryMiddleware
    func TelemetryMiddleware(base locales.Locales, instance string, spanDecorator ...func(span trace.Span, params, results map[string]interface{})) telemetryMiddleware {
    	requestMetrics, err := metrics.GetRequestMetrics()
    	if err != nil {
    		panic(err)
    	}
    
    	d := telemetryMiddleware{
    		Locales:        base,
    		_instance:      instance,
    		requestMetrics: requestMetrics,
    	}
    
    	if len(spanDecorator) > 0 && spanDecorator[0] != nil {
    		d._spanDecorator = spanDecorator[0]
    	}
    
    	return d
    }
    
    // Create implements locales.Locales
    func (_d telemetryMiddleware) Create(ctx context.Context, locale *locales.Locale) (created *locales.Locale, err error) {
    	attributes := otelmetric.WithAttributeSet(attribute.NewSet(
    		attribute.String("service", "Locales"),
    		attribute.String("method", "Create"),
    	))
    
    	_d.requestMetrics.Total.Add(ctx, 1, attributes)
    
    	start := time.Now()
    	ctx, _span := otel.Tracer(_d._instance).Start(ctx, "Locales.Create")
    
    	defer func() {
    		_d.requestMetrics.DurationMilliseconds.Record(ctx, time.Since(start).Milliseconds(), attributes)
    
    		if _d._spanDecorator != nil {
    			_d._spanDecorator(_span, map[string]interface{}{
    				"ctx":    ctx,
    				"locale": locale}, map[string]interface{}{
    				"created": created,
    				"err":     err})
    		} else if err != nil {
    			_d.requestMetrics.FailedTotal.Add(ctx, 1, attributes)
    
    			_span.RecordError(err)
    			_span.SetAttributes(attribute.String("event", "error"))
    			_span.SetAttributes(attribute.String("message", err.Error()))
    		}
    
    		_span.End()
    	}()
    	return _d.Locales.Create(ctx, locale)
    }
    
    // Delete implements locales.Locales
    func (_d telemetryMiddleware) Delete(ctx context.Context, spaceId string, localeId string) (err error) {
    	attributes := otelmetric.WithAttributeSet(attribute.NewSet(
    		attribute.String("service", "Locales"),
    		attribute.String("method", "Delete"),
    	))
    
    	_d.requestMetrics.Total.Add(ctx, 1, attributes)
    
    	start := time.Now()
    	ctx, _span := otel.Tracer(_d._instance).Start(ctx, "Locales.Delete")
    
    	defer func() {
    		_d.requestMetrics.DurationMilliseconds.Record(ctx, time.Since(start).Milliseconds(), attributes)
    
    		if _d._spanDecorator != nil {
    			_d._spanDecorator(_span, map[string]interface{}{
    				"ctx":      ctx,
    				"spaceId":  spaceId,
    				"localeId": localeId}, map[string]interface{}{
    				"err": err})
    		} else if err != nil {
    			_d.requestMetrics.FailedTotal.Add(ctx, 1, attributes)
    
    			_span.RecordError(err)
    			_span.SetAttributes(attribute.String("event", "error"))
    			_span.SetAttributes(attribute.String("message", err.Error()))
    		}
    
    		_span.End()
    	}()
    	return _d.Locales.Delete(ctx, spaceId, localeId)
    }
    
    // List implements locales.Locales
    func (_d telemetryMiddleware) List(ctx context.Context, spaceId string) (locales []*locales.Locale, err error) {
    	attributes := otelmetric.WithAttributeSet(attribute.NewSet(
    		attribute.String("service", "Locales"),
    		attribute.String("method", "List"),
    	))
    
    	_d.requestMetrics.Total.Add(ctx, 1, attributes)
    
    	start := time.Now()
    	ctx, _span := otel.Tracer(_d._instance).Start(ctx, "Locales.List")
    
    	defer func() {
    		_d.requestMetrics.DurationMilliseconds.Record(ctx, time.Since(start).Milliseconds(), attributes)
    
    		if _d._spanDecorator != nil {
    			_d._spanDecorator(_span, map[string]interface{}{
    				"ctx":     ctx,
    				"spaceId": spaceId}, map[string]interface{}{
    				"locales": locales,
    				"err":     err})
    		} else if err != nil {
    			_d.requestMetrics.FailedTotal.Add(ctx, 1, attributes)
    
    			_span.RecordError(err)
    			_span.SetAttributes(attribute.String("event", "error"))
    			_span.SetAttributes(attribute.String("message", err.Error()))
    		}
    
    		_span.End()
    	}()
    	return _d.Locales.List(ctx, spaceId)
    }