Skip to content

Trim the JSON source in indexing slow logs #38081

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

Merged
merged 3 commits into from
Feb 1, 2019

Conversation

pgomulka
Copy link
Contributor

@pgomulka pgomulka commented Jan 31, 2019

The '{' as a first character in log line is causing problems for beats when parsing plaintext logs.
This is used to distinguish if the logs are in JSON or plaintext (they want to keep the logic consistent for docker and standalone deployments, so file extension can not be used).

When the JSON in source element is not reformatted (index.indexing.slowlog.reformat: false) and the user send a document with an additional \n like here:

curl --request PUT \
  --url http://localhost:9200/index1/doc/2 \
  --header 'content-type: application/json' \
  --data '
{
      "network":{
        "name":"lo0",
        "in":{
          "errors":0,
          "dropped":0,
          "bytes":77666873,
          "packets":244595},
          "out":{
            "packets":244595,
            "bytes":77666873,
            "errors":0,
            "dropped":0
          }
        }
      }'

Then the { will endup in a new line

[2018-07-04T21:51:30,411][INFO ][index.indexing.slowlog.index] [v_VJhjV] [metricbeat-6.3.0-2018.07.04/VLKxBLvUSYuIMKzpacGjRg] took[1.7ms], took_millis[1], type[doc], id[s01HZ2QBk9jw4gtgaFtn], routing[], source[
{
  "@timestamp":"2018-07-04T21:27:30.730Z",
  "metricset":{
...

Trimming the source part of a SlowLog will solve the problem and will keep the logs readable.

2018-07-04T21:51:30,411][INFO ][index.indexing.slowlog.index] [v_VJhjV] [metricbeat-6.3.0-2018.07.04/VLKxBLvUSYuIMKzpacGjRg] took[1.7ms], took_millis[1], type[doc], id[s01HZ2QBk9jw4gtgaFtn], routing[], source[{
  "@timestamp":"2018-07-04T21:27:30.730Z",
...
...
}]

This won't happen for Search slow logs, as we repackage the user's request into our own SearchRequest object.
closes #38080

@pgomulka pgomulka self-assigned this Jan 31, 2019
@pgomulka pgomulka changed the title Trim the source to avoid problems when parsing by beats Trim the JSON source in slog logs Jan 31, 2019
@pgomulka pgomulka added the :Core/Infra/Logging Log management and logging utilities label Jan 31, 2019
@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-core-infra

@ycombinator ycombinator changed the title Trim the JSON source in slog logs Trim the JSON source in slow logs Jan 31, 2019
Copy link
Contributor

@alpar-t alpar-t left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Member

@danielmitterdorfer danielmitterdorfer left a comment

Choose a reason for hiding this comment

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

I left a comment. Based on the PR description I also could not grasp the purpose of this. It only became apparent after reading the related issue. Could you please reword the PR description as well so this becomes clearer?

@@ -194,7 +194,7 @@ public String toString() {
}
try {
String source = XContentHelper.convertToJson(doc.source(), reformat, doc.getXContentType());
sb.append(", source[").append(Strings.cleanTruncate(source, maxSourceCharsToLog)).append("]");
sb.append(", source[").append(Strings.cleanTruncate(source, maxSourceCharsToLog).trim()).append("]");
Copy link
Member

Choose a reason for hiding this comment

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

I wonder whether it is possible to create source without any leading line breaks so we can avoid the trim() at all?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

once you mentioned that I started to investigate how this could even happen.. It turns out that only when reformat=false then the code is just logging whatever data user provided.
So the trim is necessary.
Also I confirmed that this can only happen for the IndexingSLowLog. For search slow log we repackage user's request into our own object SearchRequest.

@pgomulka pgomulka changed the title Trim the JSON source in slow logs Trim the JSON source in indexing slow logs Jan 31, 2019
Copy link
Member

@danielmitterdorfer danielmitterdorfer left a comment

Choose a reason for hiding this comment

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

The changes themselves look fine but I think we should not reformat unrelated changes here so I left a bunch of comments around that.

.put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 1)
.put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1)
.put(indexSettings)
.build();
Copy link
Member

Choose a reason for hiding this comment

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

That looks a bit odd to me and also unrelated to this change? Can you please restore the old formatting?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I reverted back to previous formatting. must have done this by accident. sorry

@@ -261,16 +278,16 @@ private void assertTimeValueException(final IllegalArgumentException e, final St
assertThat(e.getCause(), instanceOf(IllegalArgumentException.class));
final IllegalArgumentException cause = (IllegalArgumentException) e.getCause();
final String causeExpected =
"failed to parse setting [" + key + "] with value [NOT A TIME VALUE] as a time value: unit is missing or unrecognized";
"failed to parse setting [" + key + "] with value [NOT A TIME VALUE] as a time value: unit is missing or unrecognized";
Copy link
Member

Choose a reason for hiding this comment

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

That looks unrelated to this change? Can you please restore the old formatting?

@@ -247,7 +264,7 @@ public void testSetLevels() {
try {
settings.updateIndexMetaData(newIndexMeta("index",
Settings.builder().put(IndexingSlowLog.INDEX_INDEXING_SLOWLOG_THRESHOLD_INDEX_WARN_SETTING.getKey(), "NOT A TIME VALUE")
.build()));
.build()));
Copy link
Member

Choose a reason for hiding this comment

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

That looks unrelated to this change? Can you please restore the old formatting?

@@ -238,7 +255,7 @@ public void testSetLevels() {
try {
settings.updateIndexMetaData(newIndexMeta("index",
Settings.builder().put(IndexingSlowLog.INDEX_INDEXING_SLOWLOG_THRESHOLD_INDEX_INFO_SETTING.getKey(), "NOT A TIME VALUE")
.build()));
.build()));
Copy link
Member

Choose a reason for hiding this comment

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

That looks unrelated to this change? Can you please restore the old formatting?

@@ -229,7 +246,7 @@ public void testSetLevels() {
try {
settings.updateIndexMetaData(newIndexMeta("index",
Settings.builder().put(IndexingSlowLog.INDEX_INDEXING_SLOWLOG_THRESHOLD_INDEX_DEBUG_SETTING.getKey(), "NOT A TIME VALUE")
.build()));
.build()));
Copy link
Member

Choose a reason for hiding this comment

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

That looks unrelated to this change? Can you please restore the old formatting?

@@ -106,8 +107,8 @@ public void testReformatSetting() {
assertTrue(log.isReformat());

metaData = newIndexMeta("index", Settings.builder()
.put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT)
.build());
.put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT)
Copy link
Member

Choose a reason for hiding this comment

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

That looks unrelated to this change? Can you please restore the old formatting?

.put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT)
.put(IndexingSlowLog.INDEX_INDEXING_SLOWLOG_REFORMAT_SETTING.getKey(), false)
.build());
.put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT)
Copy link
Member

