Skip to content

Commit

Permalink
split test it into several unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
coryb committed Aug 11, 2021
1 parent 2985447 commit a0dc0a0
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 46 deletions.
12 changes: 4 additions & 8 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,22 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
### Added

- Add the `zpages` span processor. (#894)
- Add `WithoutSubSpans`, `WithRedactedHeaders`, `WithoutHeaders`, and `WithInsecureHeaders` options for `otelhttptrace.NewClientTrace`. (#879)


### Changed

- The `b3.B3` type has been removed.
`b3.New()` and `b3.WithInjectEncoding(encoding)` are added to replace it. (#868)
- `otelhttptrace.NewClientTrace` now redacts known sensitive headers by default. (#879)


### Fixed

- Fix deadlocks and race conditions in `otelsarama.WrapAsyncProducer`.
The `messaging.message_id` and `messaging.kafka.partition` attributes are now not set if a message was not processed. (#754) (#755) (#881)
- Fix `otelsarama.WrapAsyncProducer` so that the messages from the `Errors` channel contain the original `Metadata`. (#754)

### Added

- Add `WithoutSubSpans`, `WithRedactedHeaders`, `WithoutHeaders`, and `WithInsecureHeaders` options for `otelhttptrace.NewClientTrace`. (#879)

### Changed

- `otelhttptrace.NewClientTrace` now redacts known sensitive headers by default. (#879)

## [0.21.0] - 2021-06-18

### Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,35 +255,47 @@ func TestEndBeforeStartCreatesSpan(t *testing.T) {
require.Len(t, spans, 1)
}

func TestClientTraceOptions(t *testing.T) {
sr := &oteltest.SpanRecorder{}
type clientTraceTestFixture struct {
Address string
URL string
Client *http.Client
SpanRecorder *oteltest.SpanRecorder
}

func prepareClientTraceTest(t *testing.T) clientTraceTestFixture {
fixture := clientTraceTestFixture{}
fixture.SpanRecorder = &oteltest.SpanRecorder{}
otel.SetTracerProvider(
oteltest.NewTracerProvider(oteltest.WithSpanRecorder(sr)),
oteltest.NewTracerProvider(oteltest.WithSpanRecorder(fixture.SpanRecorder)),
)

// Mock http server
ts := httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
}),
)
defer ts.Close()
address := ts.Listener.Addr().String()
t.Cleanup(ts.Close)
fixture.Client = ts.Client()
fixture.URL = ts.URL
fixture.Address = ts.Listener.Addr().String()
return fixture
}

// Test WithoutSubSpans
func TestWithoutSubSpans(t *testing.T) {
fixture := prepareClientTraceTest(t)

ctx := context.Background()
ctx = httptrace.WithClientTrace(ctx,
otelhttptrace.NewClientTrace(ctx,
otelhttptrace.WithoutSubSpans(),
),
)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, ts.URL, nil)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, fixture.URL, nil)
require.NoError(t, err)
resp, err := ts.Client().Do(req)
resp, err := fixture.Client.Do(req)
require.NoError(t, err)
resp.Body.Close()
// no spans created because we were just using background context without span
require.Len(t, sr.Completed(), 0)
require.Len(t, fixture.SpanRecorder.Completed(), 0)

// Start again with a "real" span in the context, now tracing should add
// events and annotations.
Expand All @@ -293,17 +305,17 @@ func TestClientTraceOptions(t *testing.T) {
otelhttptrace.WithoutSubSpans(),
),
)
req, err = http.NewRequestWithContext(ctx, http.MethodGet, ts.URL, nil)
req, err = http.NewRequestWithContext(ctx, http.MethodGet, fixture.URL, nil)
req.Header.Set("User-Agent", "oteltest/1.1")
req.Header.Set("Authorization", "Bearer token123")
require.NoError(t, err)
resp, err = ts.Client().Do(req)
resp, err = fixture.Client.Do(req)
require.NoError(t, err)
resp.Body.Close()
span.End()
// we just have the one span we created
require.Len(t, sr.Completed(), 1)
recSpan := sr.Completed()[0]
require.Len(t, fixture.SpanRecorder.Completed(), 1)
recSpan := fixture.SpanRecorder.Completed()[0]

gotAttributes := recSpan.Attributes()
require.Len(t, gotAttributes, 4)
Expand All @@ -321,7 +333,7 @@ func TestClientTraceOptions(t *testing.T) {
gotAttributes[attribute.Key("http.authorization")],
)
assert.Equal(t,
attribute.StringValue(address),
attribute.StringValue(fixture.Address),
gotAttributes[attribute.Key("http.host")],
)

Expand All @@ -332,7 +344,7 @@ func TestClientTraceOptions(t *testing.T) {
}{
{"http.getconn.start", func(t *testing.T, got attrMap) {
assert.Equal(t,
attribute.StringValue(address),
attribute.StringValue(fixture.Address),
got[attribute.Key("http.host")],
)
}},
Expand All @@ -348,7 +360,7 @@ func TestClientTraceOptions(t *testing.T) {
got[attribute.Key("http.conn.wasidle")],
)
assert.Equal(t,
attribute.StringValue(address),
attribute.StringValue(fixture.Address),
got[attribute.Key("http.remote")],
)
// value is dynamic, just verify we have the attribute
Expand All @@ -372,72 +384,78 @@ func TestClientTraceOptions(t *testing.T) {
})
}
}
}

