Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Increase latency of the example Java service #20

Merged
merged 1 commit into from
Feb 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}