aboutsummaryrefslogtreecommitdiff
path: root/plugin/metrics/handler.go
diff options
context:
space:
mode:
Diffstat (limited to 'plugin/metrics/handler.go')
-rw-r--r--plugin/metrics/handler.go21
1 files changed, 18 insertions, 3 deletions
diff --git a/plugin/metrics/handler.go b/plugin/metrics/handler.go
index f26f90598..90db76181 100644
--- a/plugin/metrics/handler.go
+++ b/plugin/metrics/handler.go
@@ -2,10 +2,10 @@ package metrics
import (
"context"
+ "path/filepath"
"github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/metrics/vars"
- "github.com/coredns/coredns/plugin/pkg/dnstest"
"github.com/coredns/coredns/plugin/pkg/rcode"
"github.com/coredns/coredns/request"
@@ -23,7 +23,7 @@ func (m *Metrics) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg
}
// Record response to get status code and size of the reply.
- rw := dnstest.NewRecorder(w)
+ rw := NewRecorder(w)
status, err := plugin.NextOrFailure(m.Name(), m.Next, ctx, rw, r)
rc := rw.Rcode
@@ -33,10 +33,25 @@ func (m *Metrics) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg
// see https://github.com/coredns/coredns/blob/master/core/dnsserver/server.go#L318
rc = status
}
- vars.Report(WithServer(ctx), state, zone, rcode.ToString(rc), rw.Len, rw.Start)
+ plugin := m.authoritativePlugin(rw.Caller)
+ vars.Report(WithServer(ctx), state, zone, rcode.ToString(rc), plugin, rw.Len, rw.Start)
return status, err
}
// Name implements the Handler interface.
func (m *Metrics) Name() string { return "prometheus" }
+
+// authoritativePlugin returns which of made the write, if none is found the empty string is returned.
+func (m *Metrics) authoritativePlugin(caller [3]string) string {
+ // a b and c contain the full path of the caller, the plugin name 2nd last elements
+ // .../coredns/plugin/whoami/whoami.go --> whoami
+ // this is likely FS specific, so use filepath.
+ for _, c := range caller {
+ plug := filepath.Base(filepath.Dir(c))
+ if _, ok := m.plugins[plug]; ok {
+ return plug
+ }
+ }
+ return ""
+}