diff --git a/exporter/elasticsearchexporter/internal/objmodel/objmodel.go b/exporter/elasticsearchexporter/internal/objmodel/objmodel.go index 0f514e06aaaa..25c99b19088a 100644 --- a/exporter/elasticsearchexporter/internal/objmodel/objmodel.go +++ b/exporter/elasticsearchexporter/internal/objmodel/objmodel.go @@ -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 @@ -308,7 +308,7 @@ 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 } } @@ -316,20 +316,7 @@ func (doc *Document) iterJSONFlat(w *json.Visitor, otel bool) error { 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 @@ -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 { @@ -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 } } @@ -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() @@ -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 } } diff --git a/exporter/elasticsearchexporter/internal/objmodel/objmodel_test.go b/exporter/elasticsearchexporter/internal/objmodel/objmodel_test.go index 6805a958a019..0128120508ec 100644 --- a/exporter/elasticsearchexporter/internal/objmodel/objmodel_test.go +++ b/exporter/elasticsearchexporter/internal/objmodel/objmodel_test.go @@ -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()) @@ -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()) @@ -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()) }) diff --git a/exporter/elasticsearchexporter/model.go b/exporter/elasticsearchexporter/model.go index 56f2a0ff610d..c1ac0ea17701 100644 --- a/exporter/elasticsearchexporter/model.go +++ b/exporter/elasticsearchexporter/model.go @@ -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 { @@ -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 } @@ -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 } diff --git a/exporter/elasticsearchexporter/model_test.go b/exporter/elasticsearchexporter/model_test.go index 0f9b27ba2d2d..d0a202799190 100644 --- a/exporter/elasticsearchexporter/model_test.go +++ b/exporter/elasticsearchexporter/model_test.go @@ -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", @@ -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 @@ -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, `{ @@ -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 != "" { @@ -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,