This repository has been archived by the owner on Mar 16, 2024. It is now read-only.
/
generator.go
58 lines (49 loc) · 1.64 KB
/
generator.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
package metrics
import (
"time"
"github.com/prometheus/client_golang/prometheus"
)
// Generator contains script generation metric collectors.
type Generator struct {
cacheHit prometheus.Counter
cacheMiss prometheus.Counter
duration prometheus.Histogram
}
// NewGenerator creates new Generator metrics collector.
func NewGenerator() Generator {
return Generator{
cacheHit: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: "generator",
Subsystem: "cache",
Name: "hits",
Help: "The count of cache hits during script generation.",
}),
cacheMiss: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: "generator",
Subsystem: "cache",
Name: "misses",
Help: "The count of cache misses during script generation.",
}),
duration: prometheus.NewHistogram(prometheus.HistogramOpts{
Namespace: "generator",
Subsystem: "time",
Name: "duration",
Help: "Time of script generation (in seconds).",
}),
}
}
// IncrementCacheHits increments cache hits counter.
func (g *Generator) IncrementCacheHits() { g.cacheHit.Inc() }
// IncrementCacheMisses increments cache misses counter.
func (g *Generator) IncrementCacheMisses() { g.cacheMiss.Inc() }
// ObserveGenerationDuration adds a single observation to the script generation histogram.
func (g *Generator) ObserveGenerationDuration(d time.Duration) { g.duration.Observe(d.Seconds()) }
// Register metrics with registerer.
func (g *Generator) Register(reg prometheus.Registerer) error {
for _, c := range [...]prometheus.Collector{g.cacheHit, g.cacheMiss, g.duration} {
if e := reg.Register(c); e != nil {
return e
}
}
return nil
}