Skip to content

Commit a154f49

Browse files
authored
Fix stats in slow logs to be a escaped JSON backport(#44642) #44687
Fields in JSON logs should be an escaped JSON fields. It is a broken json value at the moment "stats": "["group1", "group2"]", -> "stats": "[\"group1\", \"group2\"]", This should later be refactored into a JSON array of strings (the same as types in 7.x)
1 parent 0ce3114 commit a154f49

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

server/src/main/java/org/elasticsearch/common/logging/ESLogMessage.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
*/
3434
public abstract class ESLogMessage extends ParameterizedMessage {
3535
private static final JsonStringEncoder JSON_STRING_ENCODER = JsonStringEncoder.getInstance();
36-
3736
private final Map<String, Object> fields;
3837

3938
/**

server/src/main/java/org/elasticsearch/index/SearchSlowLog.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,8 @@ private static Map<String, Object> prepareMap(SearchContext context, long tookIn
171171
}
172172
String[] types = context.getQueryShardContext().getTypes();
173173
messageFields.put("types", escapeJson(asJsonArray(types != null ? Arrays.stream(types) : Stream.empty())));
174-
messageFields.put("stats", asJsonArray(context.groupStats() != null ? context.groupStats().stream() : Stream.empty()));
174+
messageFields.put("stats", escapeJson(asJsonArray(
175+
context.groupStats() != null ? context.groupStats().stream() : Stream.empty())));
175176
messageFields.put("search_type", context.searchType());
176177
messageFields.put("total_shards", context.numberOfShards());
177178

server/src/test/java/org/elasticsearch/index/SearchSlowLogTests.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@
4242
import org.hamcrest.Matchers;
4343

4444
import java.io.IOException;
45+
import java.util.Arrays;
4546
import java.util.Collections;
47+
import java.util.List;
4648

4749
import static org.hamcrest.Matchers.containsString;
4850
import static org.hamcrest.Matchers.endsWith;
@@ -55,6 +57,9 @@
5557
public class SearchSlowLogTests extends ESSingleNodeTestCase {
5658
@Override
5759
protected SearchContext createSearchContext(IndexService indexService) {
60+
return createSearchContext(indexService, new String[]{});
61+
}
62+
protected SearchContext createSearchContext(IndexService indexService, String ... groupStats) {
5863
BigArrays bigArrays = indexService.getBigArrays();
5964
ThreadPool threadPool = indexService.getThreadPool();
6065
return new TestSearchContext(bigArrays, indexService) {
@@ -150,6 +155,12 @@ public String getClusterAlias() {
150155
return null;
151156
}
152157
};
158+
159+
@Override
160+
public List<String> groupStats() {
161+
return Arrays.asList(groupStats);
162+
}
163+
153164
@Override
154165
public ShardSearchRequest request() {
155166
return request;
@@ -198,6 +209,26 @@ public void testSlowLogWithTypes() throws IOException {
198209
assertThat(p.getValueFor("types"), equalTo("[]"));
199210
}
200211

212+
public void testSlowLogsWithStats() throws IOException {
213+
IndexService index = createIndex("foo");
214+
SearchContext searchContext = createSearchContext(index,"group1");
215+
SearchSourceBuilder source = SearchSourceBuilder.searchSource().query(QueryBuilders.matchAllQuery());
216+
searchContext.request().source(source);
217+
searchContext.setTask(new SearchTask(0, "n/a", "n/a", "test", null,
218+
Collections.singletonMap(Task.X_OPAQUE_ID, "my_id")));
219+
220+
SearchSlowLog.SearchSlowLogMessage p = new SearchSlowLog.SearchSlowLogMessage(searchContext, 10);
221+
assertThat(p.getValueFor("stats"), equalTo("[\\\"group1\\\"]"));
222+
223+
searchContext = createSearchContext(index, "group1", "group2");
224+
source = SearchSourceBuilder.searchSource().query(QueryBuilders.matchAllQuery());
225+
searchContext.request().source(source);
226+
searchContext.setTask(new SearchTask(0, "n/a", "n/a", "test", null,
227+
Collections.singletonMap(Task.X_OPAQUE_ID, "my_id")));
228+
p = new SearchSlowLog.SearchSlowLogMessage(searchContext, 10);
229+
assertThat(p.getValueFor("stats"), equalTo("[\\\"group1\\\", \\\"group2\\\"]"));
230+
}
231+
201232
public void testSlowLogSearchContextPrinterToLog() throws IOException {
202233
IndexService index = createIndex("foo");
203234
SearchContext searchContext = createSearchContext(index);

0 commit comments

Comments
 (0)