Documentation
¶
Overview ¶
The trace package provides a Trace struct for storing and retrieving trace data.
It also provides useful functions for setting and retrieving the trace to and from the context.
There's also helpers for setting and retrieving the trace from http requests.
Index ¶
Constants ¶
const CTX_KEY = "trace"
key used to store and retrieve the trace from a context.
const (
ERR_NAME = "trace_error"
)
const HTTP_HEADER_PREFIX = "X-Trace-"
HTTP_HEADER_PREFIX is the prefix that trace headers must have.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Trace ¶
Trace is a map used to store trace data within an execution context.
i.e. An http request or an async event.
Built-in properties:
A unique identifier for the trace.
func GetFromContext ¶
GetFromContext returns the trace from a context using the key trace.CTX_KEY.
If the trace value is nil or is not of type trace.Trace, it will return a new empty trace. Returned trace will always be non-nil.
func GetFromHTTPRequest ¶
GetFromHTTPRequest builds the trace struct from an http.Request req. It does by finding headers with the trace.HTTP_HEADER_PREFIX then removeing the prefix and converting the key to snake case.
It will return an error if req is nil or if req.Header is nil. trace will always be non-nil.
Errors are of type github.com/iolave/go-errors.GenericError and have the name trace.ERR_NAME. Errors can be casted to this type.
_, err := trace.GetFromHTTPRequest(nil)
if err != nil {
e := err.(*errors.GenericError)
}
func (Trace) MarshalJSON ¶
MarshalJSON returns the JSON encoding of the trace.
If the trace is nil, it will return an empty JSON object.
func (Trace) Set ¶
Set sets the value v fot the given key k. Keys must be in snake case, otherwise it will return an error.
Errors are of type github.com/iolave/go-errors.GenericError and have the name trace.ERR_NAME. Errors can be casted to this type.
err := trace.Set("mykey", "this-will-error")
if err != nil {
e := err.(*errors.GenericError)
}
func (Trace) SetHTTPHeaders ¶
SetHTTPHeaders sets trace data into the given http.Header h. It does by converting the trace keys underscores to dashes and appending at the beginning of each header key the trace.HTTP_HEADER_PREFIX value.
If h is nil, it will do nothing.
func (Trace) SetInContext ¶
SetInContext sets the trace in a context using the key trace.CTX_KEY and returns the a new context.