-
Notifications
You must be signed in to change notification settings - Fork 897
feat: add API event recorder interface for billing #1266
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
zhenghaoz
merged 4 commits into
gorse-io:master
from
zhangzhenghao:feat-api-event-recorder
May 7, 2026
+158
−0
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
2f74459
feat: add API event recorder interface for billing
zhangzhenghao bdca917
feat: integrate event recording in LogFilter
zhangzhenghao 89dd71e
feat: implement event recording for API and storage events
zhenghaoz ec64ce7
feat: run event recording for storage and API events asynchronously
zhenghaoz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| // Copyright 2026 gorse Project Authors | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package event | ||
|
|
||
| import ( | ||
| "context" | ||
| "time" | ||
| ) | ||
|
|
||
| // APIEvent represents an API call event for billing purposes. | ||
| type APIEvent struct { | ||
| // Request metadata | ||
| RequestID string // Unique request identifier (X-Request-ID) | ||
| Method string // HTTP method (GET, POST, PUT, DELETE, PATCH) | ||
| Path string // API path (e.g., /api/recommend/{user-id}) | ||
|
|
||
| // Response metadata | ||
| StatusCode int // HTTP response status code | ||
| ResponseTime int64 // Response time in milliseconds | ||
| Timestamp time.Time // Event timestamp | ||
|
|
||
| // Additional metadata | ||
| RemoteAddr string // Client remote address | ||
| } | ||
|
|
||
| // StorageEvent represents data storage usage for billing purposes. | ||
| type StorageEvent struct { | ||
| UserCount int // Number of users in storage | ||
| ItemCount int // Number of items in storage | ||
| FeedbackCount int // Number of feedbacks in storage | ||
| Timestamp time.Time // Event timestamp | ||
| } | ||
|
|
||
| type Recorder interface { | ||
| RecordAPI(ctx context.Context, event APIEvent) | ||
| RecordStorage(ctx context.Context, event StorageEvent) | ||
| } | ||
|
|
||
| type NopRecorder struct{} | ||
|
|
||
| func (n *NopRecorder) RecordAPI(ctx context.Context, event APIEvent) { | ||
| } | ||
|
|
||
| func (n *NopRecorder) RecordStorage(ctx context.Context, event StorageEvent) { | ||
| } | ||
|
|
||
| var recorder Recorder = &NopRecorder{} | ||
|
|
||
| func EventRecorder() Recorder { | ||
| return recorder | ||
| } | ||
|
|
||
| func SetEventRecorder(r Recorder) { | ||
|
zhenghaoz marked this conversation as resolved.
|
||
| recorder = r | ||
| } | ||
|
zhenghaoz marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| // Copyright 2026 gorse Project Authors | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package event | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/mock" | ||
| ) | ||
|
|
||
| type MockRecorder struct { | ||
| mock.Mock | ||
| } | ||
|
|
||
| func (m *MockRecorder) RecordAPI(ctx context.Context, event APIEvent) { | ||
| m.Called(ctx, event) | ||
| } | ||
|
|
||
| func (m *MockRecorder) RecordStorage(ctx context.Context, event StorageEvent) { | ||
| m.Called(ctx, event) | ||
| } | ||
|
|
||
| func TestSetEventRecorder(t *testing.T) { | ||
| t.Cleanup(func() { | ||
| SetEventRecorder(&NopRecorder{}) | ||
| }) | ||
|
|
||
| ctx := context.Background() | ||
| now := time.Now() | ||
| apiEvent := APIEvent{ | ||
| RequestID: "request-id", | ||
| Method: "GET", | ||
| Path: "/api/recommend/user-id", | ||
| StatusCode: 200, | ||
| ResponseTime: 10, | ||
| Timestamp: now, | ||
| RemoteAddr: "127.0.0.1", | ||
| } | ||
| storageEvent := StorageEvent{ | ||
| UserCount: 1, | ||
| ItemCount: 2, | ||
| FeedbackCount: 3, | ||
| Timestamp: now, | ||
| } | ||
|
|
||
| recorder := new(MockRecorder) | ||
| recorder.On("RecordAPI", ctx, apiEvent).Once() | ||
| recorder.On("RecordStorage", ctx, storageEvent).Once() | ||
|
|
||
| SetEventRecorder(recorder) | ||
|
|
||
| assert.Same(t, recorder, EventRecorder()) | ||
| EventRecorder().RecordAPI(ctx, apiEvent) | ||
| EventRecorder().RecordStorage(ctx, storageEvent) | ||
|
|
||
| recorder.AssertExpectations(t) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.