How to use the Java agent plugin to capture custom metrics without changing application code

By Jack Shirazi

The Elastic APM Java agent automatically tracks many metrics , including those generated through Micrometer or the OpenTelemetry Metrics API . Therefore, if your application (or the libraries it contains) already exposes metrics from one of these APIs, installing the Elastic APM Java agent is the only step required to capture them. You'll be able to visualize and configure thresholds, alerts, and anomaly detection -- and whatever else you want to use them for!

The next easiest option is to generate custom metrics directly from code (e.g. by adding code directly to the application using the OpenTelemetry Metrics API ). The main disadvantage of this approach is that it requires modification of the application, so if you cannot or do not want to do this, you can easily generate the required custom metrics by adding instrumentation to the Elastic APM Java agent via a plugin.

This article discusses situations where the application you're monitoring isn't emitting the custom metrics you want, and you can't directly change the code or configuration to make it do so. Instead, you can use a plugin to auto-instrument your application through the Elastic APM Java agent, which will make the application emit the custom metrics you want.

Analyzing APM telemery for various measures using Elastic Kibana Lens

 

Plugin Basics

The " Create your own instrumentation using the Java agent plugin " article details the basics of the Elastic APM Java agent and how to easily plug in instrumentation. Generating metrics from plugins is just another type of instrumentation, and the referenced article provides detailed step-by-step instructions and a working example of how to create a plugin with custom instrumentation.

For this article, I'm assuming that you understand how to create a plugin with custom instrumentation based on the previous article and the example application (a simple web server, ExampleBasicHttpServer ) in our plugin examples repository .

custom indicator

For our example application, which is an HTTP server ( ExampleBasicHttpServer ), we want to add a custom metric "page_views" that is incremented every time any request is processed by the ExampleBasicHttpServer application. This means that the instrumentation we'll add will be triggered by the same ExampleBasicHttpServer.handleRequest() method used in " Using the Java Proxy Plugin to Create Your Own Instrumentation ".

15-minute line visualization of the page_views metric with Elastic APM

 

Using plugins/OpenTelemetry API

Essentially, the only difference from that article is that for metrics we will be using the OpenTelemetry metrics API instead of the OpenTelemetry tracing API .

Especially for indicators, the recommended approach for the handleRequest() method is the following code:

if (pageViewCounter == null) {
    pageViewCounter = GlobalOpenTelemetry
        .getMeter("ExampleHttpServer")
        .counterBuilder("page_views")
        .setDescription("Page view count")
        .build();
}
pageViewCounter.add(1);

That is, create the gauge lazily the first time it is needed, then increment the page view counter each time the ExampleBasicHttpServer.handleRequest() method is called.

Everything else -- setting up detection, finding detection methods, building the plug-in -- is the same as in the article " Create your own detection with the Java agent plug- in".

A complete metrics example is implemented in the plugin examples repository , the actual complete metrics instrumentation implementation is ExampleMetricsInstrumentation .

15-minute bar chart visualization of the page_views metric using Elastic APM

try it!

That's it! To run an agent with a plugin, simply build and include the jar as described in " Using the Java agent plugin to create your own instrumentation " and include it in the directory specified by the plugins_dir configuration option. The plugin examples repository provides a fully tested implementation -- just clone it and mvn install to see it in action.

The best place to start using Elastic APM is in the cloud. Start your free Elastic Cloud trial today !

原文:How to capture custom metrics without app code changes using the Java Agent Plugin | Elastic Blog

Guess you like

Origin blog.csdn.net/UbuntuTouch/article/details/131685760