-
Notifications
You must be signed in to change notification settings - Fork 20
/
reporter.go
69 lines (57 loc) · 2.42 KB
/
reporter.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
package monitoring
import (
"context"
"github.com/go-masonry/mortar/interfaces/monitor"
)
type mortarReporter struct {
externalMetrics monitor.BricksMetrics
cfg *monitorConfig
registry *externalRegistry
}
// NewMortarReporter creates a new mortar monitoring reporter which is a wrapper to support
// - ContextExtractors
// - Default Tags, for example: {"version":"v1.0.1", "service":"awesome"}
//
// Meaning, it is possible to also extract tag values from the context, this is useful when the value is set per request/call within the context.Context:
// - Canary release https://martinfowler.com/bliki/CanaryRelease.html identifier
// - Authentication Token values, but avoid using high cardinality values such as UserID
//
func newMortarReporter(cfg *monitorConfig) monitor.Reporter {
externalMetrics := cfg.reporter.Metrics()
return &mortarReporter{
externalMetrics: externalMetrics,
cfg: cfg,
registry: newRegistry(externalMetrics),
}
}
func (r *mortarReporter) Connect(ctx context.Context) error {
return r.cfg.reporter.Connect(ctx)
}
func (r *mortarReporter) Close(ctx context.Context) error {
return r.cfg.reporter.Close(ctx)
}
func (r *mortarReporter) Metrics() monitor.Metrics {
return r
}
// Counter creates a counter with possible predefined tags
func (r *mortarReporter) Counter(name string, desc string) monitor.TagsAwareCounter {
return newMetric(r.registry, r.cfg).WithTags(r.cfg.tags).Counter(name, desc)
}
// Gauge creates a gauge with possible predefined tags
func (r *mortarReporter) Gauge(name string, desc string) monitor.TagsAwareGauge {
return newMetric(r.registry, r.cfg).WithTags(r.cfg.tags).Gauge(name, desc)
}
// Histogram creates a histogram with possible predefined tags
func (r *mortarReporter) Histogram(name string, desc string, buckets monitor.Buckets) monitor.TagsAwareHistogram {
return newMetric(r.registry, r.cfg).WithTags(r.cfg.tags).Histogram(name, desc, buckets)
}
// Timer creates a timer with possible predefined tags
func (r *mortarReporter) Timer(name string, desc string) monitor.TagsAwareTimer {
return newMetric(r.registry, r.cfg).WithTags(r.cfg.tags).Timer(name, desc)
}
// WithTags sets custom tags to be included if possible in every Metric
func (r *mortarReporter) WithTags(tags monitor.Tags) monitor.Metrics {
return newMetric(r.registry, r.cfg).
WithTags(r.cfg.tags). // first apply default tags
WithTags(tags) // then apply custom ones
}