Search in sources :

Example 1 with HystrixCommandMetrics

use of com.netflix.hystrix.HystrixCommandMetrics in project Hystrix by Netflix.

the class HystrixServoMetricsPublisherCommandTest method testRollingCounters.

@Test
public void testRollingCounters() throws Exception {
    //execute 10 commands, then sleep for 2000ms to let these age out
    //execute 10 commands again, these should show up in rolling count
    HystrixCommandKey key = HystrixCommandKey.Factory.asKey("ServoCOMMAND-B");
    HystrixCircuitBreaker circuitBreaker = HystrixCircuitBreaker.Factory.getInstance(key);
    HystrixCommandProperties properties = new HystrixPropertiesCommandDefault(key, propertiesSetter);
    HystrixCommandMetrics metrics = HystrixCommandMetrics.getInstance(key, groupKey, properties);
    HystrixServoMetricsPublisherCommand servoPublisher = new HystrixServoMetricsPublisherCommand(key, groupKey, metrics, circuitBreaker, properties);
    servoPublisher.initialize();
    new SuccessCommand(key).execute();
    new SuccessCommand(key).execute();
    new SuccessCommand(key).execute();
    new TimeoutCommand(key).execute();
    new SuccessCommand(key).execute();
    new FailureCommand(key).execute();
    new SuccessCommand(key).execute();
    new SuccessCommand(key).execute();
    new SuccessCommand(key).execute();
    new SuccessCommand(key).execute();
    Thread.sleep(2000);
    new SuccessCommand(key).execute();
    new SuccessCommand(key).execute();
    new SuccessCommand(key).execute();
    new TimeoutCommand(key).execute();
    new SuccessCommand(key).execute();
    new FailureCommand(key).execute();
    new TimeoutCommand(key).execute();
    new TimeoutCommand(key).execute();
    new TimeoutCommand(key).execute();
    new TimeoutCommand(key).execute();
    //time for 1 bucket roll
    Thread.sleep(100);
    assertEquals(4L, servoPublisher.getRollingMonitor("success", HystrixEventType.SUCCESS).getValue());
    assertEquals(5L, servoPublisher.getRollingMonitor("timeout", HystrixEventType.TIMEOUT).getValue());
    assertEquals(1L, servoPublisher.getRollingMonitor("failure", HystrixEventType.FAILURE).getValue());
    assertEquals(6L, servoPublisher.getRollingMonitor("falback_success", HystrixEventType.FALLBACK_SUCCESS).getValue());
}
Also used : HystrixCommandKey(com.netflix.hystrix.HystrixCommandKey) HystrixCommandProperties(com.netflix.hystrix.HystrixCommandProperties) HystrixCircuitBreaker(com.netflix.hystrix.HystrixCircuitBreaker) HystrixPropertiesCommandDefault(com.netflix.hystrix.strategy.properties.HystrixPropertiesCommandDefault) HystrixCommandMetrics(com.netflix.hystrix.HystrixCommandMetrics) Test(org.junit.Test)

Example 2 with HystrixCommandMetrics

use of com.netflix.hystrix.HystrixCommandMetrics in project Hystrix by Netflix.

the class HystrixDashboardStreamTest method testStreamHasData.

