plugin/dnstap: some cleanup (#1172)

Some cleanup in proxy and dnstap:
* just use time pkg directly and side step the indirection for Epoch
* Use Set in SetQueryEpoch to be more Go like. (Looked like a reader)
* Don't maintain two sets of time, we already track start, so use that.
* Use time.Time and convert when needed
* dedent the toDnstap function and put in a separate file
This commit is contained in:
Miek Gieben
2017-10-25 19:46:41 +01:00
committed by GitHub
parent 25367a4329
commit c2d93f7182
6 changed files with 64 additions and 61 deletions

View File

@@ -79,7 +79,7 @@ func (h Dnstap) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg)
newCtx := context.WithValue(ctx, DnstapSendOption, &sendOption)
rw := &taprw.ResponseWriter{ResponseWriter: w, Tapper: &h, Query: r, Send: &sendOption}
rw.QueryEpoch()
rw.SetQueryEpoch()
code, err := plugin.NextOrFailure(h.Name(), h.Next, tapContext{newCtx, h}, rw, r)
if err != nil {

View File

@@ -5,7 +5,6 @@ import (
"errors"
"net"
"strconv"
"time"
tap "github.com/dnstap/golang-dnstap"
"github.com/miekg/dns"
@@ -103,16 +102,6 @@ func (d *Data) Pack(m *dns.Msg) error {
return nil
}
// Epoch returns the epoch time in seconds.
func Epoch() uint64 {
return uint64(time.Now().Unix())
}
// Epoch sets the dnstap message epoch.
func (d *Data) Epoch() {
d.TimeSec = Epoch()
}
// ToClientResponse transforms Data into a client response message.
func (d *Data) ToClientResponse() *tap.Message {
t := tap.Message_CLIENT_RESPONSE

View File

@@ -4,6 +4,7 @@ package taprw
import (
"fmt"
"time"
"github.com/coredns/coredns/plugin/dnstap/msg"
@@ -41,9 +42,9 @@ func (w ResponseWriter) DnstapError() error {
return w.err
}
// QueryEpoch sets the query epoch as reported by dnstap.
func (w *ResponseWriter) QueryEpoch() {
w.queryEpoch = msg.Epoch()
// SetQueryEpoch sets the query epoch as reported by dnstap.
func (w *ResponseWriter) SetQueryEpoch() {
w.queryEpoch = uint64(time.Now().Unix())
}
// WriteMsg writes back the response to the client and THEN works on logging the request
@@ -51,7 +52,7 @@ func (w *ResponseWriter) QueryEpoch() {
// Dnstap errors are to be checked by DnstapError.
func (w *ResponseWriter) WriteMsg(resp *dns.Msg) (writeErr error) {
writeErr = w.ResponseWriter.WriteMsg(resp)
writeEpoch := msg.Epoch()
writeEpoch := uint64(time.Now().Unix())
b := w.TapBuilder()
b.TimeSec = w.queryEpoch