Skip to content

Commit

Permalink
Faster IsComplex()
Browse files Browse the repository at this point in the history
  • Loading branch information
tigrannajaryan committed Jan 12, 2024
1 parent 6db03c0 commit f81a5ce
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 10 deletions.
65 changes: 62 additions & 3 deletions attribute/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,11 @@ func (l *Set) Value(k Key) (Value, bool) {
rValue := l.equivalent.reflectValue()
vlen := rValue.Len()

idx := sort.Search(vlen, func(idx int) bool {
return rValue.Index(idx).Interface().(KeyValue).Key >= k
})
idx := sort.Search(
vlen, func(idx int) bool {
return rValue.Index(idx).Interface().(KeyValue).Key >= k
},
)
if idx >= vlen {
return Value{}, false
}
Expand Down Expand Up @@ -175,6 +177,63 @@ func (l *Set) Encoded(encoder Encoder) string {
return encoder.Encode(l.Iter())
}

func (l *Set) IsComplex() bool {
value := l.equivalent.reflectValue()
len := value.Len()
iface := l.equivalent.iface

var slice []KeyValue
switch len {
case 1:
arr := iface.([1]KeyValue)
slice = arr[:]
case 2:
arr := iface.([2]KeyValue)
slice = arr[:]
case 3:
arr := iface.([3]KeyValue)
slice = arr[:]
case 4:
arr := iface.([4]KeyValue)
slice = arr[:]
case 5:
arr := iface.([5]KeyValue)
slice = arr[:]
case 6:
arr := iface.([6]KeyValue)
slice = arr[:]
case 7:
arr := iface.([7]KeyValue)
slice = arr[:]
case 8:
arr := iface.([8]KeyValue)
slice = arr[:]
case 9:
arr := iface.([9]KeyValue)
slice = arr[:]
case 10:
arr := iface.([10]KeyValue)
slice = arr[:]
default:
for i := 0; i < len; i++ {
elem := value.Index(i).Interface().(KeyValue)
switch t := elem.Value.Type(); t {
case SLICE, MAP:
return true
}
}
return false
}
for i := 0; i < len; i++ {
elem := slice[i]
switch t := elem.Value.Type(); t {
case SLICE, MAP:
return true
}
}
return false
}

func empty() Set {
return Set{
equivalent: emptySet.equivalent,
Expand Down
19 changes: 12 additions & 7 deletions sdk/metric/instrument.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,17 +208,22 @@ func (i *int64Inst) Record(ctx context.Context, val int64, opts ...metric.Record
var errUnsupportedAttr = errors.New("unsupported attribute value type")

func checkAttr(set attribute.Set) error {
iter := set.Iter()
for iter.Next() {
switch t := iter.Attribute().Value.Type(); t {
case attribute.SLICE, attribute.MAP:
return fmt.Errorf("%v: %s", errUnsupportedAttr, t)
}
//iter := set.Iter()
//for iter.Next() {
// switch t := iter.Attribute().Value.Type(); t {
// case attribute.SLICE, attribute.MAP:
// return fmt.Errorf("%v: %s", errUnsupportedAttr, t)
// }
//}
if set.IsComplex() {
return errUnsupportedAttr
}
return nil
}

func (i *int64Inst) aggregate(ctx context.Context, val int64, s attribute.Set) { // nolint:revive // okay to shadow pkg with method.
func (i *int64Inst) aggregate(
ctx context.Context, val int64, s attribute.Set,
) { // nolint:revive // okay to shadow pkg with method.
if err := checkAttr(s); err != nil {
global.Error(err, "dropping measurement", "context", ctx, "value", val)
return
Expand Down

0 comments on commit f81a5ce

Please sign in to comment.