7
7
:imagesdir: ./images
8
8
= Spring Metrics
9
9
10
- image:: https://circleci.com/gh/spring-projects/spring-metrics.svg?style=svg[Build Status, link=https://circleci.com/gh/spring-cloud/spring-metrics]
10
+ image:https://circleci.com/gh/spring-projects/spring-metrics.svg?style=svg[Build Status, link=https://circleci.com/gh/spring-cloud/spring-metrics]
11
11
image:https://badges.gitter.im/Join%20Chat.svg[Gitter, link="https://gitter.im/spring-projects/spring-metrics?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge"]
12
12
image:https://img.shields.io/badge/License-Apache%202.0-blue.svg[Apache License,link="http://www.apache.org/licenses/LICENSE-2.0"]
13
13
@@ -258,6 +258,18 @@ groups etc. Normally all data can be refreshed in a few minutes. If the AWS
258
258
services are having problems it can take much longer. A long duration timer can
259
259
be used to track the overall time for refreshing the metadata.
260
260
261
+ In a Spring application, it is common for such long running processes to be implemented with `@Scheduled`.
262
+ `spring-metrics` provides a special `@Timed` annotation for instrumenting these processes with a long
263
+ task timer:
264
+
265
+ ```java
266
+ @Timed(value = "aws_scrape", longTask = true)
267
+ @Scheduled(fixedDelay = 360000)
268
+ void scrapeResources() {
269
+ // find instances, volumes, auto-scaling groups, etc...
270
+ }
271
+ ```
272
+
261
273
The charts below show max latency for the refresh using a regular timer and a
262
274
long task timer. Regular timer, note that the y-axis is using a logarithmic scale:
263
275
@@ -405,7 +417,7 @@ We recommend setting up alerts for production ready apps for (1) if `jvm_gc_paus
405
417
is a good general purpose value) and (2) if `jvm_gc_live_data_size` exceeds 70% of the heap.
406
418
2. `LogbackMetrics` - Records counts for log event at various levels. Auto-configured in the presence of `logback-core`.
407
419
408
- == Spring Web MVC and Spring WebFlux
420
+ == Server-side HTTP Instrumentation
409
421
410
422
`spring-metrics` contains built-in instrumentation for timings of requests made
411
423
to Spring MVC and Spring WebFlux server endpoints.
@@ -420,13 +432,16 @@ to time. Add `@Timed` to:
420
432
421
433
1. A controller class to enable timings on every request handler in the controller.
422
434
2. A method to enable for an individual endpoint. This is not necessary if you have it on the class.
435
+ 3. A method with `longTask = true` to enable a long task timer for the method. Long task timers require a
436
+ separate metric name, and can be stacked with a short task timer.
423
437
424
438
```java
425
439
@RestController
426
440
@Timed // (1)
427
441
public class MyController {
428
442
@GetMapping("/api/people")
429
443
@Timed // (2)
444
+ @Timed(value = "all_people", longTask = true) // (3)
430
445
public List<Person> listPeople() { ... }
431
446
```
432
447
@@ -464,15 +479,33 @@ RouterFunction<ServerResponse> routes = RouterFunctions
464
479
465
480
The filter applies to all routes defined by this router function.
466
481
467
- === `RestTemplate` Timing
482
+ == Client-side HTTP Instrumentation
468
483
469
- Adding `@EnableMetrics` to your `@SpringBootApplication` class autoconfigures a `BeanPostProcessor` for `RestTemplate`,
484
+ Adding `@EnableMetrics` to your `@SpringBootApplication` class configures a `BeanPostProcessor` for `RestTemplate`,
470
485
so every instance you create via the application context will be instrumented.
471
486
472
487
A timer is recorded for each invocation that includes tags for URI (before parameter substitution), host, and status.
473
488
The name of this timer is `http_client_requests`, and can be changed via the `spring.metrics.web.client_requests.name`
474
489
property.
475
490
491
+ == Scheduling Instrumentation
492
+
493
+ Adding `@EnableMetrics` to your `@SpringBootApplication` class plus enabling AOP configures AOP advice that times
494
+ `@Scheduled` methods. For a method to be timed, it must be marked as `@Timed("my_metric_name")` with a name.
495
+
496
+ Depending on the duration of the scheduled task, you may want to choose to time the method with a `LongTaskTimer`,
497
+ a `Timer`, or both. Below is an example of measuring both long task and regular timings to a scheduled task:
498
+
499
+ ```java
500
+ @Timed("beep")
501
+ @Timed(value = "long_beep", longTask = true)
502
+ @Scheduled(fixedRate = 1000)
503
+ void longBeep() {
504
+ // calculate the meaning of life, then beep...
505
+ System.out.println("beep");
506
+ }
507
+ ```
508
+
476
509
== Prometheus
477
510
478
511
=== Quickstart for Prometheus-based monitoring
0 commit comments