-
Notifications
You must be signed in to change notification settings - Fork 3.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[health] bootstrap HealthObserver from agent to API #16141
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,21 @@ | ||||||||||||||||||||||||||||||||||||||||||||||
package org.logstash.health; | ||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's not to forget adding Elastic License headers.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
import com.google.common.collect.Iterables; | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
import java.util.EnumSet; | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
public class HealthObserver { | ||||||||||||||||||||||||||||||||||||||||||||||
public final Status getStatus() { | ||||||||||||||||||||||||||||||||||||||||||||||
// INTERNAL-ONLY Proof-of-concept to show flow-through to API results | ||||||||||||||||||||||||||||||||||||||||||||||
switch (System.getProperty("logstash.apiStatus", "green")) { | ||||||||||||||||||||||||||||||||||||||||||||||
case "green": return Status.GREEN; | ||||||||||||||||||||||||||||||||||||||||||||||
case "yellow": return Status.YELLOW; | ||||||||||||||||||||||||||||||||||||||||||||||
case "red": return Status.RED; | ||||||||||||||||||||||||||||||||||||||||||||||
case "random": | ||||||||||||||||||||||||||||||||||||||||||||||
final EnumSet<Status> statuses = EnumSet.allOf(Status.class); | ||||||||||||||||||||||||||||||||||||||||||||||
return Iterables.get(statuses, new java.util.Random().nextInt(statuses.size())); | ||||||||||||||||||||||||||||||||||||||||||||||
default: | ||||||||||||||||||||||||||||||||||||||||||||||
return Status.UNKNOWN; | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package org.logstash.health; | ||
|
||
import com.fasterxml.jackson.annotation.JsonValue; | ||
|
||
public enum Status { | ||
UNKNOWN, | ||
GREEN, | ||
YELLOW, | ||
RED, | ||
; | ||
|
||
private final String externalValue = name().toLowerCase(); | ||
|
||
@JsonValue | ||
public String externalValue() { | ||
return externalValue; | ||
} | ||
|
||
/** | ||
* Combine this status with another status. | ||
* This method is commutative. | ||
* @param status the other status | ||
* @return the more-degraded of the two statuses. | ||
*/ | ||
public Status reduce(Status status) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I couldn't get that why we need reduce here?! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's part of future work, and slightly out of scope but a required part of getting this endpoint bootstrapped. Once we have many indicator reports, we will need to reduce their individual statuses into a summary status. Similar with an indicator report from many probes, each of which can degrade status. |
||
if (compareTo(status) >= 0) { | ||
return this; | ||
} else { | ||
return status; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package org.logstash.health; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.junit.Test; | ||
import org.junit.experimental.runners.Enclosed; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.Parameterized; | ||
import org.junit.runners.Parameterized.Parameter; | ||
import org.junit.runners.Parameterized.Parameters; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.EnumSet; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import static com.google.common.collect.Collections2.orderedPermutations; | ||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.logstash.health.Status.*; | ||
|
||
@RunWith(Enclosed.class) | ||
public class StatusTest { | ||
|
||
public static class Tests { | ||
@Test | ||
public void testReduceUnknown() { | ||
assertThat(UNKNOWN.reduce(UNKNOWN), is(UNKNOWN)); | ||
assertThat(UNKNOWN.reduce(GREEN), is(GREEN)); | ||
assertThat(UNKNOWN.reduce(YELLOW), is(YELLOW)); | ||
assertThat(UNKNOWN.reduce(RED), is(RED)); | ||
} | ||
|
||
@Test | ||
public void testReduceGreen() { | ||
assertThat(GREEN.reduce(UNKNOWN), is(GREEN)); | ||
assertThat(GREEN.reduce(GREEN), is(GREEN)); | ||
assertThat(GREEN.reduce(YELLOW), is(YELLOW)); | ||
assertThat(GREEN.reduce(RED), is(RED)); | ||
} | ||
|
||
@Test | ||
public void testReduceYellow() { | ||
assertThat(YELLOW.reduce(UNKNOWN), is(YELLOW)); | ||
assertThat(YELLOW.reduce(GREEN), is(YELLOW)); | ||
assertThat(YELLOW.reduce(YELLOW), is(YELLOW)); | ||
assertThat(YELLOW.reduce(RED), is(RED)); | ||
} | ||
|
||
@Test | ||
public void testReduceRed() { | ||
assertThat(RED.reduce(UNKNOWN), is(RED)); | ||
assertThat(RED.reduce(GREEN), is(RED)); | ||
assertThat(RED.reduce(YELLOW), is(RED)); | ||
assertThat(RED.reduce(RED), is(RED)); | ||
} | ||
} | ||
|
||
@RunWith(Parameterized.class) | ||
public static class JacksonSerialization { | ||
@Parameters(name = "{0}") | ||
public static Iterable<?> data() { | ||
return EnumSet.allOf(Status.class); | ||
} | ||
|
||
@Parameter | ||
public Status status; | ||
|
||
private final ObjectMapper mapper = new ObjectMapper(); | ||
|
||
@Test | ||
public void testSerialization() throws Exception { | ||
assertThat(mapper.writeValueAsString(status), is(equalTo('"' + status.name().toLowerCase() + '"'))); | ||
} | ||
} | ||
|
||
@RunWith(Parameterized.class) | ||
public static class ReduceCommutativeSpecification { | ||
@Parameters(name = "{0}<=>{1}") | ||
public static Collection<Object[]> data() { | ||
return getCombinations(EnumSet.allOf(Status.class), 2); | ||
} | ||
|
||
@Parameter(0) | ||
public Status statusA; | ||
@Parameter(1) | ||
public Status statusB; | ||
|
||
@Test | ||
public void testReduceCommutative() { | ||
assertThat(statusA.reduce(statusB), is(statusB.reduce(statusA))); | ||
} | ||
|
||
private static <T extends Comparable<T>> List<Object[]> getCombinations(Collection<T> source, int count) { | ||
return orderedPermutations(source).stream() | ||
.map((l) -> l.subList(0, count)) | ||
.map(ArrayList::new).peek(Collections::sort) | ||
.collect(Collectors.toSet()) | ||
.stream() | ||
.map(List::toArray) | ||
.collect(Collectors.toList()); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎉 finally...