Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Use Netflix Servo API to Publish Hystrix Metrics and Custom Metrics to AWS CloudWatch

DZone's Guide to

Use Netflix Servo API to Publish Hystrix Metrics and Custom Metrics to AWS CloudWatch

Trying to publish Hystrix metrics to AWS CloudWatch? Netflix Servo's API can help. Here's how.

· Integration Zone
Free Resource

The Integration Zone is brought to you in partnership with Cloud Elements.  What’s below the surface of an API integration? Download The Definitive Guide to API Integrations to start building an API strategy.

‘Servo’ is a platform library part of Netflix Open Source Software (OSS) Center- http://netflix.github.io/#repo

Servo provides a simple interface for exposing and publishing application metrics in Java - https://github.com/Netflix/servo/wiki

This following article provides information about how to publish your app’s custom metrics and Hystrix Resiliency specific metrics to AWS cloudwatch.

Dependencies in Your POM

 <dependency>         
    <groupId>com.netflix.hystrix</groupId>  
    <artifactId>hystrix-servo-metrics-publisher</artifactId>  
</dependency>  
<dependency>  
  <groupId>com.amazonaws</groupId>  
  <artifactId>aws-java-sdk-cloudwatch</artifactId>  
</dependency>  
<dependency>  
  <groupId>com.netflix.servo</groupId>  
  <artifactId>servo-aws</artifactId>  
</dependency>  
<dependency>  
  <groupId>com.netflix.servo</groupId>  
  <artifactId>servo-core</artifactId>  
</dependency>

Annotating the Custom Metrics as Servo Monitors

Once you identify the application’s fields to be used as custom metrics, you can register them using @Monitor Annotation

import com.netflix.servo.annotations.Monitor;    

@Monitor(name = "SampleCounter", type = DataSourceType.COUNTER, description = "Sample counting monitor")  
public final AtomicInteger counter = new AtomicInteger(0);    

@Monitor(name = "SampleGauge", type = DataSourceType.GAUGE, description = "Sample gauge monitor")  
privatelongsampleGuage = 0;

Register the class containing @Monitor fields

For instance, ‘example’ is name of an instance of a class containing @Monitor fields, then that instance need to be registered with Servo.

  import com.netflix.servo.monitor.Monitors;  

  Monitors.registerObject(example);

Create a Servo Observer

We need to instantiate a Servo ‘observer’ to publish the observations (values of the metric fields at a given moment of time). Servo provides multiple observers like ‘FileMetricObserver’ – to write metrics to a file, ‘GraphiteMetricObserver’ – to write metrics to Graphite.

In this example, we used ‘CloudWatchMetricObserver’ to write our metrics to AWS CloudWatch

import com.amazonaws.auth.BasicAWSCredentials;  
import com.amazonaws.services.cloudwatch.AmazonCloudWatchClient;  
import com.netflix.servo.publish.cloudwatch.CloudWatchMetricObserver;    

BasicAWSCredentials awsCreds = new BasicAWSCredentials("Your-AWS-ACCESS-KEY", " Your-AWS-Secret-KEY ");  
CloudWatchMetricObserver observer = new CloudWatchMetricObserver("SampleObserver", "SampleDomain",new AmazonCloudWatchClient(awsCreds));

If You are Using Proxy

If your app is not running on AWS and is using proxy to connect to external internet, then you should include proxy information as part of instantiating CloudWatchMetricObserver.

import com.amazonaws.ClientConfiguration;  
import com.amazonaws.auth.BasicAWSCredentials;  
import com.amazonaws.services.cloudwatch.AmazonCloudWatchClient;  
import com.netflix.servo.publish.cloudwatch.CloudWatchMetricObserver;      

BasicAWSCredentials awsCreds = new BasicAWSCredentials("Your-AWS-ACCESS-KEY", " Your-  AWS-Secret-KEY ");  
ClientConfiguration cfg = new ClientConfiguration();                  
cfg.setProxyHost("proxy.mycompany.com");                  
cfg.setProxyPort(8099);                  
cfg.setProxyUsername("your-user-id");                  
cfg.setProxyPassword("your-password");  

CloudWatchMetricObserver observer = new CloudWatchMetricObserver("SampleObserver", "SampleDomain",new AmazonCloudWatchClient(awsCreds, cfg));

Configure a Poller and Invoke Observer’s Update

import com.netflix.servo.publish.BasicMetricFilter;  
import com.netflix.servo.publish.MonitorRegistryMetricPoller;  
import com.netflix.servo.publish.PollCallable;      

//Create a poller  
PollCallable poller = new PollCallable(new MonitorRegistryMetricPoller(), BasicMetricFilter.MATCH_ALL);      
//Invoke update on the cloudwatch observer      
observer.update(poller.call());  

Check Cloudwatch console for custom metrics

Log into AWS console, click on CloudWatch link (green icon under Admin and Security).

Then, click on Metrics on left pane and enter the metric field name in the search box.

Select the check box next to the metric name, a graph will be displayed at the bottom with values for that metric for a given time range.

Image title

Publishing Hystrix metrics.

For publishing Hystrix metrics to Cloudwatch you don’t need to tag any field separately with @Monitor attributes. If you register Servo’s Hystrix publisher during your application’s startup, all the fields that are part of Hystrix stream will be available in CloudWatch console

import com.netflix.hystrix.strategy.HystrixPlugins;  
import com.netflix.hystrix.contrib.servopublisher.HystrixServoMetricsPublisher;      

HystrixPlugins.getInstance().registerMetricsPublisher(HystrixServoMetricsPublisher.getInstance());

Image title

The State of API Integration Report provides data from the Cloud Elements platform and will help all developers navigate the recent explosion of APIs and the implications of API integrations to work more efficiently in 2017 and beyond.

Topics:
hystrix ,netflix oss ,aws ,cloudwatch ,metrics ,integration ,enterprise integration ,ei

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}