Categorygithub.com/Financial-Times/transactionid-utils-go

# README

Transaction ID library

This library supports transaction id handling. It provides methods for checking an http request for an 'X-Request-Id' header, and if present using the value of this as a transaction id.

If the header isn't present, the library will generate a transaction with a 'tid_' prefix and a random 10 character string.

Go provides support for passing around variables associated with a request lifecycle via the context package

Best practice for using this is to specify a context as the first argument to your function (see here for more information along with examples that this library draws on).

Examples

To extract a transactionID from a request, and create one if none found:

transactionID := transactionidutils.GetTransactionIDFromRequest(req)

To store that on a context:

transactionAwareContext := transactionidutils.TransactionAwareContext(context.Background(), transactionID)

NB: context.Background() is a non-nil, empty Context, typically used as the top-level context for incoming requests.

For extracting from a context:

transactionID, err := GetTransactionIDFromContext(ctx)

It's expected that the transaction ID will be used to output logs. An example using logrus:

log := log.WithFields(log.Fields{
	transactionIdKey: ctx.Value(transactionIdKey),
})

NB: this will use the standard transaction id key("transaction_id") that is already used for Content programme log files.

# Functions

GetTransactionIDFromContext will look for a transactionID value on the context and return it if found.
GetTransactionIDFromRequest will look on the request for an 'X-Request-Id' header, and use that value as the returned transactionID.
NewTransactionID generates a new random transaction ID conforming to the FT spec.
TransactionAwareContext will take the context passed in and store the transactionID on it.

# Constants

TransactionIDHeader is the request header to look for.