Skip to content

Commit

Permalink
otelhttptrace: get tracerprovider from context if available (#874)
Browse files Browse the repository at this point in the history
Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>

Co-authored-by: Aaron Clawson <Aaron.Clawson@gmail.com>
  • Loading branch information
2 people authored and rltoSD committed Dec 21, 2021
1 parent cad28fe commit fff7628
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

### Changed

- `otelhttptrace.NewClientTrace` now uses `TracerProvider` from the parent context if one exists and none was set with `WithTracerProvider` (#874)
- Changed the project minimum supported Go version from 1.15 to 1.16. (#1442)

### Fixed
Expand Down
12 changes: 9 additions & 3 deletions instrumentation/net/http/httptrace/otelhttptrace/clienttrace.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,8 @@ type clientTracer struct {
// Proxy-Authorization, Cookie, and Set-Cookie.
func NewClientTrace(ctx context.Context, opts ...ClientTraceOption) *httptrace.ClientTrace {
ct := &clientTracer{
Context: ctx,
tracerProvider: otel.GetTracerProvider(),
activeHooks: make(map[string]context.Context),
Context: ctx,
activeHooks: make(map[string]context.Context),
redactedHeaders: map[string]struct{}{
"authorization": {},
"www-authenticate": {},
Expand All @@ -160,6 +159,13 @@ func NewClientTrace(ctx context.Context, opts ...ClientTraceOption) *httptrace.C
addHeaders: true,
useSpans: true,
}

if span := trace.SpanFromContext(ctx); span.SpanContext().IsValid() {
ct.tracerProvider = span.TracerProvider()
} else {
ct.tracerProvider = otel.GetTracerProvider()
}

for _, opt := range opts {
opt.apply(ct)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -471,3 +471,36 @@ func TestWithInsecureHeaders(t *testing.T) {
gotAttributes,
)
}

func TestHTTPRequestWithTraceContext(t *testing.T) {
sr := tracetest.NewSpanRecorder()
tp := trace.NewTracerProvider(trace.WithSpanProcessor(sr))

// Mock http server
ts := httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
}),
)
defer ts.Close()

ctx, span := tp.Tracer("").Start(context.Background(), "parent_span")

req, _ := http.NewRequest("GET", ts.URL, nil)
req = req.WithContext(httptrace.WithClientTrace(req.Context(), otelhttptrace.NewClientTrace(ctx)))

client := ts.Client()
res, err := client.Do(req)
require.NoError(t, err)
_ = res.Body.Close()

span.End()

parent, ok := getSpanFromRecorder(sr, "parent_span")
require.True(t, ok)

getconn, ok := getSpanFromRecorder(sr, "http.getconn")
require.True(t, ok)

require.Equal(t, parent.SpanContext().TraceID(), getconn.SpanContext().TraceID())
require.Equal(t, parent.SpanContext().SpanID(), getconn.Parent().SpanID())
}

0 comments on commit fff7628

Please sign in to comment.