From 26c6a887c26e9a62fc1c1344dce40d19a323ba03 Mon Sep 17 00:00:00 2001 From: Trask Stalnaker Date: Thu, 21 Aug 2025 13:56:41 -0700 Subject: [PATCH] Fix CodeQL warnings --- .../etw_testapp/EtwTestController.java | 21 +++++++++---------- .../etw_testapp/LongTestController.java | 21 ++----------------- 2 files changed, 12 insertions(+), 30 deletions(-) diff --git a/etw/etw-testapp/src/main/java/com/microsoft/applicationinsights/etw_testapp/EtwTestController.java b/etw/etw-testapp/src/main/java/com/microsoft/applicationinsights/etw_testapp/EtwTestController.java index 81d7a0c263c..7d62f7ace50 100644 --- a/etw/etw-testapp/src/main/java/com/microsoft/applicationinsights/etw_testapp/EtwTestController.java +++ b/etw/etw-testapp/src/main/java/com/microsoft/applicationinsights/etw_testapp/EtwTestController.java @@ -2,8 +2,6 @@ import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.util.concurrent.atomic.AtomicInteger; @@ -14,25 +12,26 @@ public class EtwTestController { private static final DiagnosticsLoggerProxy DIAGNOSTICS_LOGGER = new DiagnosticsLoggerProxy(); + private static final String LEVEL = "info"; + private static final boolean HAS_EXCEPTION = false; + private final AtomicInteger errorCount = new AtomicInteger(); private final AtomicInteger warnCount = new AtomicInteger(); private final AtomicInteger infoCount = new AtomicInteger(); - @GetMapping("/{level}") - public ResponseEntity logPage( - @PathVariable String level, - @RequestParam(name = "e", required = false, defaultValue = "false") boolean hasException) { - String msg = "Hit /" + level + " "; + @GetMapping("/log") + public ResponseEntity logPage() { + String msg = "Hit /" + LEVEL + " "; int n; Throwable t = null; - switch (level.toLowerCase()) { + switch (LEVEL.toLowerCase()) { case "info": n = infoCount.incrementAndGet(); DIAGNOSTICS_LOGGER.info(msg + n); break; case "error": n = errorCount.incrementAndGet(); - if (hasException) { + if (HAS_EXCEPTION) { t = new Exception("the error " + n); DIAGNOSTICS_LOGGER.error(msg + n, t); } else { @@ -41,7 +40,7 @@ public ResponseEntity logPage( break; case "warn": n = warnCount.incrementAndGet(); - if (hasException) { + if (HAS_EXCEPTION) { t = new Exception("the warn " + n); DIAGNOSTICS_LOGGER.warn(msg + n, t); } else { @@ -52,6 +51,6 @@ public ResponseEntity logPage( return ResponseEntity.notFound().build(); } return ResponseEntity.ok( - level.toUpperCase() + " " + n + (t == null ? "" : "
\n" + t.toString())); + LEVEL.toUpperCase() + " " + n + (t == null ? "" : "
\n" + t.toString())); } } diff --git a/etw/etw-testapp/src/main/java/com/microsoft/applicationinsights/etw_testapp/LongTestController.java b/etw/etw-testapp/src/main/java/com/microsoft/applicationinsights/etw_testapp/LongTestController.java index 3bcd3fd5e45..f393261d532 100644 --- a/etw/etw-testapp/src/main/java/com/microsoft/applicationinsights/etw_testapp/LongTestController.java +++ b/etw/etw-testapp/src/main/java/com/microsoft/applicationinsights/etw_testapp/LongTestController.java @@ -232,29 +232,12 @@ private double rand() { } @GetMapping("/start") - public ResponseEntity startTest( - @RequestParam(name = "T", required = false, defaultValue = "1s") String periodStr) { + public ResponseEntity startTest() { if (future.get() != null) { return ResponseEntity.status(HttpStatus.CONFLICT).body("Test already in progress."); } - final Duration period; - try { - if (periodStr.toLowerCase().endsWith("ms")) { - period = Duration.ofMillis(Long.parseLong(periodStr.substring(0, periodStr.length() - 2))); - } else { - period = Duration.parse("PT" + periodStr); - } - } catch (NumberFormatException | DateTimeParseException e) { - return ResponseEntity.badRequest() - .body( - "

Period parameter 'T' could not parse \"" - + periodStr - + "\"

" - + "

"
-                  + ExceptionUtils.getStackTrace(e)
-                  + "

"); - } + final Duration period = Duration.parse("PT1s"); final long startTime; final ScheduledFuture scheduledTask;