Skip to content

Commit

Permalink
Add duration to the example Java service
Browse files Browse the repository at this point in the history
Signed-off-by: Fabian Stäber <[email protected]>
  • Loading branch information
fstab committed Feb 12, 2024
1 parent fe26090 commit f6f0e86
Showing 1 changed file with 6 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.grafana.example;

import java.util.Random;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
Expand All @@ -13,13 +14,14 @@
public class RollController {

private static final Logger logger = LoggerFactory.getLogger(RollController.class);
private final Random random = new Random(0);

@GetMapping("/rolldice")
public String index(@RequestParam("player") Optional<String> player) {
if (ThreadLocalRandom.current().nextInt(10) < 3) {
public String index(@RequestParam("player") Optional<String> player) throws InterruptedException {
Thread.sleep((long) (Math.abs((random.nextGaussian() + 1.0) * 200.0)));
if (random.nextInt(10) < 3) {
throw new RuntimeException("simulating an error");
}

int result = this.getRandomNumber(1, 6);
if (player.isPresent()) {
logger.info("{} is rolling the dice: {}", player.get(), result);
Expand All @@ -30,6 +32,6 @@ public String index(@RequestParam("player") Optional<String> player) {
}

public int getRandomNumber(int min, int max) {
return ThreadLocalRandom.current().nextInt(min, max + 1);
return random.nextInt(min, max + 1);
}
}

0 comments on commit f6f0e86

Please sign in to comment.