diff --git a/instrumentation/opentelemetry/span.go b/instrumentation/opentelemetry/span.go index 9808085..164e907 100644 --- a/instrumentation/opentelemetry/span.go +++ b/instrumentation/opentelemetry/span.go @@ -31,6 +31,15 @@ func (l *AttributeList) GetValue(key string) interface{} { return nil } +func (l *AttributeList) GetAll() []sdk.Attribute { + 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 +} + var _ sdk.Span = (*Span)(nil) type Span struct { diff --git a/instrumentation/opentelemetry/span_test.go b/instrumentation/opentelemetry/span_test.go index 456b0bb..c1c9764 100644 --- a/instrumentation/opentelemetry/span_test.go +++ b/instrumentation/opentelemetry/span_test.go @@ -109,3 +109,25 @@ 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() + + // 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" { + assert.Equal(t, "v1", fmt.Sprintf("%v", attr.Value)) + } else if attr.Key == "k2" { + assert.Equal(t, "200", fmt.Sprintf("%v", attr.Value)) + } + } +} diff --git a/sdk/internal/mock/span.go b/sdk/internal/mock/span.go index b6ba478..08f2047 100644 --- a/sdk/internal/mock/span.go +++ b/sdk/internal/mock/span.go @@ -29,6 +29,17 @@ func (l *AttributeList) GetValue(key string) interface{} { return l.attrs[key] } +func (l *AttributeList) GetAll() []sdk.Attribute { + + attributes := make([]sdk.Attribute, len(l.attrs)) + i := 0 + for key, value := range l.attrs { + attributes[i] = sdk.Attribute{Key: key, Value: value} + i++ + } + return attributes +} + var _ sdk.Span = &Span{} type Span struct { diff --git a/sdk/span.go b/sdk/span.go index a5b7d2e..54dc8c7 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