mirror of
https://github.com/coredns/coredns.git
synced 2025-11-01 18:53:43 -04:00
fix(metrics): preserve request size from plugins (#7313)
The rewrite plugin modifies DNS messages, affecting the request size observed in the coredns_dns_request_size_bytes metric. This change captures the original request size before any plugins can modify it. It adds a functional options pattern to Report() to pass this information while maintaining API compatibility. Tests have been added to verify the fix prevents rewrite from affecting the request size metrics. Docs included. Signed-off-by: Ville Vesilehto <ville@vesilehto.fi>
This commit is contained in:
@@ -6,10 +6,34 @@ import (
|
||||
"github.com/coredns/coredns/request"
|
||||
)
|
||||
|
||||
// ReportOptions is a struct that contains available options for the Report function.
|
||||
type ReportOptions struct {
|
||||
OriginalReqSize int
|
||||
}
|
||||
|
||||
// ReportOption defines a function that modifies ReportOptions
|
||||
type ReportOption func(*ReportOptions)
|
||||
|
||||
// WithOriginalReqSize returns an option to set the original request size
|
||||
func WithOriginalReqSize(size int) ReportOption {
|
||||
return func(opts *ReportOptions) {
|
||||
opts.OriginalReqSize = size
|
||||
}
|
||||
}
|
||||
|
||||
// Report reports the metrics data associated with request. This function is exported because it is also
|
||||
// called from core/dnsserver to report requests hitting the server that should not be handled and are thus
|
||||
// not sent down the plugin chain.
|
||||
func Report(server string, req request.Request, zone, view, rcode, plugin string, size int, start time.Time) {
|
||||
func Report(server string, req request.Request, zone, view, rcode, plugin string,
|
||||
size int, start time.Time, opts ...ReportOption) {
|
||||
options := ReportOptions{
|
||||
OriginalReqSize: 0,
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
opt(&options)
|
||||
}
|
||||
|
||||
// Proto and Family.
|
||||
net := req.Proto()
|
||||
fam := "1"
|
||||
@@ -27,7 +51,13 @@ func Report(server string, req request.Request, zone, view, rcode, plugin string
|
||||
RequestDuration.WithLabelValues(server, zone, view).Observe(time.Since(start).Seconds())
|
||||
|
||||
ResponseSize.WithLabelValues(server, zone, view, net).Observe(float64(size))
|
||||
RequestSize.WithLabelValues(server, zone, view, net).Observe(float64(req.Len()))
|
||||
|
||||
reqSize := req.Len()
|
||||
if options.OriginalReqSize > 0 {
|
||||
reqSize = options.OriginalReqSize
|
||||
}
|
||||
|
||||
RequestSize.WithLabelValues(server, zone, view, net).Observe(float64(reqSize))
|
||||
|
||||
ResponseRcode.WithLabelValues(server, zone, view, rcode, plugin).Inc()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user