[Monitoring] Add "collection_enabled" to usage (elastic/x-pack-elasticsearch#4128)

This adds an indicator to Monitoring's portion of X-Pack usage whether or
not collection is actually enabled. It's no longer enough to have an
exporter defined by default to know if monitoring is actually running.

Original commit: elastic/x-pack-elasticsearch@b2eb881d61
This commit is contained in:
Chris Earle 2018-03-16 11:38:18 -04:00 committed by GitHub
parent 132ac6ef52
commit 91401fcb83
6 changed files with 52 additions and 28 deletions

View File

@ -5,6 +5,7 @@
*/ */
package org.elasticsearch.xpack.core.monitoring; package org.elasticsearch.xpack.core.monitoring;
import org.elasticsearch.Version;
import org.elasticsearch.common.Nullable; import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.StreamOutput;
@ -18,19 +19,24 @@ import java.util.Map;
public class MonitoringFeatureSetUsage extends XPackFeatureSet.Usage { public class MonitoringFeatureSetUsage extends XPackFeatureSet.Usage {
private static final String ENABLED_EXPORTERS_XFIELD = "enabled_exporters"; @Nullable
private Boolean collectionEnabled;
@Nullable @Nullable
private Map<String, Object> exporters; private Map<String, Object> exporters;
public MonitoringFeatureSetUsage(StreamInput in) throws IOException { public MonitoringFeatureSetUsage(StreamInput in) throws IOException {
super(in); super(in);
exporters = in.readMap(); exporters = in.readMap();
if (in.getVersion().onOrAfter(Version.V_6_3_0)) {
collectionEnabled = in.readOptionalBoolean();
}
} }
public MonitoringFeatureSetUsage(boolean available, boolean enabled, Map<String, Object> exporters) { public MonitoringFeatureSetUsage(boolean available, boolean enabled,
boolean collectionEnabled, Map<String, Object> exporters) {
super(XPackField.MONITORING, available, enabled); super(XPackField.MONITORING, available, enabled);
this.exporters = exporters; this.exporters = exporters;
this.collectionEnabled = collectionEnabled;
} }
public Map<String, Object> getExporters() { public Map<String, Object> getExporters() {
@ -41,13 +47,19 @@ public class MonitoringFeatureSetUsage extends XPackFeatureSet.Usage {
public void writeTo(StreamOutput out) throws IOException { public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out); super.writeTo(out);
out.writeMap(exporters); out.writeMap(exporters);
if (out.getVersion().onOrAfter(Version.V_6_3_0)) {
out.writeOptionalBoolean(collectionEnabled);
}
} }
@Override @Override
protected void innerXContent(XContentBuilder builder, Params params) throws IOException { protected void innerXContent(XContentBuilder builder, Params params) throws IOException {
super.innerXContent(builder, params); super.innerXContent(builder, params);
if (collectionEnabled != null) {
builder.field("collection_enabled", collectionEnabled);
}
if (exporters != null) { if (exporters != null) {
builder.field(ENABLED_EXPORTERS_XFIELD, exporters); builder.field("enabled_exporters", exporters);
} }
} }
} }

View File

@ -109,6 +109,7 @@ public class Monitoring extends Plugin implements ActionPlugin {
modules.add(b -> { modules.add(b -> {
XPackPlugin.bindFeatureSet(b, MonitoringFeatureSet.class); XPackPlugin.bindFeatureSet(b, MonitoringFeatureSet.class);
if (transportClientMode || enabled == false) { if (transportClientMode || enabled == false) {
b.bind(MonitoringService.class).toProvider(Providers.of(null));
b.bind(Exporters.class).toProvider(Providers.of(null)); b.bind(Exporters.class).toProvider(Providers.of(null));
} }
}); });

View File

@ -23,12 +23,17 @@ import java.util.Map;
public class MonitoringFeatureSet implements XPackFeatureSet { public class MonitoringFeatureSet implements XPackFeatureSet {
private final boolean enabled; private final boolean enabled;
private final MonitoringService monitoring;
private final XPackLicenseState licenseState; private final XPackLicenseState licenseState;
private final Exporters exporters; private final Exporters exporters;
@Inject @Inject
public MonitoringFeatureSet(Settings settings, @Nullable XPackLicenseState licenseState, @Nullable Exporters exporters) { public MonitoringFeatureSet(Settings settings,
@Nullable MonitoringService monitoring,
@Nullable XPackLicenseState licenseState,
@Nullable Exporters exporters) {
this.enabled = XPackSettings.MONITORING_ENABLED.get(settings); this.enabled = XPackSettings.MONITORING_ENABLED.get(settings);
this.monitoring = monitoring;
this.licenseState = licenseState; this.licenseState = licenseState;
this.exporters = exporters; this.exporters = exporters;
} }
@ -60,7 +65,9 @@ public class MonitoringFeatureSet implements XPackFeatureSet {
@Override @Override
public void usage(ActionListener<XPackFeatureSet.Usage> listener) { public void usage(ActionListener<XPackFeatureSet.Usage> listener) {
listener.onResponse(new MonitoringFeatureSetUsage(available(), enabled(), exportersUsage(exporters))); final boolean collectionEnabled = monitoring != null && monitoring.isMonitoringActive();
listener.onResponse(new MonitoringFeatureSetUsage(available(), enabled(), collectionEnabled, exportersUsage(exporters)));
} }
static Map<String, Object> exportersUsage(Exporters exporters) { static Map<String, Object> exportersUsage(Exporters exporters) {

View File

@ -5,6 +5,7 @@
*/ */
package org.elasticsearch.xpack.monitoring; package org.elasticsearch.xpack.monitoring;
import org.elasticsearch.Version;
import org.elasticsearch.action.support.PlainActionFuture; import org.elasticsearch.action.support.PlainActionFuture;
import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.io.stream.BytesStreamOutput; import org.elasticsearch.common.io.stream.BytesStreamOutput;
@ -21,7 +22,6 @@ import org.elasticsearch.xpack.monitoring.exporter.Exporter;
import org.elasticsearch.xpack.monitoring.exporter.Exporters; import org.elasticsearch.xpack.monitoring.exporter.Exporters;
import org.elasticsearch.xpack.monitoring.exporter.http.HttpExporter; import org.elasticsearch.xpack.monitoring.exporter.http.HttpExporter;
import org.elasticsearch.xpack.monitoring.exporter.local.LocalExporter; import org.elasticsearch.xpack.monitoring.exporter.local.LocalExporter;
import org.junit.Before;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@ -36,39 +36,36 @@ import static org.mockito.Mockito.when;
public class MonitoringFeatureSetTests extends ESTestCase { public class MonitoringFeatureSetTests extends ESTestCase {
private XPackLicenseState licenseState; private final MonitoringService monitoring = mock(MonitoringService.class);
private Exporters exporters; private final XPackLicenseState licenseState = mock(XPackLicenseState.class);
private final Exporters exporters = mock(Exporters.class);
@Before public void testAvailable() {
public void init() throws Exception { MonitoringFeatureSet featureSet = new MonitoringFeatureSet(Settings.EMPTY, monitoring, licenseState, exporters);
licenseState = mock(XPackLicenseState.class);
exporters = mock(Exporters.class);
}
public void testAvailable() throws Exception {
MonitoringFeatureSet featureSet = new MonitoringFeatureSet(Settings.EMPTY, licenseState, exporters);
boolean available = randomBoolean(); boolean available = randomBoolean();
when(licenseState.isMonitoringAllowed()).thenReturn(available); when(licenseState.isMonitoringAllowed()).thenReturn(available);
assertThat(featureSet.available(), is(available)); assertThat(featureSet.available(), is(available));
} }
public void testEnabledSetting() throws Exception { public void testEnabledSetting() {
boolean enabled = randomBoolean(); boolean enabled = randomBoolean();
Settings.Builder settings = Settings.builder(); Settings.Builder settings = Settings.builder();
settings.put("xpack.monitoring.enabled", enabled); settings.put("xpack.monitoring.enabled", enabled);
MonitoringFeatureSet featureSet = new MonitoringFeatureSet(settings.build(), licenseState, exporters); MonitoringFeatureSet featureSet = new MonitoringFeatureSet(settings.build(), monitoring, licenseState, exporters);
assertThat(featureSet.enabled(), is(enabled)); assertThat(featureSet.enabled(), is(enabled));
} }
public void testEnabledDefault() throws Exception { public void testEnabledDefault() {
MonitoringFeatureSet featureSet = new MonitoringFeatureSet(Settings.EMPTY, licenseState, exporters); MonitoringFeatureSet featureSet = new MonitoringFeatureSet(Settings.EMPTY, monitoring, licenseState, exporters);
assertThat(featureSet.enabled(), is(true)); assertThat(featureSet.enabled(), is(true));
} }
public void testUsage() throws Exception { public void testUsage() throws Exception {
// anything prior to 6.3 does not include collection_enabled (so defaults it to null)
List<Exporter> exporterList = new ArrayList<>(); final Version serializedVersion = randomFrom(Version.CURRENT, Version.V_6_3_0, Version.V_6_2_2);
final boolean collectionEnabled = randomBoolean();
int localCount = randomIntBetween(0, 5); int localCount = randomIntBetween(0, 5);
List<Exporter> exporterList = new ArrayList<>();
for (int i = 0; i < localCount; i++) { for (int i = 0; i < localCount; i++) {
Exporter exporter = mockExporter(LocalExporter.TYPE, true); Exporter exporter = mockExporter(LocalExporter.TYPE, true);
exporterList.add(exporter); exporterList.add(exporter);
@ -97,12 +94,14 @@ public class MonitoringFeatureSetTests extends ESTestCase {
} }
} }
when(exporters.iterator()).thenReturn(exporterList.iterator()); when(exporters.iterator()).thenReturn(exporterList.iterator());
when(monitoring.isMonitoringActive()).thenReturn(collectionEnabled);
MonitoringFeatureSet featureSet = new MonitoringFeatureSet(Settings.EMPTY, licenseState, exporters); MonitoringFeatureSet featureSet = new MonitoringFeatureSet(Settings.EMPTY, monitoring, licenseState, exporters);
PlainActionFuture<Usage> future = new PlainActionFuture<>(); PlainActionFuture<Usage> future = new PlainActionFuture<>();
featureSet.usage(future); featureSet.usage(future);
XPackFeatureSet.Usage monitoringUsage = future.get(); XPackFeatureSet.Usage monitoringUsage = future.get();
BytesStreamOutput out = new BytesStreamOutput(); BytesStreamOutput out = new BytesStreamOutput();
out.setVersion(serializedVersion);
monitoringUsage.writeTo(out); monitoringUsage.writeTo(out);
XPackFeatureSet.Usage serializedUsage = new MonitoringFeatureSetUsage(out.bytes().streamInput()); XPackFeatureSet.Usage serializedUsage = new MonitoringFeatureSetUsage(out.bytes().streamInput());
for (XPackFeatureSet.Usage usage : Arrays.asList(monitoringUsage, serializedUsage)) { for (XPackFeatureSet.Usage usage : Arrays.asList(monitoringUsage, serializedUsage)) {
@ -113,6 +112,11 @@ public class MonitoringFeatureSetTests extends ESTestCase {
usage.toXContent(builder, ToXContent.EMPTY_PARAMS); usage.toXContent(builder, ToXContent.EMPTY_PARAMS);
source = ObjectPath.createFromXContent(builder.contentType().xContent(), BytesReference.bytes(builder)); source = ObjectPath.createFromXContent(builder.contentType().xContent(), BytesReference.bytes(builder));
} }
if (usage == monitoringUsage || serializedVersion.onOrAfter(Version.V_6_3_0)) {
assertThat(source.evaluate("collection_enabled"), is(collectionEnabled));
} else {
assertThat(source.evaluate("collection_enabled"), is(nullValue()));
}
assertThat(source.evaluate("enabled_exporters"), is(notNullValue())); assertThat(source.evaluate("enabled_exporters"), is(notNullValue()));
if (localCount > 0) { if (localCount > 0) {
assertThat(source.evaluate("enabled_exporters.local"), is(localCount)); assertThat(source.evaluate("enabled_exporters.local"), is(localCount));

View File

@ -198,9 +198,8 @@ public class ClusterStatsCollectorTests extends BaseCollectorTestCase {
when(indexNameExpressionResolver.concreteIndices(clusterState, IndicesOptions.lenientExpandOpen(), "apm-*")) when(indexNameExpressionResolver.concreteIndices(clusterState, IndicesOptions.lenientExpandOpen(), "apm-*"))
.thenReturn(indices); .thenReturn(indices);
// Baz - Changed from Logstash to Monitoring featureset
final XPackUsageResponse xPackUsageResponse = new XPackUsageResponse( final XPackUsageResponse xPackUsageResponse = new XPackUsageResponse(
singletonList(new MonitoringFeatureSetUsage(true, true, null))); singletonList(new MonitoringFeatureSetUsage(true, true, false, null)));
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
final ActionFuture<XPackUsageResponse> xPackUsageFuture = (ActionFuture<XPackUsageResponse>) mock(ActionFuture.class); final ActionFuture<XPackUsageResponse> xPackUsageFuture = (ActionFuture<XPackUsageResponse>) mock(ActionFuture.class);

View File

@ -254,7 +254,7 @@ public class ClusterStatsMonitoringDocTests extends BaseMonitoringDocTestCase<Cl
.maxNodes(2) .maxNodes(2)
.build(); .build();
final List<XPackFeatureSet.Usage> usages = singletonList(new MonitoringFeatureSetUsage(false, true, null)); final List<XPackFeatureSet.Usage> usages = singletonList(new MonitoringFeatureSetUsage(false, true, false, null));
final NodeInfo mockNodeInfo = mock(NodeInfo.class); final NodeInfo mockNodeInfo = mock(NodeInfo.class);
when(mockNodeInfo.getVersion()).thenReturn(Version.V_6_0_0_alpha2); when(mockNodeInfo.getVersion()).thenReturn(Version.V_6_0_0_alpha2);
@ -556,7 +556,8 @@ public class ClusterStatsMonitoringDocTests extends BaseMonitoringDocTestCase<Cl
+ "\"xpack\":{" + "\"xpack\":{"
+ "\"monitoring\":{" + "\"monitoring\":{"
+ "\"available\":false," + "\"available\":false,"
+ "\"enabled\":true" + "\"enabled\":true,"
+ "\"collection_enabled\":false"
+ "}" + "}"
+ "}" + "}"
+ "}" + "}"