Skip to content

Commit

Permalink
adding generic attributes interface (#217)
Browse files Browse the repository at this point in the history
* adding generic attributes interface

* adding unit test

* review changes

* review changes

* review changes

---------

Co-authored-by: Varkeychan Jacob <varkeychanjacob@Varkeychans-MacBook-Pro-2631.local>
  • Loading branch information
varkey98 and Varkeychan Jacob authored Nov 7, 2023
1 parent bf8e5c3 commit ea08a0b
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
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 {
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))
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))
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 {
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

0 comments on commit ea08a0b

Please sign in to comment.