SoFunction
Updated on 2025-04-04

How to implement custom data monitoring with Java combined with Prometheus

1. Configure prometheus

...

- job_name: 'my-service'
  metrics_path: /metrics
  static_configs:
  - targets: [':yyyy'] //The url of the monitored application
...

2. Monitored application

Ideas

  1. Introduce related dependencies
  2. Configure the endpoint exposed by monitoring metrics
  3. Custom monitoring metrics

Key Code

1. Introduce related dependencies

<!-- prometheus -->
<dependency>
		<groupId></groupId>
		<artifactId>simpleclient</artifactId>
		<version>0.3.0</version>
</dependency>
<dependency>
		<groupId></groupId>
		<artifactId>simpleclient_hotspot</artifactId>
		<version>0.3.0</version>
</dependency>
<dependency>
		<groupId></groupId>
		<artifactId>simpleclient_servlet</artifactId>
		<version>0.3.0</version>
</dependency>

2. Expose monitoring metrics to '/metrics'

import ;
import ;
import ;
...

@Configuration
public class PrometheusConfig {

		// ...

		@Bean
		public ServletRegistrationBean servletRegistrationBean(){
			();
			return new ServletRegistrationBean(new MetricsServlet(), "/metrics");
		}
}

3. Customize monitoring indicators

import ;
import ;
...

// counter only increases but not decreasesstatic final Counter customizeCounter = ()
        .name("customize_counter") //Define the name, the name can be used for search        .help("customize counter") //Definition description        .register();
(); //Add 1 in the current object
// gauge can be increased or decreasedstatic final Gauge customizeGauge = ()
        .name("customize_gauge")
        .help("customize gauge")
        .labelNames("label1", "label2", "label3") //Define the tag name, you can define multiple tags        .register();
(); //Add 1 in the current object(); //The current object is reduced by 1("value1","value2","value3").set(1100); //Set the tag value, the tag can be used for conditional filtering
// In addition,histogramandsummaryTwo indicators

3. Monitoring application

Ideas

  1. Introduce related dependencies
  2. Call prometheus API to get data
  3. Organize the data and return it

Key Code

1. Introduce related dependencies

<!-- prometheus -->
<dependency>
		<groupId></groupId>
		<artifactId>simpleclient</artifactId>
		<version>0.3.0</version>
</dependency>
<dependency>
		<groupId></groupId>
		<artifactId>simpleclient_hotspot</artifactId>
		<version>0.3.0</version>
</dependency>
<dependency>
		<groupId></groupId>
		<artifactId>simpleclient_servlet</artifactId>
		<version>0.3.0</version>
</dependency>

2. Call the prometheus API

  • Get data at a certain point in time
String query = "customize_counter";
String url = "http://[ip]:[port]/api/v1/query?query=" + query + "&time=2019-05-01T20:10:51.781Z";
HttpGet get = new HttpGet(url);
CloseableHttpClient httpClient = ().build();
CloseableHttpResponse response = (get);
  • Get data for a certain period of time
String query = "rate(customize_counter{label1 = value1}[30s])";
String url = "http://[ip]:[port]/api/v1/query_range?query=" + query + "&start=2019-05-01T20:10:51.781Z&end=2019-05-02T20:10:51.781Z";
HttpGet get = new HttpGet(url);
CloseableHttpClient httpClient = ().build();
CloseableHttpResponse response = (get);

For more usage methods, please refer toPrometheus - Query

3. Processing data

  • Prometheus sometimes returns out-of-order results, the following code can be sorted by timestamp
public class ComparatorPromData implements Comparator {

    @Override
    public int compare(Object o1, Object o2) {
        List list1 = (List)o1;
        List list2 = (List)o2;
        return ((Integer) (0)).compareTo(((Integer) (0)));
    }
}
public class SortUtil {

    public static List sortPromData(List<List> list) {
        ComparatorPromData comparator = new ComparatorPromData();
        (list, comparator);
        return list;
    }
}

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.