Merge pull request #284 from jzelinskie/promaf

prometheus: record IP AddressFamily
This commit is contained in:
Jimmy Zelinskie 2017-02-03 11:45:56 -05:00 committed by GitHub
commit 74dde3f362
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",
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
// 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
if err != nil {
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.
WithLabelValues(action, errString).
WithLabelValues(action, afString, errString).
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) {
var err error
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)
if err != nil {
WriteError(w, err)
return
}
*af = req.IP.AddressFamily
resp, err := t.logic.HandleAnnounce(context.Background(), req)
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) {
var err error
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)
if err != nil {
@ -201,6 +213,7 @@ func (t *Frontend) scrapeRoute(w http.ResponseWriter, r *http.Request, _ httprou
WriteError(w, ErrInvalidIP)
return
}
*af = req.AddressFamily
resp, err := t.logic.HandleScrape(context.Background(), req)
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",
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
// 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
if err != nil {
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.
WithLabelValues(action, errString).
WithLabelValues(action, afString, errString).
Observe(float64(duration.Nanoseconds()) / float64(time.Millisecond))
}
@ -151,12 +160,12 @@ func (t *Frontend) ListenAndServe() error {
// Handle the request.
start := time.Now()
action, err := t.handleRequest(
action, af, err := t.handleRequest(
// Make sure the IP is copied, not referenced.
Request{buffer[:n], append([]byte{}, addr.IP...)},
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.
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 {
// Malformed, no client packets are less than 16 bytes.
// 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)
return
}
*af = req.IP.AddressFamily
var resp *bittorrent.AnnounceResponse
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)
return
}
*af = req.AddressFamily
var resp *bittorrent.ScrapeResponse
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{
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.