buffet

package module
v0.1.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 23, 2018 License: MIT Imports: 5 Imported by: 3

README

buffet

Build Status

Buffet is an OpenTracing middleware for buffalo

Usage

In your main:

	tracer, closer := initTracer()
	defer closer.Close()
	opentracing.SetGlobalTracer(tracer)

	fmt.Println(tracer)
	app := actions.App(tracer)

initTracer looks like this:

func initTracer() (opentracing.Tracer, io.Closer) {
	sampler := jaeger.NewConstSampler(true)
	transport, err := udp.NewUDPTransport("", 0)
	if err != nil {
		log.Fatal(err)
	}
	reporter := jaeger.NewRemoteReporter(transport)

	tracer, closer := jaeger.NewTracer(ServiceName, sampler, reporter)
	return tracer, closer

}

Change your App() function to accept your tracer for initialization, and add the middleware:

func App(tracer opentracing.Tracer) *buffalo.App {
	if app == nil {
    ...
		app.Use(buffet.OpenTracing(tracer))
    ...

Then instrument your handlers:

// HomeHandler is a default handler to serve up
// a home page.
func HomeHandler(c buffalo.Context) error {
	slow(c)
	return c.Render(200, r.HTML("index.html"))
}

//BadHandler returns an error
func BadHandler(c buffalo.Context) error {
	return c.Error(401, errors.New("Unauthorized!"))
}
func slow(c buffalo.Context) {
	sp := buffet.ChildSpan("slow", c)
	defer sp.Finish()
	time.Sleep(1 * time.Millisecond)
}

HomeHandler and BadHandler are automatically instrumented because of the middleware. The slow() function isn't, so we pass in the buffalo context and derive a child span from the one in the buffalo context. This creates a child span that belongs to the parent (which was created automatically in the middleware).

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ChildSpan

func ChildSpan(opname string, c buffalo.Context) opentracing.Span

ChildSpan returns a child span derived from the buffalo context "c"

func OpenTracing

func OpenTracing(tr opentracing.Tracer) buffalo.MiddlewareFunc

OpenTracing is a buffalo middleware that adds the necessary components to the request to make it traced through OpenTracing. Initialize it by passing in an opentracing.Tracer.

func SpanFromContext

func SpanFromContext(c buffalo.Context) opentracing.Span

SpanFromContext attempts to retrieve a span from the Buffalo context, returning it if found. If none is found a new one is created.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL