Add metadata for response Type and Class to Log. (#7806)

This commit is contained in:
Kelly Kane
2026-01-26 08:03:03 -08:00
committed by GitHub
parent b722aadc44
commit b3d79f59e5
2 changed files with 24 additions and 2 deletions

View File

@@ -94,6 +94,19 @@ Each of these logs will be outputted with `log.Infof`, so a typical example look
[INFO] [::1]:50759 - 29008 "A IN example.org. udp 41 false 4096" NOERROR qr,rd,ra,ad 68 0.037990251s [INFO] [::1]:50759 - 29008 "A IN example.org. udp 41 false 4096" NOERROR qr,rd,ra,ad 68 0.037990251s
~~~ ~~~
## Additional metadata
The log plugin adds the following metadata to allow for granular differentiation of NOERROR denial vs success messages. These are mapped from `plugin/pkg/response/classify.go` and `plugin/pkg/response/typify.go`.
* `{/log/class}`: success, denial
* `{/log/type}`: NODATA, NXDOMAIN, NOERROR
~~~ corefile
. {
log . "{proto} Request: {name} {type} {/log/class} {/log/type}"
}
~~~
## Examples ## Examples
Log all requests to stdout Log all requests to stdout

View File

@@ -6,6 +6,7 @@ import (
"time" "time"
"github.com/coredns/coredns/plugin" "github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/metadata"
"github.com/coredns/coredns/plugin/pkg/dnstest" "github.com/coredns/coredns/plugin/pkg/dnstest"
clog "github.com/coredns/coredns/plugin/pkg/log" clog "github.com/coredns/coredns/plugin/pkg/log"
"github.com/coredns/coredns/plugin/pkg/replacer" "github.com/coredns/coredns/plugin/pkg/replacer"
@@ -35,13 +36,21 @@ func (l Logger) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg)
rrw := dnstest.NewRecorder(w) rrw := dnstest.NewRecorder(w)
rc, err := plugin.NextOrFailure(l.Name(), l.Next, ctx, rrw, r) rc, err := plugin.NextOrFailure(l.Name(), l.Next, ctx, rrw, r)
tpe, _ := response.Typify(rrw.Msg, time.Now().UTC())
metadata.SetValueFunc(ctx, "log/type", func() string {
return tpe.String()
})
class := response.Classify(tpe)
metadata.SetValueFunc(ctx, "log/class", func() string {
return class.String()
})
// If we don't set up a class in config, the default "all" will be added // If we don't set up a class in config, the default "all" will be added
// and we shouldn't have an empty rule.Class. // and we shouldn't have an empty rule.Class.
_, ok := rule.Class[response.All] _, ok := rule.Class[response.All]
var ok1 bool var ok1 bool
if !ok { if !ok {
tpe, _ := response.Typify(rrw.Msg, time.Now().UTC())
class := response.Classify(tpe)
_, ok1 = rule.Class[class] _, ok1 = rule.Class[class]
} }
if ok || ok1 { if ok || ok1 {