Correlations Id is used in distributed applications to trace requests across multiple services. This package provides a lightweight correlation id middlware. Request headers are checked for a correlation id. If found or generated, this correlation id is attached to the request context which can be used to access the current correlation id where it is required for logging etc. Based on middleware settings correlation id can be returned with response headers.
go get -u github.com/dmytrohridin/correlation-id
To get correlation id value use FromContext
function.
id := correlationid.FromContext(req.Context())
By default Correlation-Id
key used as header name. Behavior can be overridden.
middleware := correlationid.New()
middleware.HeaderName = "Request-Id"
By default google/uuid package is used for correlation id when Correlation-Id
header is not included in request headers. Behavior can be overridden by applying a custom function.
middleware := correlationid.New()
middleware.IdGenerator = func() string {
return "any_id"
}
By default Correlation-Id
is sent in response header. Behavior can be overridden.
middleware := correlationid.New()
middleware.IncludeInResponse = false
By default Correlation-Id
is not required in request headers. Behavior can be overridden.
middleware := correlationid.New()
middleware.EnforceHeader = true
If header is enforced and client does not send it - BadRequest
will be returned.