Skip to content

Commit b0cb54e

Browse files
authored
Change deprecation indexing to use a custom template (#64627)
Backport of #64417. The implementation for indexing deprecation logs to a data stream (#58924) relied on the Stack template for `logs-*-*`. This meant that if the user disabled the stack templates, the templates would also be unavailable for the deprecation logs. Change the implementation so that: * There is a separate template for deprecation logging * The data stream is marked as hidden * The data stream name is prefixed with a period (`.`)
1 parent 2c4349b commit b0cb54e

File tree

10 files changed

+213
-6
lines changed

10 files changed

+213
-6
lines changed

test/framework/src/main/java/org/elasticsearch/test/rest/ESRestTestCase.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1353,6 +1353,7 @@ protected static boolean isXPackTemplate(String name) {
13531353
case "synthetics-settings":
13541354
case "synthetics-mappings":
13551355
case ".snapshot-blob-cache":
1356+
case ".deprecation-indexing-template":
13561357
return true;
13571358
default:
13581359
return false;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"phases": {
3+
"hot": {
4+
"actions": {
5+
"rollover": {
6+
"max_size": "50gb",
7+
"max_age": "30d"
8+
}
9+
}
10+
}
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
{
2+
"template": {
3+
"mappings": {
4+
"dynamic_templates": [
5+
{
6+
"strings_as_keyword": {
7+
"mapping": {
8+
"ignore_above": 1024,
9+
"type": "keyword"
10+
},
11+
"match_mapping_type": "string"
12+
}
13+
}
14+
],
15+
"date_detection": false,
16+
"properties": {
17+
"@timestamp": {
18+
"type": "date"
19+
},
20+
"data_stream": {
21+
"properties": {
22+
"type": {
23+
"type": "constant_keyword",
24+
"value": "logs"
25+
},
26+
"dataset": {
27+
"type": "constant_keyword"
28+
},
29+
"namespace": {
30+
"type": "constant_keyword"
31+
}
32+
}
33+
},
34+
"ecs": {
35+
"properties": {
36+
"version": {
37+
"ignore_above": 1024,
38+
"type": "keyword"
39+
}
40+
}
41+
},
42+
"host": {
43+
"properties": {
44+
"ip": {
45+
"type": "ip"
46+
}
47+
}
48+
},
49+
"message": {
50+
"type": "text"
51+
}
52+
}
53+
}
54+
},
55+
"_meta": {
56+
"description": "default mappings for ES deprecation logs index template installed by x-pack",
57+
"managed": true
58+
},
59+
"version": ${xpack.deprecation.indexing.template.version}
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"template": {
3+
"settings": {
4+
"index": {
5+
"lifecycle": {
6+
"name": ".deprecation-indexing-ilm-policy"
7+
},
8+
"codec": "best_compression",
9+
"query": {
10+
"default_field": ["message"]
11+
}
12+
}
13+
}
14+
},
15+
"_meta": {
16+
"description": "default settings for ES deprecation logs index template installed by x-pack",
17+
"managed": true
18+
},
19+
"version": ${xpack.deprecation.indexing.template.version}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"index_patterns": [".logs-deprecation-elasticsearch"],
3+
"priority": 1000,
4+
"data_stream": {
5+
"hidden": true
6+
},
7+
"composed_of": [
8+
".deprecation-indexing-mappings",
9+
".deprecation-indexing-settings"
10+
],
11+
"allow_auto_create": true,
12+
"_meta": {
13+
"description": "default template for ES deprecation logs index template installed by x-pack",
14+
"managed": true
15+
},
16+
"version": ${xpack.deprecation.indexing.template.version}
17+
}

x-pack/plugin/deprecation/qa/rest/src/javaRestTest/java/org/elasticsearch/xpack/deprecation/DeprecationHttpIT.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ public void testDeprecationMessagesCanBeIndexed() throws Exception {
240240
assertBusy(() -> {
241241
Response response;
242242
try {
243-
response = client().performRequest(new Request("GET", "logs-deprecation-elasticsearch/_search"));
243+
response = client().performRequest(new Request("GET", ".logs-deprecation-elasticsearch/_search"));
244244
} catch (Exception e) {
245245
// It can take a moment for the index to be created. If it doesn't exist then the client
246246
// throws an exception. Translate it into an assertion error so that assertBusy() will
@@ -309,7 +309,7 @@ public void testDeprecationMessagesCanBeIndexed() throws Exception {
309309
});
310310
} finally {
311311
configureWriteDeprecationLogsToIndex(null);
312-
client().performRequest(new Request("DELETE", "_data_stream/logs-deprecation-elasticsearch"));
312+
client().performRequest(new Request("DELETE", "/_data_stream/.logs-deprecation-elasticsearch?expand_wildcards=hidden"));
313313
}
314314
}
315315

x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/Deprecation.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.elasticsearch.xpack.core.deprecation.DeprecationInfoAction;
3232
import org.elasticsearch.xpack.core.deprecation.NodesDeprecationCheckAction;
3333
import org.elasticsearch.xpack.deprecation.logging.DeprecationIndexingComponent;
34+
import org.elasticsearch.xpack.deprecation.logging.DeprecationIndexingTemplateRegistry;
3435

3536
import java.util.Arrays;
3637
import java.util.Collection;
@@ -76,8 +77,11 @@ public Collection<Object> createComponents(
7677
IndexNameExpressionResolver indexNameExpressionResolver,
7778
Supplier<RepositoriesService> repositoriesServiceSupplier
7879
) {
79-
DeprecationIndexingComponent component = new DeprecationIndexingComponent(client, environment.settings());
80+
final DeprecationIndexingTemplateRegistry templateRegistry =
81+
new DeprecationIndexingTemplateRegistry(environment.settings(), clusterService, threadPool, client, xContentRegistry);
82+
templateRegistry.initialize();
8083

84+
final DeprecationIndexingComponent component = new DeprecationIndexingComponent(client, environment.settings());
8185
clusterService.addListener(component);
8286

8387
return Collections.singletonList(component);

x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/logging/DeprecationIndexingAppender.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
*/
2828
@Plugin(name = "DeprecationIndexingAppender", category = Core.CATEGORY_NAME, elementType = Appender.ELEMENT_TYPE)
2929
public class DeprecationIndexingAppender extends AbstractAppender {
30-
public static final String DEPRECATION_MESSAGES_DATA_STREAM = "logs-deprecation-elasticsearch";
30+
public static final String DEPRECATION_MESSAGES_DATA_STREAM = ".logs-deprecation-elasticsearch";
3131

3232
private final Consumer<IndexRequest> requestConsumer;
3333

x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/logging/DeprecationIndexingComponent.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,12 @@ public void clusterChanged(ClusterChangedEvent event) {
118118
* @return an initialised bulk processor
119119
*/
120120
private BulkProcessor getBulkProcessor(Client client, Settings settings) {
121-
final OriginSettingClient originSettingClient = new OriginSettingClient(client, ClientHelper.DEPRECATION_ORIGIN);
122121
final BulkProcessor.Listener listener = new DeprecationBulkListener();
123122

124123
// This configuration disables the size count and size thresholds,
125124
// and instead uses a scheduled flush only. This means that calling
126125
// processor.add() will not block the calling thread.
127-
return BulkProcessor.builder(originSettingClient::bulk, listener)
126+
return BulkProcessor.builder(client::bulk, listener)
128127
.setBackoffPolicy(BackoffPolicy.exponentialBackoff(TimeValue.timeValueMillis(1000), 3))
129128
.setConcurrentRequests(Math.max(2, EsExecutors.allocatedProcessors(settings)))
130129
.setBulkActions(-1)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
7+
package org.elasticsearch.xpack.deprecation.logging;
8+
9+
import org.elasticsearch.client.Client;
10+
import org.elasticsearch.cluster.service.ClusterService;
11+
import org.elasticsearch.common.settings.Settings;
12+
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
13+
import org.elasticsearch.threadpool.ThreadPool;
14+
import org.elasticsearch.xpack.core.template.IndexTemplateConfig;
15+
import org.elasticsearch.xpack.core.template.IndexTemplateRegistry;
16+
import org.elasticsearch.xpack.core.template.LifecyclePolicyConfig;
17+
18+
import java.util.Arrays;
19+
import java.util.Collections;
20+
import java.util.List;
21+
22+
import static org.elasticsearch.xpack.core.ClientHelper.DEPRECATION_ORIGIN;
23+
24+
/**
25+
* Manages the index template and associated ILM policy for deprecation log indexing.
26+
*/
27+
public class DeprecationIndexingTemplateRegistry extends IndexTemplateRegistry {
28+
// history (please add a comment why you increased the version here)
29+
// version 1: initial
30+
public static final int INDEX_TEMPLATE_VERSION = 1;
31+
32+
public static final String DEPRECATION_INDEXING_TEMPLATE_VERSION_VARIABLE = "xpack.deprecation.indexing.template.version";
33+
34+
public static final String DEPRECATION_INDEXING_MAPPINGS_NAME = ".deprecation-indexing-mappings";
35+
public static final String DEPRECATION_INDEXING_SETTINGS_NAME = ".deprecation-indexing-settings";
36+
public static final String DEPRECATION_INDEXING_TEMPLATE_NAME = ".deprecation-indexing-template";
37+
public static final String DEPRECATION_INDEXING_POLICY_NAME = ".deprecation-indexing-ilm-policy";
38+
39+
public static final IndexTemplateConfig DEPRECATION_INDEXING_MAPPINGS = new IndexTemplateConfig(
40+
DEPRECATION_INDEXING_MAPPINGS_NAME,
41+
"/org/elasticsearch/xpack/deprecation/deprecation-indexing-mappings.json",
42+
INDEX_TEMPLATE_VERSION,
43+
DEPRECATION_INDEXING_TEMPLATE_VERSION_VARIABLE
44+
);
45+
46+
public static final IndexTemplateConfig DEPRECATION_INDEXING_SETTINGS = new IndexTemplateConfig(
47+
DEPRECATION_INDEXING_SETTINGS_NAME,
48+
"/org/elasticsearch/xpack/deprecation/deprecation-indexing-settings.json",
49+
INDEX_TEMPLATE_VERSION,
50+
DEPRECATION_INDEXING_TEMPLATE_VERSION_VARIABLE
51+
);
52+
53+
public static final IndexTemplateConfig DEPRECATION_INDEXING_INDEX_TEMPLATE = new IndexTemplateConfig(
54+
DEPRECATION_INDEXING_TEMPLATE_NAME,
55+
"/org/elasticsearch/xpack/deprecation/deprecation-indexing-template.json",
56+
INDEX_TEMPLATE_VERSION,
57+
DEPRECATION_INDEXING_TEMPLATE_VERSION_VARIABLE
58+
);
59+
60+
public static final LifecyclePolicyConfig DEPRECATION_INDEXING_HISTORY_POLICY = new LifecyclePolicyConfig(
61+
DEPRECATION_INDEXING_POLICY_NAME,
62+
"/org/elasticsearch/xpack/deprecation/deprecation-indexing-ilm-policy.json"
63+
);
64+
65+
public DeprecationIndexingTemplateRegistry(
66+
Settings nodeSettings,
67+
ClusterService clusterService,
68+
ThreadPool threadPool,
69+
Client client,
70+
NamedXContentRegistry xContentRegistry
71+
) {
72+
super(nodeSettings, clusterService, threadPool, client, xContentRegistry);
73+
}
74+
75+
@Override
76+
protected List<IndexTemplateConfig> getComponentTemplateConfigs() {
77+
return Arrays.asList(DEPRECATION_INDEXING_MAPPINGS, DEPRECATION_INDEXING_SETTINGS);
78+
}
79+
80+
@Override
81+
protected List<IndexTemplateConfig> getComposableTemplateConfigs() {
82+
return Collections.singletonList(DEPRECATION_INDEXING_INDEX_TEMPLATE);
83+
}
84+
85+
@Override
86+
protected List<LifecyclePolicyConfig> getPolicyConfigs() {
87+
return Collections.singletonList(DEPRECATION_INDEXING_HISTORY_POLICY);
88+
}
89+
90+
@Override
91+
protected String getOrigin() {
92+
return DEPRECATION_ORIGIN;
93+
}
94+
}

0 commit comments

Comments
 (0)