Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ require (
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/kylelemons/godebug v1.1.0 // indirect
github.com/lufia/plan9stats v0.0.0-20240226150601-1dcf7310316a // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
Expand Down
51 changes: 40 additions & 11 deletions man/coredns-metrics.7
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.\" Generated by Mmark Markdown Processer - mmark.miek.nl
.TH "COREDNS-METRICS" 7 "March 2021" "CoreDNS" "CoreDNS Plugins"
.TH "COREDNS-METRICS" 7 "May 2025" "CoreDNS" "CoreDNS Plugins"

.SH "NAME"
.PP
Expand All @@ -10,30 +10,38 @@
.PP
With \fIprometheus\fP you export metrics from CoreDNS and any plugin that has them.
The default location for the metrics is \fB\fClocalhost:9153\fR. The metrics path is fixed to \fB\fC/metrics\fR.
The following metrics are exported:

.PP
In addition to the default Go metrics exported by the Prometheus Go client
\[la]https://prometheus.io/docs/guides/go-application/\[ra],
the following metrics are exported:

.IP \(bu 4
\fB\fCcoredns_build_info{version, revision, goversion}\fR - info about CoreDNS itself.
.IP \(bu 4
\fB\fCcoredns_panics_total{}\fR - total number of panics.
.IP \(bu 4
\fB\fCcoredns_dns_requests_total{server, zone, proto, family, type}\fR - total query count.
\fB\fCcoredns_dns_requests_total{server, zone, view, proto, family, type}\fR - total query count.
.IP \(bu 4
\fB\fCcoredns_dns_request_duration_seconds{server, zone, view, type}\fR - duration to process each query.
.IP \(bu 4
\fB\fCcoredns_dns_request_duration_seconds{server, zone, type}\fR - duration to process each query.
\fB\fCcoredns_dns_request_size_bytes{server, zone, view, proto}\fR - size of the request in bytes. Uses the original size before any plugin rewrites.
.IP \(bu 4
\fB\fCcoredns_dns_request_size_bytes{server, zone, proto}\fR - size of the request in bytes.
\fB\fCcoredns_dns_do_requests_total{server, view, zone}\fR - queries that have the DO bit set
.IP \(bu 4
\fB\fCcoredns_dns_do_requests_total{server, zone}\fR - queries that have the DO bit set
\fB\fCcoredns_dns_response_size_bytes{server, zone, view, proto}\fR - response size in bytes.
.IP \(bu 4
\fB\fCcoredns_dns_response_size_bytes{server, zone, proto}\fR - response size in bytes.
\fB\fCcoredns_dns_responses_total{server, zone, view, rcode, plugin}\fR - response per zone, rcode and plugin.
.IP \(bu 4
\fB\fCcoredns_dns_responses_total{server, zone, rcode}\fR - response per zone and rcode.
\fB\fCcoredns_dns_https_responses_total{server, status}\fR - responses per server and http status code.
.IP \(bu 4
\fB\fCcoredns_plugin_enabled{server, zone, name}\fR - indicates whether a plugin is enabled on per server and zone basis.
\fB\fCcoredns_dns_quic_responses_total{server, status}\fR - responses per server and QUIC application code.
.IP \(bu 4
\fB\fCcoredns_plugin_enabled{server, zone, view, name}\fR - indicates whether a plugin is enabled on per server, zone and view basis.


.PP
Each counter has a label \fB\fCzone\fR which is the zonename used for the request/response.
Almost each counter has a label \fB\fCzone\fR which is the zonename used for the request/response.

.PP
Extra labels used are:
Expand All @@ -48,14 +56,35 @@ this is \fB\fCdns://:53\fR. If you are using the \fIbind\fP plugin an IP address
The address family (\fB\fCfamily\fR) of the transport (1 = IP (IP version 4), 2 = IP6 (IP version 6)).
.IP \(bu 4
\fB\fCtype\fR which holds the query type. It holds most common types (A, AAAA, MX, SOA, CNAME, PTR, TXT,
NS, SRV, DS, DNSKEY, RRSIG, NSEC, NSEC3, IXFR, AXFR and ANY) and "other" which lumps together all
NS, SRV, DS, DNSKEY, RRSIG, NSEC, NSEC3, HTTPS, IXFR, AXFR and ANY) and "other" which lumps together all
other types.
.IP \(bu 4
\fB\fCstatus\fR which holds the https status code. Possible values are:

.RS
.IP \(en 4
200 - request is processed,
.IP \(en 4
404 - request has been rejected on validation,
.IP \(en 4
400 - request to dns message conversion failed,
.IP \(en 4
500 - processing ended up with no response.

.RE
.IP \(bu 4
the \fB\fCplugin\fR label holds the name of the plugin that made the write to the client. If the server
did the write (on error for instance), the value is empty.


.PP
If monitoring is enabled, queries that do not enter the plugin chain are exported under the fake
name "dropped" (without a closing dot - this is never a valid domain name).

.PP
Other plugins may export additional stats when the \fIprometheus\fP plugin is enabled. Those stats are documented in each
plugin's README.

.PP
This plugin can only be used once per Server Block.

Expand Down
2 changes: 1 addition & 1 deletion plugin/metrics/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ the following metrics are exported:
* `coredns_panics_total{}` - total number of panics.
* `coredns_dns_requests_total{server, zone, view, proto, family, type}` - total query count.
* `coredns_dns_request_duration_seconds{server, zone, view, type}` - duration to process each query.
* `coredns_dns_request_size_bytes{server, zone, view, proto}` - size of the request in bytes.
* `coredns_dns_request_size_bytes{server, zone, view, proto}` - size of the request in bytes. Uses the original size before any plugin rewrites.
* `coredns_dns_do_requests_total{server, view, zone}` - queries that have the DO bit set
* `coredns_dns_response_size_bytes{server, zone, view, proto}` - response size in bytes.
* `coredns_dns_responses_total{server, zone, view, rcode, plugin}` - response per zone, rcode and plugin.
Expand Down
7 changes: 6 additions & 1 deletion plugin/metrics/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import (
func (m *Metrics) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
state := request.Request{W: w, Req: r}

// Capture the original request size before any plugins modify it
originalSize := r.Len()

qname := state.QName()
zone := plugin.Zones(m.ZoneNames()).Matches(qname)
if zone == "" {
Expand All @@ -34,7 +37,9 @@ func (m *Metrics) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg
rc = status
}
plugin := m.authoritativePlugin(rw.Caller)
vars.Report(WithServer(ctx), state, zone, WithView(ctx), rcode.ToString(rc), plugin, rw.Len, rw.Start)
// Pass the original request size to vars.Report
vars.Report(WithServer(ctx), state, zone, WithView(ctx), rcode.ToString(rc), plugin,
rw.Len, rw.Start, vars.WithOriginalReqSize(originalSize))

return status, err
}
Expand Down
34 changes: 32 additions & 2 deletions plugin/metrics/vars/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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()
}
101 changes: 101 additions & 0 deletions plugin/metrics/vars/report_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package vars

import (
"testing"
"time"

"github.com/coredns/coredns/plugin/test"
"github.com/coredns/coredns/request"

"github.com/miekg/dns"
"github.com/prometheus/client_golang/prometheus/testutil"
)

func TestReportWithOptions(t *testing.T) {
tests := []struct {
name string
question string
qtype uint16
edns0 bool
do bool
originalSize int
useOriginal bool
}{
{
name: "A record without DO bit",
question: "example.org.",
qtype: dns.TypeA,
edns0: true,
do: false,
originalSize: 0,
useOriginal: false,
},
{
name: "A record with DO bit",
question: "example.org.",
qtype: dns.TypeA,
edns0: true,
do: true,
originalSize: 0,
useOriginal: false,
},
{
name: "A record with original size",
question: "example.org.",
qtype: dns.TypeA,
edns0: false,
do: false,
originalSize: 42,
useOriginal: true,
},
{
name: "A record bogus qtype",
question: "example.org.",
qtype: 0, // does not exist
edns0: false,
do: false,
originalSize: 42,
useOriginal: true,
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
m := new(dns.Msg)
m.SetQuestion(tc.question, tc.qtype)
if tc.edns0 {
m.SetEdns0(4096, tc.do)
}

w := &test.ResponseWriter{}
state := request.Request{W: w, Req: m}

if state.Do() != tc.do {
t.Errorf("DO bit detection failed, got %v, want %v", state.Do(), tc.do)
}

qType := qTypeString(tc.qtype)
expectedType := dns.Type(tc.qtype).String()
if qType != expectedType && qType != "other" {
t.Errorf("qTypeString(%d) = %s, want %s or 'other'", tc.qtype, qType, expectedType)
}

var opts []ReportOption
if tc.useOriginal {
opts = append(opts, WithOriginalReqSize(tc.originalSize))
}

net := state.Proto()
fam := "1"

countBefore := testutil.ToFloat64(RequestCount.WithLabelValues("dns://:53", "example.org.", "", net, fam, qType))

Report("dns://:53", state, "example.org.", "", "NOERROR", "test", 100, time.Now(), opts...)

countAfter := testutil.ToFloat64(RequestCount.WithLabelValues("dns://:53", "example.org.", "", net, fam, qType))
if countAfter <= countBefore {
t.Errorf("RequestCount was not incremented. Before: %f, After: %f", countBefore, countAfter)
}
})
}
}
Loading