Skip to content

Commit

Permalink
client-ip in metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
Mariusz Olejnik committed Jan 10, 2023
1 parent fc6bfd4 commit 20437ed
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
4 changes: 2 additions & 2 deletions metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ var promQueryServedCountTotal = promauto.NewCounterVec(prometheus.CounterOpts{
Subsystem: pluginName,
Name: "query_served_count_total",
Help: "Total count of served queries",
}, []string{"zone", "alias", "resolver"})
}, []string{"zone", "alias", "resolver", "client-ip"})
var promQueryOmittedCountTotal = promauto.NewCounterVec(prometheus.CounterOpts{
Namespace: plugin.Namespace,
Subsystem: pluginName,
Name: "query_omitted_count_total",
Help: "Total count of omitted queries",
}, []string{"zone", "alias", "reason"})
}, []string{"zone", "alias", "reason", "client-ip"})

var promResolveCountTotal = promauto.NewCounterVec(prometheus.CounterOpts{
Namespace: plugin.Namespace,
Expand Down
17 changes: 9 additions & 8 deletions recursor.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

const pluginName = "recursor"
const pluginVersion = "1.0.1"
const pluginVersion = "1.0.2"
const defaultResolverName = "default"

// Name implements the Handler interface.
Expand Down Expand Up @@ -68,35 +68,36 @@ func (r aliasDef) String() string {
// ServeDNS implements the plugin.Handler interface. This method gets called when plugin is used in a Server.
func (r recursor) ServeDNS(ctx context.Context, out dns.ResponseWriter, query *dns.Msg) (int, error) {
state := request.Request{W: out, Req: query}
clientIp := state.IP()
domain := dns.CanonicalName(state.Name())
zoneSuffix := "." + dns.CanonicalName(r.zone)
if !strings.HasSuffix(domain, zoneSuffix) {
domain = domain + zoneSuffix
}
alias := strings.TrimSuffix(domain, zoneSuffix)
if r.verbose > 0 {
log.Infof("Recursor query domain '%s', alias '%s', zone '%s'", domain, alias, r.zone)
log.Infof("Recursor query domain '%s', alias '%s', zone '%s', client-ip '%s'", domain, alias, r.zone, clientIp)
}

qA, qAAAA := extractQuestions(query.Question)
if r.verbose > 1 {
log.Infof("Recursor query: A=%t, AAAA=%t\n```\n%s```", qA, qAAAA, query.String())
log.Infof("Recursor query: A=%t, AAAA=%t, client-ip=%s\n```\n%s```", qA, qAAAA, clientIp, query.String())
}
if !qA && !qAAAA {
promQueryOmittedCountTotal.With(prometheus.Labels{"zone": r.zone, "alias": alias, "reason": "not-supported-query-code"}).Inc()
promQueryOmittedCountTotal.With(prometheus.Labels{"zone": r.zone, "alias": alias, "reason": "not-supported-query-code", "client-ip": clientIp}).Inc()
log.Errorf("Query code not supported: zone '%s', domain '%s', alias '%s'", r.zone, domain, alias)
return plugin.NextOrFailure(r.Name(), r.Next, ctx, out, query)
}

var aDef aliasDef
var ok bool
if aDef, ok = r.aliases[alias]; !ok {
promQueryOmittedCountTotal.With(prometheus.Labels{"zone": r.zone, "alias": alias, "reason": "alias-not-found"}).Inc()
promQueryOmittedCountTotal.With(prometheus.Labels{"zone": r.zone, "alias": alias, "reason": "alias-not-found", "client-ip": clientIp}).Inc()
log.Errorf("Alias not found: zone '%s', domain '%s', alias '%s'", r.zone, domain, alias)
return plugin.NextOrFailure(r.Name(), r.Next, ctx, out, query)
}
if len(aDef.hosts) < 1 && len(aDef.ips) < 1 {
promQueryOmittedCountTotal.With(prometheus.Labels{"zone": r.zone, "alias": alias, "reason": "alias-empty-def"}).Inc()
promQueryOmittedCountTotal.With(prometheus.Labels{"zone": r.zone, "alias": alias, "reason": "alias-empty-def", "client-ip": clientIp}).Inc()
log.Errorf("Empty alias definition: zone '%s', domain '%s', alias '%s'", r.zone, domain, alias)
return plugin.NextOrFailure(r.Name(), r.Next, ctx, out, query)
}
Expand All @@ -106,7 +107,7 @@ func (r recursor) ServeDNS(ctx context.Context, out dns.ResponseWriter, query *d
for _, host := range aDef.hosts {
dynIps, err := multiResolve(ctx, aDef.resolverDefRef, r.zone, domain, alias, host)
if err != nil {
promQueryOmittedCountTotal.With(prometheus.Labels{"zone": r.zone, "alias": alias, "reason": "resolving-error"}).Inc()
promQueryOmittedCountTotal.With(prometheus.Labels{"zone": r.zone, "alias": alias, "reason": "resolving-error", "client-ip": clientIp}).Inc()
log.Errorf("Could not resolve host '%s': zone '%s', domain '%s', alias '%s'", host, r.zone, domain, alias)
return plugin.NextOrFailure(r.Name(), r.Next, ctx, out, query)
}
Expand All @@ -122,7 +123,7 @@ func (r recursor) ServeDNS(ctx context.Context, out dns.ResponseWriter, query *d
log.Errorf("Could not write message: %v", err)
return dns.RcodeServerFailure, err
}
promQueryServedCountTotal.With(prometheus.Labels{"zone": r.zone, "alias": alias, "resolver": aDef.resolverDefRef.name}).Inc()
promQueryServedCountTotal.With(prometheus.Labels{"zone": r.zone, "alias": alias, "resolver": aDef.resolverDefRef.name, "client-ip": clientIp}).Inc()
return dns.RcodeSuccess, nil
}

Expand Down

0 comments on commit 20437ed

Please sign in to comment.