Skip to content

Commit ee5aacb

Browse files
authored
[Monitoring] Add cluster metadata to cluster_stats docs (#33860) (#34023)
Backport of #33860 and #34040. This PR teaches Monitoring to collect cluster metadata, if any is set, and index it into `cluster_stats` docs in `.monitoring-es-*`. After this PR, `cluster_stats` docs in `.monitoring-es-*` will contain an additional top-level `cluster_settings` field like so: ``` { ... "cluster_settings": { "cluster": { "metadata": { ... } } } } ```
1 parent c33aa34 commit ee5aacb

File tree

4 files changed

+56
-3
lines changed

4 files changed

+56
-3
lines changed

x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/cluster/ClusterStatsCollector.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ protected Collection<MonitoringDoc> doCollect(final MonitoringDoc.Node node,
113113
// Adds a cluster stats document
114114
return Collections.singleton(
115115
new ClusterStatsMonitoringDoc(clusterUuid, timestamp(), interval, node, clusterName, version, clusterStats.getStatus(),
116-
license, apmIndicesExist, xpackUsage, clusterStats, clusterState, clusterNeedsTLSEnabled));
116+
license, apmIndicesExist, xpackUsage, clusterStats, clusterState,
117+
clusterNeedsTLSEnabled));
117118
}
118119

119120
boolean doAPMIndicesExist(final ClusterState clusterState) {

x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/cluster/ClusterStatsMonitoringDoc.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.elasticsearch.action.admin.cluster.stats.ClusterStatsResponse;
99
import org.elasticsearch.cluster.ClusterState;
1010
import org.elasticsearch.cluster.health.ClusterHealthStatus;
11+
import org.elasticsearch.cluster.metadata.MetaData;
1112
import org.elasticsearch.cluster.node.DiscoveryNode;
1213
import org.elasticsearch.cluster.node.DiscoveryNodes;
1314
import org.elasticsearch.common.Nullable;
@@ -45,6 +46,7 @@ public class ClusterStatsMonitoringDoc extends MonitoringDoc {
4546
ClusterState.Metric.NODES));
4647

4748
public static final String TYPE = "cluster_stats";
49+
protected static final String SETTING_DISPLAY_NAME = "cluster.metadata.display_name";
4850

4951
private final String clusterName;
5052
private final String version;
@@ -118,6 +120,14 @@ boolean getClusterNeedsTLSEnabled() {
118120
return clusterNeedsTLSEnabled;
119121
}
120122

123+
String getClusterDisplayName() {
124+
MetaData metaData = this.clusterState.getMetaData();
125+
if (metaData == null) {
126+
return null;
127+
}
128+
return metaData.settings().get(SETTING_DISPLAY_NAME);
129+
}
130+
121131
@Override
122132
protected void innerToXContent(XContentBuilder builder, Params params) throws IOException {
123133
builder.field("cluster_name", clusterName);
@@ -156,6 +166,23 @@ protected void innerToXContent(XContentBuilder builder, Params params) throws IO
156166
builder.endObject();
157167
}
158168

169+
String displayName = getClusterDisplayName();
170+
if (displayName != null) {
171+
builder.startObject("cluster_settings");
172+
{
173+
builder.startObject("cluster");
174+
{
175+
builder.startObject("metadata");
176+
{
177+
builder.field("display_name", displayName);
178+
}
179+
builder.endObject();
180+
}
181+
builder.endObject();
182+
}
183+
builder.endObject();
184+
}
185+
159186
builder.startObject("stack_stats");
160187
{
161188
// in the future, it may be useful to pass in an object that represents APM (and others), but for now this

x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/cluster/ClusterStatsMonitoringDocTests.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,12 @@ public void testToXContent() throws IOException {
203203
Version.V_6_0_0_beta1);
204204

205205
final ClusterState clusterState = ClusterState.builder(clusterName)
206-
.metaData(MetaData.builder().clusterUUID(clusterUuid).build())
206+
.metaData(MetaData.builder()
207+
.clusterUUID(clusterUuid)
208+
.transientSettings(Settings.builder()
209+
.put("cluster.metadata.display_name", "my_prod_cluster")
210+
.build())
211+
.build())
207212
.stateUUID("_state_uuid")
208213
.version(12L)
209214
.nodes(DiscoveryNodes.builder()
@@ -521,6 +526,13 @@ public void testToXContent() throws IOException {
521526
+ "}"
522527
+ "}"
523528
+ "},"
529+
+ "\"cluster_settings\":{"
530+
+ "\"cluster\":{"
531+
+ "\"metadata\":{"
532+
+ "\"display_name\":\"my_prod_cluster\""
533+
+ "}"
534+
+ "}"
535+
+ "},"
524536
+ "\"stack_stats\":{"
525537
+ "\"apm\":{"
526538
+ "\"found\":" + apmIndicesExist

x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/integration/MonitoringIT.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,12 @@ public void testMonitoringService() throws Exception {
207207
.status(),
208208
is(RestStatus.CREATED));
209209

210+
final Settings settings = Settings.builder()
211+
.put("cluster.metadata.display_name", "my cluster")
212+
.build();
213+
214+
assertAcked(client().admin().cluster().prepareUpdateSettings().setTransientSettings(settings));
215+
210216
whenExportersAreReady(() -> {
211217
final AtomicReference<SearchResponse> searchResponse = new AtomicReference<>();
212218

@@ -316,7 +322,7 @@ private void assertMonitoringDocSourceNode(final Map<String, Object> sourceNode)
316322
private void assertClusterStatsMonitoringDoc(final Map<String, Object> document,
317323
final boolean apmIndicesExist) {
318324
final Map<String, Object> source = (Map<String, Object>) document.get("_source");
319-
assertEquals(11, source.size());
325+
assertEquals(12, source.size());
320326

321327
assertThat((String) source.get("cluster_name"), not(isEmptyOrNullString()));
322328
assertThat(source.get("version"), equalTo(Version.CURRENT.toString()));
@@ -377,6 +383,12 @@ private void assertClusterStatsMonitoringDoc(final Map<String, Object> document,
377383
assertThat(clusterState.remove("master_node"), notNullValue());
378384
assertThat(clusterState.remove("nodes"), notNullValue());
379385
assertThat(clusterState.keySet(), empty());
386+
387+
388+
final Map<String, Object> clusterSettings = (Map<String, Object>) source.get("cluster_settings");
389+
assertThat(clusterSettings, notNullValue());
390+
assertThat(clusterSettings.remove("cluster"), notNullValue());
391+
assertThat(clusterSettings.keySet(), empty());
380392
}
381393

382394
/**
@@ -617,6 +629,7 @@ public void disableMonitoring() throws Exception {
617629
final Settings settings = Settings.builder()
618630
.putNull("xpack.monitoring.collection.enabled")
619631
.putNull("xpack.monitoring.exporters._local.enabled")
632+
.putNull("cluster.metadata.display_name")
620633
.build();
621634

622635
assertAcked(client().admin().cluster().prepareUpdateSettings().setTransientSettings(settings));

0 commit comments

Comments
 (0)