Skip to content
Snippets Groups Projects
Commit 097c706f authored by Alex Petraky's avatar Alex Petraky :basketball_player_tone1: Committed by Pavel Antonov
Browse files

feat: Добавлены в метрики по пользователям/приложениям и пространствам

Close #PRXS-2852
parent e58fe356
Branches
Tags
No related merge requests found
Showing
with 1690 additions and 145 deletions
// source template: https://github.com/hexdigest/gowrap/blob/master/templates/opentelemetry
import (
"context"
"time"
"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"
)
{{ $decorator := (or .Vars.DecoratorName "telemetryMiddleware") }}
{{ $funcName := (or .Vars.FuncName ("TelemetryMiddleware")) }}
// {{$decorator}} implements {{.Interface.Type}} interface instrumented with opentracing spans
type {{$decorator}} struct {
{{.Interface.Type}}
_instance string
requestMetrics *metrics.RequestMetrics
_spanDecorator func(span trace.Span, params, results map[string]interface{})
}
// {{$funcName}} returns {{$decorator}}
func {{$funcName}} (base {{.Interface.Type}}, instance string, spanDecorator ...func(span trace.Span, params, results map[string]interface{})) {{$decorator}} {
requestMetrics, err := metrics.GetRequestMetrics()
if err != nil {
panic(err)
}
d := {{$decorator}} {
{{.Interface.Name}}: base,
_instance: instance,
requestMetrics: requestMetrics,
}
if len(spanDecorator) > 0 && spanDecorator[0] != nil {
d._spanDecorator = spanDecorator[0]
}
return d
}
{{range $method := .Interface.Methods}}
{{if $method.AcceptsContext}}
// {{$method.Name}} implements {{$.Interface.Type}}
func (_d {{$decorator}}) {{$method.Declaration}} {
attributes := otelmetric.WithAttributeSet(attribute.NewSet(
attribute.String("service", "{{ $.Interface.Name }}"),
attribute.String("method", "{{ $method.Name }}"),
attribute.String("principal", auth.GetPrincipal(ctx).GetID(ctx)),
))
_d.requestMetrics.Total.Add(ctx, 1, attributes)
start := time.Now()
ctx, _span := otel.Tracer(_d._instance).Start(ctx, "{{$.Interface.Name}}.{{$method.Name}}")
defer func() {
_d.requestMetrics.DurationMilliseconds.Record(ctx, time.Since(start).Milliseconds(), attributes)
if _d._spanDecorator != nil {
_d._spanDecorator(_span, {{$method.ParamsMap}}, {{$method.ResultsMap}})
}{{- if $method.ReturnsError}} 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()))
}
{{end}}
_span.End()
}()
{{$method.Pass (printf "_d.%s." $.Interface.Name) }}
}
{{end}}
{{end}}
\ No newline at end of file
// source template: https://github.com/hexdigest/gowrap/blob/master/templates/opentelemetry
import (
"context"
"time"
oid "git.perx.ru/perxis/perxis-go/id"
"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"
)
type SpaceGetter interface {
GetSpaceID() string
}
{{ $decorator := (or .Vars.DecoratorName "telemetryMiddleware") }}
{{ $funcName := (or .Vars.FuncName ("TelemetryMiddleware")) }}
// {{$decorator}} implements {{.Interface.Type}} interface instrumented with opentracing spans
type {{$decorator}} struct {
{{.Interface.Type}}
_instance string
requestMetrics *metrics.RequestMetrics
_spanDecorator func(span trace.Span, params, results map[string]interface{})
}
// {{$funcName}} returns {{$decorator}}
func {{$funcName}} (base {{.Interface.Type}}, instance string, spanDecorator ...func(span trace.Span, params, results map[string]interface{})) {{$decorator}} {
requestMetrics, err := metrics.GetRequestMetrics()
if err != nil {
panic(err)
}
d := {{$decorator}} {
{{.Interface.Name}}: base,
_instance: instance,
requestMetrics: requestMetrics,
}
if len(spanDecorator) > 0 && spanDecorator[0] != nil {
d._spanDecorator = spanDecorator[0]
}
return d
}
{{- $paramsList := list -}}
{{range $method := .Interface.Methods}}
{{if $method.AcceptsContext}}
{{- $paramsList = list -}}
{{- $paramsList = append $paramsList $method.ParamsNames -}}
{{- $paramsList = append $paramsList $method.ResultsNames -}}
// {{$method.Name}} implements {{$.Interface.Type}}
func (_d {{$decorator}}) {{$method.Declaration}} {
{{ $spaceID := "" }}
{{- range $param := $method.Params -}}
{{- if (eq $param.Name "spaceId") -}}
{{- $spaceID = "spaceId" -}}
{{- end -}}
{{- end -}}
var att = []attribute.KeyValue{
attribute.String("service", "{{ $.Interface.Name }}"),
attribute.String("method", "{{ $method.Name }}"),
}
attributes := otelmetric.WithAttributeSet(attribute.NewSet(att...))
_d.requestMetrics.Total.Add(ctx, 1, attributes)
start := time.Now()
ctx, _span := otel.Tracer(_d._instance).Start(ctx, "{{$.Interface.Name}}.{{$method.Name}}")
defer func() {
_d.requestMetrics.DurationMilliseconds.Record(ctx, time.Since(start).Milliseconds(), attributes)
var spID string
{{- if (eq $spaceID "") -}}
{{- $l := len $paramsList }}
vv := []interface{}{
{{- range $i, $p := $paramsList | toStrings }}
{{- if (eq $i $l) }}
{{- $p }}
{{- else }}
{{- $p }},
{{- end -}}
{{- end -}}
}
for _, v := range vv {
if v == nil {
continue
}
if sg, ok := v.(SpaceGetter); ok {
spID = sg.GetSpaceID()
break
}
}
{{- else }}
spID = spaceId
{{- end }}
if spID != "" {
principal := auth.GetPrincipal(ctx)
if accessor, ok := principal.(auth.SpaceAccessor); ok {
principal = accessor.Space(spID)
}
caller, _ := oid.NewObjectId(principal)
att = append(att, attribute.String("spaceID", spID))
att = append(att, attribute.String("caller", caller.String()))
}
_d.requestMetrics.Total.Add(ctx, 1, otelmetric.WithAttributeSet(attribute.NewSet(att...)))
if _d._spanDecorator != nil {
_d._spanDecorator(_span, {{$method.ParamsMap}}, {{$method.ResultsMap}})
}{{- if $method.ReturnsError}} 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()))
}
{{end}}
_span.End()
}()
{{$method.Pass (printf "_d.%s." $.Interface.Name) }}
}
{{end}}
{{end}}
\ No newline at end of file
// Code generated by gowrap. DO NOT EDIT.
// template: ..\..\assets\templates\middleware\telemetry
// template: ../../assets/templates/middleware/telemetry
// gowrap: http://github.com/hexdigest/gowrap
package middleware
//go:generate gowrap gen -p git.perx.ru/perxis/perxis-go/images -i Images -t ..\..\assets\templates\middleware\telemetry -o telemetry_middleware.go -l ""
//go:generate gowrap gen -p git.perx.ru/perxis/perxis-go/images -i Images -t ../../assets/templates/middleware/telemetry -o telemetry_middleware.go -l ""
// source template: https://github.com/hexdigest/gowrap/blob/master/templates/opentelemetry
......
......@@ -50,6 +50,10 @@ func (c Client) GetID() string {
return c.ID
}
func (c Client) GetSpaceID() string {
return c.SpaceID
}
func (c *Client) SetDisabled(b bool) *Client {
c.Disabled = &b
return c
......
// Code generated by gowrap. DO NOT EDIT.
// template: ..\..\..\assets\templates\middleware\telemetry
// template: ../../../assets/templates/middleware/telemetry_content
// gowrap: http://github.com/hexdigest/gowrap
package middleware
//go:generate gowrap gen -p git.perx.ru/perxis/perxis-go/pkg/clients -i Clients -t ..\..\..\assets\templates\middleware\telemetry -o telemetry_middleware.go -l ""
//go:generate gowrap gen -p git.perx.ru/perxis/perxis-go/pkg/clients -i Clients -t ../../../assets/templates/middleware/telemetry_content -o telemetry_middleware.go -l ""
// source template: https://github.com/hexdigest/gowrap/blob/master/templates/opentelemetry
......@@ -12,6 +12,8 @@ import (
"context"
"time"
oid "git.perx.ru/perxis/perxis-go/id"
"git.perx.ru/perxis/perxis-go/pkg/auth"
"git.perx.ru/perxis/perxis-go/pkg/clients"
"git.perx.ru/perxis/perxis-go/pkg/telemetry/metrics"
"go.opentelemetry.io/otel"
......@@ -20,6 +22,10 @@ import (
"go.opentelemetry.io/otel/trace"
)
type SpaceGetter interface {
GetSpaceID() string
}
// telemetryMiddleware implements clients.Clients interface instrumented with opentracing spans
type telemetryMiddleware struct {
clients.Clients
......@@ -50,10 +56,12 @@ func TelemetryMiddleware(base clients.Clients, instance string, spanDecorator ..
// Create implements clients.Clients
func (_d telemetryMiddleware) Create(ctx context.Context, client *clients.Client) (created *clients.Client, err error) {
attributes := otelmetric.WithAttributeSet(attribute.NewSet(
var att = []attribute.KeyValue{
attribute.String("service", "Clients"),
attribute.String("method", "Create"),
))
}
attributes := otelmetric.WithAttributeSet(attribute.NewSet(att...))
_d.requestMetrics.Total.Add(ctx, 1, attributes)
......@@ -63,6 +71,30 @@ func (_d telemetryMiddleware) Create(ctx context.Context, client *clients.Client
defer func() {
_d.requestMetrics.DurationMilliseconds.Record(ctx, time.Since(start).Milliseconds(), attributes)
var spID string
vv := []interface{}{ctx, client, created, err}
for _, v := range vv {
if v == nil {
continue
}
if sg, ok := v.(SpaceGetter); ok {
spID = sg.GetSpaceID()
break
}
}
if spID != "" {
principal := auth.GetPrincipal(ctx)
if accessor, ok := principal.(auth.SpaceAccessor); ok {
principal = accessor.Space(spID)
}
caller, _ := oid.NewObjectId(principal)
att = append(att, attribute.String("spaceID", spID))
att = append(att, attribute.String("caller", caller.String()))
}
_d.requestMetrics.Total.Add(ctx, 1, otelmetric.WithAttributeSet(attribute.NewSet(att...)))
if _d._spanDecorator != nil {
_d._spanDecorator(_span, map[string]interface{}{
"ctx": ctx,
......@@ -84,10 +116,12 @@ func (_d telemetryMiddleware) Create(ctx context.Context, client *clients.Client
// Delete implements clients.Clients
func (_d telemetryMiddleware) Delete(ctx context.Context, spaceId string, id string) (err error) {
attributes := otelmetric.WithAttributeSet(attribute.NewSet(
var att = []attribute.KeyValue{
attribute.String("service", "Clients"),
attribute.String("method", "Delete"),
))
}
attributes := otelmetric.WithAttributeSet(attribute.NewSet(att...))
_d.requestMetrics.Total.Add(ctx, 1, attributes)
......@@ -97,6 +131,21 @@ func (_d telemetryMiddleware) Delete(ctx context.Context, spaceId string, id str
defer func() {
_d.requestMetrics.DurationMilliseconds.Record(ctx, time.Since(start).Milliseconds(), attributes)
var spID string
spID = spaceId
if spID != "" {
principal := auth.GetPrincipal(ctx)
if accessor, ok := principal.(auth.SpaceAccessor); ok {
principal = accessor.Space(spID)
}
caller, _ := oid.NewObjectId(principal)
att = append(att, attribute.String("spaceID", spID))
att = append(att, attribute.String("caller", caller.String()))
}
_d.requestMetrics.Total.Add(ctx, 1, otelmetric.WithAttributeSet(attribute.NewSet(att...)))
if _d._spanDecorator != nil {
_d._spanDecorator(_span, map[string]interface{}{
"ctx": ctx,
......@@ -118,10 +167,12 @@ func (_d telemetryMiddleware) Delete(ctx context.Context, spaceId string, id str
// Enable implements clients.Clients
func (_d telemetryMiddleware) Enable(ctx context.Context, spaceId string, id string, enable bool) (err error) {
attributes := otelmetric.WithAttributeSet(attribute.NewSet(
var att = []attribute.KeyValue{
attribute.String("service", "Clients"),
attribute.String("method", "Enable"),
))
}
attributes := otelmetric.WithAttributeSet(attribute.NewSet(att...))
_d.requestMetrics.Total.Add(ctx, 1, attributes)
......@@ -131,6 +182,21 @@ func (_d telemetryMiddleware) Enable(ctx context.Context, spaceId string, id str
defer func() {
_d.requestMetrics.DurationMilliseconds.Record(ctx, time.Since(start).Milliseconds(), attributes)
var spID string
spID = spaceId
if spID != "" {
principal := auth.GetPrincipal(ctx)
if accessor, ok := principal.(auth.SpaceAccessor); ok {
principal = accessor.Space(spID)
}
caller, _ := oid.NewObjectId(principal)
att = append(att, attribute.String("spaceID", spID))
att = append(att, attribute.String("caller", caller.String()))
}
_d.requestMetrics.Total.Add(ctx, 1, otelmetric.WithAttributeSet(attribute.NewSet(att...)))
if _d._spanDecorator != nil {
_d._spanDecorator(_span, map[string]interface{}{
"ctx": ctx,
......@@ -153,10 +219,12 @@ func (_d telemetryMiddleware) Enable(ctx context.Context, spaceId string, id str
// Get implements clients.Clients
func (_d telemetryMiddleware) Get(ctx context.Context, spaceId string, id string) (client *clients.Client, err error) {
attributes := otelmetric.WithAttributeSet(attribute.NewSet(
var att = []attribute.KeyValue{
attribute.String("service", "Clients"),
attribute.String("method", "Get"),
))
}
attributes := otelmetric.WithAttributeSet(attribute.NewSet(att...))
_d.requestMetrics.Total.Add(ctx, 1, attributes)
......@@ -166,6 +234,21 @@ func (_d telemetryMiddleware) Get(ctx context.Context, spaceId string, id string
defer func() {
_d.requestMetrics.DurationMilliseconds.Record(ctx, time.Since(start).Milliseconds(), attributes)
var spID string
spID = spaceId
if spID != "" {
principal := auth.GetPrincipal(ctx)
if accessor, ok := principal.(auth.SpaceAccessor); ok {
principal = accessor.Space(spID)
}
caller, _ := oid.NewObjectId(principal)
att = append(att, attribute.String("spaceID", spID))
att = append(att, attribute.String("caller", caller.String()))
}
_d.requestMetrics.Total.Add(ctx, 1, otelmetric.WithAttributeSet(attribute.NewSet(att...)))
if _d._spanDecorator != nil {
_d._spanDecorator(_span, map[string]interface{}{
"ctx": ctx,
......@@ -188,10 +271,12 @@ func (_d telemetryMiddleware) Get(ctx context.Context, spaceId string, id string
// GetBy implements clients.Clients
func (_d telemetryMiddleware) GetBy(ctx context.Context, spaceId string, params *clients.GetByParams) (client *clients.Client, err error) {
attributes := otelmetric.WithAttributeSet(attribute.NewSet(
var att = []attribute.KeyValue{
attribute.String("service", "Clients"),
attribute.String("method", "GetBy"),
))
}
attributes := otelmetric.WithAttributeSet(attribute.NewSet(att...))
_d.requestMetrics.Total.Add(ctx, 1, attributes)
......@@ -201,6 +286,21 @@ func (_d telemetryMiddleware) GetBy(ctx context.Context, spaceId string, params
defer func() {
_d.requestMetrics.DurationMilliseconds.Record(ctx, time.Since(start).Milliseconds(), attributes)
var spID string
spID = spaceId
if spID != "" {
principal := auth.GetPrincipal(ctx)
if accessor, ok := principal.(auth.SpaceAccessor); ok {
principal = accessor.Space(spID)
}
caller, _ := oid.NewObjectId(principal)
att = append(att, attribute.String("spaceID", spID))
att = append(att, attribute.String("caller", caller.String()))
}
_d.requestMetrics.Total.Add(ctx, 1, otelmetric.WithAttributeSet(attribute.NewSet(att...)))
if _d._spanDecorator != nil {
_d._spanDecorator(_span, map[string]interface{}{
"ctx": ctx,
......@@ -223,10 +323,12 @@ func (_d telemetryMiddleware) GetBy(ctx context.Context, spaceId string, params
// List implements clients.Clients
func (_d telemetryMiddleware) List(ctx context.Context, spaceId string) (clients []*clients.Client, err error) {
attributes := otelmetric.WithAttributeSet(attribute.NewSet(
var att = []attribute.KeyValue{
attribute.String("service", "Clients"),
attribute.String("method", "List"),
))
}
attributes := otelmetric.WithAttributeSet(attribute.NewSet(att...))
_d.requestMetrics.Total.Add(ctx, 1, attributes)
......@@ -236,6 +338,21 @@ func (_d telemetryMiddleware) List(ctx context.Context, spaceId string) (clients
defer func() {
_d.requestMetrics.DurationMilliseconds.Record(ctx, time.Since(start).Milliseconds(), attributes)
var spID string
spID = spaceId
if spID != "" {
principal := auth.GetPrincipal(ctx)
if accessor, ok := principal.(auth.SpaceAccessor); ok {
principal = accessor.Space(spID)
}
caller, _ := oid.NewObjectId(principal)
att = append(att, attribute.String("spaceID", spID))
att = append(att, attribute.String("caller", caller.String()))
}
_d.requestMetrics.Total.Add(ctx, 1, otelmetric.WithAttributeSet(attribute.NewSet(att...)))
if _d._spanDecorator != nil {
_d._spanDecorator(_span, map[string]interface{}{
"ctx": ctx,
......@@ -257,10 +374,12 @@ func (_d telemetryMiddleware) List(ctx context.Context, spaceId string) (clients
// Update implements clients.Clients
func (_d telemetryMiddleware) Update(ctx context.Context, client *clients.Client) (err error) {
attributes := otelmetric.WithAttributeSet(attribute.NewSet(
var att = []attribute.KeyValue{
attribute.String("service", "Clients"),
attribute.String("method", "Update"),
))
}
attributes := otelmetric.WithAttributeSet(attribute.NewSet(att...))
_d.requestMetrics.Total.Add(ctx, 1, attributes)
......@@ -270,6 +389,30 @@ func (_d telemetryMiddleware) Update(ctx context.Context, client *clients.Client
defer func() {
_d.requestMetrics.DurationMilliseconds.Record(ctx, time.Since(start).Milliseconds(), attributes)
var spID string
vv := []interface{}{ctx, client, err}
for _, v := range vv {
if v == nil {
continue
}
if sg, ok := v.(SpaceGetter); ok {
spID = sg.GetSpaceID()
break
}
}
if spID != "" {
principal := auth.GetPrincipal(ctx)
if accessor, ok := principal.(auth.SpaceAccessor); ok {
principal = accessor.Space(spID)
}
caller, _ := oid.NewObjectId(principal)
att = append(att, attribute.String("spaceID", spID))
att = append(att, attribute.String("caller", caller.String()))
}
_d.requestMetrics.Total.Add(ctx, 1, otelmetric.WithAttributeSet(attribute.NewSet(att...)))
if _d._spanDecorator != nil {
_d._spanDecorator(_span, map[string]interface{}{
"ctx": ctx,
......
......@@ -13,3 +13,7 @@ func (c Collaborator) Clone() *Collaborator {
Role: c.Role,
}
}
func (c *Collaborator) GetSpaceID() string {
return c.SpaceID
}
// Code generated by gowrap. DO NOT EDIT.
// template: ..\..\..\assets\templates\middleware\telemetry
// template: ../../../assets/templates/middleware/telemetry_content
// gowrap: http://github.com/hexdigest/gowrap
package middleware
//go:generate gowrap gen -p git.perx.ru/perxis/perxis-go/pkg/collaborators -i Collaborators -t ..\..\..\assets\templates\middleware\telemetry -o telemetry_middleware.go -l ""
//go:generate gowrap gen -p git.perx.ru/perxis/perxis-go/pkg/collaborators -i Collaborators -t ../../../assets/templates/middleware/telemetry_content -o telemetry_middleware.go -l ""
// source template: https://github.com/hexdigest/gowrap/blob/master/templates/opentelemetry
......@@ -12,6 +12,8 @@ import (
"context"
"time"
oid "git.perx.ru/perxis/perxis-go/id"
"git.perx.ru/perxis/perxis-go/pkg/auth"
"git.perx.ru/perxis/perxis-go/pkg/collaborators"
"git.perx.ru/perxis/perxis-go/pkg/telemetry/metrics"
"go.opentelemetry.io/otel"
......@@ -20,6 +22,10 @@ import (
"go.opentelemetry.io/otel/trace"
)
type SpaceGetter interface {
GetSpaceID() string
}
// telemetryMiddleware implements collaborators.Collaborators interface instrumented with opentracing spans
type telemetryMiddleware struct {
collaborators.Collaborators
......@@ -50,10 +56,12 @@ func TelemetryMiddleware(base collaborators.Collaborators, instance string, span
// Get implements collaborators.Collaborators
func (_d telemetryMiddleware) Get(ctx context.Context, spaceId string, subject string) (role string, err error) {
attributes := otelmetric.WithAttributeSet(attribute.NewSet(
var att = []attribute.KeyValue{
attribute.String("service", "Collaborators"),
attribute.String("method", "Get"),
))
}
attributes := otelmetric.WithAttributeSet(attribute.NewSet(att...))
_d.requestMetrics.Total.Add(ctx, 1, attributes)
......@@ -63,6 +71,21 @@ func (_d telemetryMiddleware) Get(ctx context.Context, spaceId string, subject s
defer func() {
_d.requestMetrics.DurationMilliseconds.Record(ctx, time.Since(start).Milliseconds(), attributes)
var spID string
spID = spaceId
if spID != "" {
principal := auth.GetPrincipal(ctx)
if accessor, ok := principal.(auth.SpaceAccessor); ok {
principal = accessor.Space(spID)
}
caller, _ := oid.NewObjectId(principal)
att = append(att, attribute.String("spaceID", spID))
att = append(att, attribute.String("caller", caller.String()))
}
_d.requestMetrics.Total.Add(ctx, 1, otelmetric.WithAttributeSet(attribute.NewSet(att...)))
if _d._spanDecorator != nil {
_d._spanDecorator(_span, map[string]interface{}{
"ctx": ctx,
......@@ -85,10 +108,12 @@ func (_d telemetryMiddleware) Get(ctx context.Context, spaceId string, subject s
// ListCollaborators implements collaborators.Collaborators
func (_d telemetryMiddleware) ListCollaborators(ctx context.Context, spaceId string) (collaborators []*collaborators.Collaborator, err error) {
attributes := otelmetric.WithAttributeSet(attribute.NewSet(
var att = []attribute.KeyValue{
attribute.String("service", "Collaborators"),
attribute.String("method", "ListCollaborators"),
))
}
attributes := otelmetric.WithAttributeSet(attribute.NewSet(att...))
_d.requestMetrics.Total.Add(ctx, 1, attributes)
......@@ -98,6 +123,21 @@ func (_d telemetryMiddleware) ListCollaborators(ctx context.Context, spaceId str
defer func() {
_d.requestMetrics.DurationMilliseconds.Record(ctx, time.Since(start).Milliseconds(), attributes)
var spID string
spID = spaceId
if spID != "" {
principal := auth.GetPrincipal(ctx)
if accessor, ok := principal.(auth.SpaceAccessor); ok {
principal = accessor.Space(spID)
}
caller, _ := oid.NewObjectId(principal)
att = append(att, attribute.String("spaceID", spID))
att = append(att, attribute.String("caller", caller.String()))
}
_d.requestMetrics.Total.Add(ctx, 1, otelmetric.WithAttributeSet(attribute.NewSet(att...)))
if _d._spanDecorator != nil {
_d._spanDecorator(_span, map[string]interface{}{
"ctx": ctx,
......@@ -119,10 +159,12 @@ func (_d telemetryMiddleware) ListCollaborators(ctx context.Context, spaceId str
// ListSpaces implements collaborators.Collaborators
func (_d telemetryMiddleware) ListSpaces(ctx context.Context, subject string) (spaces []*collaborators.Collaborator, err error) {
attributes := otelmetric.WithAttributeSet(attribute.NewSet(
var att = []attribute.KeyValue{
attribute.String("service", "Collaborators"),
attribute.String("method", "ListSpaces"),
))
}
attributes := otelmetric.WithAttributeSet(attribute.NewSet(att...))
_d.requestMetrics.Total.Add(ctx, 1, attributes)
......@@ -132,6 +174,30 @@ func (_d telemetryMiddleware) ListSpaces(ctx context.Context, subject string) (s
defer func() {
_d.requestMetrics.DurationMilliseconds.Record(ctx, time.Since(start).Milliseconds(), attributes)
var spID string
vv := []interface{}{ctx, subject, spaces, err}
for _, v := range vv {
if v == nil {
continue
}
if sg, ok := v.(SpaceGetter); ok {
spID = sg.GetSpaceID()
break
}
}
if spID != "" {
principal := auth.GetPrincipal(ctx)
if accessor, ok := principal.(auth.SpaceAccessor); ok {
principal = accessor.Space(spID)
}
caller, _ := oid.NewObjectId(principal)
att = append(att, attribute.String("spaceID", spID))
att = append(att, attribute.String("caller", caller.String()))
}
_d.requestMetrics.Total.Add(ctx, 1, otelmetric.WithAttributeSet(attribute.NewSet(att...)))
if _d._spanDecorator != nil {
_d._spanDecorator(_span, map[string]interface{}{
"ctx": ctx,
......@@ -153,10 +219,12 @@ func (_d telemetryMiddleware) ListSpaces(ctx context.Context, subject string) (s
// Remove implements collaborators.Collaborators
func (_d telemetryMiddleware) Remove(ctx context.Context, spaceId string, subject string) (err error) {
attributes := otelmetric.WithAttributeSet(attribute.NewSet(
var att = []attribute.KeyValue{
attribute.String("service", "Collaborators"),
attribute.String("method", "Remove"),
))
}
attributes := otelmetric.WithAttributeSet(attribute.NewSet(att...))
_d.requestMetrics.Total.Add(ctx, 1, attributes)
......@@ -166,6 +234,21 @@ func (_d telemetryMiddleware) Remove(ctx context.Context, spaceId string, subjec
defer func() {
_d.requestMetrics.DurationMilliseconds.Record(ctx, time.Since(start).Milliseconds(), attributes)
var spID string
spID = spaceId
if spID != "" {
principal := auth.GetPrincipal(ctx)
if accessor, ok := principal.(auth.SpaceAccessor); ok {
principal = accessor.Space(spID)
}
caller, _ := oid.NewObjectId(principal)
att = append(att, attribute.String("spaceID", spID))
att = append(att, attribute.String("caller", caller.String()))
}
_d.requestMetrics.Total.Add(ctx, 1, otelmetric.WithAttributeSet(attribute.NewSet(att...)))
if _d._spanDecorator != nil {
_d._spanDecorator(_span, map[string]interface{}{
"ctx": ctx,
......@@ -187,10 +270,12 @@ func (_d telemetryMiddleware) Remove(ctx context.Context, spaceId string, subjec
// Set implements collaborators.Collaborators
func (_d telemetryMiddleware) Set(ctx context.Context, spaceId string, subject string, role string) (err error) {
attributes := otelmetric.WithAttributeSet(attribute.NewSet(
var att = []attribute.KeyValue{
attribute.String("service", "Collaborators"),
attribute.String("method", "Set"),
))
}
attributes := otelmetric.WithAttributeSet(attribute.NewSet(att...))
_d.requestMetrics.Total.Add(ctx, 1, attributes)
......@@ -200,6 +285,21 @@ func (_d telemetryMiddleware) Set(ctx context.Context, spaceId string, subject s
defer func() {
_d.requestMetrics.DurationMilliseconds.Record(ctx, time.Since(start).Milliseconds(), attributes)
var spID string
spID = spaceId
if spID != "" {
principal := auth.GetPrincipal(ctx)
if accessor, ok := principal.(auth.SpaceAccessor); ok {
principal = accessor.Space(spID)
}
caller, _ := oid.NewObjectId(principal)
att = append(att, attribute.String("spaceID", spID))
att = append(att, attribute.String("caller", caller.String()))
}
_d.requestMetrics.Total.Add(ctx, 1, otelmetric.WithAttributeSet(attribute.NewSet(att...)))
if _d._spanDecorator != nil {
_d._spanDecorator(_span, map[string]interface{}{
"ctx": ctx,
......
......@@ -97,6 +97,10 @@ func (c Collection) GetID() string {
return c.ID
}
func (c Collection) GetSpaceID() string {
return c.SpaceID
}
// Equal сравнивает две коллекции, за исключением Schema, Access, StateInfo и Config
func (c Collection) Equal(other *Collection) bool {
if c.ID != other.ID ||
......
// Code generated by gowrap. DO NOT EDIT.
// template: ..\..\..\assets\templates\middleware\telemetry
// template: ../../../assets/templates/middleware/telemetry_content
// gowrap: http://github.com/hexdigest/gowrap
package middleware
//go:generate gowrap gen -p git.perx.ru/perxis/perxis-go/pkg/collections -i Collections -t ..\..\..\assets\templates\middleware\telemetry -o telemetry_middleware.go -l ""
//go:generate gowrap gen -p git.perx.ru/perxis/perxis-go/pkg/collections -i Collections -t ../../../assets/templates/middleware/telemetry_content -o telemetry_middleware.go -l ""
// source template: https://github.com/hexdigest/gowrap/blob/master/templates/opentelemetry
......@@ -12,6 +12,8 @@ import (
"context"
"time"
oid "git.perx.ru/perxis/perxis-go/id"
"git.perx.ru/perxis/perxis-go/pkg/auth"
"git.perx.ru/perxis/perxis-go/pkg/collections"
"git.perx.ru/perxis/perxis-go/pkg/schema"
"git.perx.ru/perxis/perxis-go/pkg/telemetry/metrics"
......@@ -21,6 +23,10 @@ import (
"go.opentelemetry.io/otel/trace"
)
type SpaceGetter interface {
GetSpaceID() string
}
// telemetryMiddleware implements collections.Collections interface instrumented with opentracing spans
type telemetryMiddleware struct {
collections.Collections
......@@ -51,10 +57,12 @@ func TelemetryMiddleware(base collections.Collections, instance string, spanDeco
// Create implements collections.Collections
func (_d telemetryMiddleware) Create(ctx context.Context, collection *collections.Collection) (created *collections.Collection, err error) {
attributes := otelmetric.WithAttributeSet(attribute.NewSet(
var att = []attribute.KeyValue{
attribute.String("service", "Collections"),
attribute.String("method", "Create"),
))
}
attributes := otelmetric.WithAttributeSet(attribute.NewSet(att...))
_d.requestMetrics.Total.Add(ctx, 1, attributes)
......@@ -64,6 +72,30 @@ func (_d telemetryMiddleware) Create(ctx context.Context, collection *collection
defer func() {
_d.requestMetrics.DurationMilliseconds.Record(ctx, time.Since(start).Milliseconds(), attributes)
var spID string
vv := []interface{}{ctx, collection, created, err}
for _, v := range vv {
if v == nil {
continue
}
if sg, ok := v.(SpaceGetter); ok {
spID = sg.GetSpaceID()
break
}
}
if spID != "" {
principal := auth.GetPrincipal(ctx)
if accessor, ok := principal.(auth.SpaceAccessor); ok {
principal = accessor.Space(spID)
}
caller, _ := oid.NewObjectId(principal)
att = append(att, attribute.String("spaceID", spID))
att = append(att, attribute.String("caller", caller.String()))
}
_d.requestMetrics.Total.Add(ctx, 1, otelmetric.WithAttributeSet(attribute.NewSet(att...)))
if _d._spanDecorator != nil {
_d._spanDecorator(_span, map[string]interface{}{
"ctx": ctx,
......@@ -85,10 +117,12 @@ func (_d telemetryMiddleware) Create(ctx context.Context, collection *collection
// Delete implements collections.Collections
func (_d telemetryMiddleware) Delete(ctx context.Context, spaceId string, envId string, collectionId string) (err error) {
attributes := otelmetric.WithAttributeSet(attribute.NewSet(
var att = []attribute.KeyValue{
attribute.String("service", "Collections"),
attribute.String("method", "Delete"),
))
}
attributes := otelmetric.WithAttributeSet(attribute.NewSet(att...))
_d.requestMetrics.Total.Add(ctx, 1, attributes)
......@@ -98,6 +132,21 @@ func (_d telemetryMiddleware) Delete(ctx context.Context, spaceId string, envId
defer func() {
_d.requestMetrics.DurationMilliseconds.Record(ctx, time.Since(start).Milliseconds(), attributes)
var spID string
spID = spaceId
if spID != "" {
principal := auth.GetPrincipal(ctx)
if accessor, ok := principal.(auth.SpaceAccessor); ok {
principal = accessor.Space(spID)
}
caller, _ := oid.NewObjectId(principal)
att = append(att, attribute.String("spaceID", spID))
att = append(att, attribute.String("caller", caller.String()))
}
_d.requestMetrics.Total.Add(ctx, 1, otelmetric.WithAttributeSet(attribute.NewSet(att...)))
if _d._spanDecorator != nil {
_d._spanDecorator(_span, map[string]interface{}{
"ctx": ctx,
......@@ -120,10 +169,12 @@ func (_d telemetryMiddleware) Delete(ctx context.Context, spaceId string, envId
// Get implements collections.Collections
func (_d telemetryMiddleware) Get(ctx context.Context, spaceId string, envId string, collectionId string, options ...*collections.GetOptions) (collection *collections.Collection, err error) {
attributes := otelmetric.WithAttributeSet(attribute.NewSet(
var att = []attribute.KeyValue{
attribute.String("service", "Collections"),
attribute.String("method", "Get"),
))
}
attributes := otelmetric.WithAttributeSet(attribute.NewSet(att...))
_d.requestMetrics.Total.Add(ctx, 1, attributes)
......@@ -133,6 +184,21 @@ func (_d telemetryMiddleware) Get(ctx context.Context, spaceId string, envId str
defer func() {
_d.requestMetrics.DurationMilliseconds.Record(ctx, time.Since(start).Milliseconds(), attributes)
var spID string
spID = spaceId
if spID != "" {
principal := auth.GetPrincipal(ctx)
if accessor, ok := principal.(auth.SpaceAccessor); ok {
principal = accessor.Space(spID)
}
caller, _ := oid.NewObjectId(principal)
att = append(att, attribute.String("spaceID", spID))
att = append(att, attribute.String("caller", caller.String()))
}
_d.requestMetrics.Total.Add(ctx, 1, otelmetric.WithAttributeSet(attribute.NewSet(att...)))
if _d._spanDecorator != nil {
_d._spanDecorator(_span, map[string]interface{}{
"ctx": ctx,
......@@ -157,10 +223,12 @@ func (_d telemetryMiddleware) Get(ctx context.Context, spaceId string, envId str
// List implements collections.Collections
func (_d telemetryMiddleware) List(ctx context.Context, spaceId string, envId string, filter *collections.Filter) (collections []*collections.Collection, err error) {
attributes := otelmetric.WithAttributeSet(attribute.NewSet(
var att = []attribute.KeyValue{
attribute.String("service", "Collections"),
attribute.String("method", "List"),
))
}
attributes := otelmetric.WithAttributeSet(attribute.NewSet(att...))
_d.requestMetrics.Total.Add(ctx, 1, attributes)
......@@ -170,6 +238,21 @@ func (_d telemetryMiddleware) List(ctx context.Context, spaceId string, envId st
defer func() {
_d.requestMetrics.DurationMilliseconds.Record(ctx, time.Since(start).Milliseconds(), attributes)
var spID string
spID = spaceId
if spID != "" {
principal := auth.GetPrincipal(ctx)
if accessor, ok := principal.(auth.SpaceAccessor); ok {
principal = accessor.Space(spID)
}
caller, _ := oid.NewObjectId(principal)
att = append(att, attribute.String("spaceID", spID))
att = append(att, attribute.String("caller", caller.String()))
}
_d.requestMetrics.Total.Add(ctx, 1, otelmetric.WithAttributeSet(attribute.NewSet(att...)))
if _d._spanDecorator != nil {
_d._spanDecorator(_span, map[string]interface{}{
"ctx": ctx,
......@@ -193,10 +276,12 @@ func (_d telemetryMiddleware) List(ctx context.Context, spaceId string, envId st
// SetSchema implements collections.Collections
func (_d telemetryMiddleware) SetSchema(ctx context.Context, spaceId string, envId string, collectionId string, schema *schema.Schema) (err error) {
attributes := otelmetric.WithAttributeSet(attribute.NewSet(
var att = []attribute.KeyValue{
attribute.String("service", "Collections"),
attribute.String("method", "SetSchema"),
))
}
attributes := otelmetric.WithAttributeSet(attribute.NewSet(att...))
_d.requestMetrics.Total.Add(ctx, 1, attributes)
......@@ -206,6 +291,21 @@ func (_d telemetryMiddleware) SetSchema(ctx context.Context, spaceId string, env
defer func() {
_d.requestMetrics.DurationMilliseconds.Record(ctx, time.Since(start).Milliseconds(), attributes)
var spID string
spID = spaceId
if spID != "" {
principal := auth.GetPrincipal(ctx)
if accessor, ok := principal.(auth.SpaceAccessor); ok {
principal = accessor.Space(spID)
}
caller, _ := oid.NewObjectId(principal)
att = append(att, attribute.String("spaceID", spID))
att = append(att, attribute.String("caller", caller.String()))
}
_d.requestMetrics.Total.Add(ctx, 1, otelmetric.WithAttributeSet(attribute.NewSet(att...)))
if _d._spanDecorator != nil {
_d._spanDecorator(_span, map[string]interface{}{
"ctx": ctx,
......@@ -229,10 +329,12 @@ func (_d telemetryMiddleware) SetSchema(ctx context.Context, spaceId string, env
// SetState implements collections.Collections
func (_d telemetryMiddleware) SetState(ctx context.Context, spaceId string, envId string, collectionId string, state *collections.StateInfo) (err error) {
attributes := otelmetric.WithAttributeSet(attribute.NewSet(
var att = []attribute.KeyValue{
attribute.String("service", "Collections"),
attribute.String("method", "SetState"),
))
}
attributes := otelmetric.WithAttributeSet(attribute.NewSet(att...))
_d.requestMetrics.Total.Add(ctx, 1, attributes)
......@@ -242,6 +344,21 @@ func (_d telemetryMiddleware) SetState(ctx context.Context, spaceId string, envI
defer func() {
_d.requestMetrics.DurationMilliseconds.Record(ctx, time.Since(start).Milliseconds(), attributes)
var spID string
spID = spaceId
if spID != "" {
principal := auth.GetPrincipal(ctx)
if accessor, ok := principal.(auth.SpaceAccessor); ok {
principal = accessor.Space(spID)
}
caller, _ := oid.NewObjectId(principal)
att = append(att, attribute.String("spaceID", spID))
att = append(att, attribute.String("caller", caller.String()))
}
_d.requestMetrics.Total.Add(ctx, 1, otelmetric.WithAttributeSet(attribute.NewSet(att...)))
if _d._spanDecorator != nil {
_d._spanDecorator(_span, map[string]interface{}{
"ctx": ctx,
......@@ -265,10 +382,12 @@ func (_d telemetryMiddleware) SetState(ctx context.Context, spaceId string, envI
// Update implements collections.Collections
func (_d telemetryMiddleware) Update(ctx context.Context, coll *collections.Collection) (err error) {
attributes := otelmetric.WithAttributeSet(attribute.NewSet(
var att = []attribute.KeyValue{
attribute.String("service", "Collections"),
attribute.String("method", "Update"),
))
}
attributes := otelmetric.WithAttributeSet(attribute.NewSet(att...))
_d.requestMetrics.Total.Add(ctx, 1, attributes)
......@@ -278,6 +397,30 @@ func (_d telemetryMiddleware) Update(ctx context.Context, coll *collections.Coll
defer func() {
_d.requestMetrics.DurationMilliseconds.Record(ctx, time.Since(start).Milliseconds(), attributes)
var spID string
vv := []interface{}{ctx, coll, err}
for _, v := range vv {
if v == nil {
continue
}
if sg, ok := v.(SpaceGetter); ok {
spID = sg.GetSpaceID()
break
}
}
if spID != "" {
principal := auth.GetPrincipal(ctx)
if accessor, ok := principal.(auth.SpaceAccessor); ok {
principal = accessor.Space(spID)
}
caller, _ := oid.NewObjectId(principal)
att = append(att, attribute.String("spaceID", spID))
att = append(att, attribute.String("caller", caller.String()))
}
_d.requestMetrics.Total.Add(ctx, 1, otelmetric.WithAttributeSet(attribute.NewSet(att...)))
if _d._spanDecorator != nil {
_d._spanDecorator(_span, map[string]interface{}{
"ctx": ctx,
......
// Code generated by gowrap. DO NOT EDIT.
// template: ..\..\..\assets\templates\middleware\telemetry
// template: ../../../assets/templates/middleware/telemetry_content
// gowrap: http://github.com/hexdigest/gowrap
package middleware
//go:generate gowrap gen -p git.perx.ru/perxis/perxis-go/pkg/delivery -i Delivery -t ..\..\..\assets\templates\middleware\telemetry -o telemetry_middleware.go -l ""
//go:generate gowrap gen -p git.perx.ru/perxis/perxis-go/pkg/delivery -i Delivery -t ../../../assets/templates/middleware/telemetry_content -o telemetry_middleware.go -l ""
// source template: https://github.com/hexdigest/gowrap/blob/master/templates/opentelemetry
......@@ -12,6 +12,8 @@ import (
"context"
"time"
oid "git.perx.ru/perxis/perxis-go/id"
"git.perx.ru/perxis/perxis-go/pkg/auth"
"git.perx.ru/perxis/perxis-go/pkg/collections"
"git.perx.ru/perxis/perxis-go/pkg/delivery"
"git.perx.ru/perxis/perxis-go/pkg/environments"
......@@ -24,6 +26,10 @@ import (
"go.opentelemetry.io/otel/trace"
)
type SpaceGetter interface {
GetSpaceID() string
}
// telemetryMiddleware implements delivery.Delivery interface instrumented with opentracing spans
type telemetryMiddleware struct {
delivery.Delivery
......@@ -54,10 +60,12 @@ func TelemetryMiddleware(base delivery.Delivery, instance string, spanDecorator
// Aggregate implements delivery.Delivery
func (_d telemetryMiddleware) Aggregate(ctx context.Context, spaceId string, envId string, collectionId string, filter *items.Filter, options ...*items.AggregatePublishedOptions) (result map[string]interface{}, err error) {
attributes := otelmetric.WithAttributeSet(attribute.NewSet(
var att = []attribute.KeyValue{
attribute.String("service", "Delivery"),
attribute.String("method", "Aggregate"),
))
}
attributes := otelmetric.WithAttributeSet(attribute.NewSet(att...))
_d.requestMetrics.Total.Add(ctx, 1, attributes)
......@@ -67,6 +75,21 @@ func (_d telemetryMiddleware) Aggregate(ctx context.Context, spaceId string, env
defer func() {
_d.requestMetrics.DurationMilliseconds.Record(ctx, time.Since(start).Milliseconds(), attributes)
var spID string
spID = spaceId
if spID != "" {
principal := auth.GetPrincipal(ctx)
if accessor, ok := principal.(auth.SpaceAccessor); ok {
principal = accessor.Space(spID)
}
caller, _ := oid.NewObjectId(principal)
att = append(att, attribute.String("spaceID", spID))
att = append(att, attribute.String("caller", caller.String()))
}
_d.requestMetrics.Total.Add(ctx, 1, otelmetric.WithAttributeSet(attribute.NewSet(att...)))
if _d._spanDecorator != nil {
_d._spanDecorator(_span, map[string]interface{}{
"ctx": ctx,
......@@ -92,10 +115,12 @@ func (_d telemetryMiddleware) Aggregate(ctx context.Context, spaceId string, env
// FindItems implements delivery.Delivery
func (_d telemetryMiddleware) FindItems(ctx context.Context, spaceId string, envId string, collectionId string, filter *items.Filter, options ...*items.FindPublishedOptions) (items []*items.Item, total int, err error) {
attributes := otelmetric.WithAttributeSet(attribute.NewSet(
var att = []attribute.KeyValue{
attribute.String("service", "Delivery"),
attribute.String("method", "FindItems"),
))
}
attributes := otelmetric.WithAttributeSet(attribute.NewSet(att...))
_d.requestMetrics.Total.Add(ctx, 1, attributes)
......@@ -105,6 +130,21 @@ func (_d telemetryMiddleware) FindItems(ctx context.Context, spaceId string, env
defer func() {
_d.requestMetrics.DurationMilliseconds.Record(ctx, time.Since(start).Milliseconds(), attributes)
var spID string
spID = spaceId
if spID != "" {
principal := auth.GetPrincipal(ctx)
if accessor, ok := principal.(auth.SpaceAccessor); ok {
principal = accessor.Space(spID)
}
caller, _ := oid.NewObjectId(principal)
att = append(att, attribute.String("spaceID", spID))
att = append(att, attribute.String("caller", caller.String()))
}
_d.requestMetrics.Total.Add(ctx, 1, otelmetric.WithAttributeSet(attribute.NewSet(att...)))
if _d._spanDecorator != nil {
_d._spanDecorator(_span, map[string]interface{}{
"ctx": ctx,
......@@ -131,10 +171,12 @@ func (_d telemetryMiddleware) FindItems(ctx context.Context, spaceId string, env
// GetCollection implements delivery.Delivery
func (_d telemetryMiddleware) GetCollection(ctx context.Context, spaceId string, envId string, collectionId string) (collection *collections.Collection, err error) {
attributes := otelmetric.WithAttributeSet(attribute.NewSet(
var att = []attribute.KeyValue{
attribute.String("service", "Delivery"),
attribute.String("method", "GetCollection"),
))
}
attributes := otelmetric.WithAttributeSet(attribute.NewSet(att...))
_d.requestMetrics.Total.Add(ctx, 1, attributes)
......@@ -144,6 +186,21 @@ func (_d telemetryMiddleware) GetCollection(ctx context.Context, spaceId string,
defer func() {
_d.requestMetrics.DurationMilliseconds.Record(ctx, time.Since(start).Milliseconds(), attributes)
var spID string
spID = spaceId
if spID != "" {
principal := auth.GetPrincipal(ctx)
if accessor, ok := principal.(auth.SpaceAccessor); ok {
principal = accessor.Space(spID)
}
caller, _ := oid.NewObjectId(principal)
att = append(att, attribute.String("spaceID", spID))
att = append(att, attribute.String("caller", caller.String()))
}
_d.requestMetrics.Total.Add(ctx, 1, otelmetric.WithAttributeSet(attribute.NewSet(att...)))
if _d._spanDecorator != nil {
_d._spanDecorator(_span, map[string]interface{}{
"ctx": ctx,
......@@ -167,10 +224,12 @@ func (_d telemetryMiddleware) GetCollection(ctx context.Context, spaceId string,
// GetEnvironment implements delivery.Delivery
func (_d telemetryMiddleware) GetEnvironment(ctx context.Context, spaceId string, envId string) (env *environments.Environment, err error) {
attributes := otelmetric.WithAttributeSet(attribute.NewSet(
var att = []attribute.KeyValue{
attribute.String("service", "Delivery"),
attribute.String("method", "GetEnvironment"),
))
}
attributes := otelmetric.WithAttributeSet(attribute.NewSet(att...))
_d.requestMetrics.Total.Add(ctx, 1, attributes)
......@@ -180,6 +239,21 @@ func (_d telemetryMiddleware) GetEnvironment(ctx context.Context, spaceId string
defer func() {
_d.requestMetrics.DurationMilliseconds.Record(ctx, time.Since(start).Milliseconds(), attributes)
var spID string
spID = spaceId
if spID != "" {
principal := auth.GetPrincipal(ctx)
if accessor, ok := principal.(auth.SpaceAccessor); ok {
principal = accessor.Space(spID)
}
caller, _ := oid.NewObjectId(principal)
att = append(att, attribute.String("spaceID", spID))
att = append(att, attribute.String("caller", caller.String()))
}
_d.requestMetrics.Total.Add(ctx, 1, otelmetric.WithAttributeSet(attribute.NewSet(att...)))
if _d._spanDecorator != nil {
_d._spanDecorator(_span, map[string]interface{}{
"ctx": ctx,
......@@ -202,10 +276,12 @@ func (_d telemetryMiddleware) GetEnvironment(ctx context.Context, spaceId string
// GetItem implements delivery.Delivery
func (_d telemetryMiddleware) GetItem(ctx context.Context, spaceId string, envId string, collectionId string, itemId string, options ...*items.GetPublishedOptions) (item *items.Item, err error) {
attributes := otelmetric.WithAttributeSet(attribute.NewSet(
var att = []attribute.KeyValue{
attribute.String("service", "Delivery"),
attribute.String("method", "GetItem"),
))
}
attributes := otelmetric.WithAttributeSet(attribute.NewSet(att...))
_d.requestMetrics.Total.Add(ctx, 1, attributes)
......@@ -215,6 +291,21 @@ func (_d telemetryMiddleware) GetItem(ctx context.Context, spaceId string, envId
defer func() {
_d.requestMetrics.DurationMilliseconds.Record(ctx, time.Since(start).Milliseconds(), attributes)
var spID string
spID = spaceId
if spID != "" {
principal := auth.GetPrincipal(ctx)
if accessor, ok := principal.(auth.SpaceAccessor); ok {
principal = accessor.Space(spID)
}
caller, _ := oid.NewObjectId(principal)
att = append(att, attribute.String("spaceID", spID))
att = append(att, attribute.String("caller", caller.String()))
}
_d.requestMetrics.Total.Add(ctx, 1, otelmetric.WithAttributeSet(attribute.NewSet(att...)))
if _d._spanDecorator != nil {
_d._spanDecorator(_span, map[string]interface{}{
"ctx": ctx,
......@@ -240,10 +331,12 @@ func (_d telemetryMiddleware) GetItem(ctx context.Context, spaceId string, envId
// ListCollections implements delivery.Delivery
func (_d telemetryMiddleware) ListCollections(ctx context.Context, spaceId string, envId string) (collections []*collections.Collection, err error) {
attributes := otelmetric.WithAttributeSet(attribute.NewSet(
var att = []attribute.KeyValue{
attribute.String("service", "Delivery"),
attribute.String("method", "ListCollections"),
))
}
attributes := otelmetric.WithAttributeSet(attribute.NewSet(att...))
_d.requestMetrics.Total.Add(ctx, 1, attributes)
......@@ -253,6 +346,21 @@ func (_d telemetryMiddleware) ListCollections(ctx context.Context, spaceId strin
defer func() {
_d.requestMetrics.DurationMilliseconds.Record(ctx, time.Since(start).Milliseconds(), attributes)
var spID string
spID = spaceId
if spID != "" {
principal := auth.GetPrincipal(ctx)
if accessor, ok := principal.(auth.SpaceAccessor); ok {
principal = accessor.Space(spID)
}
caller, _ := oid.NewObjectId(principal)
att = append(att, attribute.String("spaceID", spID))
att = append(att, attribute.String("caller", caller.String()))
}
_d.requestMetrics.Total.Add(ctx, 1, otelmetric.WithAttributeSet(attribute.NewSet(att...)))
if _d._spanDecorator != nil {
_d._spanDecorator(_span, map[string]interface{}{
"ctx": ctx,
......@@ -275,10 +383,12 @@ func (_d telemetryMiddleware) ListCollections(ctx context.Context, spaceId strin
// ListEnvironments implements delivery.Delivery
func (_d telemetryMiddleware) ListEnvironments(ctx context.Context, spaceId string) (envs []*environments.Environment, err error) {
attributes := otelmetric.WithAttributeSet(attribute.NewSet(
var att = []attribute.KeyValue{
attribute.String("service", "Delivery"),
attribute.String("method", "ListEnvironments"),
))
}
attributes := otelmetric.WithAttributeSet(attribute.NewSet(att...))
_d.requestMetrics.Total.Add(ctx, 1, attributes)
......@@ -288,6 +398,21 @@ func (_d telemetryMiddleware) ListEnvironments(ctx context.Context, spaceId stri
defer func() {
_d.requestMetrics.DurationMilliseconds.Record(ctx, time.Since(start).Milliseconds(), attributes)
var spID string
spID = spaceId
if spID != "" {
principal := auth.GetPrincipal(ctx)
if accessor, ok := principal.(auth.SpaceAccessor); ok {
principal = accessor.Space(spID)
}
caller, _ := oid.NewObjectId(principal)
att = append(att, attribute.String("spaceID", spID))
att = append(att, attribute.String("caller", caller.String()))
}
_d.requestMetrics.Total.Add(ctx, 1, otelmetric.WithAttributeSet(attribute.NewSet(att...)))
if _d._spanDecorator != nil {
_d._spanDecorator(_span, map[string]interface{}{
"ctx": ctx,
......@@ -309,10 +434,12 @@ func (_d telemetryMiddleware) ListEnvironments(ctx context.Context, spaceId stri
// ListLocales implements delivery.Delivery
func (_d telemetryMiddleware) ListLocales(ctx context.Context, spaceId string) (locales []*locales.Locale, err error) {
attributes := otelmetric.WithAttributeSet(attribute.NewSet(
var att = []attribute.KeyValue{
attribute.String("service", "Delivery"),
attribute.String("method", "ListLocales"),
))
}
attributes := otelmetric.WithAttributeSet(attribute.NewSet(att...))
_d.requestMetrics.Total.Add(ctx, 1, attributes)
......@@ -322,6 +449,21 @@ func (_d telemetryMiddleware) ListLocales(ctx context.Context, spaceId string) (
defer func() {
_d.requestMetrics.DurationMilliseconds.Record(ctx, time.Since(start).Milliseconds(), attributes)
var spID string
spID = spaceId
if spID != "" {
principal := auth.GetPrincipal(ctx)
if accessor, ok := principal.(auth.SpaceAccessor); ok {
principal = accessor.Space(spID)
}
caller, _ := oid.NewObjectId(principal)
att = append(att, attribute.String("spaceID", spID))
att = append(att, attribute.String("caller", caller.String()))
}
_d.requestMetrics.Total.Add(ctx, 1, otelmetric.WithAttributeSet(attribute.NewSet(att...)))
if _d._spanDecorator != nil {
_d._spanDecorator(_span, map[string]interface{}{
"ctx": ctx,
......
......@@ -104,3 +104,7 @@ func (e Environment) Clone() *Environment {
return clone
}
func (e Environment) GetSpaceID() string {
return e.SpaceID
}
// Code generated by gowrap. DO NOT EDIT.
// template: ..\..\..\assets\templates\middleware\telemetry
// template: ../../../assets/templates/middleware/telemetry_content
// gowrap: http://github.com/hexdigest/gowrap
package middleware
//go:generate gowrap gen -p git.perx.ru/perxis/perxis-go/pkg/environments -i Environments -t ..\..\..\assets\templates\middleware\telemetry -o telemetry_middleware.go -l ""
//go:generate gowrap gen -p git.perx.ru/perxis/perxis-go/pkg/environments -i Environments -t ../../../assets/templates/middleware/telemetry_content -o telemetry_middleware.go -l ""
// source template: https://github.com/hexdigest/gowrap/blob/master/templates/opentelemetry
......@@ -12,6 +12,8 @@ import (
"context"
"time"
oid "git.perx.ru/perxis/perxis-go/id"
"git.perx.ru/perxis/perxis-go/pkg/auth"
"git.perx.ru/perxis/perxis-go/pkg/environments"
"git.perx.ru/perxis/perxis-go/pkg/telemetry/metrics"
"go.opentelemetry.io/otel"
......@@ -20,6 +22,10 @@ import (
"go.opentelemetry.io/otel/trace"
)
type SpaceGetter interface {
GetSpaceID() string
}
// telemetryMiddleware implements environments.Environments interface instrumented with opentracing spans
type telemetryMiddleware struct {
environments.Environments
......@@ -50,10 +56,12 @@ func TelemetryMiddleware(base environments.Environments, instance string, spanDe
// Create implements environments.Environments
func (_d telemetryMiddleware) Create(ctx context.Context, env *environments.Environment) (created *environments.Environment, err error) {
attributes := otelmetric.WithAttributeSet(attribute.NewSet(
var att = []attribute.KeyValue{
attribute.String("service", "Environments"),
attribute.String("method", "Create"),
))
}
attributes := otelmetric.WithAttributeSet(attribute.NewSet(att...))
_d.requestMetrics.Total.Add(ctx, 1, attributes)
......@@ -63,6 +71,30 @@ func (_d telemetryMiddleware) Create(ctx context.Context, env *environments.Envi
defer func() {
_d.requestMetrics.DurationMilliseconds.Record(ctx, time.Since(start).Milliseconds(), attributes)
var spID string
vv := []interface{}{ctx, env, created, err}
for _, v := range vv {
if v == nil {
continue
}
if sg, ok := v.(SpaceGetter); ok {
spID = sg.GetSpaceID()
break
}
}
if spID != "" {
principal := auth.GetPrincipal(ctx)
if accessor, ok := principal.(auth.SpaceAccessor); ok {
principal = accessor.Space(spID)
}
caller, _ := oid.NewObjectId(principal)
att = append(att, attribute.String("spaceID", spID))
att = append(att, attribute.String("caller", caller.String()))
}
_d.requestMetrics.Total.Add(ctx, 1, otelmetric.WithAttributeSet(attribute.NewSet(att...)))
if _d._spanDecorator != nil {
_d._spanDecorator(_span, map[string]interface{}{
"ctx": ctx,
......@@ -84,10 +116,12 @@ func (_d telemetryMiddleware) Create(ctx context.Context, env *environments.Envi
// Delete implements environments.Environments
func (_d telemetryMiddleware) Delete(ctx context.Context, spaceId string, envId string) (err error) {
attributes := otelmetric.WithAttributeSet(attribute.NewSet(
var att = []attribute.KeyValue{
attribute.String("service", "Environments"),
attribute.String("method", "Delete"),
))
}
attributes := otelmetric.WithAttributeSet(attribute.NewSet(att...))
_d.requestMetrics.Total.Add(ctx, 1, attributes)
......@@ -97,6 +131,21 @@ func (_d telemetryMiddleware) Delete(ctx context.Context, spaceId string, envId
defer func() {
_d.requestMetrics.DurationMilliseconds.Record(ctx, time.Since(start).Milliseconds(), attributes)
var spID string
spID = spaceId
if spID != "" {
principal := auth.GetPrincipal(ctx)
if accessor, ok := principal.(auth.SpaceAccessor); ok {
principal = accessor.Space(spID)
}
caller, _ := oid.NewObjectId(principal)
att = append(att, attribute.String("spaceID", spID))
att = append(att, attribute.String("caller", caller.String()))
}
_d.requestMetrics.Total.Add(ctx, 1, otelmetric.WithAttributeSet(attribute.NewSet(att...)))
if _d._spanDecorator != nil {
_d._spanDecorator(_span, map[string]interface{}{
"ctx": ctx,
......@@ -118,10 +167,12 @@ func (_d telemetryMiddleware) Delete(ctx context.Context, spaceId string, envId
// Get implements environments.Environments
func (_d telemetryMiddleware) Get(ctx context.Context, spaceId string, envId string) (env *environments.Environment, err error) {
attributes := otelmetric.WithAttributeSet(attribute.NewSet(
var att = []attribute.KeyValue{
attribute.String("service", "Environments"),
attribute.String("method", "Get"),
))
}
attributes := otelmetric.WithAttributeSet(attribute.NewSet(att...))
_d.requestMetrics.Total.Add(ctx, 1, attributes)
......@@ -131,6 +182,21 @@ func (_d telemetryMiddleware) Get(ctx context.Context, spaceId string, envId str
defer func() {
_d.requestMetrics.DurationMilliseconds.Record(ctx, time.Since(start).Milliseconds(), attributes)
var spID string
spID = spaceId
if spID != "" {
principal := auth.GetPrincipal(ctx)
if accessor, ok := principal.(auth.SpaceAccessor); ok {
principal = accessor.Space(spID)
}
caller, _ := oid.NewObjectId(principal)
att = append(att, attribute.String("spaceID", spID))
att = append(att, attribute.String("caller", caller.String()))
}
_d.requestMetrics.Total.Add(ctx, 1, otelmetric.WithAttributeSet(attribute.NewSet(att...)))
if _d._spanDecorator != nil {
_d._spanDecorator(_span, map[string]interface{}{
"ctx": ctx,
......@@ -153,10 +219,12 @@ func (_d telemetryMiddleware) Get(ctx context.Context, spaceId string, envId str
// List implements environments.Environments
func (_d telemetryMiddleware) List(ctx context.Context, spaceId string) (envs []*environments.Environment, err error) {
attributes := otelmetric.WithAttributeSet(attribute.NewSet(
var att = []attribute.KeyValue{
attribute.String("service", "Environments"),
attribute.String("method", "List"),
))
}
attributes := otelmetric.WithAttributeSet(attribute.NewSet(att...))
_d.requestMetrics.Total.Add(ctx, 1, attributes)
......@@ -166,6 +234,21 @@ func (_d telemetryMiddleware) List(ctx context.Context, spaceId string) (envs []
defer func() {
_d.requestMetrics.DurationMilliseconds.Record(ctx, time.Since(start).Milliseconds(), attributes)
var spID string
spID = spaceId
if spID != "" {
principal := auth.GetPrincipal(ctx)
if accessor, ok := principal.(auth.SpaceAccessor); ok {
principal = accessor.Space(spID)
}
caller, _ := oid.NewObjectId(principal)
att = append(att, attribute.String("spaceID", spID))
att = append(att, attribute.String("caller", caller.String()))
}
_d.requestMetrics.Total.Add(ctx, 1, otelmetric.WithAttributeSet(attribute.NewSet(att...)))
if _d._spanDecorator != nil {
_d._spanDecorator(_span, map[string]interface{}{
"ctx": ctx,
......@@ -187,10 +270,12 @@ func (_d telemetryMiddleware) List(ctx context.Context, spaceId string) (envs []
// Migrate implements environments.Environments
func (_d telemetryMiddleware) Migrate(ctx context.Context, spaceId string, envId string, options ...*environments.MigrateOptions) (err error) {
attributes := otelmetric.WithAttributeSet(attribute.NewSet(
var att = []attribute.KeyValue{
attribute.String("service", "Environments"),
attribute.String("method", "Migrate"),
))
}
attributes := otelmetric.WithAttributeSet(attribute.NewSet(att...))
_d.requestMetrics.Total.Add(ctx, 1, attributes)
......@@ -200,6 +285,21 @@ func (_d telemetryMiddleware) Migrate(ctx context.Context, spaceId string, envId
defer func() {
_d.requestMetrics.DurationMilliseconds.Record(ctx, time.Since(start).Milliseconds(), attributes)
var spID string
spID = spaceId
if spID != "" {
principal := auth.GetPrincipal(ctx)
if accessor, ok := principal.(auth.SpaceAccessor); ok {
principal = accessor.Space(spID)
}
caller, _ := oid.NewObjectId(principal)
att = append(att, attribute.String("spaceID", spID))
att = append(att, attribute.String("caller", caller.String()))
}
_d.requestMetrics.Total.Add(ctx, 1, otelmetric.WithAttributeSet(attribute.NewSet(att...)))
if _d._spanDecorator != nil {
_d._spanDecorator(_span, map[string]interface{}{
"ctx": ctx,
......@@ -222,10 +322,12 @@ func (_d telemetryMiddleware) Migrate(ctx context.Context, spaceId string, envId
// RemoveAlias implements environments.Environments
func (_d telemetryMiddleware) RemoveAlias(ctx context.Context, spaceId string, envId string, alias string) (err error) {
attributes := otelmetric.WithAttributeSet(attribute.NewSet(
var att = []attribute.KeyValue{
attribute.String("service", "Environments"),
attribute.String("method", "RemoveAlias"),
))
}
attributes := otelmetric.WithAttributeSet(attribute.NewSet(att...))
_d.requestMetrics.Total.Add(ctx, 1, attributes)
......@@ -235,6 +337,21 @@ func (_d telemetryMiddleware) RemoveAlias(ctx context.Context, spaceId string, e
defer func() {
_d.requestMetrics.DurationMilliseconds.Record(ctx, time.Since(start).Milliseconds(), attributes)
var spID string
spID = spaceId
if spID != "" {
principal := auth.GetPrincipal(ctx)
if accessor, ok := principal.(auth.SpaceAccessor); ok {
principal = accessor.Space(spID)
}
caller, _ := oid.NewObjectId(principal)
att = append(att, attribute.String("spaceID", spID))
att = append(att, attribute.String("caller", caller.String()))
}
_d.requestMetrics.Total.Add(ctx, 1, otelmetric.WithAttributeSet(attribute.NewSet(att...)))
if _d._spanDecorator != nil {
_d._spanDecorator(_span, map[string]interface{}{
"ctx": ctx,
......@@ -257,10 +374,12 @@ func (_d telemetryMiddleware) RemoveAlias(ctx context.Context, spaceId string, e
// SetAlias implements environments.Environments
func (_d telemetryMiddleware) SetAlias(ctx context.Context, spaceId string, envId string, alias string) (err error) {
attributes := otelmetric.WithAttributeSet(attribute.NewSet(
var att = []attribute.KeyValue{
attribute.String("service", "Environments"),
attribute.String("method", "SetAlias"),
))
}
attributes := otelmetric.WithAttributeSet(attribute.NewSet(att...))
_d.requestMetrics.Total.Add(ctx, 1, attributes)
......@@ -270,6 +389,21 @@ func (_d telemetryMiddleware) SetAlias(ctx context.Context, spaceId string, envI
defer func() {
_d.requestMetrics.DurationMilliseconds.Record(ctx, time.Since(start).Milliseconds(), attributes)
var spID string
spID = spaceId
if spID != "" {
principal := auth.GetPrincipal(ctx)
if accessor, ok := principal.(auth.SpaceAccessor); ok {
principal = accessor.Space(spID)
}
caller, _ := oid.NewObjectId(principal)
att = append(att, attribute.String("spaceID", spID))
att = append(att, attribute.String("caller", caller.String()))
}
_d.requestMetrics.Total.Add(ctx, 1, otelmetric.WithAttributeSet(attribute.NewSet(att...)))
if _d._spanDecorator != nil {
_d._spanDecorator(_span, map[string]interface{}{
"ctx": ctx,
......@@ -292,10 +426,12 @@ func (_d telemetryMiddleware) SetAlias(ctx context.Context, spaceId string, envI
// Update implements environments.Environments
func (_d telemetryMiddleware) Update(ctx context.Context, env *environments.Environment) (err error) {
attributes := otelmetric.WithAttributeSet(attribute.NewSet(
var att = []attribute.KeyValue{
attribute.String("service", "Environments"),
attribute.String("method", "Update"),
))
}
attributes := otelmetric.WithAttributeSet(attribute.NewSet(att...))
_d.requestMetrics.Total.Add(ctx, 1, attributes)
......@@ -305,6 +441,30 @@ func (_d telemetryMiddleware) Update(ctx context.Context, env *environments.Envi
defer func() {
_d.requestMetrics.DurationMilliseconds.Record(ctx, time.Since(start).Milliseconds(), attributes)
var spID string
vv := []interface{}{ctx, env, err}
for _, v := range vv {
if v == nil {
continue
}
if sg, ok := v.(SpaceGetter); ok {
spID = sg.GetSpaceID()
break
}
}
if spID != "" {
principal := auth.GetPrincipal(ctx)
if accessor, ok := principal.(auth.SpaceAccessor); ok {
principal = accessor.Space(spID)
}
caller, _ := oid.NewObjectId(principal)
att = append(att, attribute.String("spaceID", spID))
att = append(att, attribute.String("caller", caller.String()))
}
_d.requestMetrics.Total.Add(ctx, 1, otelmetric.WithAttributeSet(attribute.NewSet(att...)))
if _d._spanDecorator != nil {
_d._spanDecorator(_span, map[string]interface{}{
"ctx": ctx,
......
// Code generated by gowrap. DO NOT EDIT.
// template: ..\..\..\assets\templates\middleware\telemetry
// 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/files -i Files -t ..\..\..\assets\templates\middleware\telemetry -o telemetry_middleware.go -l ""
//go:generate gowrap gen -p git.perx.ru/perxis/perxis-go/pkg/files -i Files -t ../../../assets/templates/middleware/telemetry -o telemetry_middleware.go -l ""
// source template: https://github.com/hexdigest/gowrap/blob/master/templates/opentelemetry
......
......@@ -27,3 +27,7 @@ func (i Invitation) Clone() *Invitation {
ValidUntil: i.ValidUntil,
}
}
func (i Invitation) GetSpaceID() string {
return i.SpaceID
}
// Code generated by gowrap. DO NOT EDIT.
// template: ..\..\..\assets\templates\middleware\telemetry
// template: ../../../assets/templates/middleware/telemetry_content
// gowrap: http://github.com/hexdigest/gowrap
package middleware
//go:generate gowrap gen -p git.perx.ru/perxis/perxis-go/pkg/invitations -i Invitations -t ..\..\..\assets\templates\middleware\telemetry -o telemetry_middleware.go -l ""
//go:generate gowrap gen -p git.perx.ru/perxis/perxis-go/pkg/invitations -i Invitations -t ../../../assets/templates/middleware/telemetry_content -o telemetry_middleware.go -l ""
// source template: https://github.com/hexdigest/gowrap/blob/master/templates/opentelemetry
......@@ -12,6 +12,8 @@ import (
"context"
"time"
oid "git.perx.ru/perxis/perxis-go/id"
"git.perx.ru/perxis/perxis-go/pkg/auth"
"git.perx.ru/perxis/perxis-go/pkg/invitations"
"git.perx.ru/perxis/perxis-go/pkg/options"
"git.perx.ru/perxis/perxis-go/pkg/telemetry/metrics"
......@@ -21,6 +23,10 @@ import (
"go.opentelemetry.io/otel/trace"
)
type SpaceGetter interface {
GetSpaceID() string
}
// telemetryMiddleware implements invitations.Invitations interface instrumented with opentracing spans
type telemetryMiddleware struct {
invitations.Invitations
......@@ -51,10 +57,12 @@ func TelemetryMiddleware(base invitations.Invitations, instance string, spanDeco
// Accept implements invitations.Invitations
func (_d telemetryMiddleware) Accept(ctx context.Context, invitationId string, userId string) (err error) {
attributes := otelmetric.WithAttributeSet(attribute.NewSet(
var att = []attribute.KeyValue{
attribute.String("service", "Invitations"),
attribute.String("method", "Accept"),
))
}
attributes := otelmetric.WithAttributeSet(attribute.NewSet(att...))
_d.requestMetrics.Total.Add(ctx, 1, attributes)
......@@ -64,6 +72,30 @@ func (_d telemetryMiddleware) Accept(ctx context.Context, invitationId string, u
defer func() {
_d.requestMetrics.DurationMilliseconds.Record(ctx, time.Since(start).Milliseconds(), attributes)
var spID string
vv := []interface{}{ctx, invitationId, userId, err}
for _, v := range vv {
if v == nil {
continue
}
if sg, ok := v.(SpaceGetter); ok {
spID = sg.GetSpaceID()
break
}
}
if spID != "" {
principal := auth.GetPrincipal(ctx)
if accessor, ok := principal.(auth.SpaceAccessor); ok {
principal = accessor.Space(spID)
}
caller, _ := oid.NewObjectId(principal)
att = append(att, attribute.String("spaceID", spID))
att = append(att, attribute.String("caller", caller.String()))
}
_d.requestMetrics.Total.Add(ctx, 1, otelmetric.WithAttributeSet(attribute.NewSet(att...)))
if _d._spanDecorator != nil {
_d._spanDecorator(_span, map[string]interface{}{
"ctx": ctx,
......@@ -85,10 +117,12 @@ func (_d telemetryMiddleware) Accept(ctx context.Context, invitationId string, u
// Create implements invitations.Invitations
func (_d telemetryMiddleware) Create(ctx context.Context, invitation *invitations.Invitation) (created *invitations.Invitation, err error) {
attributes := otelmetric.WithAttributeSet(attribute.NewSet(
var att = []attribute.KeyValue{
attribute.String("service", "Invitations"),
attribute.String("method", "Create"),
))
}
attributes := otelmetric.WithAttributeSet(attribute.NewSet(att...))
_d.requestMetrics.Total.Add(ctx, 1, attributes)
......@@ -98,6 +132,30 @@ func (_d telemetryMiddleware) Create(ctx context.Context, invitation *invitation
defer func() {
_d.requestMetrics.DurationMilliseconds.Record(ctx, time.Since(start).Milliseconds(), attributes)
var spID string
vv := []interface{}{ctx, invitation, created, err}
for _, v := range vv {
if v == nil {
continue
}
if sg, ok := v.(SpaceGetter); ok {
spID = sg.GetSpaceID()
break
}
}
if spID != "" {
principal := auth.GetPrincipal(ctx)
if accessor, ok := principal.(auth.SpaceAccessor); ok {
principal = accessor.Space(spID)
}
caller, _ := oid.NewObjectId(principal)
att = append(att, attribute.String("spaceID", spID))
att = append(att, attribute.String("caller", caller.String()))
}
_d.requestMetrics.Total.Add(ctx, 1, otelmetric.WithAttributeSet(attribute.NewSet(att...)))
if _d._spanDecorator != nil {
_d._spanDecorator(_span, map[string]interface{}{
"ctx": ctx,
......@@ -119,10 +177,12 @@ func (_d telemetryMiddleware) Create(ctx context.Context, invitation *invitation
// Delete implements invitations.Invitations
func (_d telemetryMiddleware) Delete(ctx context.Context, invitationId string) (err error) {
attributes := otelmetric.WithAttributeSet(attribute.NewSet(
var att = []attribute.KeyValue{
attribute.String("service", "Invitations"),
attribute.String("method", "Delete"),
))
}
attributes := otelmetric.WithAttributeSet(attribute.NewSet(att...))
_d.requestMetrics.Total.Add(ctx, 1, attributes)
......@@ -132,6 +192,30 @@ func (_d telemetryMiddleware) Delete(ctx context.Context, invitationId string) (
defer func() {
_d.requestMetrics.DurationMilliseconds.Record(ctx, time.Since(start).Milliseconds(), attributes)
var spID string
vv := []interface{}{ctx, invitationId, err}
for _, v := range vv {
if v == nil {
continue
}
if sg, ok := v.(SpaceGetter); ok {
spID = sg.GetSpaceID()
break
}
}
if spID != "" {
principal := auth.GetPrincipal(ctx)
if accessor, ok := principal.(auth.SpaceAccessor); ok {
principal = accessor.Space(spID)
}
caller, _ := oid.NewObjectId(principal)
att = append(att, attribute.String("spaceID", spID))
att = append(att, attribute.String("caller", caller.String()))
}
_d.requestMetrics.Total.Add(ctx, 1, otelmetric.WithAttributeSet(attribute.NewSet(att...)))
if _d._spanDecorator != nil {
_d._spanDecorator(_span, map[string]interface{}{
"ctx": ctx,
......@@ -152,10 +236,12 @@ func (_d telemetryMiddleware) Delete(ctx context.Context, invitationId string) (
// Find implements invitations.Invitations
func (_d telemetryMiddleware) Find(ctx context.Context, filter *invitations.Filter, opts *options.FindOptions) (invitations []*invitations.Invitation, total int, err error) {
attributes := otelmetric.WithAttributeSet(attribute.NewSet(
var att = []attribute.KeyValue{
attribute.String("service", "Invitations"),
attribute.String("method", "Find"),
))
}
attributes := otelmetric.WithAttributeSet(attribute.NewSet(att...))
_d.requestMetrics.Total.Add(ctx, 1, attributes)
......@@ -165,6 +251,30 @@ func (_d telemetryMiddleware) Find(ctx context.Context, filter *invitations.Filt
defer func() {
_d.requestMetrics.DurationMilliseconds.Record(ctx, time.Since(start).Milliseconds(), attributes)
var spID string
vv := []interface{}{ctx, filter, opts, invitations, total, err}
for _, v := range vv {
if v == nil {
continue
}
if sg, ok := v.(SpaceGetter); ok {
spID = sg.GetSpaceID()
break
}
}
if spID != "" {
principal := auth.GetPrincipal(ctx)
if accessor, ok := principal.(auth.SpaceAccessor); ok {
principal = accessor.Space(spID)
}
caller, _ := oid.NewObjectId(principal)
att = append(att, attribute.String("spaceID", spID))
att = append(att, attribute.String("caller", caller.String()))
}
_d.requestMetrics.Total.Add(ctx, 1, otelmetric.WithAttributeSet(attribute.NewSet(att...)))
if _d._spanDecorator != nil {
_d._spanDecorator(_span, map[string]interface{}{
"ctx": ctx,
......@@ -188,10 +298,12 @@ func (_d telemetryMiddleware) Find(ctx context.Context, filter *invitations.Filt
// Get implements invitations.Invitations
func (_d telemetryMiddleware) Get(ctx context.Context, invitationId string) (invitation *invitations.Invitation, err error) {
attributes := otelmetric.WithAttributeSet(attribute.NewSet(
var att = []attribute.KeyValue{
attribute.String("service", "Invitations"),
attribute.String("method", "Get"),
))
}
attributes := otelmetric.WithAttributeSet(attribute.NewSet(att...))
_d.requestMetrics.Total.Add(ctx, 1, attributes)
......@@ -201,6 +313,30 @@ func (_d telemetryMiddleware) Get(ctx context.Context, invitationId string) (inv
defer func() {
_d.requestMetrics.DurationMilliseconds.Record(ctx, time.Since(start).Milliseconds(), attributes)
var spID string
vv := []interface{}{ctx, invitationId, invitation, err}
for _, v := range vv {
if v == nil {
continue
}
if sg, ok := v.(SpaceGetter); ok {
spID = sg.GetSpaceID()
break
}
}
if spID != "" {
principal := auth.GetPrincipal(ctx)
if accessor, ok := principal.(auth.SpaceAccessor); ok {
principal = accessor.Space(spID)
}
caller, _ := oid.NewObjectId(principal)
att = append(att, attribute.String("spaceID", spID))
att = append(att, attribute.String("caller", caller.String()))
}
_d.requestMetrics.Total.Add(ctx, 1, otelmetric.WithAttributeSet(attribute.NewSet(att...)))
if _d._spanDecorator != nil {
_d._spanDecorator(_span, map[string]interface{}{
"ctx": ctx,
......
......@@ -150,6 +150,10 @@ func (i *Item) GetID() string {
return i.ID
}
func (i *Item) GetSpaceID() string {
return i.SpaceID
}
func (i *Item) Clone() *Item {
itm := *i
itm.Data = data.CloneMap(i.Data)
......
This diff is collapsed.
......@@ -55,6 +55,10 @@ func (locale *Locale) Key() string {
return locale.ID
}
func (locale *Locale) GetSpaceID() string {
return locale.SpaceID
}
// Возвращает язык локали, например "en", "ru"
func (locale *Locale) GetLanguage() string {
lang, err := language.Parse(locale.Code)
......
// Code generated by gowrap. DO NOT EDIT.
// template: ../../../assets/templates/middleware/telemetry
// template: ../../../assets/templates/middleware/telemetry_content
// 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 ""
//go:generate gowrap gen -p git.perx.ru/perxis/perxis-go/pkg/locales -i Locales -t ../../../assets/templates/middleware/telemetry_content -o telemetry_middleware.go -l ""
// source template: https://github.com/hexdigest/gowrap/blob/master/templates/opentelemetry
......@@ -12,6 +12,8 @@ import (
"context"
"time"
oid "git.perx.ru/perxis/perxis-go/id"
"git.perx.ru/perxis/perxis-go/pkg/auth"
"git.perx.ru/perxis/perxis-go/pkg/locales"
"git.perx.ru/perxis/perxis-go/pkg/telemetry/metrics"
"go.opentelemetry.io/otel"
......@@ -20,6 +22,10 @@ import (
"go.opentelemetry.io/otel/trace"
)
type SpaceGetter interface {
GetSpaceID() string
}
// telemetryMiddleware implements locales.Locales interface instrumented with opentracing spans
type telemetryMiddleware struct {
locales.Locales
......@@ -50,10 +56,12 @@ func TelemetryMiddleware(base locales.Locales, instance string, spanDecorator ..
// Create implements locales.Locales
func (_d telemetryMiddleware) Create(ctx context.Context, locale *locales.Locale) (created *locales.Locale, err error) {
attributes := otelmetric.WithAttributeSet(attribute.NewSet(
var att = []attribute.KeyValue{
attribute.String("service", "Locales"),
attribute.String("method", "Create"),
))
}
attributes := otelmetric.WithAttributeSet(attribute.NewSet(att...))
_d.requestMetrics.Total.Add(ctx, 1, attributes)
......@@ -63,6 +71,30 @@ func (_d telemetryMiddleware) Create(ctx context.Context, locale *locales.Locale
defer func() {
_d.requestMetrics.DurationMilliseconds.Record(ctx, time.Since(start).Milliseconds(), attributes)
var spID string
vv := []interface{}{ctx, locale, created, err}
for _, v := range vv {
if v == nil {
continue
}
if sg, ok := v.(SpaceGetter); ok {
spID = sg.GetSpaceID()
break
}
}
if spID != "" {
principal := auth.GetPrincipal(ctx)
if accessor, ok := principal.(auth.SpaceAccessor); ok {
principal = accessor.Space(spID)
}
caller, _ := oid.NewObjectId(principal)
att = append(att, attribute.String("spaceID", spID))
att = append(att, attribute.String("caller", caller.String()))
}
_d.requestMetrics.Total.Add(ctx, 1, otelmetric.WithAttributeSet(attribute.NewSet(att...)))
if _d._spanDecorator != nil {
_d._spanDecorator(_span, map[string]interface{}{
"ctx": ctx,
......@@ -84,10 +116,12 @@ func (_d telemetryMiddleware) Create(ctx context.Context, locale *locales.Locale
// Delete implements locales.Locales
func (_d telemetryMiddleware) Delete(ctx context.Context, spaceId string, localeId string) (err error) {
attributes := otelmetric.WithAttributeSet(attribute.NewSet(
var att = []attribute.KeyValue{
attribute.String("service", "Locales"),
attribute.String("method", "Delete"),
))
}
attributes := otelmetric.WithAttributeSet(attribute.NewSet(att...))
_d.requestMetrics.Total.Add(ctx, 1, attributes)
......@@ -97,6 +131,21 @@ func (_d telemetryMiddleware) Delete(ctx context.Context, spaceId string, locale
defer func() {
_d.requestMetrics.DurationMilliseconds.Record(ctx, time.Since(start).Milliseconds(), attributes)
var spID string
spID = spaceId
if spID != "" {
principal := auth.GetPrincipal(ctx)
if accessor, ok := principal.(auth.SpaceAccessor); ok {
principal = accessor.Space(spID)
}
caller, _ := oid.NewObjectId(principal)
att = append(att, attribute.String("spaceID", spID))
att = append(att, attribute.String("caller", caller.String()))
}
_d.requestMetrics.Total.Add(ctx, 1, otelmetric.WithAttributeSet(attribute.NewSet(att...)))
if _d._spanDecorator != nil {
_d._spanDecorator(_span, map[string]interface{}{
"ctx": ctx,
......@@ -118,10 +167,12 @@ func (_d telemetryMiddleware) Delete(ctx context.Context, spaceId string, locale
// List implements locales.Locales
func (_d telemetryMiddleware) List(ctx context.Context, spaceId string) (locales []*locales.Locale, err error) {
attributes := otelmetric.WithAttributeSet(attribute.NewSet(
var att = []attribute.KeyValue{
attribute.String("service", "Locales"),
attribute.String("method", "List"),
))
}
attributes := otelmetric.WithAttributeSet(attribute.NewSet(att...))
_d.requestMetrics.Total.Add(ctx, 1, attributes)
......@@ -131,6 +182,21 @@ func (_d telemetryMiddleware) List(ctx context.Context, spaceId string) (locales
defer func() {
_d.requestMetrics.DurationMilliseconds.Record(ctx, time.Since(start).Milliseconds(), attributes)
var spID string
spID = spaceId
if spID != "" {
principal := auth.GetPrincipal(ctx)
if accessor, ok := principal.(auth.SpaceAccessor); ok {
principal = accessor.Space(spID)
}
caller, _ := oid.NewObjectId(principal)
att = append(att, attribute.String("spaceID", spID))
att = append(att, attribute.String("caller", caller.String()))
}
_d.requestMetrics.Total.Add(ctx, 1, otelmetric.WithAttributeSet(attribute.NewSet(att...)))
if _d._spanDecorator != nil {
_d._spanDecorator(_span, map[string]interface{}{
"ctx": ctx,
......@@ -152,10 +218,12 @@ func (_d telemetryMiddleware) List(ctx context.Context, spaceId string) (locales
// Update implements locales.Locales
func (_d telemetryMiddleware) Update(ctx context.Context, locale *locales.Locale) (err error) {
attributes := otelmetric.WithAttributeSet(attribute.NewSet(
var att = []attribute.KeyValue{
attribute.String("service", "Locales"),
attribute.String("method", "Update"),
))
}
attributes := otelmetric.WithAttributeSet(attribute.NewSet(att...))
_d.requestMetrics.Total.Add(ctx, 1, attributes)
......@@ -165,6 +233,30 @@ func (_d telemetryMiddleware) Update(ctx context.Context, locale *locales.Locale
defer func() {
_d.requestMetrics.DurationMilliseconds.Record(ctx, time.Since(start).Milliseconds(), attributes)
var spID string
vv := []interface{}{ctx, locale, err}
for _, v := range vv {
if v == nil {
continue
}
if sg, ok := v.(SpaceGetter); ok {
spID = sg.GetSpaceID()
break
}
}
if spID != "" {
principal := auth.GetPrincipal(ctx)
if accessor, ok := principal.(auth.SpaceAccessor); ok {
principal = accessor.Space(spID)
}
caller, _ := oid.NewObjectId(principal)
att = append(att, attribute.String("spaceID", spID))
att = append(att, attribute.String("caller", caller.String()))
}
_d.requestMetrics.Total.Add(ctx, 1, otelmetric.WithAttributeSet(attribute.NewSet(att...)))
if _d._spanDecorator != nil {
_d._spanDecorator(_span, map[string]interface{}{
"ctx": ctx,
......
......@@ -21,7 +21,7 @@ func WithLog(s members.Members, logger *zap.Logger, log_access bool) members.Mem
if log_access {
s = AccessLoggingMiddleware(logger)(s)
}
s = LoggingMiddleware(logger)(s)
s = ErrorLoggingMiddleware(logger)(s)
s = RecoveringMiddleware(logger)(s)
return s
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment