git » dnss » commit 8cdf69a

Unify tracing calls

author Alberto Bertogli
2017-07-28 00:39:48 UTC
committer Alberto Bertogli
2017-07-30 12:03:20 UTC
parent 8b2bc9a86dcc4dd2ba6bbceb0698d2c382821dfb

Unify tracing calls

This patch makes the tracing calls more uniform, by adding some helpers
and using them in places where we previously had bits of duplicated
logic.

internal/dnstohttps/server.go +4 -12
internal/httpstodns/server.go +6 -18
internal/util/strings.go +0 -30
internal/util/trace.go +50 -0

diff --git a/internal/dnstohttps/server.go b/internal/dnstohttps/server.go
index 8ee13f7..289f40e 100644
--- a/internal/dnstohttps/server.go
+++ b/internal/dnstohttps/server.go
@@ -74,9 +74,7 @@ func (s *Server) Handler(w dns.ResponseWriter, r *dns.Msg) {
 
 	tr.LazyPrintf("from:%v   id:%v", w.RemoteAddr(), r.Id)
 
-	if glog.V(3) {
-		tr.LazyPrintf(util.QuestionsToString(r.Question))
-	}
+	util.TraceQuestion(tr, r.Question)
 
 	// We only support single-question queries.
 	if len(r.Question) != 1 {
@@ -95,9 +93,7 @@ func (s *Server) Handler(w dns.ResponseWriter, r *dns.Msg) {
 		u, err := dns.Exchange(r, s.unqUpstream)
 		if err == nil {
 			tr.LazyPrintf("used unqualified upstream")
-			if glog.V(3) {
-				util.TraceAnswer(tr, u)
-			}
+			util.TraceAnswer(tr, u)
 			w.WriteMsg(u)
 		} else {
 			tr.LazyPrintf("unqualified upstream error: %v", err)
@@ -112,9 +108,7 @@ func (s *Server) Handler(w dns.ResponseWriter, r *dns.Msg) {
 		u, err := dns.Exchange(r, s.fallbackUpstream)
 		if err == nil {
 			tr.LazyPrintf("used fallback upstream (%s)", s.fallbackUpstream)
-			if glog.V(3) {
-				util.TraceAnswer(tr, u)
-			}
+			util.TraceAnswer(tr, u)
 			w.WriteMsg(u)
 		} else {
 			tr.LazyPrintf("fallback upstream error: %v", err)
@@ -137,9 +131,7 @@ func (s *Server) Handler(w dns.ResponseWriter, r *dns.Msg) {
 		return
 	}
 
-	if glog.V(3) {
-		util.TraceAnswer(tr, from_up)
-	}
+	util.TraceAnswer(tr, from_up)
 
 	from_up.Id = oldid
 	w.WriteMsg(from_up)
diff --git a/internal/httpstodns/server.go b/internal/httpstodns/server.go
index 2233262..be0150e 100644
--- a/internal/httpstodns/server.go
+++ b/internal/httpstodns/server.go
@@ -47,6 +47,7 @@ func (s *Server) Resolve(w http.ResponseWriter, req *http.Request) {
 	// Construct the DNS request from the http query.
 	q, err := parseQuery(req.URL)
 	if err != nil {
+		util.TraceError(tr, err)
 		http.Error(w, err.Error(), http.StatusBadRequest)
 		return
 	}
@@ -77,34 +78,23 @@ func (s *Server) Resolve(w http.ResponseWriter, req *http.Request) {
 		r.Extra = append(r.Extra, o)
 	}
 
-	if glog.V(3) {
-		tr.LazyPrintf(util.QuestionsToString(r.Question))
-	}
+	util.TraceQuestion(tr, r.Question)
 
 	// Do the DNS request, get the reply.
 	from_up, err := dns.Exchange(r, s.Upstream)
 	if err != nil {
-		msg := fmt.Sprintf("dns exchange error: %v", err)
-		glog.Info(msg)
-		tr.LazyPrintf(msg)
-		tr.SetError()
-
-		// TODO: reply via json anyway?
+		err = util.TraceErrorf(tr, "dns exchange error: %v", err)
 		http.Error(w, err.Error(), http.StatusFailedDependency)
 		return
 	}
 
 	if from_up == nil {
-		err = fmt.Errorf("no response from upstream")
-		tr.LazyPrintf(err.Error())
-		tr.SetError()
+		err = util.TraceErrorf(tr, "no response from upstream")
 		http.Error(w, err.Error(), http.StatusRequestTimeout)
 		return
 	}
 
-	if glog.V(3) {
-		util.TraceAnswer(tr, from_up)
-	}
+	util.TraceAnswer(tr, from_up)
 
 	// Convert the reply to json, and write it back.
 	jr := &dnsjson.Response{
@@ -139,9 +129,7 @@ func (s *Server) Resolve(w http.ResponseWriter, req *http.Request) {
 
 	buf, err := json.Marshal(jr)
 	if err != nil {
-		err = fmt.Errorf("failed to marshal: %v", err)
-		tr.LazyPrintf(err.Error())
-		tr.SetError()
+		err = util.TraceErrorf(tr, "failed to marshal: %v", err)
 		http.Error(w, err.Error(), http.StatusInternalServerError)
 		return
 	}
diff --git a/internal/util/strings.go b/internal/util/strings.go
deleted file mode 100644
index 5624cc3..0000000
--- a/internal/util/strings.go
+++ /dev/null
@@ -1,30 +0,0 @@
-package util
-
-// Utility functions for logging DNS messages.
-
-import (
-	"fmt"
-	"strings"
-
-	"github.com/miekg/dns"
-	"golang.org/x/net/trace"
-)
-
-func QuestionsToString(qs []dns.Question) string {
-	var s []string
-	for _, q := range qs {
-		s = append(s, fmt.Sprintf("(%s %s %s)", q.Name,
-			dns.TypeToString[q.Qtype], dns.ClassToString[q.Qclass]))
-	}
-	return "Q: " + strings.Join(s, " ; ")
-}
-
-func TraceAnswer(tr trace.Trace, m *dns.Msg) {
-	if m.Rcode != dns.RcodeSuccess {
-		rcode := dns.RcodeToString[m.Rcode]
-		tr.LazyPrintf(rcode)
-	}
-	for _, rr := range m.Answer {
-		tr.LazyPrintf(rr.String())
-	}
-}
diff --git a/internal/util/trace.go b/internal/util/trace.go
new file mode 100644
index 0000000..167265d
--- /dev/null
+++ b/internal/util/trace.go
@@ -0,0 +1,50 @@
+package util
+
+import (
+	"fmt"
+	"strings"
+
+	"github.com/golang/glog"
+	"github.com/miekg/dns"
+	"golang.org/x/net/trace"
+)
+
+func TraceQuestion(tr trace.Trace, qs []dns.Question) {
+	if !glog.V(3) {
+		return
+	}
+
+	tr.LazyPrintf(questionsToString(qs))
+}
+
+func questionsToString(qs []dns.Question) string {
+	var s []string
+	for _, q := range qs {
+		s = append(s, fmt.Sprintf("(%s %s %s)", q.Name,
+			dns.TypeToString[q.Qtype], dns.ClassToString[q.Qclass]))
+	}
+	return "Q: " + strings.Join(s, " ; ")
+}
+
+func TraceAnswer(tr trace.Trace, m *dns.Msg) {
+	if !glog.V(3) {
+		return
+	}
+
+	tr.LazyPrintf(m.MsgHdr.String())
+	for _, rr := range m.Answer {
+		tr.LazyPrintf(rr.String())
+	}
+}
+
+func TraceError(tr trace.Trace, err error) {
+	glog.Info(err.Error())
+	tr.LazyPrintf(err.Error())
+	tr.SetError()
+}
+
+func TraceErrorf(tr trace.Trace, format string, a ...interface{}) error {
+	err := fmt.Errorf(format, a...)
+	TraceError(tr, err)
+	return err
+}