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

feat: adding length method to attribute list and updating http.url attribute in net/http instrumentation #224

Merged
merged 2 commits into from
Nov 23, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions instrumentation/opentelemetry/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ func (l *AttributeList) Iterate(yield func(key string, value interface{}) bool)
}
}

func (l *AttributeList) Len() int {
return len(l.attrs)
}

var _ sdk.Span = (*Span)(nil)

type Span struct {
Expand Down
14 changes: 14 additions & 0 deletions instrumentation/opentelemetry/span_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,17 @@ func TestIterate(t *testing.T) {
// service.instance.id is added implicitly in StartSpan so 3 attributes will be present.
assert.Equal(t, 3, numAttrs)
}

func TestLen(t *testing.T) {
sampler := sdktrace.AlwaysSample()
tp := sdktrace.NewTracerProvider(
sdktrace.WithSampler(sampler),
)
otel.SetTracerProvider(tp)
_, s, _ := StartSpan(context.Background(), "test_span", &sdk.SpanOptions{})
s.SetAttribute("k1", "v1")
s.SetAttribute("k2", 200)

// service.instance.id is added implicitly in StartSpan so 3 attributes will be present.
assert.Equal(t, 3, s.GetAttributes().Len())
}
7 changes: 6 additions & 1 deletion sdk/instrumentation/net/http/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"io"
"net/http"
"strings"

config "github.com/hypertrace/agent-config/gen/go/v1"
"github.com/hypertrace/goagent/sdk"
Expand Down Expand Up @@ -65,7 +66,11 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}

url := r.URL.String()
span.SetAttribute("http.url", url)
if strings.Contains(url, "://") {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

span.SetAttribute("http.url", url)
} else {
span.SetAttribute("http.target", url)
}

host := r.Host
span.SetAttribute("http.request.header.host", host)
Expand Down
33 changes: 33 additions & 0 deletions sdk/instrumentation/net/http/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,3 +516,36 @@ func TestProcessingBodyIsTrimmed(t *testing.T) {

ih.ServeHTTP(w, r)
}

func TestUrlAttribute(t *testing.T) {
defer internalconfig.ResetConfig()

h := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
rw.Header().Add("request_id", "abc123xyz")
rw.WriteHeader(202)
rw.Write([]byte("test_res"))
rw.Write([]byte("ponse_body"))
})

wh, _ := WrapHandler(h, mock.SpanFromContext, &Options{}, map[string]string{}, &metricsHandler{}).(*handler)
wh.dataCaptureConfig = emptyTestConfig
ih := &mockHandler{baseHandler: wh}

// http.url
r, _ := http.NewRequest("GET", "http://traceable.ai/foo?user_id=1", strings.NewReader("test_request_body"))
w := httptest.NewRecorder()
ih.ServeHTTP(w, r)
span := ih.spans[0]

assert.Equal(t, "http://traceable.ai/foo?user_id=1", span.ReadAttribute("http.url").(string))
assert.Nil(t, span.ReadAttribute("http.target"))

// http.target
r, _ = http.NewRequest("GET", "/foo?user_id=1", strings.NewReader("test_request_body"))
w = httptest.NewRecorder()
ih.ServeHTTP(w, r)
span = ih.spans[1]
assert.Equal(t, "/foo?user_id=1", span.ReadAttribute("http.target").(string))
assert.Nil(t, span.ReadAttribute("http.url"))

}
4 changes: 4 additions & 0 deletions sdk/internal/mock/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ func (l *AttributeList) Iterate(yield func(key string, value interface{}) bool)
}
}

func (l *AttributeList) Len() int {
return len(l.attrs)
}

var _ sdk.Span = &Span{}

type Span struct {
Expand Down
2 changes: 2 additions & 0 deletions sdk/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ type AttributeList interface {
// Iterate loops through the attributes list and applies the yield function on each attribute.
// If the yield function returns false, we exit the loop.
Iterate(yield func(key string, value interface{}) bool)

Len() int
}

// Span is an interface that accepts attributes and can be
Expand Down
Loading