Choose a reason for hiding this comment

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

That looks unrelated to this change? Can you please restore the old formatting?

@@ -82,15 +83,15 @@ public void testSlowLogParsedDocumentPrinterSourceToLog() throws IOException {
assertNotNull(e.getCause());
assertThat(e.getCause(), instanceOf(JsonParseException.class));
assertThat(e.getCause(), hasToString(containsString("Unrecognized token 'invalid':"
+ " was expecting ('true', 'false' or 'null')\n"
+ " at [Source: org.elasticsearch.common.bytes.BytesReference$MarkSupportingStreamInputWrapper")));
+ " was expecting ('true', 'false' or 'null')\n"
Copy link
Member

Choose a reason for hiding this comment

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

That looks unrelated to this change? Can you please restore the old formatting?

ParsedDocument pd = new ParsedDocument(new NumericDocValuesField("version", 1),
SeqNoFieldMapper.SequenceIDFields.emptySeqID(), "id",
"test", null, null, source, XContentType.JSON, null);
"test", null, null, source, XContentType.JSON, null);
Copy link
Member

Choose a reason for hiding this comment

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

That looks unrelated to this change? Can you please restore the old formatting?

@@ -46,10 +47,10 @@
public class IndexingSlowLogTests extends ESTestCase {
public void testSlowLogParsedDocumentPrinterSourceToLog() throws IOException {
BytesReference source = BytesReference.bytes(JsonXContent.contentBuilder()
.startObject().field("foo", "bar").endObject());
.startObject().field("foo", "bar").endObject());
Copy link
Member

Choose a reason for hiding this comment

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

That looks unrelated to this change? Can you please restore the old formatting?

Copy link
Member

@danielmitterdorfer danielmitterdorfer left a comment

Choose a reason for hiding this comment

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

Thanks for iterating. LGTM

@pgomulka pgomulka merged commit 2758578 into elastic:master Feb 1, 2019
pgomulka added a commit to pgomulka/elasticsearch that referenced this pull request Feb 1, 2019
The '{' as a first character in log line is causing problems for beats when parsing plaintext logs. This can happen if the submitted document has an additional '\n' at the beginning and we are not reformatting. 
Trimming the source part of a SlogLog solves that and keeps the logs readable.

closes elastic#38080
pgomulka added a commit that referenced this pull request Feb 1, 2019
The '{' as a first character in log line is causing problems for beats when parsing plaintext logs. This can happen if the submitted document has an additional '\n' at the beginning and we are not reformatting.
Trimming the source part of a SlogLog solves that and keeps the logs readable.

relates #38080
backport #38081
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Plaintext slow logs with multiline causing problems when parsing
5 participants