-
Notifications
You must be signed in to change notification settings - Fork 37
Histogram #151
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
Open
lksfrnz
wants to merge
3
commits into
awslabs:histogram_feature_branch
Choose a base branch
from
lksfrnz:Histogram
base: histogram_feature_branch
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Histogram #151
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
160 changes: 160 additions & 0 deletions
160
src/main/java/software/amazon/cloudwatchlogs/emf/model/Histogram.java
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,160 @@ | ||
/* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* 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 software.amazon.cloudwatchlogs.emf.model; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnore; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import software.amazon.cloudwatchlogs.emf.Constants; | ||
import software.amazon.cloudwatchlogs.emf.exception.InvalidMetricException; | ||
|
||
/** Histogram metric type */ | ||
class Histogram extends Statistics { | ||
Histogram(List<Double> values, List<Integer> counts) throws IllegalArgumentException { | ||
if (counts.size() != values.size()) { | ||
throw new IllegalArgumentException("Counts and values must have the same size"); | ||
lksfrnz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
if (values.stream().anyMatch(n -> n == null) || counts.stream().anyMatch(n -> n == null)) { | ||
throw new IllegalArgumentException("Values and counts cannot contain null values"); | ||
} | ||
|
||
if (!validSize(counts.size())) { | ||
throw new IllegalArgumentException( | ||
String.format( | ||
"Histogram provided with %d bins but CloudWatch will drop Histograms with more than %d bins", | ||
counts.size(), Constants.MAX_DATAPOINTS_PER_METRIC)); | ||
} | ||
|
||
this.max = Collections.max(values); | ||
this.min = Collections.min(values); | ||
this.count = counts.stream().mapToInt(Integer::intValue).sum(); | ||
this.sum = 0d; | ||
for (int i = 0; i < counts.size(); i++) { | ||
this.sum += values.get(i) * counts.get(i); | ||
} | ||
this.counts = counts; | ||
this.values = values; | ||
} | ||
|
||
Histogram() { | ||
count = 0; | ||
sum = 0.; | ||
values = new ArrayList<>(); | ||
counts = new ArrayList<>(); | ||
} | ||
|
||
@JsonProperty("Values") | ||
public List<Double> values; | ||
|
||
@JsonProperty("Counts") | ||
public List<Integer> counts; | ||
|
||
@JsonIgnore private boolean reduced = false; | ||
|
||
@JsonIgnore private static final double EPSILON = 0.1; | ||
@JsonIgnore private static final double BIN_SIZE = Math.log(1 + EPSILON); | ||
@JsonIgnore private final Map<Double, Integer> buckets = new HashMap<>(); | ||
|
||
/** | ||
* @param value the value to add to the histogram | ||
* @throws InvalidMetricException if adding this value would increase the number of bins in the | ||
* histogram to more than {@value Constants#MAX_DATAPOINTS_PER_METRIC} | ||
* @see Constants#MAX_DATAPOINTS_PER_METRIC | ||
*/ | ||
@Override | ||
void addValue(double value) throws InvalidMetricException { | ||
reduced = false; | ||
super.addValue(value); | ||
|
||
double bucket = getBucket(value); | ||
if (!buckets.containsKey(bucket) && !validSize(counts.size() + 1)) { | ||
throw new InvalidMetricException( | ||
String.format( | ||
"Adding this value increases the number of bins in this histogram to %d" | ||
+ ", CloudWatch will drop any Histogram metrics with more than %d bins", | ||
buckets.size() + 1, Constants.MAX_DATAPOINTS_PER_METRIC)); | ||
} | ||
// Add the value to the appropriate bucket (or create a new bucket if necessary) | ||
buckets.compute( | ||
bucket, | ||
(k, v) -> { | ||
if (v == null) { | ||
return 1; | ||
} else { | ||
return v + 1; | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Updates the Values and Counts lists to represent the buckets of this histogram. | ||
* | ||
* @return the reduced histogram | ||
*/ | ||
Histogram reduce() { | ||
if (reduced) { | ||
return this; | ||
} | ||
|
||
this.values = new ArrayList<>(buckets.size()); | ||
this.counts = new ArrayList<>(buckets.size()); | ||
|
||
for (Map.Entry<Double, Integer> entry : buckets.entrySet()) { | ||
this.values.add(entry.getKey()); | ||
this.counts.add(entry.getValue()); | ||
} | ||
lksfrnz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
reduced = true; | ||
return this; | ||
} | ||
|
||
/** | ||
* Gets the value of the bucket for the given value. | ||
* | ||
* @param value the value to find the closest bucket for | ||
* @return the value of the bucket the given value goes in | ||
*/ | ||
private static double getBucket(double value) { | ||
short index = (short) Math.floor(Math.log(value) / BIN_SIZE); | ||
return Math.exp((index + 0.5) * BIN_SIZE); | ||
} | ||
|
||
private boolean validSize(int size) { | ||
lksfrnz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return size <= Constants.MAX_DATAPOINTS_PER_METRIC; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
Histogram that = (Histogram) o; | ||
return count == that.count | ||
&& that.sum.equals(sum) | ||
&& that.max.equals(max) | ||
&& that.min.equals(min) | ||
&& buckets.equals(that.buckets); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return super.hashCode() + buckets.hashCode(); | ||
} | ||
lksfrnz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
131 changes: 131 additions & 0 deletions
131
src/main/java/software/amazon/cloudwatchlogs/emf/model/HistogramMetric.java
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,131 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* 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 software.amazon.cloudwatchlogs.emf.model; | ||
|
||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.Queue; | ||
import software.amazon.cloudwatchlogs.emf.Constants; | ||
import software.amazon.cloudwatchlogs.emf.exception.InvalidMetricException; | ||
|
||
/** Represents the Histogram of the EMF schema. */ | ||
public class HistogramMetric extends Metric<Histogram> { | ||
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. class |
||
|
||
HistogramMetric( | ||
Unit unit, | ||
StorageResolution storageResolution, | ||
List<Double> values, | ||
List<Integer> counts) | ||
throws IllegalArgumentException { | ||
this(unit, storageResolution, new Histogram(values, counts)); | ||
} | ||
|
||
protected HistogramMetric( | ||
String name, Unit unit, StorageResolution storageResolution, Histogram histogram) { | ||
this.unit = unit; | ||
this.storageResolution = storageResolution; | ||
this.values = histogram; | ||
this.name = name; | ||
} | ||
|
||
HistogramMetric(Unit unit, StorageResolution storageResolution, Histogram histogram) { | ||
this.unit = unit; | ||
this.storageResolution = storageResolution; | ||
this.values = histogram; | ||
} | ||
|
||
@Override | ||
protected Queue<Metric<Histogram>> serialize() throws InvalidMetricException { | ||
// Histograms will be rejected from CWL if they have more than | ||
// Constants.MAX_DATAPOINTS_PER_METRIC number of bins. Unlike MetricDefinition histograms | ||
// cannot be broken into multiple messages therefore an error is raised to let users know | ||
// their message won't be sent otherwise only this metric will be sent | ||
if (isOversized()) { | ||
throw new InvalidMetricException( | ||
String.format( | ||
"Histogram metric, %s, has %d values which exceeds the maximum amount " | ||
+ "of bins allowed, %d, and Histograms cannot be broken into " | ||
+ "multiple metrics therefore it will not be published", | ||
name, values.values.size(), Constants.MAX_DATAPOINTS_PER_METRIC)); | ||
} | ||
Queue<Metric<Histogram>> metrics = new LinkedList<>(); | ||
metrics.offer(this); | ||
return metrics; | ||
} | ||
|
||
@Override | ||
protected boolean isOversized() { | ||
return values.values.size() > Constants.MAX_DATAPOINTS_PER_METRIC; | ||
} | ||
|
||
@Override | ||
public boolean hasValidValues() { | ||
return values != null && values.count > 0 && !isOversized(); | ||
} | ||
|
||
public static HistogramMetricBuilder builder() { | ||
return new HistogramMetricBuilder(); | ||
} | ||
|
||
public static class HistogramMetricBuilder | ||
extends Metric.MetricBuilder<Histogram, HistogramMetricBuilder> { | ||
|
||
@Override | ||
protected HistogramMetricBuilder getThis() { | ||
return this; | ||
} | ||
|
||
public HistogramMetricBuilder() { | ||
this.values = new Histogram(); | ||
} | ||
|
||
@Override | ||
public Histogram getValues() { | ||
rwl.readLock().lock(); | ||
try { | ||
return values.reduce(); | ||
} finally { | ||
rwl.readLock().unlock(); | ||
} | ||
} | ||
|
||
@Override | ||
public HistogramMetricBuilder addValue(double value) { | ||
rwl.readLock().lock(); | ||
try { | ||
values.addValue(value); | ||
return this; | ||
} finally { | ||
rwl.readLock().unlock(); | ||
} | ||
} | ||
|
||
@Override | ||
public HistogramMetric build() { | ||
rwl.writeLock().lock(); | ||
try { | ||
values.reduce(); | ||
if (name == null) { | ||
return new HistogramMetric(unit, storageResolution, values); | ||
} | ||
return new HistogramMetric(name, unit, storageResolution, values); | ||
} finally { | ||
rwl.writeLock().unlock(); | ||
} | ||
} | ||
} | ||
} |
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.