Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added WithClientRequestName function #609

Merged
merged 2 commits into from Aug 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions module/apmhttp/client.go
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
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