Skip to content

Conversation

@DmarshalTU
Copy link

Add HTTP API Server for Querying Events and Hooks

Summary

This PR implements a minimal read-only HTTP API server that provides RESTful endpoints for querying events, hooks, statistics, and health information. This addresses the feedback from PR #9 by implementing a generic API server that shares the same data sources as the event processor and create base line of future REST capabilities..

Context

This PR is a refactored implementation based on feedback from PR #9. Key changes from the original approach:

  • ✅ Changed "sre" to "apiserver" - Generic naming for broader use cases
  • ✅ Split handlers by feature set - Replaced single 1590-line file with organized handler files
  • ✅ API server only, no alerting - Alerting removed for separate feature PR
  • ✅ Uses AgentRef instead of AgentId - Aligned with kagent standards
  • ✅ Uses concrete types - No interface{} types
  • ✅ Shares same data source as processor - Real-time consistency via shared managers

Changes

Not applicable to this PR

Severity handling — I kept a default severity mapping function for the statistics endpoint (/api/v1/stats/events/summary) to provide severity breakdowns. Making it configurable requires adding a severity field to the EventConfiguration CRD schema, which is outside this PR’s scope (API server only). The current default mapping (oom-kill/node-not-ready → "critical", probe-failed/pod-restart → "warning", pod-pending → "info") need to be replaced in a future PR's that adds the optional severity field to the Hook CRD, allowing teams to customize per event type while falling back to defaults when not specified.

NotificationSuppressionDuration — Not part of API server scope (deduplication manager concern)
Database/Postgres — Using in-memory managers (appropriate for this scope)

New Components

  • internal/apiserver/ - Complete API server implementation
    • server.go - Main server, routing, and middleware (228 lines)
    • events.go - Event query and streaming handlers (228 lines)
    • hooks.go - Hook configuration handlers (77 lines)
    • health.go - Health, diagnostics, and metrics handlers (162 lines)
    • stats.go - Event statistics handlers (179 lines)
    • json.go - JSON encoding utilities (14 lines)
    • swagger.go - Swagger/OpenAPI documentation (30 lines)
    • test/server_test.go - Comprehensive unit tests (334 lines, 58% coverage)

Modified Components

  • cmd/main.go - Integrated API server startup with shared managers
  • internal/config/config.go - Added APIServerPort configuration
  • internal/interfaces/controller.go - Added GetAllHookNames() and GetEventCount() methods
  • internal/workflow/coordinator.go - Updated to accept shared managers via NewCoordinatorWithManagers()

API Endpoints

Health & Diagnostics

  • GET /api/v1/health - Health check endpoint
  • GET /api/v1/diagnostics - Detailed diagnostics (memory, connections, event stats)
  • GET /api/v1/metrics - Prometheus-style metrics

Events

  • GET /api/v1/events - List events with optional filtering
    • Query parameters: namespace, eventType, resourceName, status
  • GET /api/v1/events/stream - Server-Sent Events stream for real-time updates

Hooks

  • GET /api/v1/hooks - List all Hook configurations

Statistics

  • GET /api/v1/stats/events/summary - Event summary with counts by severity and type
  • GET /api/v1/stats/events/by-type - Events grouped by type with percentages

Documentation

  • GET /swagger/index.html - Swagger UI
  • GET /swagger/doc.json - OpenAPI specification

Documentation

  • API Documentation: Swagger/OpenAPI at /swagger/index.html

Breaking Changes

None. This is a purely additive change.

Checklist

  • Code follows project style guidelines
  • Self-review completed
  • Comments added for complex code
  • Documentation updated
  • Swagger documentation generated
  • All PR Feat: Embedded HTTP API Server #7 #9 feedback addressed
  • No alerting features included
  • Uses AgentRef throughout
  • Uses concrete types (no interface{})
  • Handlers split by feature set
  • Shares data sources with processor

Related

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant