Skip to content
/ tracr Public

simple lib for adding a correlation id / trace id to the context

License

Notifications You must be signed in to change notification settings

micklove/tracr

Repository files navigation

tracr

simple lib for retrieving (or creating) and adding a correlation id / trace id to the context


TODO - Build / Release Pipe

tl;dr ?

Have a look at the chi, gin or stdlib examples.
(coming soon, Gorilla Mux and Echo)

correlation-id

tracr is a Go lib that allows clients to add / retrieve a correlation id header (with header name of their choice) to / from the current go context.

The library provides the following:


1. Middleware for incoming requests

The middleware checks for a correlation ID header in the current request. If none is found, it generates a new correlation ID and adds it to the request context.


Correlation ID generation

nb: If no correlation ID is found in the context, a new correlation id will be generated and added to the context:

  1. Users can provide their own correlation id generator function, when instantiating the middleware. OR
  2. The library, https://github.com/gofrs/uuid, will be used by default to generate the correlation id (in the form of a uuid)

Preferred Header Name

During middleware instantiation, users can provide a func that will return the preferred name for the correlation id http header. e.g. x-correlation-id, x-request-id, trace-id, trace_id, etc...

If no names are provided, the default header will be used: (nb: header names are case insensitive)

x-correlation-id

2. Adding correlation id to outgoing requests

See request.go.

A method is provided to allow the addition of a correlation id (and correlation id http header) to outgoing requests



3. Add middleware that logs all http request

See ./middleware/http_logger.go. See example usage in cmd/examples/stdlib/stdlib.go

Installation

Installation with go get.

go get -u github.com/micklove/tracr


Examples

See the examples folder, for chi, gin and stdlib


TODO - Diagram

add mermaid diagram(s)


References

Pages reviewed / consulted / borrowed from, when coming up with this lib

Issues to consider

If an error occurs in the middleware func, the current implementation will just write an http 500 to to the response, if there is an issue retrieving the correlation id - TODO - add a method to override this behaviour

TODO - contributing

About

simple lib for adding a correlation id / trace id to the context

Resources

License

Stars

Watchers

Forks

Packages

No packages published