// Test WithRedactedHeaders
func TestWithRedactedHeaders(t *testing.T) {
fixture := prepareClientTraceTest(t)

ctx, span = otel.Tracer("oteltest").Start(context.Background(), "root")
ctx, span := otel.Tracer("oteltest").Start(context.Background(), "root")
ctx = httptrace.WithClientTrace(ctx,
otelhttptrace.NewClientTrace(ctx,
otelhttptrace.WithoutSubSpans(),
otelhttptrace.WithRedactedHeaders("user-agent"),
),
)
req, err = http.NewRequestWithContext(ctx, http.MethodGet, ts.URL, nil)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, fixture.URL, nil)
require.NoError(t, err)
resp, err = ts.Client().Do(req)
resp, err := fixture.Client.Do(req)
require.NoError(t, err)
resp.Body.Close()
span.End()
require.Len(t, sr.Completed(), 2)
recSpan = sr.Completed()[1]
require.Len(t, fixture.SpanRecorder.Completed(), 1)
recSpan := fixture.SpanRecorder.Completed()[0]

gotAttributes = recSpan.Attributes()
gotAttributes := recSpan.Attributes()
assert.Equal(t,
attribute.StringValue("****"),
gotAttributes[attribute.Key("http.user-agent")],
)
}

// Test WithoutHeaders
func TestWithoutHeaders(t *testing.T) {
fixture := prepareClientTraceTest(t)

ctx, span = otel.Tracer("oteltest").Start(context.Background(), "root")
ctx, span := otel.Tracer("oteltest").Start(context.Background(), "root")
ctx = httptrace.WithClientTrace(ctx,
otelhttptrace.NewClientTrace(ctx,
otelhttptrace.WithoutSubSpans(),
otelhttptrace.WithoutHeaders(),
),
)
req, err = http.NewRequestWithContext(ctx, http.MethodGet, ts.URL, nil)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, fixture.URL, nil)
require.NoError(t, err)
resp, err = ts.Client().Do(req)
resp, err := fixture.Client.Do(req)
require.NoError(t, err)
resp.Body.Close()
span.End()
require.Len(t, sr.Completed(), 3)
recSpan = sr.Completed()[2]
require.Len(t, fixture.SpanRecorder.Completed(), 1)
recSpan := fixture.SpanRecorder.Completed()[0]

gotAttributes = recSpan.Attributes()
gotAttributes := recSpan.Attributes()
require.Len(t, gotAttributes, 0)
}

// Test WithInsecureHeaders
func TestWithInsecureHeaders(t *testing.T) {
fixture := prepareClientTraceTest(t)

ctx, span = otel.Tracer("oteltest").Start(context.Background(), "root")
ctx, span := otel.Tracer("oteltest").Start(context.Background(), "root")
ctx = httptrace.WithClientTrace(ctx,
otelhttptrace.NewClientTrace(ctx,
otelhttptrace.WithoutSubSpans(),
otelhttptrace.WithInsecureHeaders(),
),
)
req, err = http.NewRequestWithContext(ctx, http.MethodGet, ts.URL, nil)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, fixture.URL, nil)
req.Header.Set("Authorization", "Bearer token123")
require.NoError(t, err)
resp, err = ts.Client().Do(req)
resp, err := fixture.Client.Do(req)
require.NoError(t, err)
resp.Body.Close()
span.End()
require.Len(t, sr.Completed(), 4)
recSpan = sr.Completed()[3]
require.Len(t, fixture.SpanRecorder.Completed(), 1)
recSpan := fixture.SpanRecorder.Completed()[0]

gotAttributes = recSpan.Attributes()
gotAttributes := recSpan.Attributes()
assert.Equal(t,
attribute.StringValue("Bearer token123"),
gotAttributes[attribute.Key("http.authorization")],
Expand Down

0 comments on commit a0dc0a0

Please sign in to comment.