Skip to content

Commit 586043e

Browse files
committed
With cluster
1 parent 523cae1 commit 586043e

File tree

3 files changed

+62
-11
lines changed

3 files changed

+62
-11
lines changed

server/src/main/java/org/elasticsearch/rest/RestResponse.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public RestResponse(RestChannel channel, RestStatus status, Exception e) throws
127127
ToXContent.Params params = channel.request();
128128
if (e != null) {
129129
Level level = status.getStatus() < 500 || ExceptionsHelper.isNodeOrShardUnavailableTypeException(e) ? Level.DEBUG : Level.WARN;
130-
logSuppressedError(level, channel.request().rawPath(), channel.request().params(), status, e);
130+
logSuppressedError(level, channel.request().rawPath(), channel.request().params(), status, null, e);
131131
}
132132
// if "error_trace" is turned on in the request, we want to render it in the rest response
133133
// for that the REST_EXCEPTION_SKIP_STACK_TRACE flag that if "true" omits the stack traces is
@@ -248,13 +248,20 @@ public void close() {
248248
/**
249249
* Log failures to the {@code rest.suppressed} logger.
250250
*/
251-
public static void logSuppressedError(Level level, String rawPath, Map<String, String> params, RestStatus status, Exception e) {
251+
public static void logSuppressedError(
252+
Level level,
253+
String rawPath,
254+
Map<String, String> params,
255+
RestStatus status,
256+
@Nullable String extra,
257+
Exception e
258+
) {
252259
if (SUPPRESSED_ERROR_LOGGER.isEnabled(level)) {
253-
SUPPRESSED_ERROR_LOGGER.log(
254-
level,
255-
String.format(Locale.ROOT, "path: %s, params: %s, status: %d", rawPath, params, status.getStatus()),
256-
e
257-
);
260+
String message = String.format(Locale.ROOT, "path: %s, params: %s, status: %d", rawPath, params, status.getStatus());
261+
if (extra != null) {
262+
message += ", " + extra;
263+
}
264+
SUPPRESSED_ERROR_LOGGER.log(level, message, e);
258265
}
259266
}
260267
}

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/action/EsqlResponseListener.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.elasticsearch.rest.RestResponse;
2323
import org.elasticsearch.rest.RestStatus;
2424
import org.elasticsearch.rest.action.RestRefCountedChunkedToXContentListener;
25+
import org.elasticsearch.transport.RemoteClusterAware;
2526
import org.elasticsearch.xcontent.MediaType;
2627
import org.elasticsearch.xcontent.XContentType;
2728
import org.elasticsearch.xpack.esql.arrow.ArrowFormat;
@@ -237,10 +238,19 @@ private void checkDelimiter() {
237238
* Log all partial request failures to the {@code rest.suppressed} logger
238239
* so an operator can categorize them after the fact.
239240
*/
240-
static void logPartialFailures(String rawPath, Map<String, String> params, EsqlExecutionInfo exeuctionInfo) {
241-
for (EsqlExecutionInfo.Cluster cluster : exeuctionInfo.getClusters().values()) {
241+
static void logPartialFailures(String rawPath, Map<String, String> params, EsqlExecutionInfo executionInfo) {
242+
for (EsqlExecutionInfo.Cluster cluster : executionInfo.getClusters().values()) {
242243
for (ShardSearchFailure failure : cluster.getFailures()) {
243-
RestResponse.logSuppressedError(org.apache.logging.log4j.Level.WARN, rawPath, params, RestStatus.OK, failure);
244+
RestResponse.logSuppressedError(
245+
org.apache.logging.log4j.Level.WARN,
246+
rawPath,
247+
params,
248+
RestStatus.OK,
249+
cluster.getClusterAlias().equals(RemoteClusterAware.LOCAL_CLUSTER_GROUP_KEY)
250+
? null
251+
: "cluster: " + cluster.getClusterAlias(),
252+
failure
253+
);
244254
}
245255
}
246256
}

x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/action/EsqlResponseListenerTests.java

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.elasticsearch.search.SearchShardTarget;
2222
import org.elasticsearch.test.ESTestCase;
2323
import org.elasticsearch.transport.RemoteClusterAware;
24+
import org.junit.After;
2425
import org.junit.AfterClass;
2526
import org.junit.BeforeClass;
2627

@@ -29,6 +30,7 @@
2930
import java.util.Map;
3031

3132
import static org.hamcrest.Matchers.equalTo;
33+
import static org.hamcrest.Matchers.hasSize;
3234

3335
public class EsqlResponseListenerTests extends ESTestCase {
3436
private final String LOCAL_CLUSTER_ALIAS = RemoteClusterAware.LOCAL_CLUSTER_GROUP_KEY;
@@ -44,13 +46,18 @@ public static void init() throws IllegalAccessException {
4446
Loggers.addAppender(restSuppressedLogger, appender);
4547
}
4648

49+
@After
50+
public void clear() {
51+
appender.events.clear();
52+
}
53+
4754
@AfterClass
4855
public static void cleanup() {
4956
appender.stop();
5057
Loggers.removeAppender(restSuppressedLogger, appender);
5158
}
5259

53-
public void testLogPartialResponseErrors() {
60+
public void testLogPartialFailures() {
5461
EsqlExecutionInfo executionInfo = new EsqlExecutionInfo(false);
5562
executionInfo.swapCluster(
5663
LOCAL_CLUSTER_ALIAS,
@@ -72,6 +79,7 @@ public void testLogPartialResponseErrors() {
7279
);
7380
EsqlResponseListener.logPartialFailures("/_query", Map.of(), executionInfo);
7481

82+
assertThat(appender.events, hasSize(2));
7583
LogEvent logEvent = appender.events.get(0);
7684
assertThat(logEvent.getLevel(), equalTo(Level.WARN));
7785
assertThat(logEvent.getMessage().getFormattedMessage(), equalTo("path: /_query, params: {}, status: 200"));
@@ -82,6 +90,32 @@ public void testLogPartialResponseErrors() {
8290
assertThat(logEvent.getThrown().getCause().getMessage(), equalTo("error"));
8391
}
8492

93+
public void testLogPartialFailuresRemote() {
94+
EsqlExecutionInfo executionInfo = new EsqlExecutionInfo(false);
95+
executionInfo.swapCluster(
96+
"remote_cluster",
97+
(k, v) -> new EsqlExecutionInfo.Cluster(
98+
"remote_cluster",
99+
"idx",
100+
false,
101+
EsqlExecutionInfo.Cluster.Status.SUCCESSFUL,
102+
10,
103+
10,
104+
3,
105+
0,
106+
List.of(new ShardSearchFailure(new Exception("dummy"), target(0))),
107+
new TimeValue(4444L)
108+
)
109+
);
110+
EsqlResponseListener.logPartialFailures("/_query", Map.of(), executionInfo);
111+
112+
assertThat(appender.events, hasSize(1));
113+
LogEvent logEvent = appender.events.get(0);
114+
assertThat(logEvent.getLevel(), equalTo(Level.WARN));
115+
assertThat(logEvent.getMessage().getFormattedMessage(), equalTo("path: /_query, params: {}, status: 200, cluster: remote_cluster"));
116+
assertThat(logEvent.getThrown().getCause().getMessage(), equalTo("dummy"));
117+
}
118+
85119
private SearchShardTarget target(int shardId) {
86120
return new SearchShardTarget("node", new ShardId("idx", "uuid", 0), LOCAL_CLUSTER_ALIAS);
87121
}

0 commit comments

Comments
 (0)