Select Git revision
telemetry_middleware.go
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)
}