Skip to content

Commit

Permalink
Add minimal support for array value type
Browse files Browse the repository at this point in the history
Array value type was added to AnyValue Protobuf definition but corresponding
support in the Collector was missing.

This commit adds AnyValueArray internal data type which wraps an a slice of
AnyValue pointers. This is the same approach as we previously used for maps.

AnyValueArray supports minimal set of functions for now. We will add more
functions if needed in the future.

AnyValueArray is implemented in a separate go file, instead of common.go.
In a subsequent PR I will refactor AttributeMap to a separate file too to
avoid overloading common.go.
  • Loading branch information
Tigran Najaryan committed Aug 11, 2020
1 parent d051556 commit 1d88ecd
Show file tree
Hide file tree
Showing 7 changed files with 712 additions and 17 deletions.
75 changes: 64 additions & 11 deletions consumer/pdata/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const (
AttributeValueDOUBLE
AttributeValueBOOL
AttributeValueMAP
// TODO: add ARRAY value types.
AttributeValueARRAY
)

func (avt AttributeValueType) String() string {
Expand All @@ -59,8 +59,9 @@ func (avt AttributeValueType) String() string {
return "DOUBLE"
case AttributeValueMAP:
return "MAP"
case AttributeValueARRAY:
return "ARRAY"
}
// TODO: add cases for ARRAY value types.
return ""
}

Expand Down Expand Up @@ -124,12 +125,18 @@ func NewAttributeValueBool(v bool) AttributeValue {
return AttributeValue{orig: &orig}
}

// NewAttributeValueMap creates a new AttributeValue of array type.
// NewAttributeValueMap creates a new AttributeValue of map type.
func NewAttributeValueMap() AttributeValue {
orig := &otlpcommon.AnyValue{Value: &otlpcommon.AnyValue_KvlistValue{KvlistValue: &otlpcommon.KeyValueList{}}}
return AttributeValue{orig: &orig}
}

// NewAttributeValueArray creates a new AttributeValue of array type.
func NewAttributeValueArray() AttributeValue {
orig := &otlpcommon.AnyValue{Value: &otlpcommon.AnyValue_ArrayValue{ArrayValue: &otlpcommon.ArrayValue{}}}
return AttributeValue{orig: &orig}
}

// NewAttributeValueSlice creates a slice of attributes values that are correctly initialized.
func NewAttributeValueSlice(len int) []AttributeValue {
// Allocate 3 slices, one for AttributeValues, another for underlying OTLP structs
Expand Down Expand Up @@ -171,40 +178,41 @@ func (a AttributeValue) Type() AttributeValueType {
return AttributeValueDOUBLE
case *otlpcommon.AnyValue_KvlistValue:
return AttributeValueMAP
case *otlpcommon.AnyValue_ArrayValue:
return AttributeValueARRAY
}
// TODO: add cases for ARRAY value types.
return AttributeValueNULL
}

// Value returns the string value associated with this AttributeValue.
// StringVal returns the string value associated with this AttributeValue.
// If the Type() is not AttributeValueSTRING then returns empty string.
// Calling this function on zero-initialized AttributeValue will cause a panic.
func (a AttributeValue) StringVal() string {
return (*a.orig).GetStringValue()
}

// Value returns the int64 value associated with this AttributeValue.
// IntVal returns the int64 value associated with this AttributeValue.
// If the Type() is not AttributeValueINT then returns int64(0).
// Calling this function on zero-initialized AttributeValue will cause a panic.
func (a AttributeValue) IntVal() int64 {
return (*a.orig).GetIntValue()
}

// Value returns the float64 value associated with this AttributeValue.
// DoubleVal returns the float64 value associated with this AttributeValue.
// If the Type() is not AttributeValueDOUBLE then returns float64(0).
// Calling this function on zero-initialized AttributeValue will cause a panic.
func (a AttributeValue) DoubleVal() float64 {
return (*a.orig).GetDoubleValue()
}

// Value returns the bool value associated with this AttributeValue.
// BoolVal returns the bool value associated with this AttributeValue.
// If the Type() is not AttributeValueBOOL then returns false.
// Calling this function on zero-initialized AttributeValue will cause a panic.
func (a AttributeValue) BoolVal() bool {
return (*a.orig).GetBoolValue()
}

// Value returns the map value associated with this AttributeValue.
// MapVal returns the map value associated with this AttributeValue.
// If the Type() is not AttributeValueMAP then returns an empty map. Note that modifying
// such empty map has no effect on this AttributeValue.
//
Expand All @@ -217,6 +225,19 @@ func (a AttributeValue) MapVal() AttributeMap {
return newAttributeMap(&kvlist.Values)
}

// ArrayVal returns the array value associated with this AttributeValue.
// If the Type() is not AttributeValueARRAY then returns an empty array. Note that modifying
// such empty array has no effect on this AttributeValue.
//
// Calling this function on zero-initialized AttributeValue will cause a panic.
func (a AttributeValue) ArrayVal() AnyValueArray {
arr := (*a.orig).GetArrayValue()
if arr == nil {
return NewAnyValueArray()
}
return newAnyValueArray(&arr.Values)
}

