Modernize java-http-metrics-collector from Java 8 to Java 21

Release notes

Modernize the HTTP Metrics Collector sensor from Java 8 to Java 21, leveraging modern Java features including Virtual Threads, Records, Pattern Matching, and the new HttpClient API for improved performance and maintainability.

Problem to solve

The java-http-metrics-collector sensor currently uses Java 8 with legacy APIs and patterns:

  • HttpURLConnection for HTTP requests (verbose, limited features)
  • Traditional POJOs with boilerplate getters/setters
  • ExecutorService thread pools (resource-intensive for concurrent requests)
  • java.util.Date instead of modern java.time API
  • Anonymous inner classes instead of lambdas and method references

This creates a modernization opportunity to demonstrate how agentic AI can upgrade legacy Java codebases to leverage modern language features.

Environment information

  • Current: Java 8 (1.8)
  • Target: Java 21 LTS
  • Build tool: Maven 3.9+
  • Dependencies: Jackson, SLF4J, JUnit

Proposal

Modernize the sensor to Java 21 with the following improvements:

1. HTTP Client Upgrade

  • Replace HttpURLConnection with java.net.http.HttpClient (Java 11+)
  • Use async/reactive patterns for concurrent requests
  • Improve timeout and error handling

2. Virtual Threads (Java 21)

  • Replace ExecutorService thread pools with Virtual Threads
  • Enable massive concurrency with minimal resource overhead
  • Simplify concurrent collection logic

3. Records for DTOs

  • Convert EndpointConfig, HttpMetric, CollectorConfig to Records
  • Eliminate boilerplate getters/setters
  • Immutable by default for thread safety

4. Pattern Matching & Switch Expressions

  • Use pattern matching for cleaner error handling
  • Leverage switch expressions for output format selection

5. Modern Time API

  • Replace java.util.Date with java.time.Instant
  • Use Duration for timeout configuration

6. Testing Improvements

  • Upgrade to JUnit 5 (Jupiter)
  • Add integration tests with test containers

Success Criteria

  • All functionality preserved from Java 8 version
  • Performance improvement with Virtual Threads (benchmark concurrent requests)
  • Code reduction through Records and modern syntax
  • Updated CI/CD pipeline for Java 21
  • Documentation showing before/after comparison

Labels: enhancement ~"modernization" ~"java" priority2 workflowTo-Do