Skip to content

Commit fc72557

Browse files
committed
Add x-opaque-id to search slow logs
Add x-opaque-id to search slow logs only. Indexing slow log and audit logs will be handled as separate PRs. Relates elastic#31521
1 parent 7a150ec commit fc72557

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,11 @@ public String toString() {
174174
} else {
175175
sb.append("source[], ");
176176
}
177+
if (context.getTask().getHeader("X-Opaque-Id") != null) {
178+
sb.append("id[").append(context.getTask().getHeader("X-Opaque-Id")).append("], ");
179+
} else {
180+
sb.append("id[], ");
181+
}
177182
return sb.toString();
178183
}
179184
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package org.elasticsearch.index;
2121

2222
import org.elasticsearch.Version;
23+
import org.elasticsearch.action.search.SearchTask;
2324
import org.elasticsearch.action.search.SearchType;
2425
import org.elasticsearch.cluster.metadata.IndexMetaData;
2526
import org.elasticsearch.common.bytes.BytesReference;
@@ -39,7 +40,9 @@
3940
import org.elasticsearch.threadpool.ThreadPool;
4041

4142
import java.io.IOException;
43+
import java.util.Collections;
4244

45+
import static org.hamcrest.Matchers.endsWith;
4346
import static org.hamcrest.Matchers.hasToString;
4447
import static org.hamcrest.Matchers.instanceOf;
4548
import static org.hamcrest.Matchers.not;
@@ -166,10 +169,12 @@ public void testSlowLogSearchContextPrinterToLog() throws IOException {
166169
SearchContext searchContext = createSearchContext(index);
167170
SearchSourceBuilder source = SearchSourceBuilder.searchSource().query(QueryBuilders.matchAllQuery());
168171
searchContext.request().source(source);
172+
searchContext.setTask(new SearchTask(0, "n/a", "n/a", "test", null, Collections.singletonMap("X-Opaque-Id", "my_id")));
169173
SearchSlowLog.SlowLogSearchContextPrinter p = new SearchSlowLog.SlowLogSearchContextPrinter(searchContext, 10);
170174
assertThat(p.toString(), startsWith("[foo][0]"));
171175
// Makes sure that output doesn't contain any new lines
172176
assertThat(p.toString(), not(containsString("\n")));
177+
assertThat(p.toString(), endsWith("id[my_id], "));
173178
}
174179

175180
public void testLevelSetting() {

0 commit comments

Comments
 (0)