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
9 changes: 9 additions & 0 deletions instrumentation/opentelemetry/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ func (l *AttributeList) GetValue(key string) interface{} {
return nil
}

func (l *AttributeList) GetAll() []sdk.Attribute {
Comment thread
varkey98 marked this conversation as resolved.
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 {
Expand Down
22 changes: 22 additions & 0 deletions instrumentation/opentelemetry/span_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Comment thread
varkey98 marked this conversation as resolved.
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))
}
}
}
11 changes: 11 additions & 0 deletions sdk/internal/mock/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Comment thread
varkey98 marked this conversation as resolved.
i := 0
for key, value := range l.attrs {
attributes[i] = sdk.Attribute{Key: key, Value: value}
Comment thread
varkey98 marked this conversation as resolved.
i++
}
return attributes
}

var _ sdk.Span = &Span{}

type Span struct {
Expand Down
6 changes: 6 additions & 0 deletions sdk/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down