@Test
public void testStreamHasData() throws Exception {
    final AtomicBoolean commandShowsUp = new AtomicBoolean(false);
    final CountDownLatch latch = new CountDownLatch(1);
    final int NUM = 10;
    for (int i = 0; i < 2; i++) {
        HystrixCommand<Integer> cmd = Command.from(groupKey, commandKey, HystrixEventType.SUCCESS, 50);
        cmd.observe();
    }
    stream.observe().take(NUM).subscribe(new Subscriber<HystrixDashboardStream.DashboardData>() {

        @Override
        public void onCompleted() {
            System.out.println(System.currentTimeMillis() + " : " + Thread.currentThread().getName() + " OnCompleted");
            latch.countDown();
        }

        @Override
        public void onError(Throwable e) {
            System.out.println(System.currentTimeMillis() + " : " + Thread.currentThread().getName() + " OnError : " + e);
            latch.countDown();
        }

        @Override
        public void onNext(HystrixDashboardStream.DashboardData dashboardData) {
            System.out.println(System.currentTimeMillis() + " : " + Thread.currentThread().getName() + " : Received data with : " + dashboardData.commandMetrics.size() + " commands");
            for (HystrixCommandMetrics metrics : dashboardData.commandMetrics) {
                if (metrics.getCommandKey().equals(commandKey)) {
                    commandShowsUp.set(true);
                }
            }
        }
    });
    assertTrue(latch.await(10000, TimeUnit.MILLISECONDS));
    assertTrue(commandShowsUp.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CountDownLatch(java.util.concurrent.CountDownLatch) HystrixCommandMetrics(com.netflix.hystrix.HystrixCommandMetrics) CommandStreamTest(com.netflix.hystrix.metric.CommandStreamTest) Test(org.junit.Test)

Example 3 with HystrixCommandMetrics

use of com.netflix.hystrix.HystrixCommandMetrics in project Hystrix by Netflix.

the class HystrixCommandAsyncDemo method startMetricsMonitor.

public void startMetricsMonitor(final boolean shouldLog) {
    Thread t = new Thread(new Runnable() {

        @Override
        public void run() {
            while (true) {
                // wait 5 seconds on each loop
                try {
                    Thread.sleep(5000);
                } catch (Exception e) {
                // ignore
                }
                // we are using default names so can use class.getSimpleName() to derive the keys
                HystrixCommandMetrics creditCardMetrics = HystrixCommandMetrics.getInstance(HystrixCommandKey.Factory.asKey(CreditCardCommand.class.getSimpleName()));
                HystrixCommandMetrics orderMetrics = HystrixCommandMetrics.getInstance(HystrixCommandKey.Factory.asKey(GetOrderCommand.class.getSimpleName()));
                HystrixCommandMetrics userAccountMetrics = HystrixCommandMetrics.getInstance(HystrixCommandKey.Factory.asKey(GetUserAccountCommand.class.getSimpleName()));
                HystrixCommandMetrics paymentInformationMetrics = HystrixCommandMetrics.getInstance(HystrixCommandKey.Factory.asKey(GetPaymentInformationCommand.class.getSimpleName()));
                if (shouldLog) {
                    // print out metrics
                    StringBuilder out = new StringBuilder();
                    out.append("\n");
                    out.append("#####################################################################################").append("\n");
                    out.append("# CreditCardCommand: " + getStatsStringFromMetrics(creditCardMetrics)).append("\n");
                    out.append("# GetOrderCommand: " + getStatsStringFromMetrics(orderMetrics)).append("\n");
                    out.append("# GetUserAccountCommand: " + getStatsStringFromMetrics(userAccountMetrics)).append("\n");
                    out.append("# GetPaymentInformationCommand: " + getStatsStringFromMetrics(paymentInformationMetrics)).append("\n");
                    out.append("#####################################################################################").append("\n");
                    System.out.println(out.toString());
                }
            }
        }

        private String getStatsStringFromMetrics(HystrixCommandMetrics metrics) {
            StringBuilder m = new StringBuilder();
            if (metrics != null) {
                HealthCounts health = metrics.getHealthCounts();
                m.append("Requests: ").append(health.getTotalRequests()).append(" ");
                m.append("Errors: ").append(health.getErrorCount()).append(" (").append(health.getErrorPercentage()).append("%)   ");
                m.append("Mean: ").append(metrics.getExecutionTimePercentile(50)).append(" ");
                m.append("75th: ").append(metrics.getExecutionTimePercentile(75)).append(" ");
                m.append("90th: ").append(metrics.getExecutionTimePercentile(90)).append(" ");
                m.append("99th: ").append(metrics.getExecutionTimePercentile(99)).append(" ");
            }
            return m.toString();
        }
    });
    t.setDaemon(true);
    t.start();
}
Also used : HystrixContextRunnable(com.netflix.hystrix.strategy.concurrency.HystrixContextRunnable) HealthCounts(com.netflix.hystrix.HystrixCommandMetrics.HealthCounts) HystrixCommandMetrics(com.netflix.hystrix.HystrixCommandMetrics)

Example 4 with HystrixCommandMetrics

use of com.netflix.hystrix.HystrixCommandMetrics in project Hystrix by Netflix.

the class SerialHystrixDashboardData method writeDashboardData.

private static void writeDashboardData(JsonGenerator json, HystrixDashboardStream.DashboardData dashboardData) {
    try {
        json.writeStartArray();
        for (HystrixCommandMetrics commandMetrics : dashboardData.getCommandMetrics()) {
            writeCommandMetrics(commandMetrics, json);
        }
        for (HystrixThreadPoolMetrics threadPoolMetrics : dashboardData.getThreadPoolMetrics()) {
            writeThreadPoolMetrics(threadPoolMetrics, json);
        }
        for (HystrixCollapserMetrics collapserMetrics : dashboardData.getCollapserMetrics()) {
            writeCollapserMetrics(collapserMetrics, json);
        }
        json.writeEndArray();
        json.close();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : HystrixThreadPoolMetrics(com.netflix.hystrix.HystrixThreadPoolMetrics) HystrixCommandMetrics(com.netflix.hystrix.HystrixCommandMetrics) HystrixCollapserMetrics(com.netflix.hystrix.HystrixCollapserMetrics) IOException(java.io.IOException)

Example 5 with HystrixCommandMetrics

use of com.netflix.hystrix.HystrixCommandMetrics in project ratpack by ratpack.

the class HystrixCommandMetricsJsonMapper method apply.

@Override
public String apply(HystrixCommandMetrics commandMetrics) throws Exception {
    HystrixCommandKey key = commandMetrics.getCommandKey();
    HystrixCircuitBreaker circuitBreaker = HystrixCircuitBreaker.Factory.getInstance(key);
    StringWriter jsonString = new StringWriter();
    JsonGenerator json = jsonFactory.createGenerator(jsonString);
    json.writeStartObject();
    json.writeStringField("type", "HystrixCommand");
    json.writeStringField("name", key.name());
    json.writeStringField("group", commandMetrics.getCommandGroup().name());
    json.writeNumberField("currentTime", System.currentTimeMillis());
    // circuit breaker
    if (circuitBreaker == null) {
        // circuit breaker is disabled and thus never open
        json.writeBooleanField("isCircuitBreakerOpen", false);
    } else {
        json.writeBooleanField("isCircuitBreakerOpen", circuitBreaker.isOpen());
    }
    HystrixCommandMetrics.HealthCounts healthCounts = commandMetrics.getHealthCounts();
    json.writeNumberField("errorPercentage", healthCounts.getErrorPercentage());
    json.writeNumberField("errorCount", healthCounts.getErrorCount());
    json.writeNumberField("requestCount", healthCounts.getTotalRequests());
    // rolling counters
    json.writeNumberField("rollingCountBadRequests", commandMetrics.getRollingCount(HystrixRollingNumberEvent.BAD_REQUEST));
    json.writeNumberField("rollingCountCollapsedRequests", commandMetrics.getRollingCount(HystrixRollingNumberEvent.COLLAPSED));
    json.writeNumberField("rollingCountEmit", commandMetrics.getRollingCount(HystrixRollingNumberEvent.EMIT));
    json.writeNumberField("rollingCountExceptionsThrown", commandMetrics.getRollingCount(HystrixRollingNumberEvent.EXCEPTION_THROWN));
    json.writeNumberField("rollingCountFailure", commandMetrics.getRollingCount(HystrixRollingNumberEvent.FAILURE));
    json.writeNumberField("rollingCountEmit", commandMetrics.getRollingCount(HystrixRollingNumberEvent.FALLBACK_EMIT));
    json.writeNumberField("rollingCountFallbackFailure", commandMetrics.getRollingCount(HystrixRollingNumberEvent.FALLBACK_FAILURE));
    json.writeNumberField("rollingCountFallbackRejection", commandMetrics.getRollingCount(HystrixRollingNumberEvent.FALLBACK_REJECTION));
    json.writeNumberField("rollingCountFallbackSuccess", commandMetrics.getRollingCount(HystrixRollingNumberEvent.FALLBACK_SUCCESS));
    json.writeNumberField("rollingCountResponsesFromCache", commandMetrics.getRollingCount(HystrixRollingNumberEvent.RESPONSE_FROM_CACHE));
    json.writeNumberField("rollingCountSemaphoreRejected", commandMetrics.getRollingCount(HystrixRollingNumberEvent.SEMAPHORE_REJECTED));
    json.writeNumberField("rollingCountShortCircuited", commandMetrics.getRollingCount(HystrixRollingNumberEvent.SHORT_CIRCUITED));
    json.writeNumberField("rollingCountSuccess", commandMetrics.getRollingCount(HystrixRollingNumberEvent.SUCCESS));
    json.writeNumberField("rollingCountThreadPoolRejected", commandMetrics.getRollingCount(HystrixRollingNumberEvent.THREAD_POOL_REJECTED));
    json.writeNumberField("rollingCountTimeout", commandMetrics.getRollingCount(HystrixRollingNumberEvent.TIMEOUT));
    json.writeNumberField("currentConcurrentExecutionCount", commandMetrics.getCurrentConcurrentExecutionCount());
    json.writeNumberField("rollingMaxConcurrentExecutionCount", commandMetrics.getRollingMaxConcurrentExecutions());
    // latency percentiles
    json.writeNumberField("latencyExecute_mean", commandMetrics.getExecutionTimeMean());
    json.writeObjectFieldStart("latencyExecute");
    json.writeNumberField("0", commandMetrics.getExecutionTimePercentile(0));
    json.writeNumberField("25", commandMetrics.getExecutionTimePercentile(25));
    json.writeNumberField("50", commandMetrics.getExecutionTimePercentile(50));
    json.writeNumberField("75", commandMetrics.getExecutionTimePercentile(75));
    json.writeNumberField("90", commandMetrics.getExecutionTimePercentile(90));
    json.writeNumberField("95", commandMetrics.getExecutionTimePercentile(95));
    json.writeNumberField("99", commandMetrics.getExecutionTimePercentile(99));
    json.writeNumberField("99.5", commandMetrics.getExecutionTimePercentile(99.5));
    json.writeNumberField("100", commandMetrics.getExecutionTimePercentile(100));
    json.writeEndObject();
    //
    json.writeNumberField("latencyTotal_mean", commandMetrics.getTotalTimeMean());
    json.writeObjectFieldStart("latencyTotal");
    json.writeNumberField("0", commandMetrics.getTotalTimePercentile(0));
    json.writeNumberField("25", commandMetrics.getTotalTimePercentile(25));
    json.writeNumberField("50", commandMetrics.getTotalTimePercentile(50));
    json.writeNumberField("75", commandMetrics.getTotalTimePercentile(75));
    json.writeNumberField("90", commandMetrics.getTotalTimePercentile(90));
    json.writeNumberField("95", commandMetrics.getTotalTimePercentile(95));
    json.writeNumberField("99", commandMetrics.getTotalTimePercentile(99));
    json.writeNumberField("99.5", commandMetrics.getTotalTimePercentile(99.5));
    json.writeNumberField("100", commandMetrics.getTotalTimePercentile(100));
    json.writeEndObject();
    // property values for reporting what is actually seen by the command rather than what was set somewhere
    HystrixCommandProperties commandProperties = commandMetrics.getProperties();
    json.writeNumberField("propertyValue_circuitBreakerRequestVolumeThreshold", commandProperties.circuitBreakerRequestVolumeThreshold().get());
    json.writeNumberField("propertyValue_circuitBreakerSleepWindowInMilliseconds", commandProperties.circuitBreakerSleepWindowInMilliseconds().get());
    json.writeNumberField("propertyValue_circuitBreakerErrorThresholdPercentage", commandProperties.circuitBreakerErrorThresholdPercentage().get());
    json.writeBooleanField("propertyValue_circuitBreakerForceOpen", commandProperties.circuitBreakerForceOpen().get());
    json.writeBooleanField("propertyValue_circuitBreakerForceClosed", commandProperties.circuitBreakerForceClosed().get());
    json.writeBooleanField("propertyValue_circuitBreakerEnabled", commandProperties.circuitBreakerEnabled().get());
    json.writeStringField("propertyValue_executionIsolationStrategy", commandProperties.executionIsolationStrategy().get().name());
    json.writeNumberField("propertyValue_executionIsolationThreadTimeoutInMilliseconds", commandProperties.executionTimeoutInMilliseconds().get());
    json.writeNumberField("propertyValue_executionTimeoutInMilliseconds", commandProperties.executionTimeoutInMilliseconds().get());
    json.writeBooleanField("propertyValue_executionIsolationThreadInterruptOnTimeout", commandProperties.executionIsolationThreadInterruptOnTimeout().get());
    json.writeStringField("propertyValue_executionIsolationThreadPoolKeyOverride", commandProperties.executionIsolationThreadPoolKeyOverride().get());
    json.writeNumberField("propertyValue_executionIsolationSemaphoreMaxConcurrentRequests", commandProperties.executionIsolationSemaphoreMaxConcurrentRequests().get());
    json.writeNumberField("propertyValue_fallbackIsolationSemaphoreMaxConcurrentRequests", commandProperties.fallbackIsolationSemaphoreMaxConcurrentRequests().get());
    /*
    * The following are commented out as these rarely change and are verbose for streaming for something people don't change.
    * We could perhaps allow a property or request argument to include these.
    */
    // json.put("propertyValue_metricsRollingPercentileEnabled", commandProperties.metricsRollingPercentileEnabled().get());
    // json.put("propertyValue_metricsRollingPercentileBucketSize", commandProperties.metricsRollingPercentileBucketSize().get());
    // json.put("propertyValue_metricsRollingPercentileWindow", commandProperties.metricsRollingPercentileWindowInMilliseconds().get());
    // json.put("propertyValue_metricsRollingPercentileWindowBuckets", commandProperties.metricsRollingPercentileWindowBuckets().get());
    // json.put("propertyValue_metricsRollingStatisticalWindowBuckets", commandProperties.metricsRollingStatisticalWindowBuckets().get());
    json.writeNumberField("propertyValue_metricsRollingStatisticalWindowInMilliseconds", commandProperties.metricsRollingStatisticalWindowInMilliseconds().get());
    json.writeBooleanField("propertyValue_requestCacheEnabled", commandProperties.requestCacheEnabled().get());
    json.writeBooleanField("propertyValue_requestLogEnabled", commandProperties.requestLogEnabled().get());
    // this will get summed across all instances in a cluster
    json.writeNumberField("reportingHosts", 1);
    json.writeEndObject();
    json.close();
    return jsonString.getBuffer().toString();
}
Also used : HystrixCommandKey(com.netflix.hystrix.HystrixCommandKey) StringWriter(java.io.StringWriter) HystrixCommandProperties(com.netflix.hystrix.HystrixCommandProperties) HystrixCircuitBreaker(com.netflix.hystrix.HystrixCircuitBreaker) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) HystrixCommandMetrics(com.netflix.hystrix.HystrixCommandMetrics)

Aggregations

HystrixCommandMetrics (com.netflix.hystrix.HystrixCommandMetrics)9 HystrixCircuitBreaker (com.netflix.hystrix.HystrixCircuitBreaker)5 HystrixCommandKey (com.netflix.hystrix.HystrixCommandKey)5 HystrixCommandProperties (com.netflix.hystrix.HystrixCommandProperties)5 Test (org.junit.Test)4 HystrixPropertiesCommandDefault (com.netflix.hystrix.strategy.properties.HystrixPropertiesCommandDefault)3 HystrixCollapserMetrics (com.netflix.hystrix.HystrixCollapserMetrics)2 HystrixThreadPoolMetrics (com.netflix.hystrix.HystrixThreadPoolMetrics)2 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)1 HealthCounts (com.netflix.hystrix.HystrixCommandMetrics.HealthCounts)1 CommandStreamTest (com.netflix.hystrix.metric.CommandStreamTest)1 HystrixContextRunnable (com.netflix.hystrix.strategy.concurrency.HystrixContextRunnable)1 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 ArrayList (java.util.ArrayList)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Observable (rx.Observable)1 Subscription (rx.Subscription)1