com.netflix.hystrix.HystrixRequestLog Java Examples

The following examples show how to use com.netflix.hystrix.HystrixRequestLog. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CommandWithFallbackViaNetwork.java    From tools-journey with Apache License 2.0 6 votes vote down vote up
@Test
public void test() {
    HystrixRequestContext context = HystrixRequestContext.initializeContext();
    try {
        assertEquals(null, new CommandWithFallbackViaNetwork(1).execute());

        HystrixInvokableInfo<?> command1 = HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().toArray(new HystrixInvokableInfo<?>[2])[0];
        assertEquals("GetValueCommand", command1.getCommandKey().name());
        assertTrue(command1.getExecutionEvents().contains(HystrixEventType.FAILURE));

        HystrixInvokableInfo<?> command2 = HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().toArray(new HystrixInvokableInfo<?>[2])[1];
        assertEquals("GetValueFallbackCommand", command2.getCommandKey().name());
        assertTrue(command2.getExecutionEvents().contains(HystrixEventType.FAILURE));
    } finally {
        context.shutdown();
    }
}
 
Example #2
Source File: QuickStart.java    From javabase with Apache License 2.0 6 votes vote down vote up
private static void commandWithFallbackViaNetworkTest() {
    HystrixRequestContext context = HystrixRequestContext.initializeContext();
    try {
         log.info(new CommandWithFallbackViaNetwork(1).execute());

        HystrixInvokableInfo<?> command1 = HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().toArray(new HystrixInvokableInfo<?>[2])[0];
        log.info(command1.getCommandKey().name());
        log.info(""+command1.getExecutionEvents().contains(HystrixEventType.FAILURE));

        HystrixInvokableInfo<?> command2 = HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().toArray(new HystrixInvokableInfo<?>[2])[1];
        log.info(command2.getCommandKey().name());
        log.info(""+command2.getExecutionEvents().contains(HystrixEventType.FAILURE));
    } finally {
        context.shutdown();
    }
}
 
Example #3
Source File: HystrixContextFilter.java    From Poseidon with Apache License 2.0 5 votes vote down vote up
/**
 * Logs details like full exception stack trace for failed Hystrix commands.
 * A command might not have been executed (say threadpool/semaphore rejected,
 * short circuited). Command might have been executed but failed (say timed out,
 * command execution failed).
 *
 * This is required as Phantom's RequestLogger logs failures of sync command
 * executions alone (and not async command executions) and doesn't provide request
 * level view of all commands.
 *
 * We log global headers here as it typically contains request id
 */
private void logFailedHystrixCommands(ServletRequest request) {
    String url = ((HttpServletRequest) request).getPathInfo();
    Map<String, String> globalHeaders = RequestContext.get(HEADERS);

    HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().stream().filter(
            command -> command.isResponseTimedOut() || command.isFailedExecution() || command.isResponseShortCircuited() || command.isResponseRejected()
    ).forEach(
            command -> logger.error("URL: {}. Global headers: {}. Command: {}. Events: {}. Exception: {}",
                    url, globalHeaders, command.getCommandKey().name(), command.getExecutionEvents(),
                    command.getFailedExecutionException() == null ? "" : command.getFailedExecutionException().getMessage())
    );
}
 
Example #4
Source File: PoseidonConsumer.java    From Poseidon with Apache License 2.0 5 votes vote down vote up
private void logFailedHystrixCommands(AsyncConsumerRequest request) {
    String url = request.getUrl();
    Map<String, String> globalHeaders = RequestContext.get(HEADERS);

    HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().stream().filter(
            command -> command.isResponseTimedOut() || command.isFailedExecution() || command.isResponseShortCircuited() || command.isResponseRejected()
    ).forEach(
            command -> logger.error("URL: {}. Global headers: {}. Command: {}. Events: {}. Exception: {}",
                    url, globalHeaders, command.getCommandKey().name(), command.getExecutionEvents(),
                    command.getFailedExecutionException() == null ? "" : command.getFailedExecutionException().getMessage())
    );
}
 
Example #5
Source File: StartGatewayServer.java    From ReactiveLab with Apache License 2.0 4 votes vote down vote up
public static void main(String... args) {
    // initialize
    DiscoveryAndLoadBalancer.getFactory();
    // hystrix stream => http://localhost:9999
    startHystrixMetricsStream();

    System.out.println("Server => Starting at http://localhost:8080/");
    System.out.println("   Sample URLs: ");
    System.out.println("      - http://localhost:8080/device/home?userId=123");
    System.out.println("----------------------------------------------------------------");

    // start web services => http://localhost:8080
    RxNetty.createHttpServer(8080, (request, response) -> {
        if (request.getPath().contains("favicon.ico")) {
            return Observable.empty();
        }
        // System.out.println("Server => Request: " + request.getPath());
        return Observable.defer(() -> {
            HystrixRequestContext.initializeContext();
            try {
                return handleRoutes(request, response);
            } catch (Throwable e) {
                System.err.println("Server => Error [" + request.getPath() + "] => " + e);
                response.setStatus(HttpResponseStatus.BAD_REQUEST);
                return response.writeStringAndFlush("Error 500: Bad Request\n" + e.getMessage() + "\n");
            }
        }).onErrorResumeNext(error -> {
            System.err.println("Server => Error: " + error.getMessage());
            error.printStackTrace();
            return writeError(request, response, "Failed: " + error.getMessage());
        }).doOnTerminate(() -> {
            if (HystrixRequestContext.isCurrentThreadInitialized()) {
                System.out.println("Server => Request [" + request.getPath() + "] => " + HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString());
                HystrixRequestContext.getContextForCurrentThread().shutdown();
            } else {
                System.err.println("HystrixRequestContext not initialized for thread: " + Thread.currentThread());
            }
            response.close();
        });
    }).startAndWait();
}