Documentation ¶
Overview ¶
Package zipkin provides a monkit plugin for sending traces to Zipkin.
Example usage ¶
Your main method gets set up something like this:
package main import ( "net/http" "gopkg.in/spacemonkeygo/monkit-zipkin.v2" "gopkg.in/spacemonkeygo/monkit.v2" "gopkg.in/spacemonkeygo/monkit.v2/environment" "gopkg.in/spacemonkeygo/monkit.v2/present" ) func main() { environment.Register(monkit.Default) go http.ListenAndServe("localhost:9000", present.HTTP(monkit.Default)) collector, err := zipkin.NewScribeCollector("zipkin.whatever:9042") if err != nil { panic(err) } zipkin.RegisterZipkin(monkit.Default, collector, zipkin.Options{ Fraction: 1}) ... }
Once you've done that, you need to make sure your HTTP handlers pull Zipkin Context info from the Request. That's easy to do with zipkin.ContextWrapper.
func HandleRequest(ctx context.Context, w http.ResponseWriter, r *http.Request) { defer mon.Task()(&ctx)(nil) ... whatever } func DoTheThing(ctx context.Context) (err error) { defer mon.Task()(&ctx)(&err) return http.Serve(listener, zipkin.ContextWrapper( zipkin.TraceHandler(zipkin.ContextHTTPHandlerFunc(HandleRequest)))) }
Last, your outbound HTTP requests need to pass through Context info:
func MakeRequest(ctx context.Context) (err error) { defer mon.Task()(&ctx)(&err) req, err := http.NewRequest(...) if err != nil { return err } resp, err := zipkin.TraceRequest(ctx, http.DefaultClient, req) ... }
Index ¶
- func ContextWrapper(h ContextHTTPHandler) http.Handler
- func RedirectPackets(listen_addr string, collector *ScribeCollector) error
- func RegisterZipkin(reg *monkit.Registry, collector TraceCollector, opts Options)
- func TraceRequest(ctx context.Context, cl Client, req *http.Request) (resp *http.Response, err error)
- type Client
- type ContextHTTPHandler
- type ContextHTTPHandlerFunc
- type HeaderGetter
- type HeaderSetter
- type Options
- type Request
- type ScribeCollector
- type TraceCollector
- type UDPCollector
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ContextWrapper ¶
func ContextWrapper(h ContextHTTPHandler) http.Handler
ContextWrapper will turn a ContextHTTPHandler into an http.Handler by passing a new Context into every request.
func RedirectPackets ¶
func RedirectPackets(listen_addr string, collector *ScribeCollector) error
RedirectPackets is a method that handles incoming packets from the UDPCollector class. RedirectPackets, when running, will listen for UDP packets containing serialized zipkin.Span objects on listen_addr, then will resend those packets to the given ScribeCollector. On any error, RedirectPackets currently aborts.
func RegisterZipkin ¶
func RegisterZipkin(reg *monkit.Registry, collector TraceCollector, opts Options)
RegisterZipkin configures the given Registry reg to send the Spans from some portion of all new Traces to the given TraceCollector.
Types ¶
type ContextHTTPHandler ¶
type ContextHTTPHandler interface {
ServeHTTP(ctx context.Context, w http.ResponseWriter, r *http.Request)
}
ContextHTTPHandler is like http.Handler, but expects a Context object as the first parameter.
func TraceHandler ¶
func TraceHandler(c ContextHTTPHandler) ContextHTTPHandler
TraceHandler wraps a ContextHTTPHandler with a Span pulled from incoming requests, possibly starting new Traces if necessary.
type ContextHTTPHandlerFunc ¶
ContextHTTPHandlerFunc is like http.HandlerFunc but for ContextHTTPHandlers
func (ContextHTTPHandlerFunc) ServeHTTP ¶
func (f ContextHTTPHandlerFunc) ServeHTTP(ctx context.Context, w http.ResponseWriter, r *http.Request)
type HeaderGetter ¶
HeaderGetter is an interface that http.Header matches for RequestFromHeader
type HeaderSetter ¶
HeaderSetter is an interface that http.Header matches for Request.SetHeader
type Request ¶
Request is a structure representing an incoming RPC request. Every field is optional.
func RequestFromHeader ¶
func RequestFromHeader(header HeaderGetter) (rv Request)
RequestFromHeader will create a Request object given an http.Header or anything that matches the HeaderGetter interface.
func RequestFromSpan ¶
func RequestFromSpan(s *monkit.Span) Request
func (Request) SetHeader ¶
func (r Request) SetHeader(header HeaderSetter)
SetHeader will take a Request and fill out an http.Header, or anything that matches the HeaderSetter interface.
type ScribeCollector ¶
type ScribeCollector struct {
// contains filtered or unexported fields
}
ScribeCollector matches the TraceCollector interface, but writes directly to a connected Scribe socket.
func NewScribeCollector ¶
func NewScribeCollector(scribe_addr string) (*ScribeCollector, error)
NewScribeCollector creates a ScribeCollector. scribe_addr is the address of the Scribe endpoint, typically "127.0.0.1:9410"
func (*ScribeCollector) Close ¶
func (s *ScribeCollector) Close() error
Close closes an existing ScribeCollector
func (*ScribeCollector) Collect ¶
func (c *ScribeCollector) Collect(s *zipkin.Span)
Collect will serialize and send a zipkin.Span to the configured Scribe endpoint
func (*ScribeCollector) CollectSerialized ¶
func (c *ScribeCollector) CollectSerialized(serialized []byte) error
CollectSerialized buffers a serialized zipkin.Span to be sent to the Scribe endpoint. It returns an error and loses the log entry if the buffer is full.
type TraceCollector ¶
type TraceCollector interface { // Collect gets called with a Span whenever a Span is completed on a // SpanManager. Collect(span *zipkin.Span) }
TraceCollector is an interface dealing with completed Spans on a SpanManager. See RegisterZipkin.
type UDPCollector ¶
type UDPCollector struct {
// contains filtered or unexported fields
}
UDPCollector matches the TraceCollector interface, but sends serialized zipkin.Span objects over UDP, instead of the Scribe protocol. See RedirectPackets for the UDP server-side code.
func NewUDPCollector ¶
func NewUDPCollector(collector_addr string, buffer_size int) ( *UDPCollector, error)
NewUDPCollector creates a UDPCollector that sends packets to collector_addr. buffer_size is how many outstanding unsent zipkin.Span objects can exist before Spans start getting dropped.
func (*UDPCollector) Collect ¶
func (c *UDPCollector) Collect(span *zipkin.Span)
Collect takes a zipkin.Span object, serializes it, and sends it to the configured collector_addr.