forked from micrometer-metrics/micrometer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
afeb045
commit 7a2c656
Showing
1 changed file
with
49 additions
and
0 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
micrometer-samples/src/main/java/io/micrometer/core/samples/LatencySample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package io.micrometer.core.samples; | ||
|
||
import io.micrometer.core.instrument.MeterRegistry; | ||
import io.micrometer.core.instrument.Timer; | ||
import io.micrometer.core.samples.utils.SampleRegistries; | ||
import reactor.core.publisher.Flux; | ||
|
||
import java.time.Duration; | ||
import java.util.Random; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
public class LatencySample { | ||
public static void main(String[] args) { | ||
new LatencySample().run(); | ||
} | ||
|
||
private MeterRegistry registry = SampleRegistries.jmx(); | ||
private Random r = new Random(); | ||
private Timer timer = Timer.builder("request") | ||
.publishPercentileHistogram() | ||
.register(registry); | ||
|
||
void bar() { | ||
registry.timer("bar.latency") | ||
.record(() -> { | ||
// do something here | ||
}); | ||
} | ||
|
||
void run() { | ||
|
||
|
||
Flux.interval(Duration.ofMillis(1)) | ||
.doOnEach(n -> recordGaussian(10)) | ||
.subscribe(); | ||
|
||
Flux.interval(Duration.ofSeconds(1)) | ||
.doOnEach(n -> recordGaussian(300)) | ||
.blockLast(); | ||
} | ||
|
||
private void recordGaussian(long center) { | ||
timer.record(simulatedLatency(center), TimeUnit.MILLISECONDS); | ||
} | ||
|
||
private long simulatedLatency(long center) { | ||
return (long) (r.nextGaussian() * 10) + center; | ||
} | ||
} |