Codebase list golang-github-go-kit-kit / 4b50a0b
Remove package-global Log for parameterized logger Peter Bourgon 8 years ago
6 changed file(s) with 36 addition(s) and 30 deletion(s). Raw diff Collapse all Expand all
9797 *zipkinCollectorTimeout,
9898 *zipkinCollectorBatchSize,
9999 *zipkinCollectorBatchInterval,
100 logger,
100101 ); err != nil {
101102 logger.Log("err", err)
102103 os.Exit(1)
104105 }
105106 zipkinMethodName := "add"
106107 zipkinSpanFunc := zipkin.MakeNewSpanFunc(zipkinHostPort, *zipkinServiceName, zipkinMethodName)
107 zipkin.Log.Swap(logger) // log diagnostic/error details
108108
109109 // Our business and operational domain
110110 var a Add = pureAdd
141141 go func() {
142142 ctx, cancel := context.WithCancel(root)
143143 defer cancel()
144 before := []httptransport.BeforeFunc{zipkin.ToContext(zipkinSpanFunc)}
144 before := []httptransport.BeforeFunc{zipkin.ToContext(zipkinSpanFunc, logger)}
145145 after := []httptransport.AfterFunc{}
146146 handler := makeHTTPBinding(ctx, e, before, after)
147147 logger.Log("addr", *httpAddr, "transport", "HTTP/JSON")
99
1010 "github.com/apache/thrift/lib/go/thrift"
1111
12 "github.com/go-kit/kit/log"
1213 "github.com/go-kit/kit/tracing/zipkin/_thrift/gen-go/scribe"
1314 )
1415
3031 nextSend time.Time
3132 batchInterval time.Duration
3233 batchSize int
34 logger log.Logger
3335 }
3436
35 // NewScribeCollector returns a new Scribe-backed Collector, ready for use.
36 func NewScribeCollector(addr string, timeout time.Duration, batchSize int, batchInterval time.Duration) (Collector, error) {
37 // NewScribeCollector returns a new Scribe-backed Collector. addr should be a
38 // TCP endpoint of the form "host:port". timeout is passed to the Thrift dial
39 // function NewTSocketFromAddrTimeout. batchSize and batchInterval control the
40 // maximum size and interval of a batch of spans; as soon as either limit is
41 // reached, the batch is sent. The logger is used to log errors, such as batch
42 // send failures; users should provide an appropriate context, if desired.
43 func NewScribeCollector(addr string, timeout time.Duration, batchSize int, batchInterval time.Duration, logger log.Logger) (Collector, error) {
3744 factory := scribeClientFactory(addr, timeout)
3845 client, err := factory()
3946 if err != nil {
4855 nextSend: time.Now().Add(batchInterval),
4956 batchInterval: batchInterval,
5057 batchSize: batchSize,
58 logger: logger,
5159 }
5260 go c.loop()
5361 return c, nil
8189 case <-c.sendc:
8290 c.nextSend = time.Now().Add(c.batchInterval)
8391 if err := c.send(c.batch); err != nil {
84 Log.Log("err", err.Error())
92 c.logger.Log("err", err.Error())
8593 continue
8694 }
8795 c.batch = c.batch[:0]
22 import (
33 "encoding/base64"
44 "fmt"
5 "io/ioutil"
56 "math/rand"
67 "net"
78 "sync"
1011
1112 "github.com/apache/thrift/lib/go/thrift"
1213
14 "github.com/go-kit/kit/log"
1315 "github.com/go-kit/kit/tracing/zipkin"
1416 "github.com/go-kit/kit/tracing/zipkin/_thrift/gen-go/scribe"
1517 "github.com/go-kit/kit/tracing/zipkin/_thrift/gen-go/zipkincore"
2022
2123 timeout := time.Second
2224 batchInterval := time.Millisecond
23 c, err := zipkin.NewScribeCollector(server.addr(), timeout, 0, batchInterval)
25 c, err := zipkin.NewScribeCollector(server.addr(), timeout, 0, batchInterval, log.NewLogfmtLogger(ioutil.Discard))
2426 if err != nil {
2527 t.Fatal(err)
2628 }
2929 //binaryAnnotations []BinaryAnnotation // TODO
3030 }
3131
32 // NewSpan returns a new Span object ready for use.
32 // NewSpan returns a new Span, which can be annotated and collected by a
33 // collector. Spans are passed through the request context to each middleware
34 // under the SpanContextKey.
3335 func NewSpan(hostport, serviceName, methodName string, traceID, spanID, parentSpanID int64) *Span {
3436 return &Span{
3537 host: makeEndpoint(hostport, serviceName),
4042 }
4143 }
4244
43 // makeEndpoint will return a nil Endpoint if the input parameters are
44 // malformed.
45 // makeEndpoint takes the hostport and service name that represent this Zipkin
46 // service, and returns an endpoint that's embedded into the Zipkin core Span
47 // type. It will return a nil endpoint if the input parameters are malformed.
4548 func makeEndpoint(hostport, serviceName string) *zipkincore.Endpoint {
4649 host, port, err := net.SplitHostPort(hostport)
4750 if err != nil {
48 Log.Log("hostport", hostport, "err", err)
4951 return nil
5052 }
5153 addrs, err := net.LookupIP(host)
5254 if err != nil {
53 Log.Log("host", host, "err", err)
5455 return nil
5556 }
5657 if len(addrs) <= 0 {
57 Log.Log("host", host, "err", "no IPs")
5858 return nil
5959 }
6060 portInt, err := strconv.ParseInt(port, 10, 16)
6161 if err != nil {
62 Log.Log("port", port, "err", err)
6362 return nil
6463 }
6564 endpoint := zipkincore.NewEndpoint()
2020 // • http://www.slideshare.net/johanoskarsson/zipkin-runtime-open-house
2121 // • https://groups.google.com/forum/#!topic/zipkin-user/KilwtSA0g1k
2222 // • https://gist.github.com/yoavaa/3478d3a0df666f21a98c
23
24 // Log is used to report diagnostic information. To enable it, swap in your
25 // application's logger.
26 var Log log.SwapLogger
2723
2824 const (
2925 // https://github.com/racker/tryfer#headers
9490 // ToContext returns a function that satisfies transport/http.BeforeFunc. It
9591 // takes a Zipkin span from the incoming HTTP request, and saves it in the
9692 // request context. It's designed to be wired into a server's HTTP transport
97 // Before stack.
98 func ToContext(newSpan NewSpanFunc) func(ctx context.Context, r *http.Request) context.Context {
93 // Before stack. The logger is used to report errors.
94 func ToContext(newSpan NewSpanFunc, logger log.Logger) func(ctx context.Context, r *http.Request) context.Context {
9995 return func(ctx context.Context, r *http.Request) context.Context {
100 return context.WithValue(ctx, SpanContextKey, fromHTTP(newSpan, r))
96 return context.WithValue(ctx, SpanContextKey, fromHTTP(newSpan, r, logger))
10197 }
10298 }
10399
125121 }
126122 }
127123
128 func fromHTTP(newSpan NewSpanFunc, r *http.Request) *Span {
124 func fromHTTP(newSpan NewSpanFunc, r *http.Request, logger log.Logger) *Span {
129125 traceIDStr := r.Header.Get(traceIDHTTPHeader)
130126 if traceIDStr == "" {
131 Log.Log("debug", "make new span")
132127 return newSpan(newID(), newID(), 0) // normal; just make a new one
133128 }
134129 traceID, err := strconv.ParseInt(traceIDStr, 16, 64)
135130 if err != nil {
136 Log.Log(traceIDHTTPHeader, traceIDStr, "err", err)
131 logger.Log(traceIDHTTPHeader, traceIDStr, "err", err)
137132 return newSpan(newID(), newID(), 0)
138133 }
139134 spanIDStr := r.Header.Get(spanIDHTTPHeader)
140135 if spanIDStr == "" {
141 Log.Log("msg", "trace ID without span ID") // abnormal
142 spanIDStr = strconv.FormatInt(newID(), 64) // deal with it
136 logger.Log("msg", "trace ID without span ID") // abnormal
137 spanIDStr = strconv.FormatInt(newID(), 64) // deal with it
143138 }
144139 spanID, err := strconv.ParseInt(spanIDStr, 16, 64)
145140 if err != nil {
146 Log.Log(spanIDHTTPHeader, spanIDStr, "err", err) // abnormal
147 spanID = newID() // deal with it
141 logger.Log(spanIDHTTPHeader, spanIDStr, "err", err) // abnormal
142 spanID = newID() // deal with it
148143 }
149144 parentSpanIDStr := r.Header.Get(parentSpanIDHTTPHeader)
150145 if parentSpanIDStr == "" {
152147 }
153148 parentSpanID, err := strconv.ParseInt(parentSpanIDStr, 16, 64)
154149 if err != nil {
155 Log.Log(parentSpanIDHTTPHeader, parentSpanIDStr, "err", err) // abnormal
156 parentSpanID = 0 // the only way to deal with it
150 logger.Log(parentSpanIDHTTPHeader, parentSpanIDStr, "err", err) // abnormal
151 parentSpanID = 0 // the only way to deal with it
157152 }
158153 return newSpan(traceID, spanID, parentSpanID)
159154 }
11
22 import (
33 "fmt"
4 "io/ioutil"
45 "net/http"
56 "reflect"
67 "runtime"
1112 "golang.org/x/net/context"
1213
1314 "github.com/go-kit/kit/endpoint"
15 "github.com/go-kit/kit/log"
1416 "github.com/go-kit/kit/tracing/zipkin"
1517 )
1618
3032 r.Header.Set("X-B3-ParentSpanId", strconv.FormatInt(parentSpanID, 16))
3133
3234 newSpan := zipkin.MakeNewSpanFunc(hostport, serviceName, methodName)
33 toContext := zipkin.ToContext(newSpan)
35 toContext := zipkin.ToContext(newSpan, log.NewLogfmtLogger(ioutil.Discard))
3436
3537 ctx := toContext(context.Background(), r)
3638 val := ctx.Value(zipkin.SpanContextKey)