https://github.com/financial-times/transactionid-utils-go
For handling/creating transactionIDs and passing them around in a Go app
https://github.com/financial-times/transactionid-utils-go
universal-publishing
Last synced: 5 months ago
JSON representation
For handling/creating transactionIDs and passing them around in a Go app
- Host: GitHub
- URL: https://github.com/financial-times/transactionid-utils-go
- Owner: Financial-Times
- License: mit
- Created: 2016-01-12T21:16:01.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2024-05-13T13:19:26.000Z (about 2 years ago)
- Last Synced: 2024-06-20T11:16:52.788Z (almost 2 years ago)
- Topics: universal-publishing
- Language: Go
- Size: 17.6 KB
- Stars: 2
- Watchers: 25
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
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](https://godoc.org/golang.org/x/net/context)
Best practice for using this is to specify a context as the first argument to your function
(see [here](https://blog.golang.org/context) 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.