Skip to content

Commit

Permalink
Remove otel serialization code from objmodel
Browse files Browse the repository at this point in the history
  • Loading branch information
felixbarny committed Jan 11, 2025
1 parent cd16343 commit d150493
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 46 deletions.
48 changes: 13 additions & 35 deletions exporter/elasticsearchexporter/internal/objmodel/objmodel.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,19 +277,19 @@ func newJSONVisitor(w io.Writer) *json.Visitor {
// Serialize writes the document to the given writer. The serializer will create nested objects if dedot is true.
//
// NOTE: The documented MUST be sorted if dedot is true.
func (doc *Document) Serialize(w io.Writer, dedot bool, otel bool) error {
func (doc *Document) Serialize(w io.Writer, dedot bool) error {
v := newJSONVisitor(w)
return doc.iterJSON(v, dedot, otel)
return doc.iterJSON(v, dedot)
}

func (doc *Document) iterJSON(v *json.Visitor, dedot bool, otel bool) error {
func (doc *Document) iterJSON(v *json.Visitor, dedot bool) error {
if dedot {
return doc.iterJSONDedot(v, otel)
return doc.iterJSONDedot(v)
}
return doc.iterJSONFlat(v, otel)
return doc.iterJSONFlat(v)
}

func (doc *Document) iterJSONFlat(w *json.Visitor, otel bool) error {
func (doc *Document) iterJSONFlat(w *json.Visitor) error {
err := w.OnObjectStart(-1, structform.AnyType)
if err != nil {
return err
Expand All @@ -308,28 +308,15 @@ func (doc *Document) iterJSONFlat(w *json.Visitor, otel bool) error {
return err
}

if err := fld.value.iterJSON(w, true, otel); err != nil {
if err := fld.value.iterJSON(w, true); err != nil {
return err
}
}

return nil
}

// Under OTel mode, set of key prefixes where keys should be flattened from that level,
// such that a document (root or not) with fields {"attributes.a.b": 1} will be serialized as {"attributes": {"a.b": 1}}
// It is not aware of whether it is a root document or sub-document.
// NOTE: This works very delicately with the implementation of OTel mode that
// e.g. resource.attributes is a "resource" objmodel.Document under the root document that contains attributes
// added using AddAttributes func as flattened keys.
// Therefore, there will be correctness issues when attributes are added / used in other ways, but it is working
// for current use cases and the proper fix will be slightly too complex. YAGNI.
var otelPrefixSet = map[string]struct{}{
"attributes.": {},
"metrics.": {},
}

func (doc *Document) iterJSONDedot(w *json.Visitor, otel bool) error {
func (doc *Document) iterJSONDedot(w *json.Visitor) error {
objPrefix := ""
level := 0

Expand Down Expand Up @@ -381,15 +368,6 @@ func (doc *Document) iterJSONDedot(w *json.Visitor, otel bool) error {

// increase object level up to current field
for {
// Otel mode serialization
if otel {
// Check the prefix
_, isOtelPrefix := otelPrefixSet[objPrefix]
if isOtelPrefix {
break
}
}

start := len(objPrefix)
idx := strings.IndexByte(key[start:], '.')
if idx < 0 {
Expand All @@ -412,7 +390,7 @@ func (doc *Document) iterJSONDedot(w *json.Visitor, otel bool) error {
if err := w.OnKey(fieldName); err != nil {
return err
}
if err := fld.value.iterJSON(w, true, otel); err != nil {
if err := fld.value.iterJSON(w, true); err != nil {
return err
}
}
Expand Down Expand Up @@ -524,7 +502,7 @@ func (v *Value) IsEmpty() bool {
}
}

func (v *Value) iterJSON(w *json.Visitor, dedot bool, otel bool) error {
func (v *Value) iterJSON(w *json.Visitor, dedot bool) error {
switch v.kind {
case KindNil:
return w.OnNil()
Expand All @@ -549,18 +527,18 @@ func (v *Value) iterJSON(w *json.Visitor, dedot bool, otel bool) error {
if len(v.doc.fields) == 0 {
return w.OnNil()
}
return v.doc.iterJSON(w, dedot, otel)
return v.doc.iterJSON(w, dedot)
case KindUnflattenableObject:
if len(v.doc.fields) == 0 {
return w.OnNil()
}
return v.doc.iterJSON(w, true, otel)
return v.doc.iterJSON(w, true)
case KindArr:
if err := w.OnArrayStart(-1, structform.AnyType); err != nil {
return err
}
for i := range v.arr {
if err := v.arr[i].iterJSON(w, dedot, otel); err != nil {
if err := v.arr[i].iterJSON(w, dedot); err != nil {
return err
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ func TestDocument_Serialize_Flat(t *testing.T) {
assert.NoError(t, m.FromRaw(test.attrs))
doc := DocumentFromAttributes(m)
doc.Dedup(true)
err := doc.Serialize(&buf, false, false)
err := doc.Serialize(&buf, false)
require.NoError(t, err)

assert.Equal(t, test.want, buf.String())
Expand Down Expand Up @@ -362,7 +362,7 @@ func TestDocument_Serialize_Dedot(t *testing.T) {
assert.NoError(t, m.FromRaw(test.attrs))
doc := DocumentFromAttributes(m)
doc.Dedup(true)
err := doc.Serialize(&buf, true, false)
err := doc.Serialize(&buf, true)
require.NoError(t, err)

assert.Equal(t, test.want, buf.String())
Expand Down Expand Up @@ -410,7 +410,7 @@ func TestValue_Serialize(t *testing.T) {
for name, test := range tests {
t.Run(name, func(t *testing.T) {
var buf strings.Builder
err := test.value.iterJSON(newJSONVisitor(&buf), false, false)
err := test.value.iterJSON(newJSONVisitor(&buf), false)
require.NoError(t, err)
assert.Equal(t, test.want, buf.String())
})
Expand Down
6 changes: 3 additions & 3 deletions exporter/elasticsearchexporter/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func (m *encodeModel) encodeLog(resource pcommon.Resource, resourceSchemaURL str
}
document.Dedup(true)

return document.Serialize(buf, m.dedot, false)
return document.Serialize(buf, m.dedot)
}

func (m *encodeModel) encodeLogDefaultMode(resource pcommon.Resource, record plog.LogRecord, scope pcommon.InstrumentationScope) objmodel.Document {
Expand Down Expand Up @@ -205,7 +205,7 @@ func (m *encodeModel) encodeLogECSMode(resource pcommon.Resource, record plog.Lo
func (m *encodeModel) encodeDocument(document objmodel.Document, buf *bytes.Buffer) error {
document.Dedup(true)

err := document.Serialize(buf, m.dedot, false)
err := document.Serialize(buf, m.dedot)
if err != nil {
return err
}
Expand Down Expand Up @@ -493,7 +493,7 @@ func (m *encodeModel) encodeSpan(resource pcommon.Resource, resourceSchemaURL st
document = m.encodeSpanDefaultMode(resource, span, scope)
}
document.Dedup(true)
err := document.Serialize(buf, m.dedot, false)
err := document.Serialize(buf, m.dedot)
return err
}

Expand Down
10 changes: 5 additions & 5 deletions exporter/elasticsearchexporter/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ func TestEncodeLogECSMode(t *testing.T) {
var buf bytes.Buffer
m := encodeModel{}
doc := m.encodeLogECSMode(resource, record, scope)
require.NoError(t, doc.Serialize(&buf, false, false))
require.NoError(t, doc.Serialize(&buf, false))

require.JSONEq(t, `{
"@timestamp": "2024-03-12T20:00:41.123456789Z",
Expand Down Expand Up @@ -550,7 +550,7 @@ func TestEncodeLogECSModeAgentName(t *testing.T) {
var buf bytes.Buffer
m := encodeModel{}
doc := m.encodeLogECSMode(resource, record, scope)
require.NoError(t, doc.Serialize(&buf, false, false))
require.NoError(t, doc.Serialize(&buf, false))
require.JSONEq(t, fmt.Sprintf(`{
"@timestamp": "2024-03-13T23:50:59.123456789Z",
"agent.name": %q
Expand Down Expand Up @@ -602,7 +602,7 @@ func TestEncodeLogECSModeAgentVersion(t *testing.T) {
var buf bytes.Buffer
m := encodeModel{}
doc := m.encodeLogECSMode(resource, record, scope)
require.NoError(t, doc.Serialize(&buf, false, false))
require.NoError(t, doc.Serialize(&buf, false))

if test.expectedAgentVersion == "" {
require.JSONEq(t, `{
Expand Down Expand Up @@ -709,7 +709,7 @@ func TestEncodeLogECSModeHostOSType(t *testing.T) {
var buf bytes.Buffer
m := encodeModel{}
doc := m.encodeLogECSMode(resource, record, scope)
require.NoError(t, doc.Serialize(&buf, false, false))
require.NoError(t, doc.Serialize(&buf, false))

expectedJSON := `{"@timestamp":"2024-03-13T23:50:59.123456789Z", "agent.name":"otlp"`
if test.expectedHostOsName != "" {
Expand Down Expand Up @@ -760,7 +760,7 @@ func TestEncodeLogECSModeTimestamps(t *testing.T) {
var buf bytes.Buffer
m := encodeModel{}
doc := m.encodeLogECSMode(resource, record, scope)
require.NoError(t, doc.Serialize(&buf, false, false))
require.NoError(t, doc.Serialize(&buf, false))

require.JSONEq(t, fmt.Sprintf(
`{"@timestamp":%q,"agent.name":"otlp"}`, test.expectedTimestamp,
Expand Down

0 comments on commit d150493

Please sign in to comment.