-
Notifications
You must be signed in to change notification settings - Fork 0
feature: add StatsCollector for basic metrics aggregation #16
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
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| package com.volta.stats; | ||
|
|
||
| import java.util.concurrent.atomic.LongAccumulator; | ||
| import java.util.concurrent.atomic.LongAdder; | ||
|
|
||
| public class StatsCollector { | ||
| private final LongAdder totalRequests = new LongAdder(); | ||
| private final LongAdder successCount = new LongAdder(); | ||
| private final LongAdder totalLatencyMs = new LongAdder(); | ||
| private final LongAccumulator maxLatencyMs = new LongAccumulator(Math::max, Long.MIN_VALUE); | ||
| private final LongAccumulator minLatencyMs = new LongAccumulator(Math::min, Long.MAX_VALUE); | ||
|
|
||
| public void record(int statusCode, long latencyMs) throws IllegalArgumentException { | ||
|
Collaborator
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. IllegalArgumentException is unchecked exception |
||
| if (statusCode < 100 || statusCode > 599) { | ||
| throw new IllegalArgumentException("statusCode must be integer in [100, 599]"); | ||
| } | ||
| if (latencyMs < 0) { | ||
| throw new IllegalArgumentException("latencyMs must be non-negative"); | ||
| } | ||
|
|
||
| totalRequests.increment(); | ||
| if (200 <= statusCode && statusCode < 300) { | ||
| successCount.increment(); | ||
|
Collaborator
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. Are 1xx or 3xx statuses as errors? |
||
| } | ||
| totalLatencyMs.add(latencyMs); | ||
| maxLatencyMs.accumulate(latencyMs); | ||
| minLatencyMs.accumulate(latencyMs); | ||
| } | ||
|
|
||
| public StatsSnapshot getSnapshot() { | ||
| long snapshotTotalRequests = totalRequests.sum(); | ||
| long snapshotSuccessCount = successCount.sum(); | ||
| long snapshotTotalLatencyMs = totalLatencyMs.sum(); | ||
|
|
||
| long snapshotMinLatencyMs = (snapshotTotalRequests == 0 ? 0 : minLatencyMs.get()); | ||
| long snapshotMaxLatencyMs = (snapshotTotalRequests == 0 ? 0 : maxLatencyMs.get()); | ||
| long snapshotAvgLatencyMs = | ||
| (snapshotTotalRequests == 0 ? 0 : snapshotTotalLatencyMs / snapshotTotalRequests); | ||
|
Collaborator
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. Try to use double-precision arithmetic where possible. Integer arithmetic can introduce significant error |
||
|
|
||
| return new StatsSnapshot( | ||
| snapshotTotalRequests, | ||
| snapshotSuccessCount, | ||
| snapshotTotalRequests - snapshotSuccessCount, | ||
| snapshotAvgLatencyMs, | ||
| snapshotMinLatencyMs, | ||
| snapshotMaxLatencyMs); | ||
| } | ||
|
|
||
| public void reset() { | ||
| totalRequests.reset(); | ||
| successCount.reset(); | ||
| totalLatencyMs.reset(); | ||
| minLatencyMs.reset(); | ||
| maxLatencyMs.reset(); | ||
| } | ||
|
Comment on lines
+30
to
+55
Collaborator
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. add method getSnapshotAndReset() |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package com.volta.stats; | ||
|
|
||
| public record StatsSnapshot( | ||
| long totalRequests, | ||
| long successCount, | ||
| long errorCount, | ||
| long avgLatencyMs, | ||
| long minLatencyMs, | ||
| long maxLatencyMs) {} |
This file was deleted.
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.
Please keep in mind that this code is not thread-safe, so errorCount may end up becoming negative