forked from chihaya/chihaya
-
Notifications
You must be signed in to change notification settings - Fork 2
/
prometheus.go
43 lines (36 loc) · 1.16 KB
/
prometheus.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
package udp
import (
"errors"
"net/netip"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/sot-tech/mochi/bittorrent"
"github.com/sot-tech/mochi/pkg/metrics"
)
func init() {
prometheus.MustRegister(promResponseDurationMilliseconds)
}
var promResponseDurationMilliseconds = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "mochi_udp_response_duration_milliseconds",
Help: "The duration of time it takes to receive and write a response to an API request",
Buckets: prometheus.ExponentialBuckets(9.375, 2, 10),
},
[]string{"action", "address_family", "error"},
)
// recordResponseDuration records the duration of time to respond to a UDP
// Request in milliseconds.
func recordResponseDuration(action string, addr netip.Addr, err error, duration time.Duration) {
var errString string
if err != nil {
var clientErr bittorrent.ClientError
if errors.As(err, &clientErr) {
errString = clientErr.Error()
} else {
errString = "internal error"
}
}
promResponseDurationMilliseconds.
WithLabelValues(action, metrics.AddressFamily(addr), errString).
Observe(float64(duration.Nanoseconds()) / float64(time.Millisecond))
}