Codebase list golang-github-go-kit-kit / 4ce0cba
Exported the OpenCensus TracerOptions properties so they can actually be configured WithTracerConfig Bas van Beek 5 years ago
4 changed file(s) with 58 addition(s) and 44 deletion(s). Raw diff Collapse all Expand all
44 // EndpointOptions holds the options for tracing an endpoint
55 type EndpointOptions struct {
66 // 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.
88 IgnoreBusinessError bool
99
1010 // Attributes holds the default attributes which will be set on span
1515
1616 // GRPCClientTrace enables OpenCensus tracing of a Go kit gRPC transport client.
1717 func GRPCClientTrace(options ...TracerOption) kitgrpc.ClientOption {
18 cfg := TracerOptions{
19 sampler: trace.AlwaysSample(),
20 }
18 cfg := TracerOptions{}
2119
2220 for _, option := range options {
2321 option(&cfg)
22 }
23
24 if cfg.Sampler == nil {
25 cfg.Sampler = trace.AlwaysSample()
2426 }
2527
2628 clientBefore := kitgrpc.ClientBefore(
2729 func(ctx context.Context, md *metadata.MD) context.Context {
2830 var name string
2931
30 if cfg.name != "" {
31 name = cfg.name
32 if cfg.Name != "" {
33 name = cfg.Name
3234 } else {
3335 name = ctx.Value(kitgrpc.ContextKeyRequestMethod).(string)
3436 }
3739 name,
3840 trace.FromContext(ctx),
3941 trace.StartOptions{
40 Sampler: cfg.sampler,
42 Sampler: cfg.Sampler,
4143 SpanKind: trace.SpanKindClient,
4244 },
4345 )
4446
45 if !cfg.public {
47 if !cfg.Public {
4648 traceContextBinary := string(propagation.Binary(span.SpanContext()))
4749 (*md)[propagationKey] = append((*md)[propagationKey], traceContextBinary)
4850 }
7274
7375 // GRPCServerTrace enables OpenCensus tracing of a Go kit gRPC transport server.
7476 func GRPCServerTrace(options ...TracerOption) kitgrpc.ServerOption {
75 cfg := TracerOptions{
76 sampler: trace.AlwaysSample(),
77 }
77 cfg := TracerOptions{}
7878
7979 for _, option := range options {
8080 option(&cfg)
81 }
82
83 if cfg.Sampler == nil {
84 cfg.Sampler = trace.AlwaysSample()
8185 }
8286
8387 serverBefore := kitgrpc.ServerBefore(
8488 func(ctx context.Context, md metadata.MD) context.Context {
8589 var name string
8690
87 if cfg.name != "" {
88 name = cfg.name
91 if cfg.Name != "" {
92 name = cfg.Name
8993 } else {
9094 name, _ = ctx.Value(kitgrpc.ContextKeyRequestMethod).(string)
9195 if name == "" {
104108 if len(traceContext) > 0 {
105109 traceContextBinary := []byte(traceContext[0])
106110 parentContext, ok = propagation.FromBinary(traceContextBinary)
107 if ok && !cfg.public {
111 if ok && !cfg.Public {
108112 ctx, _ = trace.StartSpanWithRemoteParent(
109113 ctx,
110114 name,
111115 parentContext,
112116 trace.WithSpanKind(trace.SpanKindServer),
113 trace.WithSampler(cfg.sampler),
117 trace.WithSampler(cfg.Sampler),
114118 )
115119 return ctx
116120 }
119123 ctx,
120124 name,
121125 trace.WithSpanKind(trace.SpanKindServer),
122 trace.WithSampler(cfg.sampler),
126 trace.WithSampler(cfg.Sampler),
123127 )
124128 if ok {
125129 span.AddLink(
1212
1313 // HTTPClientTrace enables OpenCensus tracing of a Go kit HTTP transport client.
1414 func HTTPClientTrace(options ...TracerOption) kithttp.ClientOption {
15 cfg := TracerOptions{
16 sampler: trace.AlwaysSample(),
17 httpPropagate: &b3.HTTPFormat{},
18 }
15 cfg := TracerOptions{}
1916
2017 for _, option := range options {
2118 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{}
2227 }
2328
2429 clientBefore := kithttp.ClientBefore(
2530 func(ctx context.Context, req *http.Request) context.Context {
2631 var name string
2732
28 if cfg.name != "" {
29 name = cfg.name
33 if cfg.Name != "" {
34 name = cfg.Name
3035 } else {
3136 // OpenCensus states Path being default naming for a client span
3237 name = req.Method + " " + req.URL.Path
3641 name,
3742 trace.FromContext(ctx),
3843 trace.StartOptions{
39 Sampler: cfg.sampler,
44 Sampler: cfg.Sampler,
4045 SpanKind: trace.SpanKindClient,
4146 },
4247 )
4853 trace.StringAttribute(ochttp.UserAgentAttribute, req.UserAgent()),
4954 )
5055
51 if !cfg.public {
52 cfg.httpPropagate.SpanContextToRequest(span.SpanContext(), req)
56 if !cfg.Public {
57 cfg.HTTPPropagate.SpanContextToRequest(span.SpanContext(), req)
5358 }
5459
5560 return trace.NewContext(ctx, span)
9196
9297 // HTTPServerTrace enables OpenCensus tracing of a Go kit HTTP transport server.
9398 func HTTPServerTrace(options ...TracerOption) kithttp.ServerOption {
94 cfg := TracerOptions{
95 sampler: trace.AlwaysSample(),
96 httpPropagate: &b3.HTTPFormat{},
97 }
99 cfg := TracerOptions{}
98100
99101 for _, option := range options {
100102 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{}
101111 }
102112
103113 serverBefore := kithttp.ServerBefore(
109119 ok bool
110120 )
111121
112 if cfg.name != "" {
113 name = cfg.name
122 if cfg.Name != "" {
123 name = cfg.Name
114124 } else {
115125 name = req.Method + " " + req.URL.Path
116126 }
117127
118 spanContext, ok = cfg.httpPropagate.SpanContextFromRequest(req)
119 if ok && !cfg.public {
128 spanContext, ok = cfg.HTTPPropagate.SpanContextFromRequest(req)
129 if ok && !cfg.Public {
120130 ctx, span = trace.StartSpanWithRemoteParent(
121131 ctx,
122132 name,
123133 spanContext,
124134 trace.WithSpanKind(trace.SpanKindServer),
125 trace.WithSampler(cfg.sampler),
135 trace.WithSampler(cfg.Sampler),
126136 )
127137 } else {
128138 ctx, span = trace.StartSpan(
129139 ctx,
130140 name,
131141 trace.WithSpanKind(trace.SpanKindServer),
132 trace.WithSampler(cfg.sampler),
142 trace.WithSampler(cfg.Sampler),
133143 )
134144 if ok {
135145 span.AddLink(trace.Link{
2323 // WithSampler sets the sampler to use by our OpenCensus Tracer.
2424 func WithSampler(sampler trace.Sampler) TracerOption {
2525 return func(o *TracerOptions) {
26 o.sampler = sampler
26 o.Sampler = sampler
2727 }
2828 }
2929
3232 // name is used.
3333 func WithName(name string) TracerOption {
3434 return func(o *TracerOptions) {
35 o.name = name
35 o.Name = name
3636 }
3737 }
3838
4343 // is found, it will be added as a linked trace instead.
4444 func IsPublic(isPublic bool) TracerOption {
4545 return func(o *TracerOptions) {
46 o.public = isPublic
46 o.Public = isPublic
4747 }
4848 }
4949
5353 return func(o *TracerOptions) {
5454 if p == nil {
5555 // reset to default OC HTTP format
56 o.httpPropagate = defaultHTTPPropagate
56 o.HTTPPropagate = defaultHTTPPropagate
5757 return
5858 }
59 o.httpPropagate = p
59 o.HTTPPropagate = p
6060 }
6161 }
6262
6363 // TracerOptions holds configuration for our tracing middlewares
6464 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
6969 }