Skip to content
This repository was archived by the owner on Jul 31, 2023. It is now read-only.

Commit fc3822b

Browse files
authored
Allow replacing trace SDK (#1234)
1 parent 3fb168f commit fc3822b

13 files changed

Lines changed: 245 additions & 80 deletions

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
4848
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
4949
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
5050
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
51+
golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k=
52+
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
5153
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
5254
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
5355
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=

plugin/ochttp/client.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ type Transport struct {
5656
// name equals the URL Path.
5757
FormatSpanName func(*http.Request) string
5858

59-
// NewClientTrace may be set to a function allowing the current *trace.Span
59+
// NewClientTrace may be set to a function allowing the current trace.Span
6060
// to be annotated with HTTP request event information emitted by the
6161
// httptrace package.
62-
NewClientTrace func(*http.Request, *trace.Span) *httptrace.ClientTrace
62+
NewClientTrace func(*http.Request, trace.Span) *httptrace.ClientTrace
6363

6464
// TODO: Implement tag propagation for HTTP.
6565
}

plugin/ochttp/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func (h *Handler) startTrace(w http.ResponseWriter, r *http.Request) (*http.Requ
109109
startOpts = h.GetStartOptions(r)
110110
}
111111

112-
var span *trace.Span
112+
var span trace.Span
113113
sc, ok := h.extractSpanContext(r)
114114
if ok && !h.IsPublicEndpoint {
115115
ctx, span = trace.StartSpanWithRemoteParent(ctx, name, sc,

plugin/ochttp/server_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ func TestIgnoreHealthEndpoints(t *testing.T) {
584584
ts := httptest.NewServer(&Handler{
585585
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
586586
span := trace.FromContext(r.Context())
587-
if span != nil {
587+
if span.IsRecordingEvents() {
588588
spans++
589589
}
590590
fmt.Fprint(w, "ok")

plugin/ochttp/span_annotating_client_trace.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,21 @@ import (
2424
)
2525

2626
type spanAnnotator struct {
27-
sp *trace.Span
27+
sp trace.Span
2828
}
2929

3030
// TODO: Remove NewSpanAnnotator at the next release.
3131

3232
// NewSpanAnnotator returns a httptrace.ClientTrace which annotates
3333
// all emitted httptrace events on the provided Span.
3434
// Deprecated: Use NewSpanAnnotatingClientTrace instead
35-
func NewSpanAnnotator(r *http.Request, s *trace.Span) *httptrace.ClientTrace {
35+
func NewSpanAnnotator(r *http.Request, s trace.Span) *httptrace.ClientTrace {
3636
return NewSpanAnnotatingClientTrace(r, s)
3737
}
3838

3939
// NewSpanAnnotatingClientTrace returns a httptrace.ClientTrace which annotates
4040
// all emitted httptrace events on the provided Span.
41-
func NewSpanAnnotatingClientTrace(_ *http.Request, s *trace.Span) *httptrace.ClientTrace {
41+
func NewSpanAnnotatingClientTrace(_ *http.Request, s trace.Span) *httptrace.ClientTrace {
4242
sa := spanAnnotator{sp: s}
4343

4444
return &httptrace.ClientTrace{

plugin/ochttp/trace.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ type traceTransport struct {
4444
startOptions trace.StartOptions
4545
format propagation.HTTPFormat
4646
formatSpanName func(*http.Request) string
47-
newClientTrace func(*http.Request, *trace.Span) *httptrace.ClientTrace
47+
newClientTrace func(*http.Request, trace.Span) *httptrace.ClientTrace
4848
}
4949

5050
// TODO(jbd): Add message events for request and response size.
@@ -104,7 +104,7 @@ func (t *traceTransport) RoundTrip(req *http.Request) (*http.Response, error) {
104104
// the body of the original response.
105105
type bodyTracker struct {
106106
rc io.ReadCloser
107-
span *trace.Span
107+
span trace.Span
108108
}
109109

110110
var _ io.ReadCloser = (*bodyTracker)(nil)

plugin/ochttp/trace_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func TestTransport_RoundTrip(t *testing.T) {
106106
_, parent := trace.StartSpan(context.Background(), "parent")
107107
tests := []struct {
108108
name string
109-
parent *trace.Span
109+
parent trace.Span
110110
}{
111111
{
112112
name: "no parent",

trace/basetypes.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@ type Attribute struct {
4949
value interface{}
5050
}
5151

52+
// Key returns the attribute's key
53+
func (a *Attribute) Key() string {
54+
return a.key
55+
}
56+
57+
// Value returns the attribute's value
58+
func (a *Attribute) Value() interface{} {
59+
return a.value
60+
}
61+
5262
// BoolAttribute returns a bool-valued attribute.
5363
func BoolAttribute(key string, value bool) Attribute {
5464
return Attribute{key: key, value: value}

trace/examples_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
"go.opencensus.io/trace"
2222
)
2323

24-
// This example shows how to use StartSpan and (*Span).End to capture
24+
// This example shows how to use StartSpan and (Span).End to capture
2525
// a function execution in a Span. It assumes that the function
2626
// has a context.Context argument.
2727
func ExampleStartSpan() {

trace/spanstore.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,10 @@ func (i internalOnly) ReportActiveSpans(name string) []*SpanData {
4848
var out []*SpanData
4949
s.mu.Lock()
5050
defer s.mu.Unlock()
51-
for span := range s.active {
52-
out = append(out, span.makeSpanData())
51+
for activeSpan := range s.active {
52+
if s, ok := activeSpan.(*span); ok {
53+
out = append(out, s.makeSpanData())
54+
}
5355
}
5456
return out
5557
}
@@ -185,7 +187,7 @@ func (i internalOnly) ReportSpansByLatency(name string, minLatency, maxLatency t
185187
// bucketed by latency.
186188
type spanStore struct {
187189
mu sync.Mutex // protects everything below.
188-
active map[*Span]struct{}
190+
active map[Span]struct{}
189191
errors map[int32]*bucket
190192
latency []bucket
191193
maxSpansPerErrorBucket int
@@ -194,7 +196,7 @@ type spanStore struct {
194196
// newSpanStore creates a span store.
195197
func newSpanStore(name string, latencyBucketSize int, errorBucketSize int) *spanStore {
196198
s := &spanStore{
197-
active: make(map[*Span]struct{}),
199+
active: make(map[Span]struct{}),
198200
latency: make([]bucket, len(defaultLatencies)+1),
199201
maxSpansPerErrorBucket: errorBucketSize,
200202
}
@@ -271,15 +273,15 @@ func (s *spanStore) resize(latencyBucketSize int, errorBucketSize int) {
271273
}
272274

273275
// add adds a span to the active bucket of the spanStore.
274-
func (s *spanStore) add(span *Span) {
276+
func (s *spanStore) add(span Span) {
275277
s.mu.Lock()
276278
s.active[span] = struct{}{}
277279
s.mu.Unlock()
278280
}
279281

280282
// finished removes a span from the active set, and adds a corresponding
281283
// SpanData to a latency or error bucket.
282-
func (s *spanStore) finished(span *Span, sd *SpanData) {
284+
func (s *spanStore) finished(span Span, sd *SpanData) {
283285
latency := sd.EndTime.Sub(sd.StartTime)
284286
if latency < 0 {
285287
latency = 0

0 commit comments

Comments
 (0)