Skip to content

Fix stats in slow logs to be a escaped JSON #44642

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 1 commit into from
Jul 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@

package org.elasticsearch.common.logging;

import com.fasterxml.jackson.core.io.JsonStringEncoder;
import org.apache.logging.log4j.message.ParameterizedMessage;
import org.elasticsearch.common.SuppressLoggerChecks;

import java.nio.charset.Charset;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand All @@ -30,6 +32,7 @@
* A base class for custom log4j logger messages. Carries additional fields which will populate JSON fields in logs.
*/
public abstract class ESLogMessage extends ParameterizedMessage {
private static final JsonStringEncoder JSON_STRING_ENCODER = JsonStringEncoder.getInstance();
private final Map<String, Object> fields;

/**
Expand All @@ -42,6 +45,11 @@ public ESLogMessage(Map<String, Object> fields, String messagePattern, Object...
this.fields = fields;
}

public static String escapeJson(String text) {
byte[] sourceEscaped = JSON_STRING_ENCODER.quoteAsUTF8(text);
return new String(sourceEscaped, Charset.defaultCharset());
}

public String getValueFor(String key) {
Object value = fields.get(key);
return value!=null ? value.toString() : null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

package org.elasticsearch.index;

import com.fasterxml.jackson.core.io.JsonStringEncoder;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.common.Strings;
Expand All @@ -33,7 +32,6 @@
import org.elasticsearch.search.internal.SearchContext;
import org.elasticsearch.tasks.Task;

import java.nio.charset.Charset;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -170,14 +168,13 @@ private static Map<String, Object> prepareMap(SearchContext context, long tookIn
} else {
messageFields.put("total_hits", "-1");
}
messageFields.put("stats", asJsonArray(context.groupStats() != null ? context.groupStats().stream() : Stream.empty()));
messageFields.put("stats", escapeJson(asJsonArray(
context.groupStats() != null ? context.groupStats().stream() : Stream.empty())));
messageFields.put("search_type", context.searchType());
messageFields.put("total_shards", context.numberOfShards());

if (context.request().source() != null) {
byte[] sourceEscaped = JsonStringEncoder.getInstance()
.quoteAsUTF8(context.request().source().toString(FORMAT_PARAMS));
String source = new String(sourceEscaped, Charset.defaultCharset());
String source = escapeJson(context.request().source().toString(FORMAT_PARAMS));

messageFields.put("source", source);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@
import org.hamcrest.Matchers;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.endsWith;
Expand All @@ -55,6 +57,9 @@
public class SearchSlowLogTests extends ESSingleNodeTestCase {
@Override
protected SearchContext createSearchContext(IndexService indexService) {
return createSearchContext(indexService, new String[]{});
}
protected SearchContext createSearchContext(IndexService indexService, String ... groupStats) {
BigArrays bigArrays = indexService.getBigArrays();
ThreadPool threadPool = indexService.getThreadPool();
return new TestSearchContext(bigArrays, indexService) {
Expand Down Expand Up @@ -145,6 +150,12 @@ public String getClusterAlias() {
return null;
}
};

@Override
public List<String> groupStats() {
return Arrays.asList(groupStats);
}

@Override
public ShardSearchRequest request() {
return request;
Expand All @@ -171,6 +182,26 @@ public void testSlowLogHasJsonFields() throws IOException {
assertThat(p.getValueFor("source"), equalTo("{\\\"query\\\":{\\\"match_all\\\":{\\\"boost\\\":1.0}}}"));
}

public void testSlowLogsWithStats() throws IOException {
IndexService index = createIndex("foo");
SearchContext searchContext = createSearchContext(index,"group1");
SearchSourceBuilder source = SearchSourceBuilder.searchSource().query(QueryBuilders.matchAllQuery());
searchContext.request().source(source);
searchContext.setTask(new SearchTask(0, "n/a", "n/a", "test", null,
Collections.singletonMap(Task.X_OPAQUE_ID, "my_id")));

SearchSlowLog.SearchSlowLogMessage p = new SearchSlowLog.SearchSlowLogMessage(searchContext, 10);
assertThat(p.getValueFor("stats"), equalTo("[\\\"group1\\\"]"));

searchContext = createSearchContext(index, "group1", "group2");
source = SearchSourceBuilder.searchSource().query(QueryBuilders.matchAllQuery());
searchContext.request().source(source);
searchContext.setTask(new SearchTask(0, "n/a", "n/a", "test", null,
Collections.singletonMap(Task.X_OPAQUE_ID, "my_id")));
p = new SearchSlowLog.SearchSlowLogMessage(searchContext, 10);
assertThat(p.getValueFor("stats"), equalTo("[\\\"group1\\\", \\\"group2\\\"]"));
}

public void testSlowLogSearchContextPrinterToLog() throws IOException {
IndexService index = createIndex("foo");
SearchContext searchContext = createSearchContext(index);
Expand Down