monitoring-and-metrics.md 3.3 KB
Newer Older
茶陵後's avatar
茶陵後 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
# Monitoring and metrics

## Monitoring and metrics

Since version 4.2, Spring Batch provides support for batch monitoring and metrics
based on [Micrometer](https://micrometer.io/). This section describes
which metrics are provided out-of-the-box and how to contribute custom metrics.

### Built-in metrics

Metrics collection does not require any specific configuration. All metrics provided
by the framework are registered in[Micrometer’s global registry](https://micrometer.io/docs/concepts#_global_registry)under the `spring.batch` prefix. The following table explains all the metrics in details:

|       *Metric Name*       |     *Type*      |       *Description*       |             *Tags*              |
|---------------------------|-----------------|---------------------------|---------------------------------|
|    `spring.batch.job`     |     `TIMER`     | Duration of job execution |        `name`, `status`         |
| `spring.batch.job.active` |`LONG_TASK_TIMER`|   Currently active jobs   |             `name`              |
|    `spring.batch.step`    |     `TIMER`     |Duration of step execution |  `name`, `job.name`, `status`   |
| `spring.batch.item.read`  |     `TIMER`     | Duration of item reading  |`job.name`, `step.name`, `status`|
|`spring.batch.item.process`|     `TIMER`     |Duration of item processing|`job.name`, `step.name`, `status`|
|`spring.batch.chunk.write` |     `TIMER`     | Duration of chunk writing |`job.name`, `step.name`, `status`|

|   |The `status` tag can be either `SUCCESS` or `FAILURE`.|
|---|------------------------------------------------------|

### Custom metrics

If you want to use your own metrics in your custom components, we recommend using
Micrometer APIs directly. The following is an example of how to time a `Tasklet`:

```
import io.micrometer.core.instrument.Metrics;
import io.micrometer.core.instrument.Timer;

import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;

public class MyTimedTasklet implements Tasklet {

	@Override
	public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) {
		Timer.Sample sample = Timer.start(Metrics.globalRegistry);
		String status = "success";
		try {
			// do some work
		} catch (Exception e) {
			// handle exception
			status = "failure";
		} finally {
			sample.stop(Timer.builder("my.tasklet.timer")
					.description("Duration of MyTimedTasklet")
					.tag("status", status)
					.register(Metrics.globalRegistry));
		}
		return RepeatStatus.FINISHED;
	}
}
```

### Disabling metrics

Metrics collection is a concern similar to logging. Disabling logs is typically
done by configuring the logging library and this is no different for metrics.
There is no feature in Spring Batch to disable micrometer’s metrics, this should
be done on micrometer’s side. Since Spring Batch stores metrics in the global
registry of micrometer with the `spring.batch` prefix, it is possible to configure
micrometer to ignore/deny batch metrics with the following snippet:

```
Metrics.globalRegistry.config().meterFilter(MeterFilter.denyNameStartsWith("spring.batch"))
```

Please refer to micrometer’s [reference documentation](http://micrometer.io/docs/concepts#_meter_filters)for more details.