Skip to content

Commit

Permalink
Prefix micrometer metric tags with meter names
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
fmbenhassine committed May 19, 2022
1 parent 53eabcd commit c4076e8
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 28 deletions.
Expand Up @@ -310,8 +310,9 @@ public final void execute(JobExecution execution) {
}

JobSynchronizationManager.register(execution);
LongTaskTimer longTaskTimer = BatchMetrics.createLongTaskTimer("job.active", "Active jobs",
Tag.of("name", execution.getJobInstance().getJobName()));
String activeJobMeterName = "job.active";
LongTaskTimer longTaskTimer = BatchMetrics.createLongTaskTimer(activeJobMeterName, "Active jobs",
Tag.of(BatchMetrics.METRICS_PREFIX + activeJobMeterName + ".name", execution.getJobInstance().getJobName()));
LongTaskTimer.Sample longTaskTimerSample = longTaskTimer.start();
Observation observation = BatchMetrics.createObservation(BatchJobObservation.BATCH_JOB_OBSERVATION.getName(), new BatchJobContext(execution))
.contextualName(execution.getJobInstance().getJobName())
Expand Down
Expand Up @@ -47,7 +47,7 @@
*/
public final class BatchMetrics {

private static final String METRICS_PREFIX = "spring.batch.";
public static final String METRICS_PREFIX = "spring.batch.";

public static final String STATUS_SUCCESS = "SUCCESS";

Expand Down
Expand Up @@ -341,10 +341,11 @@ protected Chunk<O> transform(StepContribution contribution, Chunk<I> inputs) thr
}

protected void stopTimer(Timer.Sample sample, StepExecution stepExecution, String metricName, String status, String description) {
String fullyQualifiedMetricName = BatchMetrics.METRICS_PREFIX + metricName;
sample.stop(BatchMetrics.createTimer(metricName, description + " duration",
Tag.of("job.name", stepExecution.getJobExecution().getJobInstance().getJobName()),
Tag.of("step.name", stepExecution.getStepName()),
Tag.of("status", status)
Tag.of(fullyQualifiedMetricName + ".job.name", stepExecution.getJobExecution().getJobInstance().getJobName()),
Tag.of(fullyQualifiedMetricName + ".step.name", stepExecution.getStepName()),
Tag.of(fullyQualifiedMetricName + ".status", status)
));
}

Expand Down
Expand Up @@ -150,10 +150,11 @@ public RepeatStatus doInIteration(final RepeatContext context) throws Exception
}

private void stopTimer(Timer.Sample sample, StepExecution stepExecution, String status) {
String fullyQualifiedMetricName = BatchMetrics.METRICS_PREFIX + "item.read";
sample.stop(BatchMetrics.createTimer("item.read", "Item reading duration",
Tag.of("job.name", stepExecution.getJobExecution().getJobInstance().getJobName()),
Tag.of("step.name", stepExecution.getStepName()),
Tag.of("status", status)
Tag.of(fullyQualifiedMetricName + ".job.name", stepExecution.getJobExecution().getJobInstance().getJobName()),
Tag.of(fullyQualifiedMetricName + ".step.name", stepExecution.getStepName()),
Tag.of(fullyQualifiedMetricName + ".status", status)
));
}

Expand Down
Expand Up @@ -165,7 +165,7 @@ public void testBatchMetrics() throws Exception {

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

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

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

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

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

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

try {
Metrics.globalRegistry.get("spring.batch.chunk.write")
.tag("job.name", "job")
.tag("step.name", "step3")
.tag("status", "SUCCESS")
.tag("spring.batch.chunk.write.job.name", "job")
.tag("spring.batch.chunk.write.step.name", "step3")
.tag("spring.batch.chunk.write.status", "SUCCESS")
.timer();
} catch (Exception e) {
fail("There should be a meter of type TIMER named spring.batch.chunk.write" +
Expand Down

0 comments on commit c4076e8

Please sign in to comment.