prometheus: record IP AddressFamily

This commit is contained in:
Jimmy Zelinskie 2017-01-31 20:58:08 -05:00
parent 4d54980930
commit 0e07b33827
3 changed files with 36 additions and 12 deletions

View file

@ -31,19 +31,28 @@ var promResponseDurationMilliseconds = prometheus.NewHistogramVec(
Help: "The duration of time it takes to receive and write a response to an API request", Help: "The duration of time it takes to receive and write a response to an API request",
Buckets: prometheus.ExponentialBuckets(9.375, 2, 10), Buckets: prometheus.ExponentialBuckets(9.375, 2, 10),
}, },
[]string{"action", "error"}, []string{"action", "address_family", "error"},
) )
// recordResponseDuration records the duration of time to respond to a Request // recordResponseDuration records the duration of time to respond to a Request
// in milliseconds . // in milliseconds .
func recordResponseDuration(action string, err error, duration time.Duration) { func recordResponseDuration(action string, af *bittorrent.AddressFamily, err error, duration time.Duration) {
var errString string var errString string
if err != nil { if err != nil {
errString = err.Error() errString = err.Error()
} }
var afString string
if af == nil {
afString = "Unknown"
} else if *af == bittorrent.IPv4 {
afString = "IPv4"
} else if *af == bittorrent.IPv6 {
afString = "IPv6"
}
promResponseDurationMilliseconds. promResponseDurationMilliseconds.
WithLabelValues(action, errString). WithLabelValues(action, afString, errString).
Observe(float64(duration.Nanoseconds()) / float64(time.Millisecond)) Observe(float64(duration.Nanoseconds()) / float64(time.Millisecond))
} }
@ -149,13 +158,15 @@ func (t *Frontend) ListenAndServe() error {
func (t *Frontend) announceRoute(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { func (t *Frontend) announceRoute(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
var err error var err error
start := time.Now() start := time.Now()
defer func() { recordResponseDuration("announce", err, time.Since(start)) }() var af *bittorrent.AddressFamily
defer func() { recordResponseDuration("announce", af, err, time.Since(start)) }()
req, err := ParseAnnounce(r, t.RealIPHeader, t.AllowIPSpoofing) req, err := ParseAnnounce(r, t.RealIPHeader, t.AllowIPSpoofing)
if err != nil { if err != nil {
WriteError(w, err) WriteError(w, err)
return return
} }
*af = req.IP.AddressFamily
resp, err := t.logic.HandleAnnounce(context.Background(), req) resp, err := t.logic.HandleAnnounce(context.Background(), req)
if err != nil { if err != nil {
@ -176,7 +187,8 @@ func (t *Frontend) announceRoute(w http.ResponseWriter, r *http.Request, _ httpr
func (t *Frontend) scrapeRoute(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { func (t *Frontend) scrapeRoute(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
var err error var err error
start := time.Now() start := time.Now()
defer func() { recordResponseDuration("scrape", err, time.Since(start)) }() var af *bittorrent.AddressFamily
defer func() { recordResponseDuration("scrape", af, err, time.Since(start)) }()
req, err := ParseScrape(r) req, err := ParseScrape(r)
if err != nil { if err != nil {
@ -201,6 +213,7 @@ func (t *Frontend) scrapeRoute(w http.ResponseWriter, r *http.Request, _ httprou
WriteError(w, ErrInvalidIP) WriteError(w, ErrInvalidIP)
return return
} }
*af = req.AddressFamily
resp, err := t.logic.HandleScrape(context.Background(), req) resp, err := t.logic.HandleScrape(context.Background(), req)
if err != nil { if err != nil {

View file

@ -34,19 +34,28 @@ var promResponseDurationMilliseconds = prometheus.NewHistogramVec(
Help: "The duration of time it takes to receive and write a response to an API request", Help: "The duration of time it takes to receive and write a response to an API request",
Buckets: prometheus.ExponentialBuckets(9.375, 2, 10), Buckets: prometheus.ExponentialBuckets(9.375, 2, 10),
}, },
[]string{"action", "error"}, []string{"action", "address_family", "error"},
) )
// recordResponseDuration records the duration of time to respond to a UDP // recordResponseDuration records the duration of time to respond to a UDP
// Request in milliseconds . // Request in milliseconds .
func recordResponseDuration(action string, err error, duration time.Duration) { func recordResponseDuration(action string, af *bittorrent.AddressFamily, err error, duration time.Duration) {
var errString string var errString string
if err != nil { if err != nil {
errString = err.Error() errString = err.Error()
} }
var afString string
if af == nil {
afString = "Unknown"
} else if *af == bittorrent.IPv4 {
afString = "IPv4"
} else if *af == bittorrent.IPv6 {
afString = "IPv6"
}
promResponseDurationMilliseconds. promResponseDurationMilliseconds.
WithLabelValues(action, errString). WithLabelValues(action, afString, errString).
Observe(float64(duration.Nanoseconds()) / float64(time.Millisecond)) Observe(float64(duration.Nanoseconds()) / float64(time.Millisecond))
} }
@ -151,12 +160,12 @@ func (t *Frontend) ListenAndServe() error {
// Handle the request. // Handle the request.
start := time.Now() start := time.Now()
action, err := t.handleRequest( action, af, err := t.handleRequest(
// Make sure the IP is copied, not referenced. // Make sure the IP is copied, not referenced.
Request{buffer[:n], append([]byte{}, addr.IP...)}, Request{buffer[:n], append([]byte{}, addr.IP...)},
ResponseWriter{t.socket, addr}, ResponseWriter{t.socket, addr},
) )
recordResponseDuration(action, err, time.Since(start)) recordResponseDuration(action, af, err, time.Since(start))
}() }()
} }
} }
@ -181,7 +190,7 @@ func (w ResponseWriter) Write(b []byte) (int, error) {
} }
// handleRequest parses and responds to a UDP Request. // handleRequest parses and responds to a UDP Request.
func (t *Frontend) handleRequest(r Request, w ResponseWriter) (actionName string, err error) { func (t *Frontend) handleRequest(r Request, w ResponseWriter) (actionName string, af *bittorrent.AddressFamily, err error) {
if len(r.Packet) < 16 { if len(r.Packet) < 16 {
// Malformed, no client packets are less than 16 bytes. // Malformed, no client packets are less than 16 bytes.
// We explicitly return nothing in case this is a DoS attempt. // We explicitly return nothing in case this is a DoS attempt.
@ -223,6 +232,7 @@ func (t *Frontend) handleRequest(r Request, w ResponseWriter) (actionName string
WriteError(w, txID, err) WriteError(w, txID, err)
return return
} }
*af = req.IP.AddressFamily
var resp *bittorrent.AnnounceResponse var resp *bittorrent.AnnounceResponse
resp, err = t.logic.HandleAnnounce(context.Background(), req) resp, err = t.logic.HandleAnnounce(context.Background(), req)
@ -254,6 +264,7 @@ func (t *Frontend) handleRequest(r Request, w ResponseWriter) (actionName string
WriteError(w, txID, ErrInvalidIP) WriteError(w, txID, ErrInvalidIP)
return return
} }
*af = req.AddressFamily
var resp *bittorrent.ScrapeResponse var resp *bittorrent.ScrapeResponse
resp, err = t.logic.HandleScrape(context.Background(), req) resp, err = t.logic.HandleScrape(context.Background(), req)

View file

@ -28,7 +28,7 @@ var promGCDurationMilliseconds = prometheus.NewHistogram(prometheus.HistogramOpt
var promInfohashesCount = prometheus.NewGauge(prometheus.GaugeOpts{ var promInfohashesCount = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "chihaya_storage_infohashes_count", Name: "chihaya_storage_infohashes_count",
Help: "The number of Infohashes tracked", Help: "The number of infohashes tracked",
}) })
// recordGCDuration records the duration of a GC sweep. // recordGCDuration records the duration of a GC sweep.