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

[apmhttp] WithClientSpanType ClientOption #1106

Merged
merged 10 commits into from
Aug 26, 2021
10 changes: 9 additions & 1 deletion module/apmhttp/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func WrapRoundTripper(r http.RoundTripper, o ...ClientOption) http.RoundTripper
r: r,
requestName: ClientRequestName,
requestIgnorer: IgnoreNone,
spanType: "external.http",
}
for _, o := range o {
o(rt)
Expand All @@ -71,6 +72,7 @@ type roundTripper struct {
requestName RequestNameFunc
requestIgnorer RequestIgnorerFunc
traceRequests bool
spanType string
}

// RoundTrip delegates to r.r, emitting a span if req's context
Expand Down Expand Up @@ -102,7 +104,7 @@ func (r *roundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
}

name := r.requestName(req)
span := tx.StartSpan(name, "external.http", apm.SpanFromContext(ctx))
span := tx.StartSpan(name, r.spanType, apm.SpanFromContext(ctx))
var rt *requestTracer
if !span.Dropped() {
traceContext = span.TraceContext()
Expand Down Expand Up @@ -211,3 +213,9 @@ func WithClientRequestName(r RequestNameFunc) ClientOption {
rt.requestName = r
})
}

func WithClientSpanType(spanType string) ClientOption {
Jasonfran marked this conversation as resolved.
Show resolved Hide resolved
return ClientOption(func(rt *roundTripper) {
rt.spanType = spanType
})
}
16 changes: 16 additions & 0 deletions module/apmhttp/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,22 @@ func TestClientOutcome(t *testing.T) {
assert.Equal(t, "failure", spans[2].Outcome)
}

func TestWithClientSpanType(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusTeapot)
}))
defer server.Close()

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

require.Len(t, spans, 1)
span := spans[0]
assert.Equal(t, "cache", span.Type)
}

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)
Expand Down