Skip to content

Commit 689037b

Browse files
committed
1 parent ef1de53 commit 689037b

File tree

7 files changed

+76
-49
lines changed

7 files changed

+76
-49
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@
131131
<!-- samples dependencies -->
132132
<spring-rabbit.version>${spring-amqp.version}</spring-rabbit.version>
133133
<quartz.version>2.5.0</quartz.version>
134-
<prometheus_pushgateway.version>0.16.0</prometheus_pushgateway.version>
134+
<prometheus-metrics-exporter-pushgateway>1.4.1</prometheus-metrics-exporter-pushgateway>
135135
<groovy.version>3.0.22</groovy.version> <!-- change to org.apache.groovy:groovy + update to latest -->
136136

137137
<!-- documentation dependencies -->

spring-batch-samples/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ This should start the required monitoring stack:
594594
* Prometheus push gateway on port `9091`
595595
* Grafana on port `3000`
596596

597-
Once started, you need to [configure Prometheus as data source in Grafana](https://grafana.com/docs/features/datasources/prometheus/)
597+
Once started, you need to [configure Prometheus as data source in Grafana](https://grafana.com/docs/grafana/latest/datasources/prometheus/configure/)
598598
and import the ready-to-use dashboard in `spring-batch-samples/src/main/resources/org/springframework/batch/samples/metrics/spring-batch-dashboard.json`.
599599

600600
Finally, run the `org.springframework.batch.samples.metrics.BatchMetricsApplication`

spring-batch-samples/pom.xml

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,9 @@
5252
<artifactId>spring-context-support</artifactId>
5353
<version>${spring-framework.version}</version>
5454
</dependency>
55-
<!-- temporary usage of deprecated dependency until pushgateway
56-
is supported in prometheus Java client 1.x -->
5755
<dependency>
5856
<groupId>io.micrometer</groupId>
59-
<artifactId>micrometer-registry-prometheus-simpleclient</artifactId>
57+
<artifactId>micrometer-registry-prometheus</artifactId>
6058
<version>${micrometer.version}</version>
6159
</dependency>
6260
<dependency>
@@ -204,10 +202,10 @@
204202
<artifactId>mongodb-driver-sync</artifactId>
205203
<version>${mongodb-driver.version}</version>
206204
</dependency>
207-
<dependency>
208-
<groupId>io.prometheus</groupId>
209-
<artifactId>simpleclient_pushgateway</artifactId>
210-
<version>${prometheus_pushgateway.version}</version>
205+
<dependency>
206+
<groupId>io.prometheus</groupId>
207+
<artifactId>prometheus-metrics-exporter-pushgateway</artifactId>
208+
<version>${prometheus-metrics-exporter-pushgateway}</version>
211209
</dependency>
212210
<dependency>
213211
<groupId>com.fasterxml.jackson.core</groupId>

spring-batch-samples/src/main/java/org/springframework/batch/samples/metrics/BatchMetricsApplication.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022-2023 the original author or authors.
2+
* Copyright 2022-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -31,7 +31,7 @@
3131
@EnableJdbcJobRepository
3232
@Import({ Job1Configuration.class, Job2Configuration.class, JobScheduler.class, PrometheusConfiguration.class,
3333
DataSourceConfiguration.class })
34-
@PropertySource("metrics-sample.properties")
34+
@PropertySource("classpath:org/springframework/batch/samples/metrics/metrics-sample.properties")
3535
public class BatchMetricsApplication {
3636

3737
public static void main(String[] args) {

spring-batch-samples/src/main/java/org/springframework/batch/samples/metrics/JobScheduler.java

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
1+
/*
2+
* Copyright 2022-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
116
package org.springframework.batch.samples.metrics;
217

18+
import io.prometheus.metrics.exporter.pushgateway.PushGateway;
19+
import org.apache.commons.logging.Log;
20+
import org.apache.commons.logging.LogFactory;
21+
322
import org.springframework.batch.core.job.Job;
423
import org.springframework.batch.core.job.parameters.JobParameters;
524
import org.springframework.batch.core.job.parameters.JobParametersBuilder;
@@ -11,17 +30,22 @@
1130
@Component
1231
public class JobScheduler {
1332

33+
private static final Log LOGGER = LogFactory.getLog(JobScheduler.class);
34+
1435
private final Job job1;
1536

1637
private final Job job2;
1738

1839
private final JobOperator jobOperator;
1940

41+
private final PushGateway pushGateway;
42+
2043
@Autowired
21-
public JobScheduler(Job job1, Job job2, JobOperator jobOperator) {
44+
public JobScheduler(Job job1, Job job2, JobOperator jobOperator, PushGateway pushGateway) {
2245
this.job1 = job1;
2346
this.job2 = job2;
2447
this.jobOperator = jobOperator;
48+
this.pushGateway = pushGateway;
2549
}
2650

2751
@Scheduled(cron = "*/10 * * * * *")
@@ -40,4 +64,14 @@ public void launchJob2() throws Exception {
4064
jobOperator.start(job2, jobParameters);
4165
}
4266

67+
@Scheduled(fixedRateString = "${prometheus.push.rate}")
68+
public void pushMetrics() {
69+
try {
70+
pushGateway.pushAdd();
71+
}
72+
catch (Throwable ex) {
73+
LOGGER.error("Unable to push metrics to Prometheus Push Gateway", ex);
74+
}
75+
}
76+
4377
}
Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2021-2023 the original author or authors.
2+
* Copyright 2021-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,27 +15,21 @@
1515
*/
1616
package org.springframework.batch.samples.metrics;
1717

18-
import java.util.HashMap;
19-
import java.util.Map;
20-
import jakarta.annotation.PostConstruct;
21-
22-
import io.micrometer.core.instrument.Metrics;
23-
import io.micrometer.prometheus.PrometheusConfig;
24-
import io.micrometer.prometheus.PrometheusMeterRegistry;
25-
import io.prometheus.client.CollectorRegistry;
26-
import io.prometheus.client.exporter.PushGateway;
27-
import org.slf4j.Logger;
28-
import org.slf4j.LoggerFactory;
18+
import io.micrometer.core.instrument.Clock;
19+
import io.micrometer.core.instrument.observation.DefaultMeterObservationHandler;
20+
import io.micrometer.observation.ObservationRegistry;
21+
import io.micrometer.prometheusmetrics.PrometheusConfig;
22+
import io.micrometer.prometheusmetrics.PrometheusMeterRegistry;
23+
import io.prometheus.metrics.exporter.pushgateway.PushGateway;
24+
import io.prometheus.metrics.model.registry.PrometheusRegistry;
2925

3026
import org.springframework.beans.factory.annotation.Value;
27+
import org.springframework.context.annotation.Bean;
3128
import org.springframework.context.annotation.Configuration;
32-
import org.springframework.scheduling.annotation.Scheduled;
3329

3430
@Configuration
3531
public class PrometheusConfiguration {
3632

37-
private static final Logger LOGGER = LoggerFactory.getLogger(PrometheusConfiguration.class);
38-
3933
@Value("${prometheus.job.name}")
4034
private String prometheusJobName;
4135

@@ -45,29 +39,30 @@ public class PrometheusConfiguration {
4539
@Value("${prometheus.pushgateway.url}")
4640
private String prometheusPushGatewayUrl;
4741

48-
private final Map<String, String> groupingKey = new HashMap<>();
49-
50-
private PushGateway pushGateway;
42+
@Bean
43+
public PrometheusRegistry prometheusRegistry() {
44+
return new PrometheusRegistry();
45+
}
5146

52-
private CollectorRegistry collectorRegistry;
47+
@Bean
48+
public PrometheusMeterRegistry meterRegistry(PrometheusRegistry prometheusRegistry) {
49+
return new PrometheusMeterRegistry(PrometheusConfig.DEFAULT, prometheusRegistry, Clock.SYSTEM);
50+
}
5351

54-
@PostConstruct
55-
public void init() {
56-
pushGateway = new PushGateway(prometheusPushGatewayUrl);
57-
groupingKey.put(prometheusGroupingKey, prometheusJobName);
58-
PrometheusMeterRegistry prometheusMeterRegistry = new PrometheusMeterRegistry(PrometheusConfig.DEFAULT);
59-
collectorRegistry = prometheusMeterRegistry.getPrometheusRegistry();
60-
Metrics.globalRegistry.add(prometheusMeterRegistry);
52+
@Bean
53+
public PushGateway pushGateway(PrometheusRegistry prometheusRegistry) {
54+
return PushGateway.builder()
55+
.address(prometheusPushGatewayUrl)
56+
.groupingKey(prometheusGroupingKey, prometheusJobName)
57+
.registry(prometheusRegistry)
58+
.build();
6159
}
6260

63-
@Scheduled(fixedRateString = "${prometheus.push.rate}")
64-
public void pushMetrics() {
65-
try {
66-
pushGateway.pushAdd(collectorRegistry, prometheusJobName, groupingKey);
67-
}
68-
catch (Throwable ex) {
69-
LOGGER.error("Unable to push metrics to Prometheus Push Gateway", ex);
70-
}
61+
@Bean
62+
public ObservationRegistry observationRegistry(PrometheusMeterRegistry meterRegistry) {
63+
ObservationRegistry observationRegistry = ObservationRegistry.create();
64+
observationRegistry.observationConfig().observationHandler(new DefaultMeterObservationHandler(meterRegistry));
65+
return observationRegistry;
7166
}
7267

7368
}

spring-batch-samples/src/main/resources/org/springframework/batch/samples/metrics/docker-compose.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@ version: '3.3'
22
services:
33

44
prometheus:
5-
image: prom/prometheus:v2.7.2
5+
image: prom/prometheus:v3.6.0
66
container_name: 'prometheus'
77
ports:
88
- '9090:9090'
99
volumes:
1010
- ./prometheus.yml:/etc/prometheus/prometheus.yml
1111

1212
pushgateway:
13-
image: prom/pushgateway:v0.6.0
13+
image: prom/pushgateway:v1.11.1
1414
container_name: 'pushgateway'
1515
ports:
1616
- '9091:9091'
1717

1818
grafana:
19-
image: grafana/grafana:6.0.2
19+
image: grafana/grafana:12.3.0-17900959199-ubuntu
2020
container_name: 'grafana'
2121
ports:
2222
- '3000:3000'

0 commit comments

Comments
 (0)