|
| 1 | +/* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | + * |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 4 | + * You may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package software.amazon.cloudwatchlogs.emf.model; |
| 17 | + |
| 18 | +import com.fasterxml.jackson.annotation.JsonIgnore; |
| 19 | +import com.fasterxml.jackson.annotation.JsonProperty; |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.Collections; |
| 22 | +import java.util.HashMap; |
| 23 | +import java.util.List; |
| 24 | +import java.util.Map; |
| 25 | +import software.amazon.cloudwatchlogs.emf.Constants; |
| 26 | +import software.amazon.cloudwatchlogs.emf.exception.InvalidMetricException; |
| 27 | + |
| 28 | +/** Histogram metric type */ |
| 29 | +class Histogram extends Statistics { |
| 30 | + Histogram(List<Double> values, List<Integer> counts) throws IllegalArgumentException { |
| 31 | + if (counts.size() != values.size()) { |
| 32 | + throw new IllegalArgumentException("Counts and values must have the same size"); |
| 33 | + } |
| 34 | + |
| 35 | + if (values.stream().anyMatch(n -> n == null) || counts.stream().anyMatch(n -> n == null)) { |
| 36 | + throw new IllegalArgumentException("Values and counts cannot contain null values"); |
| 37 | + } |
| 38 | + |
| 39 | + if (!validSize(counts.size())) { |
| 40 | + throw new IllegalArgumentException( |
| 41 | + String.format( |
| 42 | + "Histogram provided with %d bins but CloudWatch will drop Histograms with more than %d bins", |
| 43 | + counts.size(), Constants.MAX_DATAPOINTS_PER_METRIC)); |
| 44 | + } |
| 45 | + |
| 46 | + this.max = Collections.max(values); |
| 47 | + this.min = Collections.min(values); |
| 48 | + this.count = counts.stream().mapToInt(Integer::intValue).sum(); |
| 49 | + this.sum = 0d; |
| 50 | + for (int i = 0; i < counts.size(); i++) { |
| 51 | + this.sum += values.get(i) * counts.get(i); |
| 52 | + } |
| 53 | + this.counts = counts; |
| 54 | + this.values = values; |
| 55 | + } |
| 56 | + |
| 57 | + Histogram() { |
| 58 | + count = 0; |
| 59 | + sum = 0.; |
| 60 | + values = new ArrayList<>(); |
| 61 | + counts = new ArrayList<>(); |
| 62 | + } |
| 63 | + |
| 64 | + @JsonProperty("Values") |
| 65 | + public List<Double> values; |
| 66 | + |
| 67 | + @JsonProperty("Counts") |
| 68 | + public List<Integer> counts; |
| 69 | + |
| 70 | + @JsonIgnore private boolean reduced = false; |
| 71 | + |
| 72 | + @JsonIgnore private static final double EPSILON = 0.1; |
| 73 | + @JsonIgnore private static final double BIN_SIZE = Math.log(1 + EPSILON); |
| 74 | + @JsonIgnore private final Map<Double, Integer> buckets = new HashMap<>(); |
| 75 | + |
| 76 | + /** |
| 77 | + * @param value the value to add to the histogram |
| 78 | + * @throws InvalidMetricException if adding this value would increase the number of bins in the |
| 79 | + * histogram to more than {@value Constants#MAX_DATAPOINTS_PER_METRIC} |
| 80 | + * @see Constants#MAX_DATAPOINTS_PER_METRIC |
| 81 | + */ |
| 82 | + @Override |
| 83 | + void addValue(double value) throws InvalidMetricException { |
| 84 | + reduced = false; |
| 85 | + super.addValue(value); |
| 86 | + |
| 87 | + double bucket = getBucket(value); |
| 88 | + if (!buckets.containsKey(bucket) && !validSize(counts.size() + 1)) { |
| 89 | + throw new InvalidMetricException( |
| 90 | + String.format( |
| 91 | + "Adding this value increases the number of bins in this histogram to %d" |
| 92 | + + ", CloudWatch will drop any Histogram metrics with more than %d bins", |
| 93 | + buckets.size() + 1, Constants.MAX_DATAPOINTS_PER_METRIC)); |
| 94 | + } |
| 95 | + // Add the value to the appropriate bucket (or create a new bucket if necessary) |
| 96 | + buckets.compute( |
| 97 | + bucket, |
| 98 | + (k, v) -> { |
| 99 | + if (v == null) { |
| 100 | + return 1; |
| 101 | + } else { |
| 102 | + return v + 1; |
| 103 | + } |
| 104 | + }); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Updates the Values and Counts lists to represent the buckets of this histogram. |
| 109 | + * |
| 110 | + * @return the reduced histogram |
| 111 | + */ |
| 112 | + Histogram reduce() { |
| 113 | + if (reduced) { |
| 114 | + return this; |
| 115 | + } |
| 116 | + |
| 117 | + this.values = new ArrayList<>(buckets.size()); |
| 118 | + this.counts = new ArrayList<>(buckets.size()); |
| 119 | + |
| 120 | + for (Map.Entry<Double, Integer> entry : buckets.entrySet()) { |
| 121 | + this.values.add(entry.getKey()); |
| 122 | + this.counts.add(entry.getValue()); |
| 123 | + } |
| 124 | + |
| 125 | + reduced = true; |
| 126 | + return this; |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Gets the value of the bucket for the given value. |
| 131 | + * |
| 132 | + * @param value the value to find the closest bucket for |
| 133 | + * @return the value of the bucket the given value goes in |
| 134 | + */ |
| 135 | + private static double getBucket(double value) { |
| 136 | + short index = (short) Math.floor(Math.log(value) / BIN_SIZE); |
| 137 | + return Math.exp((index + 0.5) * BIN_SIZE); |
| 138 | + } |
| 139 | + |
| 140 | + private boolean validSize(int size) { |
| 141 | + return size <= Constants.MAX_DATAPOINTS_PER_METRIC; |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + public boolean equals(Object o) { |
| 146 | + if (this == o) return true; |
| 147 | + if (o == null || getClass() != o.getClass()) return false; |
| 148 | + Histogram that = (Histogram) o; |
| 149 | + return count == that.count |
| 150 | + && that.sum.equals(sum) |
| 151 | + && that.max.equals(max) |
| 152 | + && that.min.equals(min) |
| 153 | + && buckets.equals(that.buckets); |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + public int hashCode() { |
| 158 | + return super.hashCode() + buckets.hashCode(); |
| 159 | + } |
| 160 | +} |
0 commit comments