-
Notifications
You must be signed in to change notification settings - Fork 6
/
traced_configurator.go
92 lines (75 loc) · 2.82 KB
/
traced_configurator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package helper
import (
"context"
"github.com/nais/device/internal/otel"
"github.com/nais/device/internal/pb"
ototel "go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
)
type TracedConfigurator struct {
Wrapped OSConfigurator
counter metric.Int64Counter
routesCounter metric.Int64Histogram
}
var _ OSConfigurator = &TracedConfigurator{}
func NewTracedConfigurator(w OSConfigurator) *TracedConfigurator {
meter := ototel.Meter("configurator")
counter, err := meter.Int64Counter("configurator_calls",
metric.WithDescription("The number of calls to the configurator"),
metric.WithUnit("{call}"))
if err != nil {
panic(err)
}
routesCounter, err := meter.Int64Histogram("configurator_routes",
metric.WithDescription("The number of routes created by the configurator"),
metric.WithExplicitBucketBoundaries(20, 40, 60, 80, 100, 120, 140, 160, 180, 200),
)
if err != nil {
panic(err)
}
return &TracedConfigurator{
Wrapped: w,
counter: counter,
routesCounter: routesCounter,
}
}
func (tc *TracedConfigurator) SetupInterface(ctx context.Context, cfg *pb.Configuration) error {
ctx, span := otel.Start(ctx, "SetupInterface")
defer span.End()
err := tc.Wrapped.SetupInterface(ctx, cfg)
span.RecordError(err)
tc.counter.Add(ctx, 1, metric.WithAttributes(attribute.String("method", "SetupInterface"), attribute.Bool("error", err != nil)))
return err
}
func (tc *TracedConfigurator) TeardownInterface(ctx context.Context) error {
ctx, span := otel.Start(ctx, "TeardownInterface")
defer span.End()
err := tc.Wrapped.TeardownInterface(ctx)
span.RecordError(err)
tc.counter.Add(ctx, 1, metric.WithAttributes(attribute.String("method", "TeardownInterface"), attribute.Bool("error", err != nil)))
return err
}
func (tc *TracedConfigurator) SyncConf(ctx context.Context, cfg *pb.Configuration) error {
ctx, span := otel.Start(ctx, "SyncConf")
defer span.End()
err := tc.Wrapped.SyncConf(ctx, cfg)
span.RecordError(err)
tc.counter.Add(ctx, 1, metric.WithAttributes(attribute.String("method", "SyncConf"), attribute.Bool("error", err != nil)))
return err
}
func (tc *TracedConfigurator) SetupRoutes(ctx context.Context, gateways []*pb.Gateway) (int, error) {
ctx, span := otel.Start(ctx, "SetupRoutes")
defer span.End()
i, err := tc.Wrapped.SetupRoutes(ctx, gateways)
span.RecordError(err)
span.SetAttributes(attribute.Int("routes", i))
tc.counter.Add(ctx, 1, metric.WithAttributes(attribute.String("method", "SetupRoutes"), attribute.Bool("error", err != nil)))
tc.routesCounter.Record(ctx, int64(i))
return i, err
}
func (tc *TracedConfigurator) Prerequisites() error {
err := tc.Wrapped.Prerequisites()
tc.counter.Add(context.Background(), 1, metric.WithAttributes(attribute.String("method", "Prerequisites"), attribute.Bool("error", err != nil)))
return err
}