mirror of
https://github.com/rclone/rclone.git
synced 2024-11-25 01:44:41 +01:00
38dc3e93ee
This patch adds rclone_http_status_code counter vector labeled by * host, * method, * code. It allows to see HTTP errors, backoffs etc. The Metrics struct is designed for extensibility. Adding new metrics is a matter of adding them to Metrics struct and including them in the response handling. This feature has been discussed in the forum [1]. [1] https://forum.rclone.org/t/prometheus-metrics/14484
52 lines
1.1 KiB
Go
52 lines
1.1 KiB
Go
package fshttp
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
)
|
|
|
|
// Metrics provide Transport HTTP level metrics.
|
|
type Metrics struct {
|
|
StatusCode *prometheus.CounterVec
|
|
}
|
|
|
|
// NewMetrics creates a new metrics instance, the instance shall be assigned to
|
|
// DefaultMetrics before any processing takes place.
|
|
func NewMetrics(namespace string) *Metrics {
|
|
return &Metrics{
|
|
StatusCode: prometheus.NewCounterVec(prometheus.CounterOpts{
|
|
Namespace: namespace,
|
|
Subsystem: "http",
|
|
Name: "status_code",
|
|
}, []string{"host", "method", "code"}),
|
|
}
|
|
}
|
|
|
|
// DefaultMetrics specifies metrics used for new Transports.
|
|
var DefaultMetrics = (*Metrics)(nil)
|
|
|
|
// Collectors returns all prometheus metrics as collectors for registration.
|
|
func (m *Metrics) Collectors() []prometheus.Collector {
|
|
if m == nil {
|
|
return nil
|
|
}
|
|
return []prometheus.Collector{
|
|
m.StatusCode,
|
|
}
|
|
}
|
|
|
|
func (m *Metrics) onResponse(req *http.Request, resp *http.Response) {
|
|
if m == nil {
|
|
return
|
|
}
|
|
|
|
var statusCode = 0
|
|
if resp != nil {
|
|
statusCode = resp.StatusCode
|
|
}
|
|
|
|
m.StatusCode.WithLabelValues(req.Host, req.Method, fmt.Sprint(statusCode)).Inc()
|
|
}
|