Capture and expose system exception statistics in Mule using Coda Hale Metrics

This article describes how to use the Coda Hale metrics library to capture the counts and types of system exceptions thrown outside of Mule ESB flow processing.  Historical exception information is exposed via JMX or written to disk using CodaHale reporters as described on the CodaHale metrics web site. You can use this same technique in any Java application. 

We inject instrumentation at the system context level.  That component converts the wrapped exception stack to a counter name that is then created and incremented in the Coda Hale registry.  I use the simple running counter because I don't find the native Coda Hale histogram data useful for this type of metric. You can use other metric types if you want more complex statistics.


Components

We use 2 injection components 2 codahale components and a custom listener  to make this work:
  1. MetricsRegistry: A CodaHale singleton that maintains a reference to all statistics. It is injected into other components.
  2. MetricAgent: A Mule Agent that manages the lifecyle of the JMXReporter
  3. JMXReporter: A CodaHale reporter that exposes the MetricsRegistry via JMX. Instantiated by MetricAgent. Codahale supports other reporters in place of or in addition to the JMXReporter. 
  4. SystemExceptionMetricContextShim: A MuleContextAware class that injects the SystemExceptionMetricListener into the MuleContext
  5. SystemExceptionMetricListener: A custom class injected into the MuleContext as the ExceptionLister. This creates metric counters concatenating the Simple Names of each Exception in the stack. The failed class name is appended to the counter name if it is some type of Connection Exception

Initializing Coda Hale library

We use the same initialization configuration we used in the flow exception metrics example

Metrics Registry

Instantiate this component as a singleton Spring Bean so that it can be injected into all other objects that wish to record metrics. Note that we inject the app name so that we can add that to the JMX MBean domain name
...

JMX Reporter

Instantiate the JMXReporter using a custom Mule Agent that is injected with the MetricsRegistry spring bean.   
...

Injecting the Metric Listener into the MuleContext 

MuleContextAware / Mule Context

Create SystemExceptionMetricContextShim, a MuleContextAware component in spring that will get called by Mule when initializes the context.  This Shim injects the SystemExceptionMetricListener which actually counts all the exceptions. Create a exception listener spring bean as a singleton that we inject into the Shim.  This listener is set into the MuleContext by the shim.
...

The listener creates a metric name from the nested exception names and the Connection type if it is a connection exception:
...

Example Program

You can find an example program on github at  https://github.com/freemansoft/Mule-demo.  It is a Mule maven project originally built with Mule ESB 3.5.2.  This is the same demo used in the flow exception metrics example.

Th demo has a mis-configured HTTPS endpoint/connector that throws an System exception at deployment time.

Viewing the statistics

This sample exposes metrics via JMX.  You can use any MBean viewer to see the statistics including most Java friendly monitoring tools.  Ever Java JDK comes with jvisualvm that is capable of showing you JMX MBeans with the help of the MBean plugin. Run jvisuavm from the command line or your file viewer.  I run the following command prompt on my windows machine
C:\Program Files\Java\jdk1.7.0_55\bin>jvisualvm.exe
The following image displays the value for ConnectionExcepton raised in the HttpsConnector.  We get one exception for the endpoint on startup because of a configuration error.


 You could see higher numbers for connectors whose remote connections go up and down.

Conclusion

Teams can build production support, problem triage and troubleshooting aids into their applications by integrating Coda Hale metrics and Mule's exception handling strategies.  These metrics can be exposed via files , JMX beans or other tooling to make them visible in a variety of environments.

Created 11/30/2014
All code samples were created using Mule ESB 3.5.2

Comments

Popular posts from this blog

Understanding your WSL2 RAM and swap - Changing the default 50%-25%

Installing the RNDIS driver on Windows 11 to use USB Raspberry Pi as network attached

DNS for Azure Point to Site (P2S) VPN - getting the internal IPs