A Netflix Hystrix module that exposes circuit breaker and thread pool metrics in Server-Sent Events format for real-time monitoring and dashboard integration
—
Servlet that streams individual Hystrix request events as they occur, providing detailed information about each request execution including events, latencies, and collapser information.
Servlet that writes SSE JSON data every time a Hystrix request is made, providing detailed execution information.
/**
* Servlet that writes SSE JSON every time a request is made
* Provides detailed request execution information including events and latencies
*/
public class HystrixRequestEventsSseServlet extends HystrixSampleSseServlet {
/**
* Default constructor using HystrixRequestEventsStream and default delay
*/
public HystrixRequestEventsSseServlet();
/**
* Package-private constructor for testing with custom stream and delay
* @param sampleStream Observable stream of request events
* @param pausePollerThreadDelayInMs Delay between polling cycles in milliseconds
*/
HystrixRequestEventsSseServlet(Observable<HystrixRequestEvents> sampleStream, int pausePollerThreadDelayInMs);
/**
* Returns maximum number of concurrent connections allowed
* @return Maximum concurrent connections (default: 5)
*/
protected int getMaxNumberConcurrentConnectionsAllowed();
/**
* Returns current number of active connections
* @return Current connection count
*/
protected int getNumberCurrentConnections();
/**
* Atomically increments and returns current concurrent connection count
* @return New connection count after increment
*/
protected int incrementAndGetCurrentConcurrentConnections();
/**
* Atomically decrements current concurrent connection count
*/
protected void decrementCurrentConcurrentConnections();
}Legacy JSON stream utility for converting request events to JSON format.
/**
* Stream that converts HystrixRequestEvents into JSON
* @deprecated Since 1.5.4 - prefer mapping serialization on HystrixRequestEventsStream.observe()
*/
@Deprecated
public class HystrixRequestEventsJsonStream {
/**
* Get the underlying request events stream
* @return Observable stream of request events
*/
public Observable<HystrixRequestEvents> getStream();
/**
* Convert collection of request events to JSON string
* @param requests Collection of request events to convert
* @return JSON string representation
* @throws IOException if JSON generation fails
*/
public static String convertRequestsToJson(Collection<HystrixRequestEvents> requests) throws IOException;
/**
* Convert single request event to JSON string
* @param request Request event to convert
* @return JSON string representation
* @throws IOException if JSON generation fails
*/
public static String convertRequestToJson(HystrixRequestEvents request) throws IOException;
/**
* Write request as JSON to a JsonGenerator
* @param json JsonGenerator to write to
* @param request Request event to write
* @throws IOException if JSON writing fails
*/
private static void writeRequestAsJson(JsonGenerator json, HystrixRequestEvents request) throws IOException;
/**
* Convert execution signature to JSON
* @param json JsonGenerator to write to
* @param executionSignature Execution signature containing command details
* @param latencies List of execution latencies in milliseconds
* @throws IOException if JSON writing fails
*/
private static void convertExecutionToJson(JsonGenerator json, HystrixRequestEvents.ExecutionSignature executionSignature, List<Integer> latencies) throws IOException;
}
/**
* Request events types for detailed request information
*/
public interface HystrixRequestEvents.ExecutionSignature {
String getCommandName();
ExecutionResult.EventCounts getEventCounts();
int getCachedCount();
HystrixCollapserKey getCollapserKey();
int getCollapserBatchSize();
}Web.xml Configuration:
<servlet>
<servlet-name>HystrixRequestEventsSseServlet</servlet-name>
<servlet-class>com.netflix.hystrix.contrib.requests.stream.HystrixRequestEventsSseServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HystrixRequestEventsSseServlet</servlet-name>
<url-pattern>/hystrix/requests.stream</url-pattern>
</servlet-mapping>Usage Examples:
// Deploy servlet via web.xml configuration
// Access stream endpoint
curl http://localhost:8080/app/hystrix/requests.stream
// Custom request events processing (using deprecated API)
HystrixRequestEventsJsonStream stream = new HystrixRequestEventsJsonStream();
stream.getStream().subscribe(requestEvents -> {
String json = HystrixRequestEventsJsonStream.convertRequestToJson(requestEvents);
System.out.println("Request event: " + json);
});The servlet outputs detailed request execution information:
data: [
{
"name": "GetUser",
"events": [
"SUCCESS"
],
"latencies": [45],
"cached": 0
},
{
"name": "GetUserPreferences",
"events": [
"TIMEOUT",
"FALLBACK_SUCCESS"
],
"latencies": [800],
"cached": 0
},
{
"name": "GetUserProfile",
"events": [
{
"name": "FAILURE",
"count": 3
},
"FALLBACK_SUCCESS"
],
"latencies": [23, 45, 67],
"cached": 2,
"collapsed": {
"name": "UserDataCollapser",
"count": 5
}
}
]Common Hystrix event types that appear in request events:
SUCCESS - Command executed successfullyFAILURE - Command execution failedTIMEOUT - Command execution timed outSHORT_CIRCUITED - Circuit breaker short-circuited the requestTHREAD_POOL_REJECTED - Thread pool rejected the requestSEMAPHORE_REJECTED - Semaphore rejected the requestFALLBACK_SUCCESS - Fallback executed successfullyFALLBACK_FAILURE - Fallback execution failedFALLBACK_REJECTION - Fallback was rejectedFALLBACK_MISSING - No fallback was implementedEXCEPTION_THROWN - Exception was thrown during executionRESPONSE_FROM_CACHE - Response served from cacheCOLLAPSED - Request was collapsed (batched)EMIT - Observable emitted a valueBAD_REQUEST - Request was considered bad/invalidname - Command name that was executedevents - Array of events that occurred during execution (strings or objects with count)latencies - Array of execution latencies in millisecondscached - Number of cached responses (if > 0)collapsed - Information about request collapsing/batching (if present)
name - Name of the collapser that batched the requestcount - Size of the batch this request was part ofUses the same connection management pattern as other SSE servlets:
// Configuration property for max concurrent connections
public static final String MAX_CONCURRENT_CONNECTIONS_PROPERTY = "hystrix.config.stream.maxConcurrentConnections";
public static final int DEFAULT_MAX_CONCURRENT_CONNECTIONS = 5;// Old approach (deprecated)
HystrixRequestEventsJsonStream jsonStream = new HystrixRequestEventsJsonStream();
jsonStream.getStream().subscribe(events -> {
String json = HystrixRequestEventsJsonStream.convertRequestToJson(events);
// process json
});
// New approach (recommended)
HystrixRequestEventsStream.getInstance()
.observe()
.map(SerialHystrixRequestEvents::toJsonString)
.subscribe(json -> {
// process json
});Install with Tessl CLI
npx tessl i tessl/maven-com-netflix-hystrix--hystrix-metrics-event-stream