Exported the OpenCensus TracerOptions properties so they can actually be configured WithTracerConfig
Bas van Beek
5 years ago
4 | 4 | // EndpointOptions holds the options for tracing an endpoint |
5 | 5 | type EndpointOptions struct { |
6 | 6 | // IgnoreBusinessError if set to true will not treat a business error |
7 | // identified trough the endpoint.Failer interface as a span error. | |
7 | // identified through the endpoint.Failer interface as a span error. | |
8 | 8 | IgnoreBusinessError bool |
9 | 9 | |
10 | 10 | // Attributes holds the default attributes which will be set on span |
15 | 15 | |
16 | 16 | // GRPCClientTrace enables OpenCensus tracing of a Go kit gRPC transport client. |
17 | 17 | func GRPCClientTrace(options ...TracerOption) kitgrpc.ClientOption { |
18 | cfg := TracerOptions{ | |
19 | sampler: trace.AlwaysSample(), | |
20 | } | |
18 | cfg := TracerOptions{} | |
21 | 19 | |
22 | 20 | for _, option := range options { |
23 | 21 | option(&cfg) |
22 | } | |
23 | ||
24 | if cfg.Sampler == nil { | |
25 | cfg.Sampler = trace.AlwaysSample() | |
24 | 26 | } |
25 | 27 | |
26 | 28 | clientBefore := kitgrpc.ClientBefore( |
27 | 29 | func(ctx context.Context, md *metadata.MD) context.Context { |
28 | 30 | var name string |
29 | 31 | |
30 | if cfg.name != "" { | |
31 | name = cfg.name | |
32 | if cfg.Name != "" { | |
33 | name = cfg.Name | |
32 | 34 | } else { |
33 | 35 | name = ctx.Value(kitgrpc.ContextKeyRequestMethod).(string) |
34 | 36 | } |
37 | 39 | name, |
38 | 40 | trace.FromContext(ctx), |
39 | 41 | trace.StartOptions{ |
40 | Sampler: cfg.sampler, | |
42 | Sampler: cfg.Sampler, | |
41 | 43 | SpanKind: trace.SpanKindClient, |
42 | 44 | }, |
43 | 45 | ) |
44 | 46 | |
45 | if !cfg.public { | |
47 | if !cfg.Public { | |
46 | 48 | traceContextBinary := string(propagation.Binary(span.SpanContext())) |
47 | 49 | (*md)[propagationKey] = append((*md)[propagationKey], traceContextBinary) |
48 | 50 | } |
72 | 74 | |
73 | 75 | // GRPCServerTrace enables OpenCensus tracing of a Go kit gRPC transport server. |
74 | 76 | func GRPCServerTrace(options ...TracerOption) kitgrpc.ServerOption { |
75 | cfg := TracerOptions{ | |
76 | sampler: trace.AlwaysSample(), | |
77 | } | |
77 | cfg := TracerOptions{} | |
78 | 78 | |
79 | 79 | for _, option := range options { |
80 | 80 | option(&cfg) |
81 | } | |
82 | ||
83 | if cfg.Sampler == nil { | |
84 | cfg.Sampler = trace.AlwaysSample() | |
81 | 85 | } |
82 | 86 | |
83 | 87 | serverBefore := kitgrpc.ServerBefore( |
84 | 88 | func(ctx context.Context, md metadata.MD) context.Context { |
85 | 89 | var name string |
86 | 90 | |
87 | if cfg.name != "" { | |
88 | name = cfg.name | |
91 | if cfg.Name != "" { | |
92 | name = cfg.Name | |
89 | 93 | } else { |
90 | 94 | name, _ = ctx.Value(kitgrpc.ContextKeyRequestMethod).(string) |
91 | 95 | if name == "" { |
104 | 108 | if len(traceContext) > 0 { |
105 | 109 | traceContextBinary := []byte(traceContext[0]) |
106 | 110 | parentContext, ok = propagation.FromBinary(traceContextBinary) |
107 | if ok && !cfg.public { | |
111 | if ok && !cfg.Public { | |
108 | 112 | ctx, _ = trace.StartSpanWithRemoteParent( |
109 | 113 | ctx, |
110 | 114 | name, |
111 | 115 | parentContext, |
112 | 116 | trace.WithSpanKind(trace.SpanKindServer), |
113 | trace.WithSampler(cfg.sampler), | |
117 | trace.WithSampler(cfg.Sampler), | |
114 | 118 | ) |
115 | 119 | return ctx |
116 | 120 | } |
119 | 123 | ctx, |
120 | 124 | name, |
121 | 125 | trace.WithSpanKind(trace.SpanKindServer), |
122 | trace.WithSampler(cfg.sampler), | |
126 | trace.WithSampler(cfg.Sampler), | |
123 | 127 | ) |
124 | 128 | if ok { |
125 | 129 | span.AddLink( |
12 | 12 | |
13 | 13 | // HTTPClientTrace enables OpenCensus tracing of a Go kit HTTP transport client. |
14 | 14 | func HTTPClientTrace(options ...TracerOption) kithttp.ClientOption { |
15 | cfg := TracerOptions{ | |
16 | sampler: trace.AlwaysSample(), | |
17 | httpPropagate: &b3.HTTPFormat{}, | |
18 | } | |
15 | cfg := TracerOptions{} | |
19 | 16 | |
20 | 17 | for _, option := range options { |
21 | 18 | option(&cfg) |
19 | } | |
20 | ||
21 | if cfg.Sampler == nil { | |
22 | cfg.Sampler = trace.AlwaysSample() | |
23 | } | |
24 | ||
25 | if !cfg.Public && cfg.HTTPPropagate == nil { | |
26 | cfg.HTTPPropagate = &b3.HTTPFormat{} | |
22 | 27 | } |
23 | 28 | |
24 | 29 | clientBefore := kithttp.ClientBefore( |
25 | 30 | func(ctx context.Context, req *http.Request) context.Context { |
26 | 31 | var name string |
27 | 32 | |
28 | if cfg.name != "" { | |
29 | name = cfg.name | |
33 | if cfg.Name != "" { | |
34 | name = cfg.Name | |
30 | 35 | } else { |
31 | 36 | // OpenCensus states Path being default naming for a client span |
32 | 37 | name = req.Method + " " + req.URL.Path |
36 | 41 | name, |
37 | 42 | trace.FromContext(ctx), |
38 | 43 | trace.StartOptions{ |
39 | Sampler: cfg.sampler, | |
44 | Sampler: cfg.Sampler, | |
40 | 45 | SpanKind: trace.SpanKindClient, |
41 | 46 | }, |
42 | 47 | ) |
48 | 53 | trace.StringAttribute(ochttp.UserAgentAttribute, req.UserAgent()), |
49 | 54 | ) |
50 | 55 | |
51 | if !cfg.public { | |
52 | cfg.httpPropagate.SpanContextToRequest(span.SpanContext(), req) | |
56 | if !cfg.Public { | |
57 | cfg.HTTPPropagate.SpanContextToRequest(span.SpanContext(), req) | |
53 | 58 | } |
54 | 59 | |
55 | 60 | return trace.NewContext(ctx, span) |
91 | 96 | |
92 | 97 | // HTTPServerTrace enables OpenCensus tracing of a Go kit HTTP transport server. |
93 | 98 | func HTTPServerTrace(options ...TracerOption) kithttp.ServerOption { |
94 | cfg := TracerOptions{ | |
95 | sampler: trace.AlwaysSample(), | |
96 | httpPropagate: &b3.HTTPFormat{}, | |
97 | } | |
99 | cfg := TracerOptions{} | |
98 | 100 | |
99 | 101 | for _, option := range options { |
100 | 102 | option(&cfg) |
103 | } | |
104 | ||
105 | if cfg.Sampler == nil { | |
106 | cfg.Sampler = trace.AlwaysSample() | |
107 | } | |
108 | ||
109 | if !cfg.Public && cfg.HTTPPropagate == nil { | |
110 | cfg.HTTPPropagate = &b3.HTTPFormat{} | |
101 | 111 | } |
102 | 112 | |
103 | 113 | serverBefore := kithttp.ServerBefore( |
109 | 119 | ok bool |
110 | 120 | ) |
111 | 121 | |
112 | if cfg.name != "" { | |
113 | name = cfg.name | |
122 | if cfg.Name != "" { | |
123 | name = cfg.Name | |
114 | 124 | } else { |
115 | 125 | name = req.Method + " " + req.URL.Path |
116 | 126 | } |
117 | 127 | |
118 | spanContext, ok = cfg.httpPropagate.SpanContextFromRequest(req) | |
119 | if ok && !cfg.public { | |
128 | spanContext, ok = cfg.HTTPPropagate.SpanContextFromRequest(req) | |
129 | if ok && !cfg.Public { | |
120 | 130 | ctx, span = trace.StartSpanWithRemoteParent( |
121 | 131 | ctx, |
122 | 132 | name, |
123 | 133 | spanContext, |
124 | 134 | trace.WithSpanKind(trace.SpanKindServer), |
125 | trace.WithSampler(cfg.sampler), | |
135 | trace.WithSampler(cfg.Sampler), | |
126 | 136 | ) |
127 | 137 | } else { |
128 | 138 | ctx, span = trace.StartSpan( |
129 | 139 | ctx, |
130 | 140 | name, |
131 | 141 | trace.WithSpanKind(trace.SpanKindServer), |
132 | trace.WithSampler(cfg.sampler), | |
142 | trace.WithSampler(cfg.Sampler), | |
133 | 143 | ) |
134 | 144 | if ok { |
135 | 145 | span.AddLink(trace.Link{ |
23 | 23 | // WithSampler sets the sampler to use by our OpenCensus Tracer. |
24 | 24 | func WithSampler(sampler trace.Sampler) TracerOption { |
25 | 25 | return func(o *TracerOptions) { |
26 | o.sampler = sampler | |
26 | o.Sampler = sampler | |
27 | 27 | } |
28 | 28 | } |
29 | 29 | |
32 | 32 | // name is used. |
33 | 33 | func WithName(name string) TracerOption { |
34 | 34 | return func(o *TracerOptions) { |
35 | o.name = name | |
35 | o.Name = name | |
36 | 36 | } |
37 | 37 | } |
38 | 38 | |
43 | 43 | // is found, it will be added as a linked trace instead. |
44 | 44 | func IsPublic(isPublic bool) TracerOption { |
45 | 45 | return func(o *TracerOptions) { |
46 | o.public = isPublic | |
46 | o.Public = isPublic | |
47 | 47 | } |
48 | 48 | } |
49 | 49 | |
53 | 53 | return func(o *TracerOptions) { |
54 | 54 | if p == nil { |
55 | 55 | // reset to default OC HTTP format |
56 | o.httpPropagate = defaultHTTPPropagate | |
56 | o.HTTPPropagate = defaultHTTPPropagate | |
57 | 57 | return |
58 | 58 | } |
59 | o.httpPropagate = p | |
59 | o.HTTPPropagate = p | |
60 | 60 | } |
61 | 61 | } |
62 | 62 | |
63 | 63 | // TracerOptions holds configuration for our tracing middlewares |
64 | 64 | type TracerOptions struct { |
65 | sampler trace.Sampler | |
66 | name string | |
67 | public bool | |
68 | httpPropagate propagation.HTTPFormat | |
65 | Sampler trace.Sampler | |
66 | Name string | |
67 | Public bool | |
68 | HTTPPropagate propagation.HTTPFormat | |
69 | 69 | } |