tracing

package
v0.0.0-...-81c75b8 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2024 License: MIT Imports: 13 Imported by: 0

README

Opentracing

This package supports distributed tracing of requests by linking together all of the parts of work that go into fulfilling a request.

For example, with a HTML front-end talking to back-end HTTPS APIs, it will link the original front-end request with any/all HTTP requests made to the back-end. Also, it can link together deeper requests made by the back-end to other APIs and services.

Currently this library requires the jaeger-lib dependency to be pinned at v1.5.0.

Middleware

To configure tracing of incoming HTTP requests:

  1. Set JAEGER_* environment variables.

  2. Initialise tracer:

    closer, tracer, err := tracing.Tracer(log)
    if err != nil {
    	log.WithError(err).Log()
    } else {
    	defer closer.Close()
    }
    

    The returned io.Closer is used to ensure that traces are sent to the server before the process exits.

    Initialising the tracer installs it as the Opentracing global tracer.

  3. Use the returned tracer middleware with your HTTP handlers:

    http.ListenAndServe(tracer(handler))
    

    If you're using server.Compose, add the tracer to the bottom of your stack, just above server.DefaultMiddleware. This will make the tracing system available to other middleware such as errornotifier (see below).

Trace Propagation

The middleware will automatically continue spans if the incoming HTTP request has span propagation headers.

To pass tracing headers downstream when making HTTP requests use Inject:

// Transmit the span's TraceContext as HTTP headers on our
// outbound request.
opentracing.GlobalTracer().Inject(
	span.Context(),
	opentracing.HTTPHeaders,
	opentracing.HTTPHeadersCarrier(httpReq.Header))

Adding Detail to Traces

this package exports a Span function that is used to add more detail to a trace. When you perform some kind of sub-operation, such as an SQL query, or API call to another service, wrap the call in a call to Span:

tracing.Span(ctx, "<operation name>", func(ctx context.Context, span opentracing.Span) error {
     httpReq := ...

    // Add info to the span
    ext.HTTPMethod.Set(span, httpReq.Method)
    ext.HTTPUrl.Set(span, httpReq.URL.String())
	ext.SpanKind.Set(span, ext.SpanKindRPCClientEnum)

    // Propagate trace id to back-end service
    opentracing.GlobalTracer().Inject(
	    span.Context(),
	    opentracing.HTTPHeaders,
	    opentracing.HTTPHeadersCarrier(httpReq.Header))


    // perform your operation, and update span with any details
    resp, err := doOperation(httpReq)

    if err != nil {
        // `tracing.Span` will automatically mark the span as an
        // error and log the error message, if we return an error.
        return err
    }
    
    if opFailed(resp) {
        // manually mark the span as an error if there was some kind
        // of logical problem
        ext.Error.Set(span, true)
		span.LogKV("error", "the operation failed!!")
    }
    return nil
})

Background

This package uses OpenTracing and is configured to use a Jaeger server by default. The OpenTracing site has a good overview of the concepts.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Span

Span will trace execution of function `f` as a (sub-)span of any span on ctx.

* If `f` returns an error, `Span` will return the same error.

  • If `f` panics, `Span` will also panic with the error wrapped in a `loggedError`, to avoid logging the error multiple times when calls to `Span` are nested.

In either case, the span will be marked with an error, and the error's message will be added to the span log.

func Tracer

func Tracer(logger log.Logger) (io.Closer, server.Middleware, error)

Tracer is used to create tracing middleware that uses Jaeger (https://www.jaegertracing.io) for implementation. The tracer is configured via environment variables: https://github.com/jaegertracing/jaeger-client-go#environment-variables

The purpose of return the `io.Closer` is to ensure that any pending traces have been sent to the tracing system before the program exits, so `defer closer.Close()` should be called at the top level of your program.

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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