Skip to content

Commit

Permalink
Replace log with glog, and increase the level of payload logging
Browse files Browse the repository at this point in the history
  • Loading branch information
albertito committed Sep 11, 2015
1 parent 9eb23e0 commit 2958a6a
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 29 deletions.
27 changes: 22 additions & 5 deletions dnss.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ package main

import (
"flag"
"log"
"sync"
"time"

"github.com/golang/glog"

// Make GRPC log to glog.
_ "google.golang.org/grpc/grpclog/glogger"

"blitiri.com.ar/go/dnss/dnstogrpc"
"blitiri.com.ar/go/dnss/grpctodns"
"blitiri.com.ar/go/logconfig"
"blitiri.com.ar/go/profile"
)

Expand All @@ -32,17 +36,30 @@ var (
"certificate file for the GRPC server")
grpcKey = flag.String("grpc_key", "",
"key file for the GRPC server")

logFlushEvery = flag.Duration("log_flush_every", 30*time.Second,
"how often to flush logs")
)

func flushLogs() {
c := time.Tick(*logFlushEvery)
for range c {
glog.Flush()
}
}

func main() {
defer glog.Flush()

flag.Parse()

logconfig.Init("dnss")
profile.Init()

go flushLogs()

if !*enableDNStoGRPC && !*enableGRPCtoDNS {
log.Fatalf(
"ERROR: pass --enable_dns_to_grpc or --enable_grpc_to_dns\n")
glog.Fatal(
"Error: pass --enable_dns_to_grpc or --enable_grpc_to_dns")
}

var wg sync.WaitGroup
Expand Down
27 changes: 13 additions & 14 deletions dnstogrpc/dnstogrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ package dnstogrpc

import (
"fmt"
"log"
"sync"
"time"

pb "blitiri.com.ar/go/dnss/proto"
"blitiri.com.ar/go/dnss/util"
"github.com/golang/glog"
"github.com/miekg/dns"
"golang.org/x/net/context"
"google.golang.org/grpc"
Expand Down Expand Up @@ -80,28 +80,28 @@ func New(addr, upstream, caFile string) *Server {
}
}

func p(w dns.ResponseWriter, r *dns.Msg) string {
return fmt.Sprintf("%v %v", w.RemoteAddr(), r.Id)
}

func (s *Server) Handler(w dns.ResponseWriter, r *dns.Msg) {
log.Printf("DNS %v %v\n", p(w, r), util.QuestionsToString(r.Question))
prefix := ""
if glog.V(3) {
prefix = fmt.Sprintf("%v %v", w.RemoteAddr(), r.Id)
glog.Infof("DNS %v %v", prefix, util.QuestionsToString(r.Question))
}

// TODO: we should create our own IDs, in case different users pick the
// same id and we pass that upstream.

from_up, err := s.client.Query(r)
if err != nil {
log.Printf("DNS %v ERR: %v\n", p(w, r), err)
glog.V(3).Infof("DNS %v ERR: %v", prefix, err)
}

if from_up != nil {
if from_up.Rcode != dns.RcodeSuccess {
rcode := dns.RcodeToString[from_up.Rcode]
log.Printf("DNS %v !-> %v\n", p(w, r), rcode)
glog.V(3).Infof("DNS %v !-> %v", prefix, rcode)
}
for _, rr := range from_up.Answer {
log.Printf("DNS %v -> %v\n", p(w, r), rr)
glog.V(3).Infof("DNS %v -> %v", prefix, rr)
}
w.WriteMsg(from_up)
}
Expand All @@ -110,26 +110,25 @@ func (s *Server) Handler(w dns.ResponseWriter, r *dns.Msg) {
func (s *Server) ListenAndServe() {
err := s.client.Connect()
if err != nil {
// TODO: handle errors and reconnect.
log.Printf("Error creating GRPC client: %v\n", err)
glog.Errorf("Error creating GRPC client: %v", err)
return
}

log.Printf("DNS listening on %s\n", s.Addr)
glog.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))
log.Printf("Exiting UDP: %v\n", err)
glog.Errorf("Exiting UDP: %v", err)
}()

wg.Add(1)
go func() {
defer wg.Done()
err := dns.ListenAndServe(s.Addr, "tcp", dns.HandlerFunc(s.Handler))
log.Printf("Exiting TCP: %v\n", err)
glog.Errorf("Exiting TCP: %v", err)
}()

wg.Wait()
Expand Down
23 changes: 13 additions & 10 deletions grpctodns/grpctodns.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ package grpctodns

import (
"fmt"
"log"
"net"
"strings"

pb "blitiri.com.ar/go/dnss/proto"
"blitiri.com.ar/go/dnss/util"
"github.com/golang/glog"
"github.com/miekg/dns"
"golang.org/x/net/context"
"google.golang.org/grpc"
Expand Down Expand Up @@ -48,13 +48,15 @@ func (s *Server) Query(ctx context.Context, in *pb.RawMsg) (*pb.RawMsg, error) {
return nil, err
}

log.Printf("GRPC %v\n", util.QuestionsToString(r.Question))
if glog.V(3) {
glog.Infof("GRPC %v", util.QuestionsToString(r.Question))
}

// TODO: we should create our own IDs, in case different users pick the
// same id and we pass that upstream.
from_up, err := dns.Exchange(r, s.Upstream)
if err != nil {
log.Printf("GRPC ERR: %v\n", err)
glog.V(3).Infof("GRPC ERR: %v", err)
return nil, err
}

Expand All @@ -64,15 +66,15 @@ func (s *Server) Query(ctx context.Context, in *pb.RawMsg) (*pb.RawMsg, error) {

if from_up.Rcode != dns.RcodeSuccess {
rcode := dns.RcodeToString[from_up.Rcode]
log.Printf("GPRC !-> %v\n", rcode)
glog.V(3).Infof("GPRC !-> %v", rcode)
}
for _, rr := range from_up.Answer {
log.Printf("GRPC -> %v\n", rr)
glog.V(3).Infof("GRPC -> %v", rr)
}

buf, err := from_up.Pack()
if err != nil {
log.Printf("GRPC ERR: %v\n", err)
glog.V(3).Infof("GRPC ERR: %v", err)
return nil, err
}

Expand All @@ -82,19 +84,20 @@ func (s *Server) Query(ctx context.Context, in *pb.RawMsg) (*pb.RawMsg, error) {
func (s *Server) ListenAndServe() {
lis, err := net.Listen("tcp", s.Addr)
if err != nil {
log.Printf("failed to listen: %v", err)
glog.Errorf("failed to listen: %v", err)
return
}

ta, err := credentials.NewServerTLSFromFile(s.CertFile, s.KeyFile)
if err != nil {
log.Printf("failed to create TLS transport auth: %v", err)
glog.Errorf("failed to create TLS transport auth: %v", err)
return
}

grpcServer := grpc.NewServer(grpc.Creds(ta))
pb.RegisterDNSServiceServer(grpcServer, s)

log.Printf("GRPC listening on %s\n", s.Addr)
grpcServer.Serve(lis)
glog.Errorf("GRPC listening on %s", s.Addr)
err = grpcServer.Serve(lis)
glog.Errorf("GRPC exiting: %s", err)
}

0 comments on commit 2958a6a

Please sign in to comment.