-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
refactor: streamline usage reporting by consolidating record publishi… #2254
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| package usage | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
| "time" | ||
|
|
||
| coreusage "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/usage" | ||
| ) | ||
|
|
||
| func TestRequestStatisticsRecordIncludesLatency(t *testing.T) { | ||
| stats := NewRequestStatistics() | ||
| stats.Record(context.Background(), coreusage.Record{ | ||
| APIKey: "test-key", | ||
| Model: "gpt-5.4", | ||
| RequestedAt: time.Date(2026, 3, 20, 12, 0, 0, 0, time.UTC), | ||
| Latency: 1500 * time.Millisecond, | ||
| Detail: coreusage.Detail{ | ||
| InputTokens: 10, | ||
| OutputTokens: 20, | ||
| TotalTokens: 30, | ||
| }, | ||
| }) | ||
|
|
||
| snapshot := stats.Snapshot() | ||
| details := snapshot.APIs["test-key"].Models["gpt-5.4"].Details | ||
| if len(details) != 1 { | ||
| t.Fatalf("details len = %d, want 1", len(details)) | ||
| } | ||
| if details[0].LatencyMs != 1500 { | ||
| t.Fatalf("latency_ms = %d, want 1500", details[0].LatencyMs) | ||
| } | ||
| } | ||
|
|
||
| func TestRequestStatisticsMergeSnapshotDedupIgnoresLatency(t *testing.T) { | ||
| stats := NewRequestStatistics() | ||
| timestamp := time.Date(2026, 3, 20, 12, 0, 0, 0, time.UTC) | ||
| first := StatisticsSnapshot{ | ||
| APIs: map[string]APISnapshot{ | ||
| "test-key": { | ||
| Models: map[string]ModelSnapshot{ | ||
| "gpt-5.4": { | ||
| Details: []RequestDetail{{ | ||
| Timestamp: timestamp, | ||
| LatencyMs: 0, | ||
| Source: "[email protected]", | ||
| AuthIndex: "0", | ||
| Tokens: TokenStats{ | ||
| InputTokens: 10, | ||
| OutputTokens: 20, | ||
| TotalTokens: 30, | ||
| }, | ||
| }}, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
| second := StatisticsSnapshot{ | ||
| APIs: map[string]APISnapshot{ | ||
| "test-key": { | ||
| Models: map[string]ModelSnapshot{ | ||
| "gpt-5.4": { | ||
| Details: []RequestDetail{{ | ||
| Timestamp: timestamp, | ||
| LatencyMs: 2500, | ||
| Source: "[email protected]", | ||
| AuthIndex: "0", | ||
| Tokens: TokenStats{ | ||
| InputTokens: 10, | ||
| OutputTokens: 20, | ||
| TotalTokens: 30, | ||
| }, | ||
| }}, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| result := stats.MergeSnapshot(first) | ||
| if result.Added != 1 || result.Skipped != 0 { | ||
| t.Fatalf("first merge = %+v, want added=1 skipped=0", result) | ||
| } | ||
|
|
||
| result = stats.MergeSnapshot(second) | ||
| if result.Added != 0 || result.Skipped != 1 { | ||
| t.Fatalf("second merge = %+v, want added=0 skipped=1", result) | ||
| } | ||
|
|
||
| snapshot := stats.Snapshot() | ||
| details := snapshot.APIs["test-key"].Models["gpt-5.4"].Details | ||
| if len(details) != 1 { | ||
| t.Fatalf("details len = %d, want 1", len(details)) | ||
| } | ||
| } | ||
|
Comment on lines
+35
to
+96
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The setup for |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Latency is fixed at the first call to
buildRecordviaLatency: r.latency(), but several streaming executors publish usage as soon as an intermediate usage event arrives (before the stream is finished). SincepublishWithOutcomeis protected byonce.Do, long streams keep that early value and systematically under-report end-to-end request latency, making the new metric misleading for operational monitoring.Useful? React with 👍 / 👎.