在Graphite中存储Hystrix的几个月历史指标

Hystrix的杀手级功能之一是低延迟,数据密集型且美观的仪表板










在Graphite中存储Hystrix的几个月历史指标

即使这只是Hystrix实际操作的副作用(断路器,线程池,超时等),它也往往是最令人印象深刻的功能。 为了使其工作,您必须包括hystrix-metrics-event-stream依赖项:

<dependency>
    <groupId>com.netflix.hystrix</groupId>
    <artifactId>hystrix-metrics-event-stream</artifactId>
    <version>1.4.0-RC6</version>
</dependency>

并注册内置的servlet,例如在嵌入式Jetty中:

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
 
//...
 
Server server = new Server(8090);
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.NO_SESSIONS);
server.setHandler(context);
final HystrixMetricsStreamServlet servlet = new HystrixMetricsStreamServlet();
final ServletHolder holder = new ServletHolder(servlet);
context.addServlet(holder, "/hystrix.stream");
server.start();

当然,如果您已经有一个Web应用程序,则要简单得多。 这是Spring Boot中的一个示例:

@Bean
public ServletRegistrationBean servletRegistrationBean() {
    return new ServletRegistrationBean(new HystrixMetricsStreamServlet(), "/hystrix.stream");
}

从现在开始,您的应用程序将以JSON格式流式传输实时指标,可以使用开源仪表板轻松使用它,该仪表板几乎完全用JavaScript编写:

$ git clone [email protected]:Netflix/Hystrix.git
$ cd Hystrix
$ ./gradlew :hystrix-dashboard:jettyRun

几秒钟后,您可以浏览到localhost:7979并指向/hystrix.stream servlet。 假设您的应用程序是集群的,则很可能会将Turbine添加到聚会中。

如果您使用的是Hystrix,您已经了解了所有这些。 但是,我最常被问到的问题之一是: 为什么这些指标这么短 的确,如果您查看上面的仪表板,度量值将通过滑动窗口(从10秒到1分钟)进行汇总。 如果您收到有关生产中某些情况的自动电子邮件通知,经历了短暂的缓慢运行或听到了客户的性能问题,则有关此事件的相关统计信息可能已经丢失–或随后被普遍不稳定所掩盖。

这实际上是设计使然–您不能同时拥有低延迟,接近实时的统计数据,而且统计数据也很持久,即使没有几个月就可以浏览数天。 但是您不需要两个用于短期指标和长期趋势的监视系统。 相反,您可以直接使用Hystrix指标提供Graphite 几乎没有代码,到处都是胶水。

将指标发布到Dropwizard指标

事实证明,所有构建块均可用并且已准备就绪,您只需连接它们即可。 Hystrix指标不仅限于发布servlet,您还可以插入其他使用者,例如Dropwizard指标

<dependency>
    <groupId>com.netflix.hystrix</groupId>
    <artifactId>hystrix-codahale-metrics-publisher</artifactId>
    <version>1.4.0-RC6</version>
    <exclusions>
        <exclusion>
            <groupId>com.codahale.metrics</groupId>
            <artifactId>metrics-core</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>io.dropwizard.metrics</groupId>
    <artifactId>metrics-core</artifactId>
    <version>3.1.0</version>
</dependency>

您必须显式连接这两个库,我使用Spring Boot进行编排,请注意, MetricRegistry 由Boot自动创建

@Bean
HystrixMetricsPublisher hystrixMetricsPublisher(MetricRegistry metricRegistry) {
    HystrixCodaHaleMetricsPublisher publisher = new HystrixCodaHaleMetricsPublisher(metricRegistry);
    HystrixPlugins.getInstance().registerMetricsPublisher(publisher);
    return publisher;
}

Hystrix在发布Dropwizard指标时,我们可以将这些指标重定向到SLF4J,JMX或Graphite!

石墨和格拉法纳

我们还需要一个依赖项:

<dependency>
    <groupId>io.dropwizard.metrics</groupId>
    <artifactId>metrics-graphite</artifactId>
    <version>3.1.0</version>
</dependency>

这使metrics库可以将数据直接发布到Graphite,而又需要一点点胶水:

@Bean
public GraphiteReporter graphiteReporter(MetricRegistry metricRegistry) {
    final GraphiteReporter reporter = GraphiteReporter
            .forRegistry(metricRegistry)
            .build(graphite());
    reporter.start(1, TimeUnit.SECONDS);
    return reporter;
}
 
@Bean
GraphiteSender graphite() {
    return new Graphite(new InetSocketAddress("localhost", 2003));
}

显然,您想调整Graphite地址。 设置Graphite和Grafana很麻烦,幸运的是有一个Docker镜像

$ docker run -d \
    -p 8070:80 -p 2003:2003 -p 8125:8125/udp -p 8126:8126 \
    --name grafana-dashboard \
    choopooly/grafana_graphite

如果一切设置正确,请直接进入localhost:8070并使用一些仪表板。 这是我的:

在Graphite中存储Hystrix的几个月历史指标

新的可能性

内置的Hystrix仪表板反应灵敏且实用。 然而,拥有数天,数周甚至数月的统计信息可以提供很多可能性。 内置仪表板无法实现的功能选择,您可以使用Graphite / Grafana轻松设置:

  • 月的统计资料(显然),相比秒
  • 标准仪表板中忽略的指标,例如较低的百分位数,总计数器等。
  • 某些指标的完整历史记录,而不是即时值(例如,线程池利用率)
  • 能够在单个图表上比较看似无关的指标,例如,几个不同的命令延迟与线程池队列大小的比较–全部具有完整的历史记录
  • 深入研究–查看几周或放大到几分钟

示例可以在前面的屏幕截图中找到。 这完全取决于您的用例,但除非您的系统着火了,否则您可以在事件发生几小时或几周后检查的长期统计数据可能比内置仪表板更有用。

* Hystrix指标发布者中有一个小错误 ,将在1.4.0-RC7中修复
**上述功能可在我们的微红外开源项目中直接使用

翻译自: https://www.javacodegeeks.com/2015/02/storing-months-historical-metrics-hystrix-graphite.html