transactionidutils

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2021 License: MIT Imports: 4 Imported by: 49

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.

Documentation

Index

Constants

View Source
const (
	//TransactionIDHeader is the request header to look for
	TransactionIDHeader = "X-Request-Id"
)

Variables

This section is empty.

Functions

func GetTransactionIDFromContext

func GetTransactionIDFromContext(ctx context.Context) (string, error)

GetTransactionIDFromContext will look for a transactionID value on the context and return it if found. If none is found, return empty string and an error

func GetTransactionIDFromRequest

func GetTransactionIDFromRequest(req *http.Request) string

GetTransactionIDFromRequest will look on the request for an 'X-Request-Id' header, and use that value as the returned transactionID. If none is found, one will be autogenerated, with a 'tid_' prefix and a random ten character string and it will be set as request header.

func NewTransactionID added in v0.2.0

func NewTransactionID() string

NewTransactionID generates a new random transaction ID conforming to the FT spec

func TransactionAwareContext

func TransactionAwareContext(ctx context.Context, transactionID string) context.Context

TransactionAwareContext will take the context passed in and store the transactionID on it

Types

This section is empty.

Jump to

Keyboard shortcuts

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