Skip to content

feat: add API event recorder interface for billing#1266

Merged
zhenghaoz merged 4 commits intogorse-io:masterfrom
zhangzhenghao:feat-api-event-recorder
May 7, 2026
Merged

feat: add API event recorder interface for billing#1266
zhenghaoz merged 4 commits intogorse-io:masterfrom
zhangzhenghao:feat-api-event-recorder

Conversation

@zhangzhenghao
Copy link
Copy Markdown
Contributor

Summary

Add common/event package for recording API call events, designed for future billing/metering functionality.

Changes

  • APIEvent struct: Captures request metadata, response info, and billing quantities (users, items, feedbacks, recommendations)
  • Recorder interface: Abstract interface for event storage backends (database, message queue, external service)
  • NopRecorder: No-op implementation when recording is disabled
  • BufferRecorder: In-memory buffer for testing and debugging
  • Global recorder: SetRecorder/GetRecorder/Record functions for easy integration

Usage

// Set recorder
event.SetRecorder(event.NewBufferRecorder())

// Record event
event.Record(ctx, event.APIEvent{
    RequestID:   requestId,
    APIKey:      apikey,
    Method:      "GET",
    Path:        "/api/recommend/user123",
    StatusCode:  200,
    ResponseTime: 45,
    RecommendCount: 10,
})

Future Extensions

  • DatabaseRecorder: Persist events to MySQL/PostgreSQL/MongoDB
  • KafkaRecorder: Send events to message queue
  • BillingServiceRecorder: Push to external billing API

Testing

  • Build passes: go build ./common/event/
  • No additional dependencies added

Add common/event package with:
- APIEvent struct for recording API call metadata
- Recorder interface for event storage backends
- NopRecorder (disabled/no-op implementation)
- BufferRecorder (in-memory buffer for testing)
- Global recorder setter/getter functions

This provides the foundation for future billing and metering functionality.
@codecov
Copy link
Copy Markdown

codecov Bot commented May 7, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 72.95%. Comparing base (d54a2ea) to head (ec64ce7).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1266      +/-   ##
==========================================
+ Coverage   72.89%   72.95%   +0.05%     
==========================================
  Files          91       92       +1     
  Lines       16743    16764      +21     
==========================================
+ Hits        12205    12230      +25     
+ Misses       3293     3288       -5     
- Partials     1245     1246       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

zhangzhenghao and others added 2 commits May 7, 2026 11:07
- Add event.Handle call in server LogFilter
- Record API request metadata for billing
- Add global handler with AddFlags/SetHandler pattern (like log package)
- Rename Recorder to Handler for consistency
Co-authored-by: Copilot <copilot@github.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Introduces a new common/event package intended to record API and storage usage events for future billing/metering, and wires it into the REST access filter and master dataset-loading path.

Changes:

  • Added common/event with APIEvent/StorageEvent types, a Recorder interface, and a global recorder setter/getter (defaulting to NopRecorder).
  • Hooked API event recording into server.RestServer.LogFilter.
  • Hooked storage event recording into master.(*Master).loadDataset, plus added basic unit tests for recorder swapping.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 6 comments.

File Description
server/rest.go Records API call metadata via the global event recorder from the REST logging filter.
master/tasks.go Records storage usage counts after dataset load via the global event recorder.
common/event/event.go New event types + recorder interface + global recorder wiring (defaults to no-op).
common/event/event_test.go Adds a mock-based test verifying SetEventRecorder/EventRecorder behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread server/rest.go
Comment thread server/rest.go Outdated
Comment thread common/event/event.go
Comment thread common/event/event.go
Comment thread common/event/event.go
Comment thread master/tasks.go Outdated
@zhenghaoz zhenghaoz merged commit ae86dfa into gorse-io:master May 7, 2026
12 checks passed
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.

3 participants