// SetStringVal replaces the string value associated with this AttributeValue,
// it also changes the type to be AttributeValueSTRING.
// Calling this function on zero-initialized AttributeValue will cause a panic.
Expand Down Expand Up @@ -284,6 +305,32 @@ func (a AttributeValue) SetMapVal(m AttributeMap) {
destMap.InitFromAttributeMap(m)
}

// SetArrayVal replaces the value associated with this AttributeValue,
// it also changes the type to be AttributeValueARRAY. The `arr` argument will be deep
// copied into this AttributeValue.
//
// Calling this function on zero-initialized AttributeValue will cause a panic.
func (a AttributeValue) SetArrayVal(arr AnyValueArray) {
if *a.orig == nil {
*a.orig = &otlpcommon.AnyValue{}
}
var dest *otlpcommon.ArrayValue
switch v := (*a.orig).Value.(type) {
case *otlpcommon.AnyValue_ArrayValue:
if v.ArrayValue == nil {
v.ArrayValue = &otlpcommon.ArrayValue{}
}
dest = v.ArrayValue

default:
dest = &otlpcommon.ArrayValue{}
(*a.orig).Value = &otlpcommon.AnyValue_ArrayValue{ArrayValue: dest}
}

destMap := newAnyValueArray(&dest.Values)
destMap.InitFromAnyValueArray(arr)
}

