Package list golang-github-go-kit-kit / 42b253c
Unify logging idioms - Context shall be provided by caller, not in constructor - Don't bother with _ = logger.Log; turn off that lint complaint - Minor linter fixes Closes #148 Peter Bourgon 6 years ago
12 changed file(s) with 49 addition(s) and 49 deletion(s). Raw diff Collapse all Expand all
5454 case "grpc":
5555 cc, err := grpc.Dial(*grpcAddr)
5656 if err != nil {
57 _ = logger.Log("err", err)
57 logger.Log("err", err)
5858 os.Exit(1)
5959 }
6060 defer cc.Close()
6767 }
6868 baseurl, err := url.Parse(rawurl)
6969 if err != nil {
70 _ = logger.Log("err", err)
70 logger.Log("err", err)
7171 os.Exit(1)
7272 }
7373 svc = httpjsonclient.New(root, baseurl, logger, nil)
7575 case "netrpc":
7676 cli, err := rpc.DialHTTP("tcp", *netrpcAddr)
7777 if err != nil {
78 _ = logger.Log("err", err)
78 logger.Log("err", err)
7979 os.Exit(1)
8080 }
8181 defer cli.Close()
9393 case "binary", "":
9494 protocolFactory = thrift.NewTBinaryProtocolFactoryDefault()
9595 default:
96 _ = logger.Log("protocol", *thriftProtocol, "err", "invalid protocol")
96 logger.Log("protocol", *thriftProtocol, "err", "invalid protocol")
9797 os.Exit(1)
9898 }
9999 var transportFactory thrift.TTransportFactory
107107 }
108108 transportSocket, err := thrift.NewTSocket(*thriftAddr)
109109 if err != nil {
110 _ = logger.Log("during", "thrift.NewTSocket", "err", err)
110 logger.Log("during", "thrift.NewTSocket", "err", err)
111111 os.Exit(1)
112112 }
113113 trans := transportFactory.GetTransport(transportSocket)
114114 defer trans.Close()
115115 if err := trans.Open(); err != nil {
116 _ = logger.Log("during", "thrift transport.Open", "err", err)
116 logger.Log("during", "thrift transport.Open", "err", err)
117117 os.Exit(1)
118118 }
119119 cli := thriftadd.NewAddServiceClientFactory(trans, protocolFactory)
120120 svc = thriftclient.New(cli, logger)
121121
122122 default:
123 _ = logger.Log("err", "invalid transport")
123 logger.Log("err", "invalid transport")
124124 os.Exit(1)
125125 }
126126
130130 a, _ := strconv.Atoi(s1)
131131 b, _ := strconv.Atoi(s2)
132132 v := svc.Sum(a, b)
133 _ = logger.Log("method", "sum", "a", a, "b", b, "v", v, "took", time.Since(begin))
133 logger.Log("method", "sum", "a", a, "b", b, "v", v, "took", time.Since(begin))
134134
135135 case "concat":
136136 a, b := s1, s2
137137 v := svc.Concat(a, b)
138 _ = logger.Log("method", "concat", "a", a, "b", b, "v", v, "took", time.Since(begin))
138 logger.Log("method", "concat", "a", a, "b", b, "v", v, "took", time.Since(begin))
139139
140140 default:
141 _ = logger.Log("err", "invalid method "+method)
141 logger.Log("err", "invalid method "+method)
142142 os.Exit(1)
143143 }
144144 }
9393 zipkin.ScribeBatchInterval(*zipkinCollectorBatchInterval),
9494 zipkin.ScribeLogger(zipkinLogger),
9595 ); err != nil {
96 _ = zipkinLogger.Log("err", err)
96 zipkinLogger.Log("err", err)
9797 os.Exit(1)
9898 }
9999 }
119119 // Debug/instrumentation
120120 go func() {
121121 transportLogger := log.NewContext(logger).With("transport", "debug")
122 _ = transportLogger.Log("addr", *debugAddr)
122 transportLogger.Log("addr", *debugAddr)
123123 errc <- http.ListenAndServe(*debugAddr, nil) // DefaultServeMux
124124 }()
125125
158158 httptransport.ServerErrorLogger(transportLogger),
159159 ))
160160
161 _ = transportLogger.Log("addr", *httpAddr)
161 transportLogger.Log("addr", *httpAddr)
162162 errc <- http.ListenAndServe(*httpAddr, mux)
163163 }()
164164
172172 }
173173 s := grpc.NewServer() // uses its own, internal context
174174 pb.RegisterAddServer(s, grpcBinding{svc})
175 _ = transportLogger.Log("addr", *grpcAddr)
175 transportLogger.Log("addr", *grpcAddr)
176176 errc <- s.Serve(ln)
177177 }()
178178
185185 return
186186 }
187187 s.HandleHTTP(rpc.DefaultRPCPath, rpc.DefaultDebugPath)
188 _ = transportLogger.Log("addr", *netrpcAddr)
188 transportLogger.Log("addr", *netrpcAddr)
189189 errc <- http.ListenAndServe(*netrpcAddr, s)
190190 }()
191191
220220 return
221221 }
222222 transportLogger := log.NewContext(logger).With("transport", "net/rpc")
223 _ = transportLogger.Log("addr", *thriftAddr)
223 transportLogger.Log("addr", *thriftAddr)
224224 errc <- thrift.NewTSimpleServer4(
225225 thriftadd.NewAddServiceProcessor(thriftBinding{svc}),
226226 transport,
229229 ).Serve()
230230 }()
231231
232 _ = logger.Log("fatal", <-errc)
232 logger.Log("fatal", <-errc)
233233 }
234234
235235 func interrupt() error {
246246 for i, a := range annotations {
247247 values[i] = a.Value
248248 }
249 _ = c.Logger.Log(
249 c.Logger.Log(
250250 "trace_id", s.TraceID(),
251251 "span_id", s.SpanID(),
252252 "parent_span_id", s.ParentSpanID(),
5959 http.Handle("/uppercase", uppercaseHandler)
6060 http.Handle("/count", countHandler)
6161 http.Handle("/metrics", stdprometheus.Handler())
62 _ = logger.Log("msg", "HTTP", "addr", ":8080")
63 _ = logger.Log("err", http.ListenAndServe(":8080", nil))
62 logger.Log("msg", "HTTP", "addr", ":8080")
63 logger.Log("err", http.ListenAndServe(":8080", nil))
6464 }
6969 http.Handle("/uppercase", uppercaseHandler)
7070 http.Handle("/count", countHandler)
7171 http.Handle("/metrics", stdprometheus.Handler())
72 _ = logger.Log("msg", "HTTP", "addr", *listen)
73 _ = logger.Log("err", http.ListenAndServe(*listen, nil))
72 logger.Log("msg", "HTTP", "addr", *listen)
73 logger.Log("err", http.ListenAndServe(*listen, nil))
7474 }
2222
2323 func proxyingMiddleware(proxyList string, ctx context.Context, logger log.Logger) ServiceMiddleware {
2424 if proxyList == "" {
25 _ = logger.Log("proxy_to", "none")
25 logger.Log("proxy_to", "none")
2626 return func(next StringService) StringService { return next }
2727 }
2828 proxies := split(proxyList)
29 _ = logger.Log("proxy_to", fmt.Sprint(proxies))
29 logger.Log("proxy_to", fmt.Sprint(proxies))
3030
3131 return func(next StringService) StringService {
3232 var (
11
22 import (
33 "fmt"
4 "strings"
45
56 consul "github.com/hashicorp/consul/api"
67
3334 service string,
3435 tags ...string,
3536 ) (*Publisher, error) {
36 logger = log.NewContext(logger).With("component", "Consul Publisher")
37
3837 p := &Publisher{
3938 cache: loadbalancer.NewEndpointCache(factory, logger),
4039 client: client,
4443 quitc: make(chan struct{}),
4544 }
4645
47 is, index, err := p.getInstances(defaultIndex)
48 if err != nil {
49 return nil, err
46 instances, index, err := p.getInstances(defaultIndex)
47 if err == nil {
48 logger.Log("service", service, "tags", strings.Join(tags, ", "), "instances", len(instances))
49 } else {
50 logger.Log("service", service, "tags", strings.Join(tags, ", "), "err", err)
5051 }
51
52 p.cache.Replace(is)
52 p.cache.Replace(instances)
5353
5454 go p.loop(index)
5555
7474
7575 for {
7676 go func() {
77 is, index, err := p.getInstances(lastIndex)
77 instances, index, err := p.getInstances(lastIndex)
7878 if err != nil {
7979 errc <- err
8080 return
8181 }
82
8382 resc <- response{
8483 index: index,
85 instances: is,
84 instances: instances,
8685 }
8786 }()
8887
158157 }
159158
160159 func makeInstances(entries []*consul.ServiceEntry) []string {
161 is := make([]string, len(entries))
160 instances := make([]string, len(entries))
162161
163162 for i, entry := range entries {
164163 addr := entry.Node.Address
167166 addr = entry.Service.Address
168167 }
169168
170 is[i] = fmt.Sprintf("%s:%d", addr, entry.Service.Port)
169 instances[i] = fmt.Sprintf("%s:%d", addr, entry.Service.Port)
171170 }
172171
173 return is
172 return instances
174173 }
3434 }
3535
3636 instances, err := p.resolve()
37 if err != nil {
38 logger.Log(name, len(instances))
37 if err == nil {
38 logger.Log("name", name, "instances", len(instances))
3939 } else {
40 logger.Log(name, err)
40 logger.Log("name", name, "err", err)
4141 }
4242 p.cache.Replace(instances)
4343
3030
3131 instances, err := p.client.GetEntries(p.prefix)
3232 if err == nil {
33 logger.Log(p.prefix, len(instances))
33 logger.Log("prefix", p.prefix, "instances", len(instances))
3434 } else {
35 logger.Log("msg", "failed to retrieve entries", "err", err)
35 logger.Log("prefix", p.prefix, "err", err)
3636 }
3737 p.cache.Replace(instances)
3838
1616 for _, instance := range instances {
1717 e, _, err := factory(instance) // never close
1818 if err != nil {
19 _ = logger.Log("instance", instance, "err", err)
19 logger.Log("instance", instance, "err", err)
2020 continue
2121 }
2222 endpoints = append(endpoints, e)
11
22 type nopLogger struct{}
33
4 // NewNopLogger returns a logger that doesn't do anything.
5 func NewNopLogger() Logger { return nopLogger{} }
6
47 func (nopLogger) Log(...interface{}) error { return nil }
5
6 func NewNopLogger() Logger { return nopLogger{} }
8484
8585 request, err := s.dec(r)
8686 if err != nil {
87 _ = s.logger.Log("err", err)
87 s.logger.Log("err", err)
8888 s.errorEncoder(w, BadRequestError{err})
8989 return
9090 }
9191
9292 response, err := s.e(ctx, request)
9393 if err != nil {
94 _ = s.logger.Log("err", err)
94 s.logger.Log("err", err)
9595 s.errorEncoder(w, err)
9696 return
9797 }
101101 }
102102
103103 if err := s.enc(w, response); err != nil {
104 _ = s.logger.Log("err", err)
104 s.logger.Log("err", err)
105105 s.errorEncoder(w, err)
106106 return
107107 }
128128
129129 func TestBadRequestError(t *testing.T) {
130130 inner := testBadRequestError{1234}
131 var outer error = httptransport.BadRequestError{inner}
131 var outer error = httptransport.BadRequestError{Err: inner}
132132 err := outer.(httptransport.BadRequestError)
133133 if want, have := inner, err.Err; want != have {
134134 t.Errorf("want %#v, have %#v", want, have)