-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmetrics.go
113 lines (95 loc) · 2.87 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
package plexhooks
import (
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/acamilleri/go-plexhooks/plex"
)
var (
httpRequestDuration = promauto.NewHistogramVec(prometheus.HistogramOpts{
Namespace: "plexhooks",
Subsystem: "http",
Name: "request_duration_seconds",
Help: "The latency of the HTTP requests.",
Buckets: prometheus.DefBuckets,
}, []string{"handler", "method"})
httpRequestTotal = promauto.NewCounterVec(prometheus.CounterOpts{
Namespace: "plexhooks",
Subsystem: "http",
Name: "request_total",
Help: "How many http requests processed, partitioned by handler, status code and http method.",
}, []string{"handler", "method", "code"})
eventsReceivedTotal = promauto.NewCounterVec(
prometheus.CounterOpts{
Namespace: "plexhooks",
Subsystem: "events",
Name: "received_total",
Help: "Total number of events received.",
}, []string{"event"})
actionsSuccessTotal = promauto.NewCounterVec(
prometheus.CounterOpts{
Namespace: "plexhooks",
Subsystem: "actions",
Name: "success_total",
Help: "Total number of actions by hook executed with success.",
}, []string{"event", "action"})
actionsDurationDuration = promauto.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: "plexhooks",
Subsystem: "actions",
Name: "duration_seconds",
Help: "A histogram of time to running action by hook.",
Buckets: prometheus.DefBuckets,
}, []string{"event", "action"})
actionsErrorTotal = promauto.NewCounterVec(
prometheus.CounterOpts{
Namespace: "plexhooks",
Subsystem: "actions",
Name: "error_total",
Help: "Total number of actions by hook failed.",
}, []string{"event", "action"})
)
type trackActionDuration struct {
event string
action string
start time.Time
stop time.Time
}
func newTrackActionDuration(event plex.Event, action Action) *trackActionDuration {
return &trackActionDuration{
event: event.Name.String(),
action: action.Name(),
start: time.Now(),
stop: time.Time{},
}
}
func (track *trackActionDuration) Finish() {
track.stop = time.Now()
duration := track.stop.Sub(track.start).Seconds()
actionsDurationDuration.With(prometheus.Labels{
"event": track.event,
"action": track.action,
}).Observe(duration)
}
type trackRequestDuration struct {
method string
handler string
start time.Time
stop time.Time
}
func newTrackRequestDuration(method, handler string) *trackRequestDuration {
return &trackRequestDuration{
method: method,
handler: handler,
start: time.Now(),
stop: time.Time{},
}
}
func (track *trackRequestDuration) Finish() {
track.stop = time.Now()
duration := track.stop.Sub(track.start).Seconds()
httpRequestDuration.With(prometheus.Labels{
"method": track.method,
"handler": track.handler,
}).Observe(duration)
}