-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmetrics.go
362 lines (284 loc) · 9.81 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package metrics
import (
"context"
"fmt"
"net/http"
"os"
"strings"
"sync"
prometheusclient "github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
dto "github.com/prometheus/client_model/go"
"github.com/takenet/deckard/internal/config"
"github.com/takenet/deckard/internal/logger"
"github.com/takenet/deckard/internal/project"
"github.com/takenet/deckard/internal/queue/utils"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/exporters/prometheus"
"go.opentelemetry.io/otel/metric"
instrument "go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/resource"
"go.uber.org/zap"
)
var (
prometheusStarted bool
mutex = sync.Mutex{}
meter metric.Meter
MetricsMap *QueueMetricsMap
// Keep tracking of features used by clients to be able to deprecate them
CodeUsage metric.Int64Counter
// Housekeepermetric
HousekeeperExceedingStorageRemoved metric.Int64Counter
HousekeeperExceedingCacheRemoved metric.Int64Counter
HousekeeperTTLStorageRemoved metric.Int64Counter
HousekeeperTTLCacheRemoved metric.Int64Counter
HousekeeperUnlock metric.Int64Counter
HousekeeperTaskLatency metric.Int64Histogram
HousekeeperOldestMessage metric.Int64ObservableGauge
HousekeeperTotalElements metric.Int64ObservableGauge
// Message Pool
QueueTimeout metric.Int64Counter
QueueAck metric.Int64Counter
QueueNack metric.Int64Counter
QueueEmptyQueue metric.Int64Counter
QueueEmptyQueueStorage metric.Int64Counter
QueueNotFoundInStorage metric.Int64Counter
// Storage
StorageLatency metric.Int64Histogram
// Cache
CacheLatency metric.Int64Histogram
// Auditor
AuditorAddToStoreLatency metric.Int64Histogram
AuditorStoreLatency metric.Int64Histogram
exporter *prometheus.Exporter
registry *WrappedRegistry
)
func panicInstrumentationError(err error) {
if err != nil {
logger.S(context.Background()).Panicf("Failed to initialize instrument: %v", err)
}
}
func init() {
registry = NewWrappedRegistry(prometheusclient.NewRegistry(), createDefaultMetrics()...)
var err error
exporter, err = prometheus.New(
prometheus.WithRegisterer(registry),
prometheus.WithoutUnits(),
prometheus.WithAggregationSelector(func(ik instrument.InstrumentKind) instrument.Aggregation {
switch ik {
case instrument.InstrumentKindHistogram:
return instrument.AggregationExplicitBucketHistogram{
Boundaries: getHistogramBuckets(),
NoMinMax: false,
}
}
return instrument.DefaultAggregationSelector(ik)
}))
if err != nil {
logger.S(context.Background()).Errorf("Failed to initialize prometheus exporter %v", err)
}
provider := instrument.NewMeterProvider(
instrument.WithReader(exporter),
instrument.WithResource(resource.Environment()),
)
otel.SetMeterProvider(provider)
createMetrics()
}
func ListenAndServe() {
if !config.MetricsEnabled.GetBool() || prometheusStarted {
return
}
mutex.Lock()
defer mutex.Unlock()
http.Handle(config.MetricsPath.Get(), promhttp.HandlerFor(registry, promhttp.HandlerOpts{
EnableOpenMetrics: config.MetricsOpenMetricsEnabled.GetBool(),
}))
prometheusStarted = true
port := fmt.Sprintf(":%d", config.MetricsPort.GetInt())
logger.S(context.Background()).Debugf("Listening to metrics at %s%s", port, config.MetricsPath.Get())
err := http.ListenAndServe(port, nil)
if err != nil {
logger.S(context.Background()).Error("Error starting prometheus exporter.", err)
}
}
func createDefaultMetrics() []*dto.LabelPair {
result := make([]*dto.LabelPair, 0)
strPtr := func(s string) *string { return &s }
otelAttributes := os.Getenv("OTEL_RESOURCE_ATTRIBUTES")
if !strings.Contains(otelAttributes, "service.version") {
if otelAttributes != "" {
otelAttributes = otelAttributes + ","
}
otelAttributes = otelAttributes + "service.version=" + project.Version
}
attributes := strings.Split(otelAttributes, ",")
for _, attribute := range attributes {
parts := strings.Split(attribute, "=")
if len(parts) != 2 {
continue
}
result = append(result, &dto.LabelPair{
Name: strPtr(strings.Replace(parts[0], ".", "_", -1)),
Value: strPtr(parts[1]),
})
}
otelServiceName := os.Getenv("OTEL_SERVICE_NAME")
if otelServiceName == "" {
otelServiceName = project.Name
}
result = append(result, &dto.LabelPair{
Name: strPtr("service_name"),
Value: strPtr(otelServiceName),
})
return result
}
func createMetrics() {
meter = otel.GetMeterProvider().Meter(project.Name)
MetricsMap = NewQueueMetricsMap()
// Housekeeper
var err error
CodeUsage, err = meter.Int64Counter(
"deckard_code_usage",
metric.WithDescription("Number of times a specific feature was used by a client"),
)
panicInstrumentationError(err)
HousekeeperExceedingStorageRemoved, err = meter.Int64Counter(
"deckard_exceeding_messages_removed",
metric.WithDescription("Number of messages removed from storage for exceeding maximum queue size"),
)
panicInstrumentationError(err)
HousekeeperExceedingCacheRemoved, err = meter.Int64Counter(
"deckard_exceeding_messages_cache_removed",
metric.WithDescription("Number of messages removed from cache for exceeding maximum queue size"),
)
panicInstrumentationError(err)
HousekeeperTaskLatency, err = meter.Int64Histogram(
"deckard_housekeeper_task_latency",
metric.WithDescription("Time in milliseconds to complete a housekeeper task."),
metric.WithUnit("ms"),
)
panicInstrumentationError(err)
HousekeeperOldestMessage, err = meter.Int64ObservableGauge(
"deckard_oldest_message",
metric.WithDescription("Time the oldest queue message was used."),
metric.WithInt64Callback(func(_ context.Context, obs metric.Int64Observer) error {
return metrifyOldestMessages(obs)
}))
panicInstrumentationError(err)
HousekeeperTotalElements, err = meter.Int64ObservableGauge(
"deckard_total_messages",
metric.WithDescription("Number of messages a queue has."),
metric.WithInt64Callback(func(_ context.Context, obs metric.Int64Observer) error {
return metrifyTotalElements(obs)
}))
panicInstrumentationError(err)
HousekeeperTTLStorageRemoved, err = meter.Int64Counter(
"deckard_ttl_messages_removed",
metric.WithDescription("Number of messages removed from storage for ttl"),
)
panicInstrumentationError(err)
HousekeeperTTLCacheRemoved, err = meter.Int64Counter(
"deckard_ttl_messages_cache_removed",
metric.WithDescription("Number of messages removed from cache for ttl"),
)
panicInstrumentationError(err)
HousekeeperUnlock, err = meter.Int64Counter(
"deckard_message_unlock",
metric.WithDescription("Number of unlocked messages."),
)
panicInstrumentationError(err)
// Queue
QueueTimeout, err = meter.Int64Counter(
"deckard_message_timeout",
metric.WithDescription("Number of message timeouts"),
)
panicInstrumentationError(err)
QueueAck, err = meter.Int64Counter(
"deckard_ack",
metric.WithDescription("Number of acks received"),
)
panicInstrumentationError(err)
QueueNack, err = meter.Int64Counter(
"deckard_nack",
metric.WithDescription("Number of nacks received"),
)
panicInstrumentationError(err)
QueueEmptyQueue, err = meter.Int64Counter(
"deckard_messages_empty_queue",
metric.WithDescription("Number of times a pull is made against an empty queue."),
)
panicInstrumentationError(err)
QueueEmptyQueueStorage, err = meter.Int64Counter(
"deckard_messages_empty_queue_not_found_storage",
metric.WithDescription("Number of times a pull is made against an empty queue because the messages were not found in the storage."),
)
panicInstrumentationError(err)
QueueNotFoundInStorage, err = meter.Int64Counter(
"deckard_messages_not_found_in_storage",
metric.WithDescription("Number of messages in cache but not found in the storage."),
)
panicInstrumentationError(err)
// Storage
StorageLatency, err = meter.Int64Histogram(
"deckard_storage_latency",
metric.WithDescription("Storage access latency"),
metric.WithUnit("ms"),
)
panicInstrumentationError(err)
// Cache
CacheLatency, err = meter.Int64Histogram(
"deckard_cache_latency",
metric.WithDescription("Cache access latency"),
metric.WithUnit("ms"),
)
panicInstrumentationError(err)
// Auditor
AuditorAddToStoreLatency, err = meter.Int64Histogram(
"deckard_auditor_store_add_latency",
metric.WithDescription("Latency to add an entry to be saved by the audit storer"),
metric.WithUnit("ms"),
)
panicInstrumentationError(err)
AuditorStoreLatency, err = meter.Int64Histogram(
"deckard_auditor_store_latency",
metric.WithDescription("Latency sending elements to the audit database"),
metric.WithUnit("ms"),
)
panicInstrumentationError(err)
}
func metrifyOldestMessages(obs metric.Int64Observer) error {
return metrify(obs, &MetricsMap.OldestElement)
}
func metrifyTotalElements(obs metric.Int64Observer) error {
return metrify(obs, &MetricsMap.TotalElements)
}
func metrify(obs metric.Int64Observer, data *map[string]int64) error {
for key := range *data {
obs.Observe((*data)[key], metric.WithAttributeSet(attribute.NewSet(attribute.String("queue", key))))
}
return nil
}
func getHistogramBuckets() []float64 {
buckets, err := parseHistogramBuckets(config.MetricsHistogramBuckets.Get())
if len(buckets) == 0 || err != nil {
logger.L(context.Background()).Error("Error parsing buckets. Using default", zap.Error(err))
buckets, _ = parseHistogramBuckets(config.MetricsHistogramBuckets.GetDefault().(string))
}
return buckets
}
func parseHistogramBuckets(data string) ([]float64, error) {
if data == "" {
return []float64{}, nil
}
buckets := strings.Split(data, ",")
bucketsFloat := make([]float64, len(buckets))
for i, value := range buckets {
var err error
bucketsFloat[i], err = utils.StrToFloat64(strings.TrimSpace(value))
if err != nil {
return nil, err
}
}
return bucketsFloat, nil
}