-
Notifications
You must be signed in to change notification settings - Fork 20
Explicitly aggregate counts within a context in MetricRecorder #34
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: master
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 |
|---|---|---|
|
|
@@ -15,6 +15,8 @@ | |
| package software.amazon.swage.metrics.record; | ||
|
|
||
| import java.time.Instant; | ||
| import java.util.Map; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
|
|
||
| import software.amazon.swage.collection.TypedMap; | ||
| import software.amazon.swage.metrics.Metric; | ||
|
|
@@ -121,6 +123,8 @@ public TypedMap attributes() { | |
| public MetricContext context(TypedMap attributes) { | ||
| R context = newRecorderContext(attributes); | ||
| return new MetricContext() { | ||
| private final Map<Metric,Long> counts = new ConcurrentHashMap<>(); | ||
|
|
||
| @Override | ||
| public TypedMap attributes() { | ||
| return context.attributes(); | ||
|
|
@@ -133,11 +137,12 @@ public void record(Metric label, Number value, Unit unit, Instant time) { | |
|
|
||
| @Override | ||
| public void count(Metric label, long delta) { | ||
| MetricRecorder.this.count(label, delta, context); | ||
| counts.compute(label, (x, previous) -> (previous == null) ? delta : previous + delta); | ||
| } | ||
|
|
||
| @Override | ||
| public void close() { | ||
| counts.forEach((key, value) -> MetricRecorder.this.count(key, value, context)); | ||
|
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. Shouldn't these metrics have the same timestamp? |
||
| MetricRecorder.this.close(context); | ||
| } | ||
| }; | ||
|
|
@@ -172,29 +177,28 @@ protected abstract void record( | |
| R context); | ||
|
|
||
| /** | ||
| * Count the increase or decrease of a metric in the given context. | ||
| * Record the value of a count metric at the closure of a context. | ||
| * | ||
| * These are explicitly aggregated values, where only the total number of | ||
| * occurrences in a context matter. | ||
| * Examples include counting number of times a method is called, or keeping | ||
| * track of the number of errors encountered, or the amount of attributes sent | ||
| * while in the given context. | ||
| * | ||
| * Changes to the count are timestamped with the time of context closure, | ||
| * as only the total value of all counts for a metric have any meaning. | ||
| * If you need to distinguish between occurrences of an event, or care that | ||
| * an event did not occur (for ratios of success for example) then you want | ||
| * to record those individually and not use a count. | ||
| * Changes to the count are not timestamped as only the total value of all | ||
| * counts for a metric have any meaning - if the individual change needs to | ||
| * be tracked, you probably want to record the change as a gauged event. | ||
| * | ||
| * This method will fail silently, as application code is not expected | ||
| * to depend on success or failure. | ||
| * | ||
| * @param label the metric to capture | ||
| * @param delta the increase (or decrease) in the value | ||
| * @param value the total value of the count at the end of the context | ||
| * @param context the context the value belongs in. | ||
| */ | ||
| protected abstract void count(Metric label, long delta, R context); | ||
| protected abstract void count(Metric label, long value, R context); | ||
|
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. Is this api needed anymore? |
||
|
|
||
|
|
||
| /** | ||
|
|
||
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.
I think this comment is a little confusing. Only the final value for the count is recorded. So the changes are not timestamped at all.