Skip to content

Commit c4076e8

Browse files
committed
Prefix micrometer metric tags with meter names
This is what is recommended by the observability team in terms of tag naming with micrometer 1.10+ for consistency between both metrics and tracing. In #4065, only some metrics have been updated to follow this tag naming convention, but not all of them. This commit updates all metrics in a similar way for consistency. Related to #4065 and #4098.
1 parent 53eabcd commit c4076e8

File tree

5 files changed

+31
-28
lines changed

5 files changed

+31
-28
lines changed

spring-batch-core/src/main/java/org/springframework/batch/core/job/AbstractJob.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,8 +310,9 @@ public final void execute(JobExecution execution) {
310310
}
311311

312312
JobSynchronizationManager.register(execution);
313-
LongTaskTimer longTaskTimer = BatchMetrics.createLongTaskTimer("job.active", "Active jobs",
314-
Tag.of("name", execution.getJobInstance().getJobName()));
313+
String activeJobMeterName = "job.active";
314+
LongTaskTimer longTaskTimer = BatchMetrics.createLongTaskTimer(activeJobMeterName, "Active jobs",
315+
Tag.of(BatchMetrics.METRICS_PREFIX + activeJobMeterName + ".name", execution.getJobInstance().getJobName()));
315316
LongTaskTimer.Sample longTaskTimerSample = longTaskTimer.start();
316317
Observation observation = BatchMetrics.createObservation(BatchJobObservation.BATCH_JOB_OBSERVATION.getName(), new BatchJobContext(execution))
317318
.contextualName(execution.getJobInstance().getJobName())

spring-batch-core/src/main/java/org/springframework/batch/core/observability/BatchMetrics.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
*/
4848
public final class BatchMetrics {
4949

50-
private static final String METRICS_PREFIX = "spring.batch.";
50+
public static final String METRICS_PREFIX = "spring.batch.";
5151

5252
public static final String STATUS_SUCCESS = "SUCCESS";
5353

spring-batch-core/src/main/java/org/springframework/batch/core/step/item/SimpleChunkProcessor.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -341,10 +341,11 @@ protected Chunk<O> transform(StepContribution contribution, Chunk<I> inputs) thr
341341
}
342342

343343
protected void stopTimer(Timer.Sample sample, StepExecution stepExecution, String metricName, String status, String description) {
344+
String fullyQualifiedMetricName = BatchMetrics.METRICS_PREFIX + metricName;
344345
sample.stop(BatchMetrics.createTimer(metricName, description + " duration",
345-
Tag.of("job.name", stepExecution.getJobExecution().getJobInstance().getJobName()),
346-
Tag.of("step.name", stepExecution.getStepName()),
347-
Tag.of("status", status)
346+
Tag.of(fullyQualifiedMetricName + ".job.name", stepExecution.getJobExecution().getJobInstance().getJobName()),
347+
Tag.of(fullyQualifiedMetricName + ".step.name", stepExecution.getStepName()),
348+
Tag.of(fullyQualifiedMetricName + ".status", status)
348349
));
349350
}
350351

spring-batch-core/src/main/java/org/springframework/batch/core/step/item/SimpleChunkProvider.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,11 @@ public RepeatStatus doInIteration(final RepeatContext context) throws Exception
150150
}
151151

152152
private void stopTimer(Timer.Sample sample, StepExecution stepExecution, String status) {
153+
String fullyQualifiedMetricName = BatchMetrics.METRICS_PREFIX + "item.read";
153154
sample.stop(BatchMetrics.createTimer("item.read", "Item reading duration",
154-
Tag.of("job.name", stepExecution.getJobExecution().getJobInstance().getJobName()),
155-
Tag.of("step.name", stepExecution.getStepName()),
156-
Tag.of("status", status)
155+
Tag.of(fullyQualifiedMetricName + ".job.name", stepExecution.getJobExecution().getJobInstance().getJobName()),
156+
Tag.of(fullyQualifiedMetricName + ".step.name", stepExecution.getStepName()),
157+
Tag.of(fullyQualifiedMetricName + ".status", status)
157158
));
158159
}
159160

