Skip to content

Commit

Permalink
Add array serialization to logging_exporter (#1994)
Browse files Browse the repository at this point in the history
Bug fix - adds serialization of array attributes, including nested arrays, in the `logging_exporter`. This allows them to be printed like the primitive attribute types when debugging. AFAICT, there aren't any attributes with array values today, but they are permitted types in the [spec](https://github.com/open-telemetry/opentelemetry-specification/blob/master/semantic_conventions/syntax.md#attributes), and I will be contributing a resource detector that uses array attributes in the future.
  • Loading branch information
willarmiros committed Oct 27, 2020
1 parent 5db4935 commit c6c96f7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
17 changes: 17 additions & 0 deletions exporter/loggingexporter/logging_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,11 +278,28 @@ func attributeValueToString(av pdata.AttributeValue) string {
return strconv.FormatFloat(av.DoubleVal(), 'f', -1, 64)
case pdata.AttributeValueINT:
return strconv.FormatInt(av.IntVal(), 10)
case pdata.AttributeValueARRAY:
return attributeValueArrayToString(av.ArrayVal())
default:
return fmt.Sprintf("<Unknown OpenTelemetry attribute value type %q>", av.Type())
}
}

func attributeValueArrayToString(av pdata.AnyValueArray) string {
var b strings.Builder
b.WriteByte('[')
for i := 0; i < av.Len(); i++ {
if i < av.Len()-1 {
fmt.Fprintf(&b, "%s, ", attributeValueToString(av.At(i)))
} else {
b.WriteString(attributeValueToString(av.At(i)))
}
}

b.WriteByte(']')
return b.String()
}

type loggingExporter struct {
logger *zap.Logger
debug bool
Expand Down
19 changes: 19 additions & 0 deletions exporter/loggingexporter/logging_exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"go.uber.org/zap"

"go.opentelemetry.io/collector/config/configmodels"
"go.opentelemetry.io/collector/consumer/pdata"
"go.opentelemetry.io/collector/internal/data/testdata"
)

Expand Down Expand Up @@ -69,3 +70,21 @@ func TestLoggingLogsExporterNoErrors(t *testing.T) {

assert.NoError(t, lle.Shutdown(context.Background()))
}

func TestNestedArraySerializesCorrectly(t *testing.T) {
av := pdata.NewAnyValueArray()
ava := pdata.NewAttributeValueArray()
av.Append(pdata.NewAttributeValueString("foo"))
av.Append(pdata.NewAttributeValueInt(42))

av2 := pdata.NewAnyValueArray()
av2.Append(pdata.NewAttributeValueString("bar"))
ava2 := pdata.NewAttributeValueArray()
ava2.SetArrayVal(av2)

av.Append(ava2)
ava.SetArrayVal(av)

assert.Equal(t, 3, ava.ArrayVal().Len())
assert.Equal(t, "[foo, 42, [bar]]", attributeValueToString(ava))
}

0 comments on commit c6c96f7

Please sign in to comment.