Status: Production
This project contains middleware for HTTP services and clients that uses openzipkin-go and logevent to both propagate traces between HTTP services and emit traces to the service logs.
The middleware exported is a func(http.Handler) http.Handler
and should
work with virtually any router/mux implementation that supports middleware.
var middleware = httptrace.NewMiddleware(
httptrace.MiddlewareOptionServiceName("my-service"),
)
The middleware uses
opentracing-go to manage
spans and should interoperate with other uses of opentracing that leverage the
context
as a source of the tracer. If no trace is found in the incoming
request then the middleware will generate a new trace and root span using the
given service name. If the incoming request does contain a trace, via zipkin
headers, then the middleware generate a span within that trace that is a child
of the incoming span.
If you need the identifier of the active trace at any point within a request,
you can use the TraceIDFromContext
or SpanIDFromContext
helpers which will
return the ID in a hex encoded string which is what typically ships over via
headers to other services.
In addition to an HTTP middleware, there is also an http.RoundTripper
wrapper
included that will properly manage spans for outgoing HTTP requests. To apply:
var client = &http.Client{
Transport: httptrace.NewTransport(
httptrace.TransportOptionSpanName("outgoing_http_request"),
httptrace.TransportOptionPeerName("remote-service-name"),
)(http.DefaultTransport),
}
As each span is marked as complete the middlewares will use the
logevent.Logger
contained within the request context to emit a line like:
{"message": "span-complete", "zipkin": {"traceId": "", "id": "", "parentId": "", "name": "", "timestamp": "", "duration": "", "annotations": [{"timestamp": "", "value": ""}], "binaryAnnotations": [{"key": "", "value": ""}]}}
This project is licensed under Apache 2.0. See LICENSE.txt for details.
Atlassian requires signing a contributor's agreement before we can accept a patch. If you are an individual you can fill out the individual CLA. If you are contributing on behalf of your company then please fill out the corporate CLA.