|
@@ -25,14 +25,14 @@ import org.elasticsearch.common.logging.Loggers;
|
|
|
import org.elasticsearch.common.settings.Setting;
|
|
|
import org.elasticsearch.common.settings.Setting.Property;
|
|
|
import org.elasticsearch.common.unit.TimeValue;
|
|
|
+import org.elasticsearch.common.xcontent.ToXContent;
|
|
|
import org.elasticsearch.index.shard.SearchOperationListener;
|
|
|
import org.elasticsearch.search.internal.SearchContext;
|
|
|
|
|
|
+import java.util.Collections;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
public final class SearchSlowLog implements SearchOperationListener {
|
|
|
- private boolean reformat;
|
|
|
-
|
|
|
private long queryWarnThreshold;
|
|
|
private long queryInfoThreshold;
|
|
|
private long queryDebugThreshold;
|
|
@@ -73,20 +73,17 @@ public final class SearchSlowLog implements SearchOperationListener {
|
|
|
public static final Setting<TimeValue> INDEX_SEARCH_SLOWLOG_THRESHOLD_FETCH_TRACE_SETTING =
|
|
|
Setting.timeSetting(INDEX_SEARCH_SLOWLOG_PREFIX + ".threshold.fetch.trace", TimeValue.timeValueNanos(-1),
|
|
|
TimeValue.timeValueMillis(-1), Property.Dynamic, Property.IndexScope);
|
|
|
- public static final Setting<Boolean> INDEX_SEARCH_SLOWLOG_REFORMAT =
|
|
|
- Setting.boolSetting(INDEX_SEARCH_SLOWLOG_PREFIX + ".reformat", true, Property.Dynamic, Property.IndexScope);
|
|
|
public static final Setting<SlowLogLevel> INDEX_SEARCH_SLOWLOG_LEVEL =
|
|
|
new Setting<>(INDEX_SEARCH_SLOWLOG_PREFIX + ".level", SlowLogLevel.TRACE.name(), SlowLogLevel::parse, Property.Dynamic,
|
|
|
Property.IndexScope);
|
|
|
|
|
|
+ private static final ToXContent.Params FORMAT_PARAMS = new ToXContent.MapParams(Collections.singletonMap("pretty", "false"));
|
|
|
+
|
|
|
public SearchSlowLog(IndexSettings indexSettings) {
|
|
|
|
|
|
this.queryLogger = Loggers.getLogger(INDEX_SEARCH_SLOWLOG_PREFIX + ".query", indexSettings.getSettings());
|
|
|
this.fetchLogger = Loggers.getLogger(INDEX_SEARCH_SLOWLOG_PREFIX + ".fetch", indexSettings.getSettings());
|
|
|
|
|
|
- indexSettings.getScopedSettings().addSettingsUpdateConsumer(INDEX_SEARCH_SLOWLOG_REFORMAT, this::setReformat);
|
|
|
- this.reformat = indexSettings.getValue(INDEX_SEARCH_SLOWLOG_REFORMAT);
|
|
|
-
|
|
|
indexSettings.getScopedSettings().addSettingsUpdateConsumer(INDEX_SEARCH_SLOWLOG_THRESHOLD_QUERY_WARN_SETTING, this::setQueryWarnThreshold);
|
|
|
this.queryWarnThreshold = indexSettings.getValue(INDEX_SEARCH_SLOWLOG_THRESHOLD_QUERY_WARN_SETTING).nanos();
|
|
|
indexSettings.getScopedSettings().addSettingsUpdateConsumer(INDEX_SEARCH_SLOWLOG_THRESHOLD_QUERY_INFO_SETTING, this::setQueryInfoThreshold);
|
|
@@ -117,38 +114,36 @@ public final class SearchSlowLog implements SearchOperationListener {
|
|
|
@Override
|
|
|
public void onQueryPhase(SearchContext context, long tookInNanos) {
|
|
|
if (queryWarnThreshold >= 0 && tookInNanos > queryWarnThreshold) {
|
|
|
- queryLogger.warn("{}", new SlowLogSearchContextPrinter(context, tookInNanos, reformat));
|
|
|
+ queryLogger.warn("{}", new SlowLogSearchContextPrinter(context, tookInNanos));
|
|
|
} else if (queryInfoThreshold >= 0 && tookInNanos > queryInfoThreshold) {
|
|
|
- queryLogger.info("{}", new SlowLogSearchContextPrinter(context, tookInNanos, reformat));
|
|
|
+ queryLogger.info("{}", new SlowLogSearchContextPrinter(context, tookInNanos));
|
|
|
} else if (queryDebugThreshold >= 0 && tookInNanos > queryDebugThreshold) {
|
|
|
- queryLogger.debug("{}", new SlowLogSearchContextPrinter(context, tookInNanos, reformat));
|
|
|
+ queryLogger.debug("{}", new SlowLogSearchContextPrinter(context, tookInNanos));
|
|
|
} else if (queryTraceThreshold >= 0 && tookInNanos > queryTraceThreshold) {
|
|
|
- queryLogger.trace("{}", new SlowLogSearchContextPrinter(context, tookInNanos, reformat));
|
|
|
+ queryLogger.trace("{}", new SlowLogSearchContextPrinter(context, tookInNanos));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void onFetchPhase(SearchContext context, long tookInNanos) {
|
|
|
if (fetchWarnThreshold >= 0 && tookInNanos > fetchWarnThreshold) {
|
|
|
- fetchLogger.warn("{}", new SlowLogSearchContextPrinter(context, tookInNanos, reformat));
|
|
|
+ fetchLogger.warn("{}", new SlowLogSearchContextPrinter(context, tookInNanos));
|
|
|
} else if (fetchInfoThreshold >= 0 && tookInNanos > fetchInfoThreshold) {
|
|
|
- fetchLogger.info("{}", new SlowLogSearchContextPrinter(context, tookInNanos, reformat));
|
|
|
+ fetchLogger.info("{}", new SlowLogSearchContextPrinter(context, tookInNanos));
|
|
|
} else if (fetchDebugThreshold >= 0 && tookInNanos > fetchDebugThreshold) {
|
|
|
- fetchLogger.debug("{}", new SlowLogSearchContextPrinter(context, tookInNanos, reformat));
|
|
|
+ fetchLogger.debug("{}", new SlowLogSearchContextPrinter(context, tookInNanos));
|
|
|
} else if (fetchTraceThreshold >= 0 && tookInNanos > fetchTraceThreshold) {
|
|
|
- fetchLogger.trace("{}", new SlowLogSearchContextPrinter(context, tookInNanos, reformat));
|
|
|
+ fetchLogger.trace("{}", new SlowLogSearchContextPrinter(context, tookInNanos));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
static final class SlowLogSearchContextPrinter {
|
|
|
private final SearchContext context;
|
|
|
private final long tookInNanos;
|
|
|
- private final boolean reformat;
|
|
|
|
|
|
- public SlowLogSearchContextPrinter(SearchContext context, long tookInNanos, boolean reformat) {
|
|
|
+ public SlowLogSearchContextPrinter(SearchContext context, long tookInNanos) {
|
|
|
this.context = context;
|
|
|
this.tookInNanos = tookInNanos;
|
|
|
- this.reformat = reformat;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -172,7 +167,7 @@ public final class SearchSlowLog implements SearchOperationListener {
|
|
|
}
|
|
|
sb.append("search_type[").append(context.searchType()).append("], total_shards[").append(context.numberOfShards()).append("], ");
|
|
|
if (context.request().source() != null) {
|
|
|
- sb.append("source[").append(context.request().source()).append("], ");
|
|
|
+ sb.append("source[").append(context.request().source().toString(FORMAT_PARAMS)).append("], ");
|
|
|
} else {
|
|
|
sb.append("source[], ");
|
|
|
}
|
|
@@ -180,10 +175,6 @@ public final class SearchSlowLog implements SearchOperationListener {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private void setReformat(boolean reformat) {
|
|
|
- this.reformat = reformat;
|
|
|
- }
|
|
|
-
|
|
|
private void setQueryWarnThreshold(TimeValue warnThreshold) {
|
|
|
this.queryWarnThreshold = warnThreshold.nanos();
|
|
|
}
|
|
@@ -216,10 +207,6 @@ public final class SearchSlowLog implements SearchOperationListener {
|
|
|
this.fetchTraceThreshold = traceThreshold.nanos();
|
|
|
}
|
|
|
|
|
|
- boolean isReformat() {
|
|
|
- return reformat;
|
|
|
- }
|
|
|
-
|
|
|
long getQueryWarnThreshold() {
|
|
|
return queryWarnThreshold;
|
|
|
}
|