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

adding iterator interface and removing GetAll() method from attribute list #220

Merged
merged 4 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
32 changes: 26 additions & 6 deletions instrumentation/opentelemetry/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,27 @@ import (
"go.opentelemetry.io/otel/trace"
)

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

type Iterator struct {
size int
curr int
attrs []attribute.KeyValue
}

func (i *Iterator) HasNext() bool {
return i.curr < i.size
}

func (i *Iterator) Next() sdk.Attribute {
attr := sdk.Attribute{
Key: string(i.attrs[i.curr].Key),
Value: i.attrs[i.curr].Value.AsInterface(),
}
i.curr++
return attr
}

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

type AttributeList struct {
Expand All @@ -31,13 +52,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) GetIterator() sdk.Iterator {
return &Iterator{
size: len(l.attrs),
curr: 0,
attrs: l.attrs,
}
return attributes
}

var _ sdk.Span = (*Span)(nil)
Expand Down
13 changes: 8 additions & 5 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 TestGetIterator(t *testing.T) {
sampler := sdktrace.AlwaysSample()
tp := sdktrace.NewTracerProvider(
sdktrace.WithSampler(sampler),
Expand All @@ -119,15 +119,18 @@ 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()
itr := s.GetAttributes().GetIterator()

// service.instance.id is added implicitly in StartSpan so 3 attributes will be present.
assert.Equal(t, 3, len(attrs))
for _, attr := range attrs {
numAttrs := 0
for itr.HasNext() {
numAttrs++
attr := itr.Next()
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))
}
}
// service.instance.id is added implicitly in StartSpan so 3 attributes will be present.
assert.Equal(t, 3, numAttrs)
}
39 changes: 34 additions & 5 deletions sdk/internal/mock/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,29 @@ type Status struct {
Message string
}

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

type Iterator struct {
size int
curr int
keySet []string
attrs map[string]interface{}
}

func (i *Iterator) HasNext() bool {
return i.curr < i.size
}

func (i *Iterator) Next() sdk.Attribute {
key := i.keySet[i.curr]
attr := sdk.Attribute{
Key: key,
Value: i.attrs[key],
}
i.curr++
return attr
}

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

type AttributeList struct {
Expand All @@ -29,15 +52,21 @@ func (l *AttributeList) GetValue(key string) interface{} {
return l.attrs[key]
}

func (l *AttributeList) GetAll() []sdk.Attribute {
func (l *AttributeList) GetIterator() sdk.Iterator {

attributes := make([]sdk.Attribute, len(l.attrs))
keySet := make([]string, len(l.attrs))
i := 0
for key, value := range l.attrs {
attributes[i] = sdk.Attribute{Key: key, Value: value}
for k := range l.attrs {
keySet[i] = k
i++
}
return attributes

return &Iterator{
size: len(l.attrs),
curr: 0,
keySet: keySet,
attrs: l.attrs,
}
}

var _ sdk.Span = &Span{}
Expand Down
7 changes: 6 additions & 1 deletion sdk/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@ type Attribute struct {
Value interface{}
}

type Iterator interface {
HasNext() bool
Next() Attribute
varkey98 marked this conversation as resolved.
Show resolved Hide resolved
}

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

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