Skip to content
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
11 changes: 5 additions & 6 deletions instrumentation/opentelemetry/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,12 @@ 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()}
func (l *AttributeList) Iterate(yield func(key string, value interface{}) bool) {
Comment thread
tim-mwangi marked this conversation as resolved.
for _, attr := range l.attrs {
if !yield(string(attr.Key), attr.Value.AsInterface()) {
return
}
}
return attributes
}

var _ sdk.Span = (*Span)(nil)
Expand Down
22 changes: 12 additions & 10 deletions instrumentation/opentelemetry/span_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func TestGetAttributes(t *testing.T) {
assert.Equal(t, nil, attrs.GetValue("non_existent"))
}

func TestGetAllAttributes(t *testing.T) {
func TestIterate(t *testing.T) {
sampler := sdktrace.AlwaysSample()
tp := sdktrace.NewTracerProvider(
sdktrace.WithSampler(sampler),
Expand All @@ -119,15 +119,17 @@ func TestGetAllAttributes(t *testing.T) {
_, 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))
numAttrs := 0
s.GetAttributes().Iterate(func(key string, value interface{}) bool {
if key == "k1" {
assert.Equal(t, "v1", fmt.Sprintf("%v", value))
} else if key == "k2" {
assert.Equal(t, "200", fmt.Sprintf("%v", value))
}
}
numAttrs++
return true
})
// service.instance.id is added implicitly in StartSpan so 3 attributes will be present.
assert.Equal(t, 3, numAttrs)
}
11 changes: 4 additions & 7 deletions sdk/internal/mock/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,12 @@ 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
func (l *AttributeList) Iterate(yield func(key string, value interface{}) bool) {
for key, value := range l.attrs {
attributes[i] = sdk.Attribute{Key: key, Value: value}
i++
if !yield(key, value) {
return
}
}
return attributes
}

var _ sdk.Span = &Span{}
Expand Down
10 changes: 4 additions & 6 deletions sdk/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@ import (
"time"
)

type Attribute struct {
Key string
Value interface{}
}

type AttributeList interface {
GetValue(key string) interface{}
GetAll() []Attribute

// 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)
}

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