Skip to content

Commit d607fb0

Browse files
ycombinatorkcm
authored andcommitted
[Monitoring] Add cluster metadata to cluster_stats docs (#33860)
* WIP * Adding cluster metadata to cluster stats monitoring doc * Fixing checkstyle errors * Adding missing license header * Updating tests * Getting cluster settings from cluster state * Removing more unnecessary changes * Adding cluster metadata settings to cluster_stats docs * Updating test to include cluster metadata * Fixing checkstyle * Guarding against NPE * Updating test fixture
1 parent 198e450 commit d607fb0

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
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
@@ -112,7 +112,8 @@ protected Collection<MonitoringDoc> doCollect(final MonitoringDoc.Node node,
112112
// Adds a cluster stats document
113113
return Collections.singleton(
114114
new ClusterStatsMonitoringDoc(clusterUuid, timestamp(), interval, node, clusterName, version, clusterStats.getStatus(),
115-
license, apmIndicesExist, xpackUsage, clusterStats, clusterState, clusterNeedsTLSEnabled));
115+
license, apmIndicesExist, xpackUsage, clusterStats, clusterState,
116+
clusterNeedsTLSEnabled));
116117
}
117118

118119
boolean doAPMIndicesExist(final ClusterState clusterState) {

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
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;
1415
import org.elasticsearch.common.collect.MapBuilder;
16+
import org.elasticsearch.common.settings.Settings;
1517
import org.elasticsearch.common.xcontent.ToXContent;
1618
import org.elasticsearch.common.xcontent.XContentBuilder;
1719
import org.elasticsearch.license.License;
@@ -45,6 +47,7 @@ public class ClusterStatsMonitoringDoc extends MonitoringDoc {
4547
ClusterState.Metric.NODES));
4648

4749
public static final String TYPE = "cluster_stats";
50+
protected static final String SETTING_CLUSTER_METADATA = "cluster.metadata";
4851

4952
private final String clusterName;
5053
private final String version;
@@ -118,6 +121,14 @@ boolean getClusterNeedsTLSEnabled() {
118121
return clusterNeedsTLSEnabled;
119122
}
120123

124+
Settings getClusterMetaDataSettings() {
125+
MetaData metaData = this.clusterState.getMetaData();
126+
if (metaData == null) {
127+
return Settings.EMPTY;
128+
}
129+
return metaData.settings().getAsSettings(SETTING_CLUSTER_METADATA);
130+
}
131+
121132
@Override
122133
protected void innerToXContent(XContentBuilder builder, Params params) throws IOException {
123134
builder.field("cluster_name", clusterName);
@@ -156,6 +167,25 @@ protected void innerToXContent(XContentBuilder builder, Params params) throws IO
156167
builder.endObject();
157168
}
158169

170+
Settings clusterMetaDataSettings = getClusterMetaDataSettings();
171+
if (clusterMetaDataSettings != null) {
172+
builder.startObject("cluster_settings");
173+
{
174+
if (clusterMetaDataSettings.size() > 0) {
175+
builder.startObject("cluster");
176+
{
177+
builder.startObject("metadata");
178+
{
179+
clusterMetaDataSettings.toXContent(builder, params);
180+
}
181+
builder.endObject();
182+
}
183+
builder.endObject();
184+
}
185+
}
186+
builder.endObject();
187+
}
188+
159189
builder.startObject("stack_stats");
160190
{
161191
// 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

0 commit comments

Comments
 (0)