spring-batch-core/src/test/java/org/springframework/batch/core/observability/BatchMetricsTests.java

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ public void testBatchMetrics() throws Exception {
165165

166166
try {
167167
Metrics.globalRegistry.get("spring.batch.job.active")
168-
.tag("name", "job")
168+
.tag("spring.batch.job.active.name", "job")
169169
.longTaskTimer();
170170
} catch (Exception e) {
171171
fail("There should be a meter of type LONG_TASK_TIMER named spring.batch.job.active" +
@@ -200,9 +200,9 @@ public void testBatchMetrics() throws Exception {
200200

201201
try {
202202
Metrics.globalRegistry.get("spring.batch.item.read")
203-
.tag("job.name", "job")
204-
.tag("step.name", "step2")
205-
.tag("status", "SUCCESS")
203+
.tag("spring.batch.item.read.job.name", "job")
204+
.tag("spring.batch.item.read.step.name", "step2")
205+
.tag("spring.batch.item.read.status", "SUCCESS")
206206
.timer();
207207
} catch (Exception e) {
208208
fail("There should be a meter of type TIMER named spring.batch.item.read" +
@@ -211,9 +211,9 @@ public void testBatchMetrics() throws Exception {
211211

212212
try {
213213
Metrics.globalRegistry.get("spring.batch.item.process")
214-
.tag("job.name", "job")
215-
.tag("step.name", "step2")
216-
.tag("status", "SUCCESS")
214+
.tag("spring.batch.item.process.job.name", "job")
215+
.tag("spring.batch.item.process.step.name", "step2")
216+
.tag("spring.batch.item.process.status", "SUCCESS")
217217
.timer();
218218
} catch (Exception e) {
219219
fail("There should be a meter of type TIMER named spring.batch.item.process" +
@@ -222,9 +222,9 @@ public void testBatchMetrics() throws Exception {
222222

223223
try {
224224
Metrics.globalRegistry.get("spring.batch.chunk.write")
225-
.tag("job.name", "job")
226-
.tag("step.name", "step2")
227-
.tag("status", "SUCCESS")
225+
.tag("spring.batch.chunk.write.job.name", "job")
226+
.tag("spring.batch.chunk.write.step.name", "step2")
227+
.tag("spring.batch.chunk.write.status", "SUCCESS")
228228
.timer();
229229
} catch (Exception e) {
230230
fail("There should be a meter of type TIMER named spring.batch.chunk.write" +
@@ -246,9 +246,9 @@ public void testBatchMetrics() throws Exception {
246246

247247
try {
248248
Metrics.globalRegistry.get("spring.batch.item.read")
249-
.tag("job.name", "job")
250-
.tag("step.name", "step3")
251-
.tag("status", "SUCCESS")
249+
.tag("spring.batch.item.read.job.name", "job")
250+
.tag("spring.batch.item.read.step.name", "step3")
251+
.tag("spring.batch.item.read.status", "SUCCESS")
252252
.timer();
253253
} catch (Exception e) {
254254
fail("There should be a meter of type TIMER named spring.batch.item.read" +
@@ -257,9 +257,9 @@ public void testBatchMetrics() throws Exception {
257257

258258
try {
259259
Metrics.globalRegistry.get("spring.batch.item.process")
260-
.tag("job.name", "job")
261-
.tag("step.name", "step3")
262-
.tag("status", "SUCCESS")
260+
.tag("spring.batch.item.process.job.name", "job")
261+
.tag("spring.batch.item.process.step.name", "step3")
262+
.tag("spring.batch.item.process.status", "SUCCESS")
263263
.timer();
264264
} catch (Exception e) {
265265
fail("There should be a meter of type TIMER named spring.batch.item.process" +
@@ -268,9 +268,9 @@ public void testBatchMetrics() throws Exception {
268268

269269
try {
270270
Metrics.globalRegistry.get("spring.batch.chunk.write")
271-
.tag("job.name", "job")
272-
.tag("step.name", "step3")
273-
.tag("status", "SUCCESS")
271+
.tag("spring.batch.chunk.write.job.name", "job")
272+
.tag("spring.batch.chunk.write.step.name", "step3")
273+
.tag("spring.batch.chunk.write.status", "SUCCESS")
274274
.timer();
275275
} catch (Exception e) {
276276
fail("There should be a meter of type TIMER named spring.batch.chunk.write" +

0 commit comments

Comments
 (0)