-
Notifications
You must be signed in to change notification settings - Fork 71
/
metrics.go
197 lines (167 loc) · 7.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
package telemetry
import (
"context"
"time"
"github.com/prometheus/client_golang/prometheus"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/sdk/instrumentation"
msdk "go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/resource"
semconv "go.opentelemetry.io/otel/semconv/v1.18.0"
)
const (
ProviderName = "flagd"
FeatureFlagReasonKey = attribute.Key("feature_flag.reason")
ExceptionTypeKey = attribute.Key("ExceptionTypeKeyName")
httpRequestDurationMetric = "http.server.duration"
httpResponseSizeMetric = "http.server.response.size"
httpActiveRequestsMetric = "http.server.active_requests"
impressionMetric = "feature_flag." + ProviderName + ".impression"
reasonMetric = "feature_flag." + ProviderName + ".evaluation.reason"
)
type IMetricsRecorder interface {
HTTPAttributes(svcName, url, method, code string) []attribute.KeyValue
HTTPRequestDuration(ctx context.Context, duration time.Duration, attrs []attribute.KeyValue)
HTTPResponseSize(ctx context.Context, sizeBytes int64, attrs []attribute.KeyValue)
InFlightRequestStart(ctx context.Context, attrs []attribute.KeyValue)
InFlightRequestEnd(ctx context.Context, attrs []attribute.KeyValue)
RecordEvaluation(ctx context.Context, err error, reason, variant, key string)
Impressions(ctx context.Context, reason, variant, key string)
}
type NoopMetricsRecorder struct{}
func (NoopMetricsRecorder) HTTPAttributes(_, _, _, _ string) []attribute.KeyValue {
return []attribute.KeyValue{}
}
func (NoopMetricsRecorder) HTTPRequestDuration(_ context.Context, _ time.Duration, _ []attribute.KeyValue) {
}
func (NoopMetricsRecorder) HTTPResponseSize(_ context.Context, _ int64, _ []attribute.KeyValue) {
}
func (NoopMetricsRecorder) InFlightRequestStart(_ context.Context, _ []attribute.KeyValue) {
}
func (NoopMetricsRecorder) InFlightRequestEnd(_ context.Context, _ []attribute.KeyValue) {
}
func (NoopMetricsRecorder) RecordEvaluation(_ context.Context, _ error, _, _, _ string) {
}
func (NoopMetricsRecorder) Impressions(_ context.Context, _, _, _ string) {
}
type MetricsRecorder struct {
httpRequestDurHistogram metric.Float64Histogram
httpResponseSizeHistogram metric.Float64Histogram
httpRequestsInflight metric.Int64UpDownCounter
impressions metric.Int64Counter
reasons metric.Int64Counter
}
func (r MetricsRecorder) HTTPAttributes(svcName, url, method, code string) []attribute.KeyValue {
return []attribute.KeyValue{
semconv.ServiceNameKey.String(svcName),
semconv.HTTPURLKey.String(url),
semconv.HTTPMethodKey.String(method),
semconv.HTTPStatusCodeKey.String(code),
}
}
func (r MetricsRecorder) HTTPRequestDuration(ctx context.Context, duration time.Duration, attrs []attribute.KeyValue) {
r.httpRequestDurHistogram.Record(ctx, duration.Seconds(), metric.WithAttributes(attrs...))
}
func (r MetricsRecorder) HTTPResponseSize(ctx context.Context, sizeBytes int64, attrs []attribute.KeyValue) {
r.httpResponseSizeHistogram.Record(ctx, float64(sizeBytes), metric.WithAttributes(attrs...))
}
func (r MetricsRecorder) InFlightRequestStart(ctx context.Context, attrs []attribute.KeyValue) {
r.httpRequestsInflight.Add(ctx, 1, metric.WithAttributes(attrs...))
}
func (r MetricsRecorder) InFlightRequestEnd(ctx context.Context, attrs []attribute.KeyValue) {
r.httpRequestsInflight.Add(ctx, -1, metric.WithAttributes(attrs...))
}
func (r MetricsRecorder) RecordEvaluation(ctx context.Context, err error, reason, variant, key string) {
if err == nil {
r.Impressions(ctx, reason, variant, key)
}
r.Reasons(ctx, key, reason, err)
}
func (r MetricsRecorder) Impressions(ctx context.Context, reason, variant, key string) {
r.impressions.Add(ctx,
1,
metric.WithAttributes(append(SemConvFeatureFlagAttributes(key, variant), FeatureFlagReason(reason))...))
}
func (r MetricsRecorder) Reasons(ctx context.Context, key string, reason string, err error) {
attrs := []attribute.KeyValue{
semconv.FeatureFlagProviderName(ProviderName),
FeatureFlagReason(reason),
}
if err == nil {
// record flag key only if evaluation is successful
attrs = append(attrs, semconv.FeatureFlagKey(key))
} else {
attrs = append(attrs, ExceptionType(err.Error()))
}
r.reasons.Add(ctx, 1, metric.WithAttributes(attrs...))
}
func getDurationView(svcName, viewName string, bucket []float64) msdk.View {
return msdk.NewView(
msdk.Instrument{
// we change aggregation only for instruments with this name and scope
Name: viewName,
Scope: instrumentation.Scope{
Name: svcName,
},
},
msdk.Stream{Aggregation: msdk.AggregationExplicitBucketHistogram{
Boundaries: bucket,
}},
)
}
func FeatureFlagReason(val string) attribute.KeyValue {
return FeatureFlagReasonKey.String(val)
}
func ExceptionType(val string) attribute.KeyValue {
return ExceptionTypeKey.String(val)
}
// NewOTelRecorder creates a MetricsRecorder based on the provided metric.Reader. Note that, metric.NewMeterProvider is
// created here but not registered globally as this is the only place we derive a metric.Meter. Consider global provider
// registration if we need more meters
func NewOTelRecorder(exporter msdk.Reader, resource *resource.Resource, serviceName string) *MetricsRecorder {
// create a metric provider with custom bucket size for histograms
provider := msdk.NewMeterProvider(
msdk.WithReader(exporter),
// for the request duration metric we use the default bucket size which are tailored for response time in seconds
msdk.WithView(getDurationView(httpRequestDurationMetric, serviceName, prometheus.DefBuckets)),
// for response size we want 8 exponential bucket starting from 100 Bytes
msdk.WithView(getDurationView(httpResponseSizeMetric, serviceName, prometheus.ExponentialBuckets(100, 10, 8))),
// set entity producing telemetry
msdk.WithResource(resource),
)
meter := provider.Meter(serviceName)
// we can ignore errors from OpenTelemetry since they could occur if we select the wrong aggregator
hduration, _ := meter.Float64Histogram(
httpRequestDurationMetric,
metric.WithDescription("Measures the duration of inbound HTTP requests."),
metric.WithUnit("s"),
)
hsize, _ := meter.Float64Histogram(
httpResponseSizeMetric,
metric.WithDescription("Measures the size of HTTP request messages (compressed)."),
metric.WithUnit("By"),
)
reqCounter, _ := meter.Int64UpDownCounter(
httpActiveRequestsMetric,
metric.WithDescription("Measures the number of concurrent HTTP requests that are currently in-flight."),
metric.WithUnit("{request}"),
)
impressions, _ := meter.Int64Counter(
impressionMetric,
metric.WithDescription("Measures the number of evaluations for a given flag."),
metric.WithUnit("{impression}"),
)
reasons, _ := meter.Int64Counter(
reasonMetric,
metric.WithDescription("Measures the number of evaluations for a given reason."),
metric.WithUnit("{reason}"),
)
return &MetricsRecorder{
httpRequestDurHistogram: hduration,
httpResponseSizeHistogram: hsize,
httpRequestsInflight: reqCounter,
impressions: impressions,
reasons: reasons,
}
}