Select Git revision
telemetry_middleware.go 6.98 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/collaborators -i log -i Service -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/collaborators"
"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 collaborators.Collaborators interface instrumented with opentracing spans
type telemetryMiddleware struct {
collaborators.Collaborators
_instance string
requestMetrics *metrics.RequestMetrics
_spanDecorator func(span trace.Span, params, results map[string]interface{})
}
// TelemetryMiddleware returns telemetryMiddleware
func TelemetryMiddleware(base collaborators.Collaborators, instance string, spanDecorator ...func(span trace.Span, params, results map[string]interface{})) telemetryMiddleware {
requestMetrics, err := metrics.GetRequestMetrics()
if err != nil {
panic(err)
}
d := telemetryMiddleware{
Collaborators: base,
_instance: instance,
requestMetrics: requestMetrics,
}
if len(spanDecorator) > 0 && spanDecorator[0] != nil {
d._spanDecorator = spanDecorator[0]
}
return d
}
// Get implements collaborators.Collaborators
func (_d telemetryMiddleware) Get(ctx context.Context, spaceId string, subject string) (role string, err error) {
attributes := otelmetric.WithAttributeSet(attribute.NewSet(
attribute.String("service", "Collaborators"),
attribute.String("method", "Get"),
))
_d.requestMetrics.Total.Add(ctx, 1, attributes)
start := time.Now()
ctx, _span := otel.Tracer(_d._instance).Start(ctx, "Collaborators.Get")
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,
"subject": subject}, map[string]interface{}{
"role": role,
"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.Collaborators.Get(ctx, spaceId, subject)
}
// ListCollaborators implements collaborators.Collaborators
func (_d telemetryMiddleware) ListCollaborators(ctx context.Context, spaceId string) (collaborators []*collaborators.Collaborator, err error) {
attributes := otelmetric.WithAttributeSet(attribute.NewSet(
attribute.String("service", "Collaborators"),
attribute.String("method", "ListCollaborators"),
))
_d.requestMetrics.Total.Add(ctx, 1, attributes)
start := time.Now()
ctx, _span := otel.Tracer(_d._instance).Start(ctx, "Collaborators.ListCollaborators")
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{}{
"collaborators": collaborators,
"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.Collaborators.ListCollaborators(ctx, spaceId)
}
// ListSpaces implements collaborators.Collaborators
func (_d telemetryMiddleware) ListSpaces(ctx context.Context, subject string) (spaces []*collaborators.Collaborator, err error) {
attributes := otelmetric.WithAttributeSet(attribute.NewSet(
attribute.String("service", "Collaborators"),
attribute.String("method", "ListSpaces"),
))
_d.requestMetrics.Total.Add(ctx, 1, attributes)
start := time.Now()
ctx, _span := otel.Tracer(_d._instance).Start(ctx, "Collaborators.ListSpaces")
defer func() {
_d.requestMetrics.DurationMilliseconds.Record(ctx, time.Since(start).Milliseconds(), attributes)
if _d._spanDecorator != nil {
_d._spanDecorator(_span, map[string]interface{}{
"ctx": ctx,
"subject": subject}, map[string]interface{}{
"spaces": spaces,
"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.Collaborators.ListSpaces(ctx, subject)
}
// Remove implements collaborators.Collaborators
func (_d telemetryMiddleware) Remove(ctx context.Context, spaceId string, subject string) (err error) {
attributes := otelmetric.WithAttributeSet(attribute.NewSet(
attribute.String("service", "Collaborators"),
attribute.String("method", "Remove"),
))
_d.requestMetrics.Total.Add(ctx, 1, attributes)
start := time.Now()
ctx, _span := otel.Tracer(_d._instance).Start(ctx, "Collaborators.Remove")
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,
"subject": subject}, 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.Collaborators.Remove(ctx, spaceId, subject)
}
// Set implements collaborators.Collaborators
func (_d telemetryMiddleware) Set(ctx context.Context, spaceId string, subject string, role string) (err error) {
attributes := otelmetric.WithAttributeSet(attribute.NewSet(
attribute.String("service", "Collaborators"),
attribute.String("method", "Set"),
))
_d.requestMetrics.Total.Add(ctx, 1, attributes)
start := time.Now()
ctx, _span := otel.Tracer(_d._instance).Start(ctx, "Collaborators.Set")
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,
"subject": subject,
"role": role}, 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.Collaborators.Set(ctx, spaceId, subject, role)
}