git » dnss » commit b491dd3

Replace github.com/golang/glog with blitiri.com.ar/go/log

author Alberto Bertogli
2018-04-13 09:14:40 UTC
committer Alberto Bertogli
2018-04-13 09:28:30 UTC
parent 61da531116cefa8e8e716bbc0f55359c4a11ac4e

Replace github.com/golang/glog with blitiri.com.ar/go/log

For the most common uses of dnss, using github.com/golang/glog
introduces a bit of complexity as it needs to be flushed regularly and
before exit, and also requires some parameters to be nice to systemd and
log rotators.

This patch replaces it with blitiri.com.ar/go/log, which has a very
similar API and functionality, but it does not require flushing and has
more practical defaults.

dnss.go +14 -23
dnss_test.go +6 -5
etc/systemd/dns-to-https/dnss.service +0 -1
internal/dnsserver/resolver.go +4 -3
internal/dnsserver/server.go +13 -13
internal/dnstohttps/https_test.go +4 -13
internal/dnstohttps/resolver.go +3 -3
internal/httpserver/server.go +3 -3
internal/util/trace.go +5 -4

diff --git a/dnss.go b/dnss.go
index d80fe83..7a9ab9d 100644
--- a/dnss.go
+++ b/dnss.go
@@ -18,13 +18,11 @@ import (
 	"net/url"
 	"strings"
 	"sync"
-	"time"
 
 	"blitiri.com.ar/go/dnss/internal/dnsserver"
 	"blitiri.com.ar/go/dnss/internal/dnstohttps"
 	"blitiri.com.ar/go/dnss/internal/httpserver"
-
-	"github.com/golang/glog"
+	"blitiri.com.ar/go/log"
 
 	// Register pprof handlers for monitoring and debugging.
 	_ "net/http/pprof"
@@ -64,8 +62,6 @@ var (
 	httpsAddr = flag.String("https_server_addr", ":443",
 		"address to listen on for HTTPS-to-DNS requests")
 
-	logFlushEvery = flag.Duration("log_flush_every", 30*time.Second,
-		"how often to flush logs")
 	monitoringListenAddr = flag.String("monitoring_listen_addr", "",
 		"address to listen on for monitoring HTTP requests")
 
@@ -74,31 +70,26 @@ var (
 
 	dohMode = flag.Bool("experimental__doh_mode", false,
 		"DoH mode (experimental)")
-)
 
-func flushLogs() {
-	c := time.Tick(*logFlushEvery)
-	for range c {
-		glog.Flush()
-	}
-}
+	// Deprecated flags that no longer make sense; we keep them for backwards
+	// compatibility but may be removed in the future.
+	_ = flag.Duration("log_flush_every", 0, "deprecated, will be removed")
+	_ = flag.Bool("logtostderr", false, "deprecated, will be removed")
+)
 
 func main() {
-	defer glog.Flush()
-
 	flag.Parse()
-
-	go flushLogs()
+	log.Init()
 
 	if *monitoringListenAddr != "" {
 		launchMonitoringServer(*monitoringListenAddr)
 	}
 
 	if !(*enableDNStoHTTPS || *enableHTTPStoDNS) {
-		glog.Error("Need to set one of the following:")
-		glog.Error("  --enable_dns_to_https")
-		glog.Error("  --enable_https_to_dns")
-		glog.Fatal("")
+		log.Errorf("Need to set one of the following:")
+		log.Errorf("  --enable_dns_to_https")
+		log.Errorf("  --enable_https_to_dns")
+		log.Fatalf("")
 	}
 
 	if *insecureForTesting {
@@ -111,7 +102,7 @@ func main() {
 	if *enableDNStoHTTPS {
 		upstream, err := url.Parse(*httpsUpstream)
 		if err != nil {
-			glog.Fatalf("-https_upstream is not a valid URL: %v", err)
+			log.Fatalf("-https_upstream is not a valid URL: %v", err)
 		}
 
 		var resolver dnsserver.Resolver
@@ -134,7 +125,7 @@ func main() {
 		// so we don't have problems resolving it.
 		fallbackDoms := strings.Split(*fallbackDomains, " ")
 		if proxyDomain := proxyServerDomain(); proxyDomain != "" {
-			glog.Infof("Adding proxy %q to fallback domains", proxyDomain)
+			log.Infof("Adding proxy %q to fallback domains", proxyDomain)
 			fallbackDoms = append(fallbackDoms, proxyDomain)
 		}
 
@@ -203,7 +194,7 @@ func extractHostname(host string) string {
 }
 
 func launchMonitoringServer(addr string) {
-	glog.Infof("Monitoring HTTP server listening on %s", addr)
+	log.Infof("Monitoring HTTP server listening on %s", addr)
 
 	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
 		if r.URL.Path != "/" {
diff --git a/dnss_test.go b/dnss_test.go
index cb6842f..01a6de1 100644
--- a/dnss_test.go
+++ b/dnss_test.go
@@ -15,16 +15,17 @@ import (
 	"blitiri.com.ar/go/dnss/internal/dnstohttps"
 	"blitiri.com.ar/go/dnss/internal/httpserver"
 	"blitiri.com.ar/go/dnss/internal/testutil"
-	"github.com/golang/glog"
+	"blitiri.com.ar/go/log"
 	"github.com/miekg/dns"
 )
 
-// Custom test main, so we parse the flags and flush the logs before exiting.
+// Custom test main, so we reduce the default logging to avoid overly verbose
+// tests.
 func TestMain(m *testing.M) {
 	flag.Parse()
-	r := m.Run()
-	glog.Flush()
-	os.Exit(r)
+	log.Init()
+	log.Default.Level = log.Error
+	os.Exit(m.Run())
 }
 
 /////////////////////////////////////////////////////////////////////
diff --git a/etc/systemd/dns-to-https/dnss.service b/etc/systemd/dns-to-https/dnss.service
index 262092a..154ba5f 100644
--- a/etc/systemd/dns-to-https/dnss.service
+++ b/etc/systemd/dns-to-https/dnss.service
@@ -7,7 +7,6 @@ Requires=dnss.socket
 [Service]
 ExecStart=/usr/local/bin/dnss \
         --dns_listen_addr=systemd \
-        --logtostderr \
         --monitoring_listen_addr=127.0.0.1:8081 \
         --enable_dns_to_https
 
diff --git a/internal/dnsserver/resolver.go b/internal/dnsserver/resolver.go
index 8ca4130..94dc3a3 100644
--- a/internal/dnsserver/resolver.go
+++ b/internal/dnsserver/resolver.go
@@ -8,7 +8,8 @@ import (
 	"sync"
 	"time"
 
-	"github.com/golang/glog"
+	"blitiri.com.ar/go/log"
+
 	"github.com/miekg/dns"
 	"golang.org/x/net/trace"
 )
@@ -120,7 +121,7 @@ func (c *cachingResolver) DumpCache(w http.ResponseWriter, r *http.Request) {
 	for q, ans := range c.answer {
 		// Only include names and records if we are running verbosily.
 		name := "<hidden>"
-		if glog.V(3) {
+		if log.V(3) {
 			name = q.Name
 		}
 
@@ -130,7 +131,7 @@ func (c *cachingResolver) DumpCache(w http.ResponseWriter, r *http.Request) {
 		ttl := getTTL(ans)
 		fmt.Fprintf(buf, "   expires in %s (%s)\n", ttl, time.Now().Add(ttl))
 
-		if glog.V(3) {
+		if log.V(3) {
 			for _, rr := range ans {
 				fmt.Fprintf(buf, "   %s\n", rr.String())
 			}
diff --git a/internal/dnsserver/server.go b/internal/dnsserver/server.go
index 4e9fcce..6f817fa 100644
--- a/internal/dnsserver/server.go
+++ b/internal/dnsserver/server.go
@@ -11,11 +11,11 @@ import (
 	"sync"
 
 	"github.com/coreos/go-systemd/activation"
-	"github.com/golang/glog"
 	"github.com/miekg/dns"
 	"golang.org/x/net/trace"
 
 	"blitiri.com.ar/go/dnss/internal/util"
+	"blitiri.com.ar/go/log"
 )
 
 // newID is a channel used to generate new request IDs.
@@ -131,7 +131,7 @@ func (s *Server) Handler(w dns.ResponseWriter, r *dns.Msg) {
 
 	fromUp, err := s.resolver.Query(r, tr)
 	if err != nil {
-		glog.Infof(err.Error())
+		log.Infof(err.Error())
 		tr.LazyPrintf(err.Error())
 		tr.SetError()
 		return
@@ -147,7 +147,7 @@ func (s *Server) Handler(w dns.ResponseWriter, r *dns.Msg) {
 func (s *Server) ListenAndServe() {
 	err := s.resolver.Init()
 	if err != nil {
-		glog.Fatalf("Error initializing: %v", err)
+		log.Fatalf("Error initializing: %v", err)
 	}
 
 	go s.resolver.Maintain()
@@ -160,21 +160,21 @@ func (s *Server) ListenAndServe() {
 }
 
 func (s *Server) classicServe() {
-	glog.Infof("DNS listening on %s", s.Addr)
+	log.Infof("DNS listening on %s", s.Addr)
 
 	var wg sync.WaitGroup
 	wg.Add(1)
 	go func() {
 		defer wg.Done()
 		err := dns.ListenAndServe(s.Addr, "udp", dns.HandlerFunc(s.Handler))
-		glog.Fatalf("Exiting UDP: %v", err)
+		log.Fatalf("Exiting UDP: %v", err)
 	}()
 
 	wg.Add(1)
 	go func() {
 		defer wg.Done()
 		err := dns.ListenAndServe(s.Addr, "tcp", dns.HandlerFunc(s.Handler))
-		glog.Fatalf("Exiting TCP: %v", err)
+		log.Fatalf("Exiting TCP: %v", err)
 	}()
 
 	wg.Wait()
@@ -187,12 +187,12 @@ func (s *Server) systemdServe() {
 	// entries for the file descriptors that don't match.
 	pconns, err := activation.PacketConns(false)
 	if err != nil {
-		glog.Fatalf("Error getting systemd packet conns: %v", err)
+		log.Fatalf("Error getting systemd packet conns: %v", err)
 	}
 
 	listeners, err := activation.Listeners(false)
 	if err != nil {
-		glog.Fatalf("Error getting systemd listeners: %v", err)
+		log.Fatalf("Error getting systemd listeners: %v", err)
 	}
 
 	var wg sync.WaitGroup
@@ -205,9 +205,9 @@ func (s *Server) systemdServe() {
 		wg.Add(1)
 		go func(c net.PacketConn) {
 			defer wg.Done()
-			glog.Infof("Activate on packet connection (UDP)")
+			log.Infof("Activate on packet connection (UDP)")
 			err := dns.ActivateAndServe(nil, c, dns.HandlerFunc(s.Handler))
-			glog.Fatalf("Exiting UDP listener: %v", err)
+			log.Fatalf("Exiting UDP listener: %v", err)
 		}(pconn)
 	}
 
@@ -219,14 +219,14 @@ func (s *Server) systemdServe() {
 		wg.Add(1)
 		go func(l net.Listener) {
 			defer wg.Done()
-			glog.Infof("Activate on listening socket (TCP)")
+			log.Infof("Activate on listening socket (TCP)")
 			err := dns.ActivateAndServe(l, nil, dns.HandlerFunc(s.Handler))
-			glog.Fatalf("Exiting TCP listener: %v", err)
+			log.Fatalf("Exiting TCP listener: %v", err)
 		}(lis)
 	}
 
 	wg.Wait()
 
 	// We should only get here if there were no useful sockets.
-	glog.Fatalf("No systemd sockets, did you forget the .socket?")
+	log.Fatalf("No systemd sockets, did you forget the .socket?")
 }
diff --git a/internal/dnstohttps/https_test.go b/internal/dnstohttps/https_test.go
index 2a52108..ac5a57e 100644
--- a/internal/dnstohttps/https_test.go
+++ b/internal/dnstohttps/https_test.go
@@ -13,7 +13,6 @@ import (
 	"blitiri.com.ar/go/dnss/internal/dnsserver"
 	"blitiri.com.ar/go/dnss/internal/testutil"
 
-	"github.com/golang/glog"
 	"github.com/miekg/dns"
 )
 
@@ -119,11 +118,8 @@ const jsonNXDOMAIN = ` {
 // Address where we will set up the DNS server.
 var DNSAddr string
 
-// realMain is the real main function, which returns the value to pass to
-// os.Exit(). We have to do this so we can use defer.
-func realMain(m *testing.M) int {
+func TestMain(m *testing.M) {
 	flag.Parse()
-	defer glog.Flush()
 
 	DNSAddr = testutil.GetFreePort()
 
@@ -134,7 +130,7 @@ func realMain(m *testing.M) int {
 	srvURL, err := url.Parse(httpsrv.URL)
 	if err != nil {
 		fmt.Printf("Failed to parse test http server URL: %v\n", err)
-		return 1
+		os.Exit(1)
 	}
 	r := NewJSONResolver(srvURL, "")
 	dth := dnsserver.New(DNSAddr, r, "")
@@ -145,12 +141,7 @@ func realMain(m *testing.M) int {
 	if err != nil {
 		fmt.Printf("Error waiting for the test servers to start: %v\n", err)
 		fmt.Printf("Check the INFO logs for more details\n")
-		return 1
+		os.Exit(1)
 	}
-
-	return m.Run()
-}
-
-func TestMain(m *testing.M) {
-	os.Exit(realMain(m))
+	os.Exit(m.Run())
 }
diff --git a/internal/dnstohttps/resolver.go b/internal/dnstohttps/resolver.go
index 5465e0c..233d0c8 100644
--- a/internal/dnstohttps/resolver.go
+++ b/internal/dnstohttps/resolver.go
@@ -15,8 +15,8 @@ import (
 
 	"blitiri.com.ar/go/dnss/internal/dnsjson"
 	"blitiri.com.ar/go/dnss/internal/dnsserver"
+	"blitiri.com.ar/go/log"
 
-	"github.com/golang/glog"
 	"github.com/miekg/dns"
 	"golang.org/x/net/trace"
 )
@@ -115,7 +115,7 @@ func (r *httpsResolver) queryDoH(req *dns.Msg, tr trace.Trace) (*dns.Msg, error)
 		return nil, fmt.Errorf("cannot pack query: %v", err)
 	}
 
-	if glog.V(3) {
+	if log.V(3) {
 		tr.LazyPrintf("DoH POST %v", r.Upstream)
 	}
 
@@ -181,7 +181,7 @@ func (r *httpsResolver) queryJSON(req *dns.Msg, tr trace.Trace) (*dns.Msg, error
 	url.RawQuery = vs.Encode()
 	// TODO: add random_padding.
 
-	if glog.V(3) {
+	if log.V(3) {
 		tr.LazyPrintf("JSON GET %v", url)
 	}
 
diff --git a/internal/httpserver/server.go b/internal/httpserver/server.go
index 7882656..e14c339 100644
--- a/internal/httpserver/server.go
+++ b/internal/httpserver/server.go
@@ -25,7 +25,7 @@ import (
 
 	"blitiri.com.ar/go/dnss/internal/dnsjson"
 	"blitiri.com.ar/go/dnss/internal/util"
-	"github.com/golang/glog"
+	"blitiri.com.ar/go/log"
 	"github.com/miekg/dns"
 	"golang.org/x/net/trace"
 )
@@ -53,14 +53,14 @@ func (s *Server) ListenAndServe() {
 		Handler: mux,
 	}
 
-	glog.Infof("HTTPS listening on %s", s.Addr)
+	log.Infof("HTTPS listening on %s", s.Addr)
 	var err error
 	if InsecureForTesting {
 		err = srv.ListenAndServe()
 	} else {
 		err = srv.ListenAndServeTLS(s.CertFile, s.KeyFile)
 	}
-	glog.Fatalf("HTTPS exiting: %s", err)
+	log.Fatalf("HTTPS exiting: %s", err)
 }
 
 // Resolve implements the HTTP handler for incoming DNS resolution requests.
diff --git a/internal/util/trace.go b/internal/util/trace.go
index 7cfc8f6..3ce1bf7 100644
--- a/internal/util/trace.go
+++ b/internal/util/trace.go
@@ -4,14 +4,15 @@ import (
 	"fmt"
 	"strings"
 
-	"github.com/golang/glog"
+	"blitiri.com.ar/go/log"
+
 	"github.com/miekg/dns"
 	"golang.org/x/net/trace"
 )
 
 // TraceQuestion adds the given question to the trace.
 func TraceQuestion(tr trace.Trace, qs []dns.Question) {
-	if !glog.V(3) {
+	if !log.V(3) {
 		return
 	}
 
@@ -29,7 +30,7 @@ func questionsToString(qs []dns.Question) string {
 
 // TraceAnswer adds the given DNS answer to the trace.
 func TraceAnswer(tr trace.Trace, m *dns.Msg) {
-	if !glog.V(3) {
+	if !log.V(3) {
 		return
 	}
 
@@ -41,7 +42,7 @@ func TraceAnswer(tr trace.Trace, m *dns.Msg) {
 
 // TraceError adds the given error to the trace.
 func TraceError(tr trace.Trace, err error) {
-	glog.Info(err.Error())
+	log.Infof(err.Error())
 	tr.LazyPrintf(err.Error())
 	tr.SetError()
 }