// copyTo copies the value to AnyValue. Will panic if dest is nil.
// Calling this function on zero-initialized AttributeValue will cause a panic.
func (a AttributeValue) copyTo(dest *otlpcommon.AnyValue) {
Expand All @@ -302,7 +349,13 @@ func (a AttributeValue) copyTo(dest *otlpcommon.AnyValue) {
AttributeValue{&dest}.SetMapVal(newAttributeMap(&v.KvlistValue.Values))
}
case *otlpcommon.AnyValue_ArrayValue:
// TODO: handle ARRAY data type. We need to make a deep copy.
if v.ArrayValue == nil {
// Source is empty.
AttributeValue{&dest}.SetArrayVal(NewAnyValueArray())
} else {
// Deep copy to dest.
AttributeValue{&dest}.SetArrayVal(newAnyValueArray(&v.ArrayValue.Values))
}
default:
// Primitive immutable type, no need for deep copy.
dest.Value = (*a.orig).Value
Expand Down Expand Up @@ -660,7 +713,7 @@ func (am AttributeMap) Len() int {
//
// Example:
//
// it := sm.ForEach(func(k string, v StringValue) {
// it := sm.ForEach(func(k string, v AttributeValue) {
// ...
// })
func (am AttributeMap) ForEach(f func(k string, v AttributeValue)) {
Expand Down
10 changes: 6 additions & 4 deletions consumer/pdata/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,10 @@ func fromVal(v interface{}) AttributeValue {
return NewAttributeValueDouble(val)
case map[string]interface{}:
return fromMap(val)
case []interface{}:
return fromArray(val)
}
return NewAttributeValueNull()
panic("data type is not supported in fromVal()")
}

func fromMap(v map[string]interface{}) AttributeValue {
Expand All @@ -88,7 +90,7 @@ func fromMap(v map[string]interface{}) AttributeValue {
return av
}

func fromJSON(jsonStr string) AttributeValue {
func fromJSONMap(jsonStr string) AttributeValue {
var src map[string]interface{}
err := json.Unmarshal([]byte(jsonStr), &src)
if err != nil {
Expand All @@ -98,12 +100,12 @@ func fromJSON(jsonStr string) AttributeValue {
}

func assertMapJSON(t *testing.T, expectedJSON string, actualMap AttributeValue) {
assert.EqualValues(t, fromJSON(expectedJSON).MapVal(), actualMap.MapVal().Sort())
assert.EqualValues(t, fromJSONMap(expectedJSON).MapVal(), actualMap.MapVal().Sort())
}

func TestAttributeValueMap(t *testing.T) {
m1 := NewAttributeValueMap()
assert.EqualValues(t, fromJSON(`{}`), m1)
assert.EqualValues(t, fromJSONMap(`{}`), m1)
assert.EqualValues(t, AttributeValueMAP, m1.Type())
assert.EqualValues(t, NewAttributeMap(), m1.MapVal())
assert.EqualValues(t, 0, m1.MapVal().Len())
Expand Down
174 changes: 174 additions & 0 deletions consumer/pdata/value_array.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package pdata

// This file contains implementation of array value type.

import (
otlpcommon "go.opentelemetry.io/collector/internal/data/opentelemetry-proto-gen/common/v1"
)

// AnyValueArray stores an array of values.
type AnyValueArray struct {
orig *[]*otlpcommon.AnyValue
}

// NewAnyValueArray creates a AnyValueArray with 0 elements.
func NewAnyValueArray() AnyValueArray {
orig := []*otlpcommon.AnyValue(nil)
return AnyValueArray{&orig}
}

func newAnyValueArray(orig *[]*otlpcommon.AnyValue) AnyValueArray {
return AnyValueArray{orig}
}

// InitFromSlice overwrites the entire AnyValueArray and reconstructs the AnyValueArray
// with values from the given AttributeBalue slice.
//
// Returns the same instance to allow nicer code like:
// assert.EqualValues(t, NewAnyValueArray().InitFromSlice([]AttributeValue{...}), actual)
func (am AnyValueArray) InitFromSlice(values []AttributeValue) AnyValueArray {
if len(values) == 0 {
*am.orig = []*otlpcommon.AnyValue(nil)
return am
}
origs := make([]otlpcommon.AnyValue, len(values))
wrappers := make([]*otlpcommon.AnyValue, len(values))
for ix, v := range values {
wrappers[ix] = &origs[ix]
v.copyTo(&origs[ix])
}
*am.orig = wrappers
return am
}

// InitFromAnyValueArray overwrites the entire AnyValueArray and reconstructs the AnyValueArray
// with values from the given AnyValueArray.
//
// Returns the same instance to allow nicer code like:
// assert.EqualValues(t, NewAnyValueArray().InitFromAnyValueArray([]AttributeValue{...}), actual)
func (am AnyValueArray) InitFromAnyValueArray(values AnyValueArray) AnyValueArray {
srcLen := values.Len()
if srcLen == 0 || values.orig == nil {
*am.orig = []*otlpcommon.AnyValue(nil)
return am
}
origs := make([]otlpcommon.AnyValue, srcLen)
wrappers := make([]*otlpcommon.AnyValue, srcLen)
for ix := range *values.orig {
wrappers[ix] = &origs[ix]
AttributeValue{&(*values.orig)[ix]}.copyTo(&origs[ix])
}
*am.orig = wrappers
return am
}

// InitEmptyWithCapacity constructs an empty AnyValueArray with predefined slice capacity.
func (am AnyValueArray) InitEmptyWithCapacity(cap int) {
if cap == 0 {
*am.orig = []*otlpcommon.AnyValue(nil)
}
*am.orig = make([]*otlpcommon.AnyValue, 0, cap)
}

// InitLen constructs an AnyValueArray with predefined length. All elements of the array
// will be empty values.
func (am AnyValueArray) InitLen(len int) {
if len == 0 {
*am.orig = []*otlpcommon.AnyValue(nil)
}

origs := make([]otlpcommon.AnyValue, len)
wrappers := make([]*otlpcommon.AnyValue, len)
for ix := range origs {
wrappers[ix] = &origs[ix]
}
*am.orig = wrappers
}

// GetAt returns the AttributeValue at specified index.
//
// Calling this function with a zero-initialized AnyValueArray struct will cause a panic.
// Will cause a panic if index is out of bounds.
func (am AnyValueArray) GetAt(index int) AttributeValue {
return AttributeValue{&(*am.orig)[index]}
}

// SetAt sets the value at the specified index.
//
// Calling this function with a zero-initialized AnyValueArray struct will cause a panic.
// Will cause a panic if index is out of bounds.
func (am AnyValueArray) SetAt(index int, v AttributeValue) {
v.copyTo((*am.orig)[index])
}

// Append a value at the end of the array.
//
// Calling this function with a zero-initialized AnyValueArray struct will cause a panic.
//
// Important: instead of Append it is preferable to use InitLen followed by SetAt calls
// to minimize the number of allocations.
func (am AnyValueArray) Append(v AttributeValue) {
*am.orig = append(*am.orig, &otlpcommon.AnyValue{})
v.copyTo((*am.orig)[len(*am.orig)-1])
}

// Len returns the length of this array.
func (am AnyValueArray) Len() int {
return len(*am.orig)
}

// ForEach iterates over the every elements in the array by calling the provided func.
//
// Example:
//
// it := sm.ForEach(func(index int, v AttributeValue) {
// ...
// })
func (am AnyValueArray) ForEach(f func(index int, v AttributeValue)) {
for i := range *am.orig {
f(i, AttributeValue{&(*am.orig)[i]})
}
}

// CopyTo copies all elements from the current array to the dest.
func (am AnyValueArray) CopyTo(dest AnyValueArray) {
newLen := len(*am.orig)
if newLen == 0 {
*dest.orig = []*otlpcommon.AnyValue(nil)
return
}

oldLen := len(*dest.orig)
if newLen <= oldLen {
// New slice fits in existing slice, no need to reallocate.
*dest.orig = (*dest.orig)[:newLen]
for i := range *am.orig {
destAkv := (*dest.orig)[i]
AttributeValue{&(*am.orig)[i]}.copyTo(destAkv)
}
return
}

// New slice is bigger than exist slice. Allocate new space.
origs := make([]otlpcommon.AnyValue, len(*am.orig))
wrappers := make([]*otlpcommon.AnyValue, len(*am.orig))
for i := range *am.orig {
wrappers[i] = &origs[i]
AttributeValue{&(*am.orig)[i]}.copyTo(&origs[i])
}
*dest.orig = wrappers
}
Loading

0 comments on commit 1d88ecd

Please sign in to comment.