mirror of
https://github.com/coredns/coredns.git
synced 2026-01-28 11:13:08 -05:00
Add metadata for response Type and Class to Log. (#7806)
This commit is contained in:
@@ -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
|
||||
~~~
|
||||
|
||||
## 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
|
||||
|
||||
Log all requests to stdout
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/coredns/coredns/plugin"
|
||||
"github.com/coredns/coredns/plugin/metadata"
|
||||
"github.com/coredns/coredns/plugin/pkg/dnstest"
|
||||
clog "github.com/coredns/coredns/plugin/pkg/log"
|
||||
"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)
|
||||
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
|
||||
// and we shouldn't have an empty rule.Class.
|
||||
_, ok := rule.Class[response.All]
|
||||
var ok1 bool
|
||||
if !ok {
|
||||
tpe, _ := response.Typify(rrw.Msg, time.Now().UTC())
|
||||
class := response.Classify(tpe)
|
||||
_, ok1 = rule.Class[class]
|
||||
}
|
||||
if ok || ok1 {
|
||||
|
||||
Reference in New Issue
Block a user