Back to Repositories

Validating Hystrix Command Metrics Publishing in Netflix/Hystrix

This test suite validates the integration between Hystrix and Codahale Metrics Publisher, focusing on command execution metrics publishing. It verifies proper metric registration and value reporting through the Codahale MetricRegistry system.

Test Coverage Overview

The test coverage focuses on command execution metrics and thread pool monitoring.

Key areas tested include:
  • Command success count metrics
  • Thread pool task count metrics
  • Metric registry integration
  • Hystrix command execution flow

Implementation Analysis

The testing approach uses JUnit with a combination of command execution and metric verification. The implementation demonstrates the publisher integration pattern using:

  • Custom Command class extension
  • MetricRegistry initialization
  • Hystrix plugin registration
  • Asynchronous metric verification

Technical Details

Testing tools and configuration:

  • JUnit test framework
  • Codahale MetricRegistry
  • HystrixCommand implementation
  • Custom metric naming convention
  • Thread sleep for metric propagation
  • Hamcrest matchers for assertions

Best Practices Demonstrated

The test suite exhibits several quality testing practices:

  • Clean test setup using @Before annotation
  • Proper isolation of test dependencies
  • Clear metric naming conventions
  • Effective use of static inner test classes
  • Appropriate timing management for async operations

netflix/hystrix

hystrix-contrib/hystrix-codahale-metrics-publisher/src/test/java/com/netflix/hystrix/contrib/codahalemetricspublisher/HystrixCodaHaleMetricsPublisherCommandTest.java

            
package com.netflix.hystrix.contrib.codahalemetricspublisher;

import com.codahale.metrics.MetricRegistry;
import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandKey;
import com.netflix.hystrix.HystrixThreadPoolKey;
import com.netflix.hystrix.strategy.HystrixPlugins;
import org.junit.Before;
import org.junit.Test;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;

public class HystrixCodaHaleMetricsPublisherCommandTest {
    private final MetricRegistry metricRegistry = new MetricRegistry();

    @Before
    public void setup() {
        HystrixPlugins.getInstance().registerMetricsPublisher(new HystrixCodaHaleMetricsPublisher("hystrix", metricRegistry));
    }

    @Test
    public void testCommandSuccess() throws InterruptedException {
        Command command = new Command();
        command.execute();

        Thread.sleep(1000);

        assertThat((Long) metricRegistry.getGauges().get("hystrix.testGroup.testCommand.countSuccess").getValue(), is(1L));
        assertThat((Long) metricRegistry.getGauges().get("hystrix.HystrixThreadPool.threadGroup.totalTaskCount").getValue(), is(1L));

    }

    private static class Command extends HystrixCommand<Void> {
        final static HystrixCommandKey hystrixCommandKey = HystrixCommandKey.Factory.asKey("testCommand");
        final static HystrixCommandGroupKey hystrixCommandGroupKey = HystrixCommandGroupKey.Factory.asKey("testGroup");
        final static HystrixThreadPoolKey hystrixThreadPool = HystrixThreadPoolKey.Factory.asKey("threadGroup");

        Command() {
            super(Setter.withGroupKey(hystrixCommandGroupKey).andCommandKey(hystrixCommandKey).andThreadPoolKey(hystrixThreadPool));
        }

        @Override
        protected Void run() throws Exception {
            return null;
        }
    }
}