Codebase list golang-github-go-kit-kit / dff50435-7a33-4f0c-bbdc-f455eb10d80a/v0.10.0
Add a span name getter to opencensus endpoint options (#948) * Add a span name getter to opencensus endpoint options * Add GetSpanDetails option * Split GetSpanDetails function * Simplify getting the name * Improve documentation * Simplify attributes * Update documentation * Update tracing/opencensus/endpoint_options.go Co-authored-by: Peter Bourgon <peterbourgon@users.noreply.github.com> Márk Sági-Kazár authored 4 years ago GitHub committed 4 years ago
3 changed file(s) with 69 addition(s) and 3 deletion(s). Raw diff Collapse all Expand all
3030
3131 return func(next endpoint.Endpoint) endpoint.Endpoint {
3232 return func(ctx context.Context, request interface{}) (response interface{}, err error) {
33 if cfg.GetName != nil {
34 if newName := cfg.GetName(ctx, name); newName != "" {
35 name = newName
36 }
37 }
38
3339 ctx, span := trace.StartSpan(ctx, name)
3440 if len(cfg.Attributes) > 0 {
3541 span.AddAttributes(cfg.Attributes...)
3642 }
3743 defer span.End()
44
45 if cfg.GetAttributes != nil {
46 if attrs := cfg.GetAttributes(ctx); len(attrs) > 0 {
47 span.AddAttributes(attrs...)
48 }
49 }
3850
3951 defer func() {
4052 if err != nil {
00 package opencensus
11
2 import "go.opencensus.io/trace"
2 import (
3 "context"
4
5 "go.opencensus.io/trace"
6 )
37
48 // EndpointOptions holds the options for tracing an endpoint
59 type EndpointOptions struct {
1014 // Attributes holds the default attributes which will be set on span
1115 // creation by our Endpoint middleware.
1216 Attributes []trace.Attribute
17
18 // GetName is an optional function that can set the span name based on the existing name
19 // for the endpoint and information in the context.
20 //
21 // If the function is nil, or the returned name is empty, the existing name for the endpoint is used.
22 GetName func(ctx context.Context, name string) string
23
24 // GetAttributes is an optional function that can extract trace attributes
25 // from the context and add them to the span.
26 GetAttributes func(ctx context.Context) []trace.Attribute
1327 }
1428
1529 // EndpointOption allows for functional options to our OpenCensus endpoint
3953 o.IgnoreBusinessError = val
4054 }
4155 }
56
57 // WithSpanName extracts additional attributes from the request context.
58 func WithSpanName(fn func(ctx context.Context, name string) string) EndpointOption {
59 return func(o *EndpointOptions) {
60 o.GetName = fn
61 }
62 }
63
64 // WithSpanAttributes extracts additional attributes from the request context.
65 func WithSpanAttributes(fn func(ctx context.Context) []trace.Attribute) EndpointOption {
66 return func(o *EndpointOptions) {
67 o.GetAttributes = fn
68 }
69 }
1919 span3 = "SPAN-3"
2020 span4 = "SPAN-4"
2121 span5 = "SPAN-5"
22 span6 = "SPAN-6"
2223 )
2324
2425 var (
7576 mw = opencensus.TraceEndpoint(span4)
7677 mw(passEndpoint)(ctx, failedResponse{err: err3})
7778
78 // span4
79 // span5
7980 mw = opencensus.TraceEndpoint(span5, opencensus.WithIgnoreBusinessError(true))
8081 mw(passEndpoint)(ctx, failedResponse{err: err4})
8182
83 // span6
84 span6Attrs := []trace.Attribute{
85 trace.StringAttribute("string", "value"),
86 trace.Int64Attribute("int64", 42),
87 }
88 mw = opencensus.TraceEndpoint(
89 "",
90 opencensus.WithSpanName(func(ctx context.Context, name string) string {
91 return span6
92 }),
93 opencensus.WithSpanAttributes(func(ctx context.Context) []trace.Attribute {
94 return span6Attrs
95 }),
96 )
97 mw(endpoint.Nop)(ctx, nil)
98
8299 // check span count
83100 spans := e.Flush()
84 if want, have := 5, len(spans); want != have {
101 if want, have := 6, len(spans); want != have {
85102 t.Fatalf("incorrected number of spans, wanted %d, got %d", want, have)
86103 }
87104
155172 t.Fatalf("incorrect attribute count, wanted %d, got %d", want, have)
156173 }
157174
175 // test span 6
176 span = spans[5]
177 if want, have := span6, span.Name; want != have {
178 t.Errorf("incorrect span name, wanted %q, got %q", want, have)
179 }
180
181 if want, have := 2, len(span.Attributes); want != have {
182 t.Fatalf("incorrect attribute count, wanted %d, got %d", want, have)
183 }
158184 }