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

Commit

Permalink
Allow replacing trace SDK (#1234)
Browse files Browse the repository at this point in the history
  • Loading branch information
dashpole committed Oct 22, 2020
1 parent 3fb168f commit fc3822b
Show file tree
Hide file tree
Showing 13 changed files with 245 additions and 80 deletions.
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
Expand Down
4 changes: 2 additions & 2 deletions plugin/ochttp/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ type Transport struct {
// name equals the URL Path.
FormatSpanName func(*http.Request) string

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

// TODO: Implement tag propagation for HTTP.
}
Expand Down
2 changes: 1 addition & 1 deletion plugin/ochttp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func (h *Handler) startTrace(w http.ResponseWriter, r *http.Request) (*http.Requ
startOpts = h.GetStartOptions(r)
}

var span *trace.Span
var span trace.Span
sc, ok := h.extractSpanContext(r)
if ok && !h.IsPublicEndpoint {
ctx, span = trace.StartSpanWithRemoteParent(ctx, name, sc,
Expand Down
2 changes: 1 addition & 1 deletion plugin/ochttp/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ func TestIgnoreHealthEndpoints(t *testing.T) {
ts := httptest.NewServer(&Handler{
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
span := trace.FromContext(r.Context())
if span != nil {
if span.IsRecordingEvents() {
spans++
}
fmt.Fprint(w, "ok")
Expand Down
6 changes: 3 additions & 3 deletions plugin/ochttp/span_annotating_client_trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,21 @@ import (
)

type spanAnnotator struct {
sp *trace.Span
sp trace.Span
}

// TODO: Remove NewSpanAnnotator at the next release.

// NewSpanAnnotator returns a httptrace.ClientTrace which annotates
// all emitted httptrace events on the provided Span.
// Deprecated: Use NewSpanAnnotatingClientTrace instead
func NewSpanAnnotator(r *http.Request, s *trace.Span) *httptrace.ClientTrace {
func NewSpanAnnotator(r *http.Request, s trace.Span) *httptrace.ClientTrace {
return NewSpanAnnotatingClientTrace(r, s)
}

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

return &httptrace.ClientTrace{
Expand Down
4 changes: 2 additions & 2 deletions plugin/ochttp/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type traceTransport struct {
startOptions trace.StartOptions
format propagation.HTTPFormat
formatSpanName func(*http.Request) string
newClientTrace func(*http.Request, *trace.Span) *httptrace.ClientTrace
newClientTrace func(*http.Request, trace.Span) *httptrace.ClientTrace
}

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

var _ io.ReadCloser = (*bodyTracker)(nil)
Expand Down
2 changes: 1 addition & 1 deletion plugin/ochttp/trace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func TestTransport_RoundTrip(t *testing.T) {
_, parent := trace.StartSpan(context.Background(), "parent")
tests := []struct {
name string
parent *trace.Span
parent trace.Span
}{
{
name: "no parent",
Expand Down
10 changes: 10 additions & 0 deletions trace/basetypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ type Attribute struct {
value interface{}
}

// Key returns the attribute's key
func (a *Attribute) Key() string {
return a.key
}

// Value returns the attribute's value
func (a *Attribute) Value() interface{} {
return a.value
}

// BoolAttribute returns a bool-valued attribute.
func BoolAttribute(key string, value bool) Attribute {
return Attribute{key: key, value: value}
Expand Down
2 changes: 1 addition & 1 deletion trace/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"go.opencensus.io/trace"
)

// This example shows how to use StartSpan and (*Span).End to capture
// This example shows how to use StartSpan and (Span).End to capture
// a function execution in a Span. It assumes that the function
// has a context.Context argument.
func ExampleStartSpan() {
Expand Down
14 changes: 8 additions & 6 deletions trace/spanstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ func (i internalOnly) ReportActiveSpans(name string) []*SpanData {
var out []*SpanData
s.mu.Lock()
defer s.mu.Unlock()
for span := range s.active {
out = append(out, span.makeSpanData())
for activeSpan := range s.active {
if s, ok := activeSpan.(*span); ok {
out = append(out, s.makeSpanData())
}
}
return out
}
Expand Down Expand Up @@ -185,7 +187,7 @@ func (i internalOnly) ReportSpansByLatency(name string, minLatency, maxLatency t
// bucketed by latency.
type spanStore struct {
mu sync.Mutex // protects everything below.
active map[*Span]struct{}
active map[Span]struct{}
errors map[int32]*bucket
latency []bucket
maxSpansPerErrorBucket int
Expand All @@ -194,7 +196,7 @@ type spanStore struct {
// newSpanStore creates a span store.
func newSpanStore(name string, latencyBucketSize int, errorBucketSize int) *spanStore {
s := &spanStore{
active: make(map[*Span]struct{}),
active: make(map[Span]struct{}),
latency: make([]bucket, len(defaultLatencies)+1),
maxSpansPerErrorBucket: errorBucketSize,
}
Expand Down Expand Up @@ -271,15 +273,15 @@ func (s *spanStore) resize(latencyBucketSize int, errorBucketSize int) {
}

// add adds a span to the active bucket of the spanStore.
func (s *spanStore) add(span *Span) {
func (s *spanStore) add(span Span) {
s.mu.Lock()
s.active[span] = struct{}{}
s.mu.Unlock()
}

// finished removes a span from the active set, and adds a corresponding
// SpanData to a latency or error bucket.
func (s *spanStore) finished(span *Span, sd *SpanData) {
func (s *spanStore) finished(span Span, sd *SpanData) {
latency := sd.EndTime.Sub(sd.StartTime)
if latency < 0 {
latency = 0
Expand Down

0 comments on commit fc3822b

Please sign in to comment.