From 5e4457e41874761c25acbe603770022b4a9bafa4 Mon Sep 17 00:00:00 2001 From: Varkeychan Jacob Date: Thu, 23 Nov 2023 15:44:25 +0530 Subject: [PATCH 1/2] adding length method to attribute list --- instrumentation/opentelemetry/span.go | 4 +++ sdk/instrumentation/net/http/handler.go | 7 ++++- sdk/instrumentation/net/http/handler_test.go | 33 ++++++++++++++++++++ sdk/internal/mock/span.go | 4 +++ sdk/span.go | 2 ++ 5 files changed, 49 insertions(+), 1 deletion(-) diff --git a/instrumentation/opentelemetry/span.go b/instrumentation/opentelemetry/span.go index 79e7de0..0872fbd 100644 --- a/instrumentation/opentelemetry/span.go +++ b/instrumentation/opentelemetry/span.go @@ -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 { diff --git a/sdk/instrumentation/net/http/handler.go b/sdk/instrumentation/net/http/handler.go index a454d7a..99249eb 100644 --- a/sdk/instrumentation/net/http/handler.go +++ b/sdk/instrumentation/net/http/handler.go @@ -4,6 +4,7 @@ import ( "bytes" "io" "net/http" + "strings" config "github.com/hypertrace/agent-config/gen/go/v1" "github.com/hypertrace/goagent/sdk" @@ -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, "://") { + span.SetAttribute("http.url", url) + } else { + span.SetAttribute("http.target", url) + } host := r.Host span.SetAttribute("http.request.header.host", host) diff --git a/sdk/instrumentation/net/http/handler_test.go b/sdk/instrumentation/net/http/handler_test.go index 36147eb..403c87f 100644 --- a/sdk/instrumentation/net/http/handler_test.go +++ b/sdk/instrumentation/net/http/handler_test.go @@ -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")) + +} diff --git a/sdk/internal/mock/span.go b/sdk/internal/mock/span.go index e8b279f..c04d779 100644 --- a/sdk/internal/mock/span.go +++ b/sdk/internal/mock/span.go @@ -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 { diff --git a/sdk/span.go b/sdk/span.go index 8f34577..8dc1f75 100644 --- a/sdk/span.go +++ b/sdk/span.go @@ -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 From 074687b26b9ee31ae912e8866a7708cd3b0bcf90 Mon Sep 17 00:00:00 2001 From: Varkeychan Jacob Date: Thu, 23 Nov 2023 15:49:18 +0530 Subject: [PATCH 2/2] adding unit tests --- instrumentation/opentelemetry/span_test.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/instrumentation/opentelemetry/span_test.go b/instrumentation/opentelemetry/span_test.go index 2e8d01e..2a6aa98 100644 --- a/instrumentation/opentelemetry/span_test.go +++ b/instrumentation/opentelemetry/span_test.go @@ -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()) +}