Skip to content

Commit 1070621

Browse files
committed
Setting to disable x-opaque-id in logs throttling backport(elastic#78911)
Introduces a setting cluster.deprecation_indexing.x_opaque_id_used.enabled to disable use of x-opaque-id in RateLimitingFilter. This will be used for deprecation logs indexing and will not affect logging to files (it uses different instance of RateLimitingFilter with this flag enabled by default) Changes the indices backing a deprecation log data stream to be hidden. Refactors DeprecationHttpIT to be more reliable relates elastic#76292 closes elastic#77936
1 parent 3dda4bd commit 1070621

File tree

7 files changed

+463
-307
lines changed

7 files changed

+463
-307
lines changed

docs/reference/setup/logging-config.asciidoc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,17 @@ The user ID is included in the `X-Opaque-ID` field in deprecation JSON logs.
234234
---------------------------
235235
// NOTCONSOLE
236236

237+
Deprecation logs can be indexed into `.logs-deprecation.elasticsearch-default` data stream
238+
`cluster.deprecation_indexing.enabled` setting is set to true.
239+
240+
==== Deprecation logs throttling
241+
Deprecation logs are deduplicated based on a deprecated feature key
242+
and x-opaque-id so that if a feature is repeatedly used, it will not overload the deprecation logs.
243+
This applies to both indexed deprecation logs and logs emitted to log files.
244+
You can disable the use of `x-opaque-id` in throttling by changing
245+
`cluster.deprecation_indexing.x_opaque_id_used.enabled` to false
246+
See link:./server/src/main/java/org/elasticsearch/common/logging/RateLimitingFilter.java[RateLimitingFilter]
247+
237248
[discrete]
238249
[[json-logging]]
239250
=== JSON log format

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

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,24 @@
2525
import java.util.Map;
2626
import java.util.Set;
2727

28+
import static org.elasticsearch.common.logging.DeprecatedMessage.KEY_FIELD_NAME;
2829
import static org.elasticsearch.common.logging.DeprecatedMessage.X_OPAQUE_ID_FIELD_NAME;
2930

31+
/**
32+
* A filter used for throttling deprecation logs.
33+
* A throttling is based on a combined key which consists of `key` from the logged ESMessage and `x-opaque-id`
34+
* passed by a user on a HTTP header.
35+
* This filter works by using a lruKeyCache - a set of keys which prevents a second message with the same key to be logged.
36+
* The lruKeyCache has a size limited to 128, which when breached will remove the oldest entries.
37+
*
38+
* It is possible to disable use of `x-opaque-id` as a key with {@link RateLimitingFilter#setUseXOpaqueId(boolean) }
39+
* @see <a href="https://logging.apache.org/log4j/2.x/manual/filters.htmlf">Log4j2 Filters</a>
40+
*/
3041
@Plugin(name = "RateLimitingFilter", category = Node.CATEGORY, elementType = Filter.ELEMENT_TYPE)
3142
public class RateLimitingFilter extends AbstractFilter {
3243

44+
private volatile boolean useXOpaqueId = true;
45+
3346
private final Set<String> lruKeyCache = Collections.newSetFromMap(Collections.synchronizedMap(new LinkedHashMap<String, Boolean>() {
3447
@Override
3548
protected boolean removeEldestEntry(final Map.Entry<String, Boolean> eldest) {
@@ -56,16 +69,23 @@ public Result filter(Message message) {
5669
if (message instanceof ESLogMessage) {
5770
final ESLogMessage esLogMessage = (ESLogMessage) message;
5871

59-
String xOpaqueId = esLogMessage.getValueFor(X_OPAQUE_ID_FIELD_NAME);
60-
final String key = esLogMessage.getValueFor("key");
61-
62-
return lruKeyCache.add(xOpaqueId + key) ? Result.ACCEPT : Result.DENY;
72+
final String key = getKey(esLogMessage);
73+
return lruKeyCache.add(key) ? Result.ACCEPT : Result.DENY;
6374

6475
} else {
6576
return Result.NEUTRAL;
6677
}
6778
}
6879

80+
private String getKey(ESLogMessage esLogMessage) {
81+
final String key = esLogMessage.getValueFor(KEY_FIELD_NAME);
82+
if (useXOpaqueId) {
83+
String xOpaqueId = esLogMessage.getValueFor(X_OPAQUE_ID_FIELD_NAME);
84+
return xOpaqueId + key;
85+
}
86+
return key;
87+
}
88+
6989
@Override
7090
public Result filter(LogEvent event) {
7191
return filter(event.getMessage());
@@ -83,4 +103,8 @@ public static RateLimitingFilter createFilter(
83103
) {
84104
return new RateLimitingFilter(match, mismatch);
85105
}
106+
107+
public void setUseXOpaqueId(boolean useXOpaqueId) {
108+
this.useXOpaqueId = useXOpaqueId;
109+
}
86110
}

server/src/test/java/org/elasticsearch/common/logging/RateLimitingFilterTests.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,4 +147,26 @@ public void testFilterCanBeReset() {
147147
// Third time, it is allowed again
148148
assertThat(filter.filter(message), equalTo(Result.ACCEPT));
149149
}
150+
151+
public void testMessagesXOpaqueIsIgnoredWhenDisabled() {
152+
RateLimitingFilter filter = new RateLimitingFilter();
153+
filter.setUseXOpaqueId(false);
154+
filter.start();
155+
156+
// Should NOT be rate-limited because it's not in the cache
157+
Message message = new DeprecatedMessage(DeprecationCategory.OTHER, "key 0", "opaque-id 0", "msg 0");
158+
assertThat(filter.filter(message), equalTo(Result.ACCEPT));
159+
160+
// Should be rate-limited because it was just added to the cache
161+
message = new DeprecatedMessage(DeprecationCategory.OTHER, "key 0", "opaque-id 0", "msg 0");
162+
assertThat(filter.filter(message), equalTo(Result.DENY));
163+
164+
// Should be rate-limited because X-Opaque-Id is not used
165+
message = new DeprecatedMessage(DeprecationCategory.OTHER, "key 0", "opaque-id 1", "msg 0");
166+
assertThat(filter.filter(message), equalTo(Result.DENY));
167+
168+
// Should NOT be rate-limited because "key 1" it not in the cache
169+
message = new DeprecatedMessage(DeprecationCategory.OTHER, "key 1", "opaque-id 1", "msg 0");
170+
assertThat(filter.filter(message), equalTo(Result.ACCEPT));
171+
}
150172
}

x-pack/plugin/core/src/main/resources/org/elasticsearch/xpack/deprecation/deprecation-indexing-settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"template": {
33
"settings": {
44
"index": {
5+
"hidden" : true,
56
"lifecycle": {
67
"name": ".deprecation-indexing-ilm-policy"
78
},

0 commit comments

Comments
 (0)