Skip to content

Commit

Permalink
module/apmhttp: add WithClientRequestName option
Browse files Browse the repository at this point in the history
  • Loading branch information
yklyahin authored and axw committed Aug 5, 2019
1 parent 755a78b commit 203d013
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
12 changes: 12 additions & 0 deletions module/apmhttp/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,15 @@ func (b *responseBody) endSpan() {

// ClientOption sets options for tracing client requests.
type ClientOption func(*roundTripper)

// WithClientRequestName returns a ClientOption which sets r as the function
// to use to obtain the span name for the given http request.
func WithClientRequestName(r RequestNameFunc) ClientOption {
if r == nil {
panic("r == nil")
}

return ClientOption(func(rt *roundTripper) {
rt.requestName = r
})
}
23 changes: 21 additions & 2 deletions module/apmhttp/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,27 @@ func TestClientCancelRequest(t *testing.T) {
}
}

func mustGET(ctx context.Context, url string) (statusCode int, responseBody string) {
client := apmhttp.WrapClient(http.DefaultClient)
func TestWithClientRequestName(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusTeapot)
}))
defer server.Close()

option := apmhttp.WithClientRequestName(func(_ *http.Request) string {
return "http://test"
})

_, spans, _ := apmtest.WithTransaction(func(ctx context.Context) {
mustGET(ctx, server.URL, option)
})

require.Len(t, spans, 1)
span := spans[0]
assert.Equal(t, "http://test", span.Name)
}

func mustGET(ctx context.Context, url string, o ...apmhttp.ClientOption) (statusCode int, responseBody string) {
client := apmhttp.WrapClient(http.DefaultClient, o...)
resp, err := ctxhttp.Get(ctx, client, url)
if err != nil {
panic(err)
Expand Down

0 comments on commit 203d013

Please sign in to comment.