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
8 changes: 7 additions & 1 deletion CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,17 @@ endif::[]
[[unreleased]]
=== Unreleased

https://github.com/elastic/apm-agent-go/compare/v1.13.1...master[View commits]
https://github.com/elastic/apm-agent-go/compare/v1.14.0...master[View commits]
Jasonfran marked this conversation as resolved.
Show resolved Hide resolved

[[release-notes-1.x]]
=== Go Agent version 1.x

[[release-notes-1.14.0]]
==== 1.14.0 - 2021/08/24

- module/apmhttp: add apmhttp.WithClientSpanType ClientOption to set the span type of http client requests {pull}1106[#(1106)]


Jasonfran marked this conversation as resolved.
Show resolved Hide resolved
[[release-notes-1.13.1]]
==== 1.13.1 - 2021/08/05

Expand Down
13 changes: 12 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,12 @@ func WithClientRequestName(r RequestNameFunc) ClientOption {
rt.requestName = r
})
}

// WithClientSpanType sets the span type for HTTP client requests.
//
// Defaults to "external.http".
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