Categorygithub.com/asecurityteam/httptrace
modulepackage
2.1.0+incompatible
Repository: https://github.com/asecurityteam/httptrace.git
Documentation: pkg.go.dev

# README

httptrace -Zipkin tracing integration for HTTP services.

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.

Usage

HTTP Service

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.

HTTP Client

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),
}

Span Logs

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": ""}]}}

Contributing

License

This project is licensed under Apache 2.0. See LICENSE.txt for details.

Contributing Agreement

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.

# Functions

MiddlewareOptionHostPort sets host:port annotation used to represent the service in spans associated with the incoming request.
MiddlewareOptionServiceName sets the service name annotation of the spans associated with the incoming request.
NewMiddleware creates a middleware.
NewTracer generates an opentracing.Tracer implementation that uses the given Logger and metadata when generating and emitting spans.
NewTransport creats an http.RoundTripper wrapper that injects zipkin headers into all outgoing requests.
SpanIDFromContext returns the active TraceID value as a string.
TraceIDFromContext returns the active TraceID value as a string.
TransportOptionPeerName sets the name of the remote peer being called.
TransportOptionPeerNamer is similar to TransportOptionPeerName but allows for mapping an outgoing request object to a particular peer name.
TransportOptionSpanName sets the name of the zipkin span for the outgoing request.

# Structs

Middleware adds zipkin style request tracing.
Transport adds zipkin style request tracing headers to outgoing requests.

# Type aliases

MiddlewareOption is a configuration setting for the HTTP middleware.
TransportOption is a configuration setting for the Transport wrapper.