From 5ecf25a4de2239c6d5acd221caba0a3d4ad2b7d8 Mon Sep 17 00:00:00 2001 From: Varkeychan Jacob Date: Sun, 5 Nov 2023 11:33:11 +0530 Subject: [PATCH 1/5] adding generic attributes interface --- instrumentation/opentelemetry/span.go | 8 ++++++++ sdk/internal/mock/span.go | 8 ++++++++ sdk/span.go | 6 ++++++ 3 files changed, 22 insertions(+) diff --git a/instrumentation/opentelemetry/span.go b/instrumentation/opentelemetry/span.go index 9808085c..528d3531 100644 --- a/instrumentation/opentelemetry/span.go +++ b/instrumentation/opentelemetry/span.go @@ -31,6 +31,14 @@ func (l *AttributeList) GetValue(key string) interface{} { return nil } +func (l *AttributeList) GetAll() []sdk.Attribute { + attributes := make([]sdk.Attribute, len(l.attrs)) + for _, attr := range l.attrs { + attributes = append(attributes, sdk.Attribute{Key: string(attr.Key), Value: attr.Value.AsInterface()}) + } + return attributes +} + var _ sdk.Span = (*Span)(nil) type Span struct { diff --git a/sdk/internal/mock/span.go b/sdk/internal/mock/span.go index b6ba4781..ab197555 100644 --- a/sdk/internal/mock/span.go +++ b/sdk/internal/mock/span.go @@ -29,6 +29,14 @@ func (l *AttributeList) GetValue(key string) interface{} { return l.attrs[key] } +func (l *AttributeList) GetAll() []sdk.Attribute { + attributes := make([]sdk.Attribute, len(l.attrs)) + for key, value := range l.attrs { + attributes = append(attributes, sdk.Attribute{Key: key, Value: value}) + } + return attributes +} + var _ sdk.Span = &Span{} type Span struct { diff --git a/sdk/span.go b/sdk/span.go index a5b7d2ed..54dc8c72 100644 --- a/sdk/span.go +++ b/sdk/span.go @@ -5,8 +5,14 @@ import ( "time" ) +type Attribute struct { + Key string + Value interface{} +} + type AttributeList interface { GetValue(key string) interface{} + GetAll() []Attribute } // Span is an interface that accepts attributes and can be From 44aaea62d223cbae71082eb16ab739084f74682e Mon Sep 17 00:00:00 2001 From: Varkeychan Jacob Date: Sun, 5 Nov 2023 12:59:23 +0530 Subject: [PATCH 2/5] adding unit test --- instrumentation/opentelemetry/span_test.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/instrumentation/opentelemetry/span_test.go b/instrumentation/opentelemetry/span_test.go index 456b0bb9..beac5cb4 100644 --- a/instrumentation/opentelemetry/span_test.go +++ b/instrumentation/opentelemetry/span_test.go @@ -109,3 +109,23 @@ func TestGetAttributes(t *testing.T) { assert.Equal(t, "string_value", attrs.GetValue("string_key")) assert.Equal(t, nil, attrs.GetValue("non_existent")) } + +func TestGetAllAttributes(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) + attrs := s.GetAttributes().GetAll() + + for _, attr := range attrs { + if attr.Key == "k1" { + assert.Equal(t, "v1", fmt.Sprintf("%v", attr.Value)) + } else if attr.Key == "k2" { + assert.Equal(t, "200", fmt.Sprintf("%v", attr.Value)) + } + } +} From 3c1d53dc4423e0f7e69bd2b6e50348bc035eef73 Mon Sep 17 00:00:00 2001 From: Varkeychan Jacob Date: Tue, 7 Nov 2023 11:14:07 +0530 Subject: [PATCH 3/5] review changes --- instrumentation/opentelemetry/span.go | 7 ++++--- instrumentation/opentelemetry/span_test.go | 1 + 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/instrumentation/opentelemetry/span.go b/instrumentation/opentelemetry/span.go index 528d3531..164e907a 100644 --- a/instrumentation/opentelemetry/span.go +++ b/instrumentation/opentelemetry/span.go @@ -32,9 +32,10 @@ func (l *AttributeList) GetValue(key string) interface{} { } func (l *AttributeList) GetAll() []sdk.Attribute { - attributes := make([]sdk.Attribute, len(l.attrs)) - for _, attr := range l.attrs { - attributes = append(attributes, sdk.Attribute{Key: string(attr.Key), Value: attr.Value.AsInterface()}) + size := len(l.attrs) + attributes := make([]sdk.Attribute, size) + for i := 0; i < size; i++ { + attributes[i] = sdk.Attribute{Key: string(l.attrs[i].Key), Value: l.attrs[i].Value.AsInterface()} } return attributes } diff --git a/instrumentation/opentelemetry/span_test.go b/instrumentation/opentelemetry/span_test.go index beac5cb4..87fc88ce 100644 --- a/instrumentation/opentelemetry/span_test.go +++ b/instrumentation/opentelemetry/span_test.go @@ -121,6 +121,7 @@ func TestGetAllAttributes(t *testing.T) { s.SetAttribute("k2", 200) attrs := s.GetAttributes().GetAll() + assert.Equal(t, 3, len(attrs)) for _, attr := range attrs { if attr.Key == "k1" { assert.Equal(t, "v1", fmt.Sprintf("%v", attr.Value)) From dfcb4c704d949d61776a34b1ce5da6f3cbd5974f Mon Sep 17 00:00:00 2001 From: Varkeychan Jacob Date: Tue, 7 Nov 2023 18:20:55 +0530 Subject: [PATCH 4/5] review changes --- sdk/internal/mock/span.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/sdk/internal/mock/span.go b/sdk/internal/mock/span.go index ab197555..9b4aba11 100644 --- a/sdk/internal/mock/span.go +++ b/sdk/internal/mock/span.go @@ -30,9 +30,11 @@ func (l *AttributeList) GetValue(key string) interface{} { } func (l *AttributeList) GetAll() []sdk.Attribute { + attributes := make([]sdk.Attribute, len(l.attrs)) + i := 0 for key, value := range l.attrs { - attributes = append(attributes, sdk.Attribute{Key: key, Value: value}) + attributes[i] = sdk.Attribute{Key: key, Value: value} } return attributes } From d9abff12bae5cdb4018ba4f2533b0a78a89e851e Mon Sep 17 00:00:00 2001 From: Varkeychan Jacob Date: Tue, 7 Nov 2023 19:41:22 +0530 Subject: [PATCH 5/5] review changes --- instrumentation/opentelemetry/span_test.go | 1 + sdk/internal/mock/span.go | 1 + 2 files changed, 2 insertions(+) diff --git a/instrumentation/opentelemetry/span_test.go b/instrumentation/opentelemetry/span_test.go index 87fc88ce..c1c97649 100644 --- a/instrumentation/opentelemetry/span_test.go +++ b/instrumentation/opentelemetry/span_test.go @@ -121,6 +121,7 @@ func TestGetAllAttributes(t *testing.T) { s.SetAttribute("k2", 200) attrs := s.GetAttributes().GetAll() + // service.instance.id is added implicitly in StartSpan so 3 attributes will be present. assert.Equal(t, 3, len(attrs)) for _, attr := range attrs { if attr.Key == "k1" { diff --git a/sdk/internal/mock/span.go b/sdk/internal/mock/span.go index 9b4aba11..08f2047a 100644 --- a/sdk/internal/mock/span.go +++ b/sdk/internal/mock/span.go @@ -35,6 +35,7 @@ func (l *AttributeList) GetAll() []sdk.Attribute { i := 0 for key, value := range l.attrs { attributes[i] = sdk.Attribute{Key: key, Value: value} + i++ } return attributes }