blob: 952775f88d405e01adf04e648caf971496a24342 [file] [log] [blame]
Girish Kumar2ed051b2020-07-28 16:35:25 +00001// Copyright 2017 Michal Witkowski. All Rights Reserved.
2// See LICENSE for licensing terms.
3
4package grpc_ctxtags
5
6var (
7 defaultOptions = &options{
8 requestFieldsFunc: nil,
9 }
10)
11
12type options struct {
13 requestFieldsFunc RequestFieldExtractorFunc
14 requestFieldsFromInitial bool
15}
16
17func evaluateOptions(opts []Option) *options {
18 optCopy := &options{}
19 *optCopy = *defaultOptions
20 for _, o := range opts {
21 o(optCopy)
22 }
23 return optCopy
24}
25
26type Option func(*options)
27
28// WithFieldExtractor customizes the function for extracting log fields from protobuf messages, for
29// unary and server-streamed methods only.
30func WithFieldExtractor(f RequestFieldExtractorFunc) Option {
31 return func(o *options) {
32 o.requestFieldsFunc = f
33 }
34}
35
36// WithFieldExtractorForInitialReq customizes the function for extracting log fields from protobuf messages,
37// for all unary and streaming methods. For client-streams and bidirectional-streams, the tags will be
38// extracted from the first message from the client.
39func WithFieldExtractorForInitialReq(f RequestFieldExtractorFunc) Option {
40 return func(o *options) {
41 o.requestFieldsFunc = f
42 o.requestFieldsFromInitial = true
43 }
44}