2018-06-15 00:57:23 +02:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
2022-01-16 05:28:52 +01:00
|
|
|
"errors"
|
2018-06-15 00:57:23 +02:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
|
|
|
|
"github.com/chihaya/chihaya/bittorrent"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
prometheus.MustRegister(promResponseDurationMilliseconds)
|
|
|
|
}
|
|
|
|
|
|
|
|
var promResponseDurationMilliseconds = prometheus.NewHistogramVec(
|
|
|
|
prometheus.HistogramOpts{
|
|
|
|
Name: "chihaya_http_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 Request
|
|
|
|
// in milliseconds.
|
|
|
|
func recordResponseDuration(action string, af *bittorrent.AddressFamily, err error, duration time.Duration) {
|
|
|
|
var errString string
|
|
|
|
if err != nil {
|
2022-01-16 05:28:52 +01:00
|
|
|
var clientErr bittorrent.ClientError
|
|
|
|
if errors.As(err, &clientErr) {
|
|
|
|
errString = clientErr.Error()
|
2018-06-15 00:57:23 +02:00
|
|
|
} else {
|
|
|
|
errString = "internal error"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var afString string
|
|
|
|
if af == nil {
|
|
|
|
afString = "Unknown"
|
|
|
|
} else if *af == bittorrent.IPv4 {
|
|
|
|
afString = "IPv4"
|
|
|
|
} else if *af == bittorrent.IPv6 {
|
|
|
|
afString = "IPv6"
|
|
|
|
}
|
|
|
|
|
|
|
|
promResponseDurationMilliseconds.
|
|
|
|
WithLabelValues(action, afString, errString).
|
|
|
|
Observe(float64(duration.Nanoseconds()) / float64(time.Millisecond))
|
|
|
|
}
|