Skip to content

feat(agent): inject an AgentHandlerProtocol bean into GuardMiddleware - #18

Merged
rennf93 merged 2 commits into
masterfrom
feat/agent-handler-bean
Oct 13, 2026
Merged

rennf93 merged 2 commits into
masterfrom
feat/agent-handler-bean

Conversation

@rennf93

@rennf93 rennf93 commented Oct 13, 2026

Copy link
Copy Markdown
Member

spring-boot-guard could not use the Java agent at all. GuardMiddleware kept its agent handler in a private field that nothing ever set, and with guard.enable-agent=true it always degraded with "guard-agent-jvm is not available in this build yet (W7 artifact)".

What changes, mirroring ktor-guard's GuardCoreMiddleware:

  • GuardMiddleware takes an optional injectedAgentHandler. The auto-configuration passes any AgentHandlerProtocol bean through ObjectProvider.
  • The handler feeds the initial event bus, the metrics collector and the HandlerInitializer. On initialize(), the middleware adopts the engine composite as its agent handler, as python initialize() does.
  • The new GuardMiddleware.shutdown() stops the agent integrations. Spring calls it as the bean's inferred destroy method, so the agent flushes on context close.
  • The degrade path (python AgentPackageNotInstalledError) now applies only when no handler bean is registered, with an accurate message.
  • The README has a "Telemetry agent" section with the bean definition. The engine pin moves to 9067b7b (fix(agent): wire injected agent handlers into the composite and route metrics to sendMetric guard-core-jvm#48), because without that engine fix an injected agent is never started and receives nothing.

Tests (GuardAgentWiringTest.kt):

  • The degrade path without a handler.
  • An injected handler is started and receives events and metrics (metrics via sendMetric), and shutdown() stops it.
  • A full @SpringBootTest where a registered bean receives the events of a blocked request.
    Against the old engine pin, the two delivery tests fail with expected: 1 but was: 0; against 9067b7b they pass.

End to end, I ran a scratch Boot app with this adapter, the real guard-agent-jvm, and the engine at the fix, against a local capture server. Three blocked and three allowed requests produced one events batch (user_agent_blocked, security_headers_applied) and one metrics batch (request_count, response_time). Both validate against guard-core-app's ingestion model (BatchTelemetryRequest). Before the engine fix, the same run sent 0 requests.

Local gate: ./gradlew build passes (39 tests, 0 failures) against guard-core-jvm 9067b7b. Nothing is released.

@rennf93 rennf93 added the no-issue Chore or dependency PR that does not need an issue label Oct 13, 2026
@github-actions github-actions Bot added documentation Docs site, README, CHANGELOG, license files examples examples/ binaries ci GitHub Actions and pre-commit labels Oct 13, 2026
@rennf93
rennf93 merged commit d3cebb7 into master Oct 13, 2026
9 checks passed
@rennf93
rennf93 deleted the feat/agent-handler-bean branch October 13, 2026 20:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci GitHub Actions and pre-commit documentation Docs site, README, CHANGELOG, license files examples examples/ binaries no-issue Chore or dependency PR that does not need an issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant