Documentation ¶
Overview ¶
`grpc_ctxtags` adds a Tag object to the context that can be used by other middleware to add context about a request.
Request Context Tags ¶
Tags describe information about the request, and can be set and used by other middleware, or handlers. Tags are used for logging and tracing of requests. Tags are populated both upwards, *and* downwards in the interceptor-handler stack.
You can automatically extract tags (in `grpc.request.<field_name>`) from request payloads.
For unary and server-streaming methods, pass in the `WithFieldExtractor` option. For client-streams and bidirectional-streams, you can use `WithFieldExtractorForInitialReq` which will extract the tags from the first message passed from client to server. Note the tags will not be modified for subsequent requests, so this option only makes sense when the initial message establishes the meta-data for the stream.
If a user doesn't use the interceptors that initialize the `Tags` object, all operations following from an `Extract(ctx)` will be no-ops. This is to ensure that code doesn't panic if the interceptors weren't used.
Tags fields are typed, and shallow and should follow the OpenTracing semantics convention: https://github.com/opentracing/specification/blob/master/semantic_conventions.md
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CodeGenRequestFieldExtractor ¶
CodeGenRequestFieldExtractor is a function that relies on code-generated functions that export log fields from requests. These are usually coming from a protoc-plugin that generates additional information based on custom field options.
func StreamServerInterceptor ¶
func StreamServerInterceptor(opts ...Option) grpc.StreamServerInterceptor
StreamServerInterceptor returns a new streaming server interceptor that sets the values for request tags.
func UnaryServerInterceptor ¶
func UnaryServerInterceptor(opts ...Option) grpc.UnaryServerInterceptor
UnaryServerInterceptor returns a new unary server interceptors that sets the values for request tags.
Types ¶
type Option ¶
type Option func(*options)
func WithFieldExtractor ¶
func WithFieldExtractor(f RequestFieldExtractorFunc) Option
WithFieldExtractor customizes the function for extracting log fields from protobuf messages, for unary and server-streamed methods only.
func WithFieldExtractorForInitialReq ¶
func WithFieldExtractorForInitialReq(f RequestFieldExtractorFunc) Option
WithFieldExtractorForInitialReq customizes the function for extracting log fields from protobuf messages, for all unary and streaming methods. For client-streams and bidirectional-streams, the tags will be extracted from the first message from the client.
type RequestFieldExtractorFunc ¶
RequestFieldExtractorFunc is a user-provided function that extracts field information from a gRPC request. It is called from tags middleware on arrival of unary request or a server-stream request. Keys and values will be added to the context tags of the request. If there are no fields, you should return a nil.
func TagBasedRequestFieldExtractor ¶
func TagBasedRequestFieldExtractor(tagName string) RequestFieldExtractorFunc
TagBasedRequestFieldExtractor is a function that relies on Go struct tags to export log fields from requests. These are usualy coming from a protoc-plugin, such as Gogo protobuf.
message Metadata { repeated string tags = 1 [ (gogoproto.moretags) = "log_field:\"meta_tags\"" ]; }
The tagName is configurable using the tagName variable. Here it would be "log_field".
type Tags ¶
type Tags struct {
// contains filtered or unexported fields
}
Tags is the struct used for storing request tags between Context calls. This object is *not* thread safe, and should be handled only in the context of the request.
func Extract ¶
Extracts returns a pre-existing Tags object in the Context. If the context wasn't set in a tag interceptor, a no-op Tag storage is returned that will *not* be propagated in context.
Directories ¶
Path | Synopsis |
---|---|
`ctx_logrus` is a ctxlogger that is backed by logrus It accepts a user-configured `logrus.Logger` that will be used for logging.
|
`ctx_logrus` is a ctxlogger that is backed by logrus It accepts a user-configured `logrus.Logger` that will be used for logging. |
`ctx_zap` is a ctxlogger that is backed by Zap It accepts a user-configured `zap.Logger` that will be used for logging.
|
`ctx_zap` is a ctxlogger that is backed by Zap It accepts a user-configured `zap.Logger` that will be used for logging. |