Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed esexporter serialized document to avoid conflicting keys #33454

Merged
merged 11 commits into from
Jun 11, 2024
Merged
12 changes: 3 additions & 9 deletions exporter/elasticsearchexporter/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,9 @@ func TestExporterLogs(t *testing.T) {
server := newESTestServer(t, func(docs []itemRequest) ([]itemResponse, error) {
rec.Record(docs)

var expectedDoc, actualDoc map[string]any
expected := []byte(`{"attrKey1":"abc","attrKey2":"def","application":"myapp","service":{"name":"myservice"},"error":{"stacktrace":"no no no no"},"agent":{"name":"otlp"},"@timestamp":"1970-01-01T00:00:00.000000000Z","message":"hello world"}`)
err := json.Unmarshal(expected, &expectedDoc)
require.NoError(t, err)

actual := docs[0].Document
err = json.Unmarshal(actual, &actualDoc)
require.NoError(t, err)
assert.Equal(t, expectedDoc, actualDoc)
expected := `{"@timestamp":"1970-01-01T00:00:00.000000000Z","agent":{"name":"otlp"},"application":"myapp","attrKey1":"abc","attrKey2":"def","error":{"stacktrace":"no no no no"},"message":"hello world","service":{"name":"myservice"}}`
actual := string(docs[0].Document)
assert.Equal(t, expected, actual)

return itemsAllOK(docs)
})
Expand Down
11 changes: 5 additions & 6 deletions exporter/elasticsearchexporter/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ func (m *encodeModel) encodeLog(resource pcommon.Resource, record plog.LogRecord
}

var buf bytes.Buffer
if m.dedup {
document.Dedup()
} else if m.dedot {
document.Sort()
}
err := document.Serialize(&buf, m.dedot)
return buf.Bytes(), err
}
Expand All @@ -104,12 +109,6 @@ func (m *encodeModel) encodeLogDefaultMode(resource pcommon.Resource, record plo
document.AddAttributes("Resource", resource.Attributes())
document.AddAttributes("Scope", scopeToAttributes(scope))

if m.dedup {
document.Dedup()
} else if m.dedot {
document.Sort()
}

return document

}
Expand Down
42 changes: 42 additions & 0 deletions exporter/elasticsearchexporter/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,48 @@ func TestEncodeEvents(t *testing.T) {
}
}

func TestEncodeLogECSModeDuplication(t *testing.T) {
resource := pcommon.NewResource()
err := resource.Attributes().FromRaw(map[string]any{
semconv.AttributeServiceName: "foo.bar",
semconv.AttributeHostName: "localhost",
semconv.AttributeServiceVersion: "1.1.0",
semconv.AttributeOSType: "darwin",
semconv.AttributeOSDescription: "Mac OS Mojave",
semconv.AttributeOSName: "Mac OS X",
semconv.AttributeOSVersion: "10.14.1",
})
require.NoError(t, err)

want := `{"@timestamp":"2024-03-12T20:00:41.123456789Z","agent":{"name":"otlp"},"container":{"image":{"tag":["v3.4.0"]}},"event":{"action":"user-password-change"},"host":{"hostname":"localhost","os":{"full":"Mac OS Mojave","name":"Mac OS X","platform":"darwin","type":"macos","version":"10.14.1"}},"service":{"name":"foo.bar","version":"1.1.0"}}`
require.NoError(t, err)

resourceContainerImageTags := resource.Attributes().PutEmptySlice(semconv.AttributeContainerImageTags)
err = resourceContainerImageTags.FromRaw([]any{"v3.4.0"})
require.NoError(t, err)

scope := pcommon.NewInstrumentationScope()

record := plog.NewLogRecord()
err = record.Attributes().FromRaw(map[string]any{
"event.name": "user-password-change",
})
require.NoError(t, err)
observedTimestamp := pcommon.Timestamp(1710273641123456789)
record.SetObservedTimestamp(observedTimestamp)

m := encodeModel{
mode: MappingECS,
dedot: true,
dedup: true,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is this test testing de-duplication? I ran it with setting dedup: false and it still passes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you run it without a fix, resulting document is malformed.
the fix was really in calling Sort or Dedup before Serailize

}
doc, err := m.encodeLog(resource, record, scope)
require.NoError(t, err)

assert.Equal(t, want, string(doc))

}

func TestEncodeLogECSMode(t *testing.T) {
resource := pcommon.NewResource()
err := resource.Attributes().FromRaw(map[string]any{
Expand Down
Loading