Skip to content

Commit

Permalink
Add documentation for @counted (#5613)
Browse files Browse the repository at this point in the history
Co-authored-by: Tommy Ludwig <8924140+shakuzen@users.noreply.github.com>
(cherry picked from commit f0293b8)
  • Loading branch information
jonatan-ivanov committed Oct 31, 2024
1 parent 6ab4a7d commit 9b4413e
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions docs/modules/ROOT/pages/concepts/counters.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,49 @@ Counter counter = Counter
.register(registry);
----


== The `@Counted` Annotation

The `micrometer-core` module contains a `@Counted` annotation that frameworks can use to add counting support to either specific types of methods such as those serving web request endpoints or, more generally, to all methods.

WARNING: Micrometer's Spring Boot configuration does _not_ recognize `@Counted` on arbitrary methods.

Also, an incubating AspectJ aspect is included in `micrometer-core`. You can use it in your application either through compile/load time AspectJ weaving or through framework facilities that interpret AspectJ aspects and proxy targeted methods in some other way, such as Spring AOP. Here is a sample Spring AOP configuration:

[source,java]
----
@Configuration
public class CountedConfiguration {
@Bean
public CountedAspect countedAspect(MeterRegistry registry) {
return new CountedAspect(registry);
}
}
----

Applying `CountedAspect` makes `@Counted` usable on any arbitrary method in an AspectJ proxied instance, as the following example shows:

[source,java]
----
@Service
public class ExampleService {
@Counted
public void sync() {
// @Counted will record the number of executions of this method
...
}
@Async
@Counted
public CompletableFuture<?> async() {
// @Counted will record the number of executions of this method
return CompletableFuture.supplyAsync(...);
}
}
----

== Function-tracking Counters

Micrometer also provides a more infrequently used counter pattern that tracks a monotonically increasing function (a function that stays the same or increases over time but never decreases). Some monitoring systems, such as Prometheus, push cumulative values for counters to the backend, but others publish the rate at which a counter is incrementing over the push interval. By employing this pattern, you let the Micrometer implementation for your monitoring system choose whether to rate-normalize the counter, and your counter remains portable across different types of monitoring systems.
Expand Down

0 comments on commit 9b4413e

Please sign in to comment.