-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathmetrics.go
198 lines (158 loc) · 6.04 KB
/
metrics.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package otel
import (
"context"
"github.com/open-feature/go-sdk/openfeature"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
api "go.opentelemetry.io/otel/metric"
semconv "go.opentelemetry.io/otel/semconv/v1.18.0"
)
const (
meterName = "go.openfeature.dev"
evaluationActive = "feature_flag.evaluation_active_count"
evaluationRequests = "feature_flag.evaluation_requests_total"
evaluationSuccess = "feature_flag.evaluation_success_total"
evaluationErrors = "feature_flag.evaluation_error_total"
)
type MetricsHook struct {
activeCounter api.Int64UpDownCounter
requestCounter api.Int64Counter
successCounter api.Int64Counter
errorCounter api.Int64Counter
flagEvalMetadataDimensions []DimensionDescription
attributeMapperCallback func(openfeature.FlagMetadata) []attribute.KeyValue
}
var _ openfeature.Hook = &MetricsHook{}
// NewMetricsHook builds a metric hook backed by a globally set metric.MeterProvider.
// Use otel.SetMeterProvider to set the global provider or use NewMetricsHookForProvider.
func NewMetricsHook(opts ...MetricOptions) (*MetricsHook, error) {
return NewMetricsHookForProvider(otel.GetMeterProvider(), opts...)
}
// NewMetricsHookForProvider builds a metric hook backed by metric.MeterProvider.
func NewMetricsHookForProvider(provider api.MeterProvider, opts ...MetricOptions) (*MetricsHook, error) {
meter := provider.Meter(meterName)
activeCounter, err := meter.Int64UpDownCounter(evaluationActive, api.WithDescription("active flag evaluations counter"))
if err != nil {
return nil, err
}
evalCounter, err := meter.Int64Counter(evaluationRequests, api.WithDescription("feature flag evaluation request counter"))
if err != nil {
return nil, err
}
successCounter, err := meter.Int64Counter(evaluationSuccess, api.WithDescription("feature flag evaluation success counter"))
if err != nil {
return nil, err
}
errorCounter, err := meter.Int64Counter(evaluationErrors, api.WithDescription("feature flag evaluation error counter"))
if err != nil {
return nil, err
}
m := &MetricsHook{
activeCounter: activeCounter,
requestCounter: evalCounter,
successCounter: successCounter,
errorCounter: errorCounter,
}
for _, opt := range opts {
opt(m)
}
return m, nil
}
func (h *MetricsHook) Before(ctx context.Context, hCtx openfeature.HookContext,
hint openfeature.HookHints) (*openfeature.EvaluationContext, error) {
h.activeCounter.Add(ctx, +1, api.WithAttributes(semconv.FeatureFlagKey(hCtx.FlagKey())))
h.requestCounter.Add(ctx, 1,
api.WithAttributes(
semconv.FeatureFlagKey(hCtx.FlagKey()),
semconv.FeatureFlagProviderName(hCtx.ProviderMetadata().Name)))
return nil, nil
}
func (h *MetricsHook) After(ctx context.Context, hCtx openfeature.HookContext,
details openfeature.InterfaceEvaluationDetails, hint openfeature.HookHints) error {
attribs := []attribute.KeyValue{
semconv.FeatureFlagKey(hCtx.FlagKey()),
semconv.FeatureFlagProviderName(hCtx.ProviderMetadata().Name)}
if details.Variant != "" {
attribs = append(attribs, semconv.FeatureFlagVariant(details.Variant))
}
if details.Reason != "" {
attribs = append(attribs, attribute.String("reason", string(details.Reason)))
}
fromMetadata := descriptionsToAttributes(details.FlagMetadata, h.flagEvalMetadataDimensions)
attribs = append(attribs, fromMetadata...)
if h.attributeMapperCallback != nil {
attribs = append(attribs, h.attributeMapperCallback(details.FlagMetadata)...)
}
h.successCounter.Add(ctx, 1, api.WithAttributes(attribs...))
return nil
}
func (h *MetricsHook) Error(ctx context.Context, hCtx openfeature.HookContext, err error, hint openfeature.HookHints) {
h.errorCounter.Add(ctx, 1,
api.WithAttributes(
semconv.FeatureFlagKey(hCtx.FlagKey()),
semconv.FeatureFlagProviderName(hCtx.ProviderMetadata().Name),
attribute.String(semconv.ExceptionEventName, err.Error())))
}
func (h *MetricsHook) Finally(ctx context.Context, hCtx openfeature.HookContext, hint openfeature.HookHints) {
h.activeCounter.Add(ctx, -1, api.WithAttributes(semconv.FeatureFlagKey(hCtx.FlagKey())))
}
// Extra options for metrics hook
type Type int
// Type helper
const (
Bool = iota
String
Int
Float
)
// Options of the hook
type MetricOptions func(*MetricsHook)
// DimensionDescription is key and Type description of the dimension
type DimensionDescription struct {
Key string
Type
}
// WithFlagMetadataDimensions allows configuring extra dimensions for feature_flag.evaluation_success_total metric.
// If provided, dimensions will be extracted from openfeature.FlagMetadata & added to the metric with the same key
func WithFlagMetadataDimensions(descriptions ...DimensionDescription) MetricOptions {
return func(metricsHook *MetricsHook) {
metricsHook.flagEvalMetadataDimensions = descriptions
}
}
// WithMetricsAttributeSetter allows to set a extractionCallback which accept openfeature.FlagMetadata and returns
// []attribute.KeyValue derived from those metadata.
func WithMetricsAttributeSetter(callback func(openfeature.FlagMetadata) []attribute.KeyValue) MetricOptions {
return func(metricsHook *MetricsHook) {
metricsHook.attributeMapperCallback = callback
}
}
// descriptionsToAttributes is a helper to extract dimensions from openfeature.FlagMetadata. Missing metadata
// dimensions are ignore.
func descriptionsToAttributes(metadata openfeature.FlagMetadata, descriptions []DimensionDescription) []attribute.KeyValue {
attribs := []attribute.KeyValue{}
for _, dimension := range descriptions {
switch dimension.Type {
case Bool:
b, err := metadata.GetBool(dimension.Key)
if err == nil {
attribs = append(attribs, attribute.Bool(dimension.Key, b))
}
case String:
s, err := metadata.GetString(dimension.Key)
if err == nil {
attribs = append(attribs, attribute.String(dimension.Key, s))
}
case Int:
i, err := metadata.GetInt(dimension.Key)
if err == nil {
attribs = append(attribs, attribute.Int64(dimension.Key, i))
}
case Float:
f, err := metadata.GetFloat(dimension.Key)
if err == nil {
attribs = append(attribs, attribute.Float64(dimension.Key, f))
}
}
}
return attribs
}