in Elastiflix/java-favorite-otel-manual/src/main/java/com/movieapi/ApiServlet.java [191:212]
private void handleCanary() throws Exception {
Span span = GlobalOpenTelemetry.getTracer(SERVICE_NAME).spanBuilder("handleCanary").startSpan();
Scope scope = span.makeCurrent();
Random random = new Random();
if (sleepTime > 0 && random.nextDouble() < 0.5) {
double randomGaussDelay = Math.min(sleepTime*5, Math.max(0, random.nextGaussian() * (sleepTime)));
TimeUnit.MILLISECONDS.sleep((long) randomGaussDelay);
logger.info("Canary enabled");
Span.current().setAttribute("canary", "test-new-feature");
Span.current().setAttribute("quiz_solution", "correlations");
if (random.nextDouble() < toggleCanaryFailure) {
logger.error("Something went wrong");
throw new Exception("Something went wrong");
}
}
span.addEvent("a span event", Attributes.of(AttributeKey.longKey("someKey"), Long.valueOf(93)));
span.setStatus(StatusCode.OK);
span.end();
scope.close();
}