From 8c8b37a30ea400603c9cb381cc9b71c4da93a536 Mon Sep 17 00:00:00 2001
From: Miek Gieben <miek@miek.nl>
Date: Tue, 29 Nov 2016 11:02:43 +0000
Subject: [PATCH] middleware/metrics: export actual size (#448)

The `*_size*` metrics now export the actual packet length, not the
advertised one (although that might be nice as well).
---
 middleware/metrics/handler.go          | 2 +-
 middleware/metrics/vars/report.go      | 4 ++--
 middleware/pkg/dnsrecorder/recorder.go | 8 ++++----
 middleware/pkg/replacer/replacer.go    | 2 +-
 request/request.go                     | 5 ++++-
 5 files changed, 12 insertions(+), 9 deletions(-)

diff --git a/middleware/metrics/handler.go b/middleware/metrics/handler.go
index ff8aa059f..11998165e 100644
--- a/middleware/metrics/handler.go
+++ b/middleware/metrics/handler.go
@@ -25,7 +25,7 @@ func (m *Metrics) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg
 	rw := dnsrecorder.New(w)
 	status, err := m.Next.ServeDNS(ctx, rw, r)
 
-	vars.Report(state, zone, rcode.ToString(rw.Rcode), rw.Size, rw.Start)
+	vars.Report(state, zone, rcode.ToString(rw.Rcode), rw.Len, rw.Start)
 
 	return status, err
 }
diff --git a/middleware/metrics/vars/report.go b/middleware/metrics/vars/report.go
index 996826599..0c9945440 100644
--- a/middleware/metrics/vars/report.go
+++ b/middleware/metrics/vars/report.go
@@ -10,7 +10,7 @@ import (
 
 // Report reports the metrics data associcated with request.
 func Report(req request.Request, zone, rcode string, size int, start time.Time) {
-	// Proto and Family
+	// Proto and Family.
 	net := req.Proto()
 	fam := "1"
 	if req.Family() == 2 {
@@ -33,7 +33,7 @@ func Report(req request.Request, zone, rcode string, size int, start time.Time)
 	}
 
 	ResponseSize.WithLabelValues(zone, net).Observe(float64(size))
-	RequestSize.WithLabelValues(zone, net).Observe(float64(req.Size()))
+	RequestSize.WithLabelValues(zone, net).Observe(float64(req.Len()))
 
 	ResponseRcode.WithLabelValues(zone, rcode).Inc()
 }
diff --git a/middleware/pkg/dnsrecorder/recorder.go b/middleware/pkg/dnsrecorder/recorder.go
index a4e9ec56b..3ca5f00d0 100644
--- a/middleware/pkg/dnsrecorder/recorder.go
+++ b/middleware/pkg/dnsrecorder/recorder.go
@@ -16,7 +16,7 @@ import (
 type Recorder struct {
 	dns.ResponseWriter
 	Rcode int
-	Size  int
+	Len   int
 	Msg   *dns.Msg
 	Start time.Time
 }
@@ -39,16 +39,16 @@ func (r *Recorder) WriteMsg(res *dns.Msg) error {
 	r.Rcode = res.Rcode
 	// We may get called multiple times (axfr for instance).
 	// Save the last message, but add the sizes.
-	r.Size += res.Len()
+	r.Len += res.Len()
 	r.Msg = res
 	return r.ResponseWriter.WriteMsg(res)
 }
 
-// Write is a wrapper that records the size of the message that gets written.
+// Write is a wrapper that records the length of the message that gets written.
 func (r *Recorder) Write(buf []byte) (int, error) {
 	n, err := r.ResponseWriter.Write(buf)
 	if err == nil {
-		r.Size += n
+		r.Len += n
 	}
 	return n, err
 }
diff --git a/middleware/pkg/replacer/replacer.go b/middleware/pkg/replacer/replacer.go
index e90f29368..71aa1c1b5 100644
--- a/middleware/pkg/replacer/replacer.go
+++ b/middleware/pkg/replacer/replacer.go
@@ -53,7 +53,7 @@ func New(r *dns.Msg, rr *dnsrecorder.Recorder, emptyValue string) Replacer {
 			rcode = strconv.Itoa(rr.Rcode)
 		}
 		rep.replacements["{rcode}"] = rcode
-		rep.replacements["{size}"] = strconv.Itoa(rr.Size)
+		rep.replacements["{size}"] = strconv.Itoa(rr.Len)
 		rep.replacements["{duration}"] = time.Since(rr.Start).String()
 	}
 
diff --git a/request/request.go b/request/request.go
index b5da38771..00388caca 100644
--- a/request/request.go
+++ b/request/request.go
@@ -106,7 +106,10 @@ func (r *Request) Do() bool {
 	return false
 }
 
-// Size returns if UDP buffer size advertised in the requests OPT record.
+// Len returns the length in bytes in the request.
+func (r *Request) Len() int { return r.Req.Len() }
+
+// Size returns if buffer size *advertised* in the requests OPT record.
 // Or when the request was over TCP, we return the maximum allowed size of 64K.
 func (r *Request) Size() int {
 	if r.size != 0 {