import java.util.LinkedHashMap;
import java.util.Map;
+import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.action.admin.cluster.node.stats.NodeStats;
import org.elasticsearch.action.admin.cluster.node.stats.NodesStatsResponse;
import org.elasticsearch.action.admin.cluster.stats.ClusterStatsResponse;
import org.elasticsearch.cluster.health.ClusterHealthStatus;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.common.breaker.CircuitBreaker;
+import org.sonar.api.utils.log.Loggers;
import org.sonar.server.es.EsClient;
import static org.apache.commons.io.FileUtils.byteCountToDisplaySize;
@Override
public Map<String, Object> attributes() {
- Map<String, Object> attributes = new LinkedHashMap<>();
- attributes.put("State", getStateAsEnum());
- attributes.put("Indices", indexAttributes());
- attributes.put("Number of Nodes", getNumberOfNodes());
- attributes.put("Nodes", nodeAttributes());
- return attributes;
+ try {
+ Map<String, Object> attributes = new LinkedHashMap<>();
+ attributes.put("State", getStateAsEnum());
+ attributes.put("Indices", indexAttributes());
+ attributes.put("Number of Nodes", getNumberOfNodes());
+ attributes.put("Nodes", nodeAttributes());
+ return attributes;
+ } catch (Exception es) {
+ Loggers.get(EsMonitor.class).warn("Failed to retrieve ES attributes. There will be only a single \"state\" attribute.", es);
+ Map<String, Object> attributes = new LinkedHashMap<>();
+ attributes.put("State", (es.getCause() instanceof ElasticsearchException ? es.getCause().getMessage() : es.getMessage()));
+ return attributes;
+ }
}
private LinkedHashMap<String, LinkedHashMap<String, Object>> indexAttributes() {
package org.sonar.server.platform.monitoring;
import java.util.Map;
+import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.cluster.health.ClusterHealthStatus;
import org.junit.Rule;
import org.junit.Test;
import org.sonar.api.config.MapSettings;
+import org.sonar.server.es.EsClient;
import org.sonar.server.es.EsTester;
import org.sonar.server.issue.index.IssueIndexDefinition;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
public class EsMonitorTest {
@Rule
public EsTester esTester = new EsTester(new IssueIndexDefinition(new MapSettings()));
- EsMonitor underTest = new EsMonitor(esTester.client());
+ private EsMonitor underTest = new EsMonitor(esTester.client());
@Test
public void name() {
assertThat((int) indexAttributes.get("Shards")).isGreaterThan(0);
assertThat(indexAttributes.get("Store Size")).isNotNull();
}
+
+ @Test
+ public void attributes_displays_exception_message_when_cause_null_when_client_fails() {
+ EsClient esClientMock = mock(EsClient.class);
+ EsMonitor underTest = new EsMonitor(esClientMock);
+ when(esClientMock.prepareClusterStats()).thenThrow(new RuntimeException("RuntimeException with no cause"));
+
+ Map<String, Object> attributes = underTest.attributes();
+ assertThat(attributes).hasSize(1);
+ assertThat(attributes.get("State")).isEqualTo("RuntimeException with no cause");
+ }
+
+ @Test
+ public void attributes_displays_exception_message_when_cause_is_not_ElasticSearchException_when_client_fails() {
+ EsClient esClientMock = mock(EsClient.class);
+ EsMonitor underTest = new EsMonitor(esClientMock);
+ when(esClientMock.prepareClusterStats()).thenThrow(new RuntimeException("RuntimeException with cause not ES", new IllegalArgumentException("some cause message")));
+
+ Map<String, Object> attributes = underTest.attributes();
+ assertThat(attributes).hasSize(1);
+ assertThat(attributes.get("State")).isEqualTo("RuntimeException with cause not ES");
+ }
+
+ @Test
+ public void attributes_displays_cause_message_when_cause_is_ElasticSearchException_when_client_fails() {
+ EsClient esClientMock = mock(EsClient.class);
+ EsMonitor underTest = new EsMonitor(esClientMock);
+ when(esClientMock.prepareClusterStats()).thenThrow(new RuntimeException("RuntimeException with ES cause", new ElasticsearchException("some cause message")));
+
+ Map<String, Object> attributes = underTest.attributes();
+ assertThat(attributes).hasSize(1);
+ assertThat(attributes.get("State")).isEqualTo("some cause message");
+ }
}