@Override public int hashCode() { int hashCode = 1; hashCode = 31 * hashCode + Objects.hashCode(metricName()); hashCode = 31 * hashCode + Objects.hashCode(dimensions()); hashCode = 31 * hashCode + Objects.hashCode(timestamp()); hashCode = 31 * hashCode + Objects.hashCode(value()); hashCode = 31 * hashCode + Objects.hashCode(statisticValues()); hashCode = 31 * hashCode + Objects.hashCode(values()); hashCode = 31 * hashCode + Objects.hashCode(counts()); hashCode = 31 * hashCode + Objects.hashCode(unitAsString()); hashCode = 31 * hashCode + Objects.hashCode(storageResolution()); return hashCode; }
@Override public MetricDatum build() { return new MetricDatum(this); }
/** * @param datum data point */ public CloudWatchMetricKey(MetricDatum datum) { this.dimensions = datum.dimensions(); this.metricName = datum.metricName(); }
private void accumulate(MetricDatumWithKey<KeyType> metricDatumWithKey, MetricDatum newDatum) { MetricDatum oldDatum = metricDatumWithKey.datum; if (!oldDatum.unit().equals(newDatum.unit())) { throw new IllegalArgumentException("Unit mismatch for datum named " + oldDatum.metricName()); } StatisticSet oldStats = oldDatum.statisticValues(); StatisticSet newStats = newDatum.statisticValues(); StatisticSet statisticSet = oldStats.toBuilder().sum(oldStats.sum() + newStats.sum()) .minimum(Math.min(oldStats.minimum(), newStats.minimum())) .maximum(Math.max(oldStats.maximum(), newStats.maximum())) .sampleCount(oldStats.sampleCount() + newStats.sampleCount()).build(); MetricDatum datum = oldDatum.toBuilder().statisticValues(statisticSet).build(); metricDatumWithKey.datum(datum); } }
final MetricDatum metricDatum; if (datum == null) { metricDatum = MetricDatum.builder().metricName(name).unit(unit) .statisticValues( StatisticSet.builder().maximum(value).minimum(value).sampleCount(1.0).sum(value).build()) .build(); } else { if (!datum.unit().equals(unit)) { throw new IllegalArgumentException("Cannot add to existing metric with different unit"); final StatisticSet oldStatisticSet = datum.statisticValues(); final StatisticSet statisticSet = oldStatisticSet.toBuilder() .maximum(Math.max(value, oldStatisticSet.maximum())) .sum(oldStatisticSet.sum() + value).build(); metricDatum = datum.toBuilder().statisticValues(statisticSet).build();
@Override public void end() { StringBuilder output = new StringBuilder(); output.append("Metrics:\n"); output.append("Dimensions: "); boolean needsComma = false; for (Dimension dimension : getDimensions()) { output.append(String.format("%s[%s: %s]", needsComma ? ", " : "", dimension.name(), dimension.value())); needsComma = true; } output.append("\n"); for (MetricDatum datum : data.values()) { StatisticSet statistics = datum.statisticValues(); output.append(String.format("Name=%25s\tMin=%.2f\tMax=%.2f\tCount=%.2f\tSum=%.2f\tAvg=%.2f\tUnit=%s\n", datum.metricName(), statistics.minimum(), statistics.maximum(), statistics.sampleCount(), statistics.sum(), statistics.sum() / statistics.sampleCount(), datum.unit())); } log.info(output.toString()); } }
@Override @SafeVarargs public final Builder metricData(Consumer<MetricDatum.Builder>... metricData) { metricData(Stream.of(metricData).map(c -> MetricDatum.builder().applyMutation(c).build()) .collect(Collectors.toList())); return this; }
/** * Once we call this method, all MetricDatums added to the scope will be enqueued to the publisher runnable. * We enqueue MetricDatumWithKey because the publisher will aggregate similar metrics (i.e. MetricDatum with the * same metricName) in the background thread. Hence aggregation using MetricDatumWithKey will be especially useful * when aggregating across multiple MetricScopes. */ @Override public void end() { super.end(); final List<MetricDatumWithKey<CloudWatchMetricKey>> dataWithKeys = data.values().stream() .map(metricDatum -> metricDatum.toBuilder().dimensions(getDimensions()).build()) .map(metricDatum -> new MetricDatumWithKey<>(new CloudWatchMetricKey(metricDatum), metricDatum)) .collect(Collectors.toList()); publisher.enqueue(dataWithKeys); }
private void accumulate(MetricDatumWithKey<KeyType> metricDatumWithKey, MetricDatum newDatum) { MetricDatum oldDatum = metricDatumWithKey.datum; if (!oldDatum.unit().equals(newDatum.unit())) { throw new IllegalArgumentException("Unit mismatch for datum named " + oldDatum.metricName()); } StatisticSet oldStats = oldDatum.statisticValues(); StatisticSet newStats = newDatum.statisticValues(); StatisticSet statisticSet = oldStats.toBuilder().sum(oldStats.sum() + newStats.sum()) .minimum(Math.min(oldStats.minimum(), newStats.minimum())) .maximum(Math.max(oldStats.maximum(), newStats.maximum())) .sampleCount(oldStats.sampleCount() + newStats.sampleCount()).build(); MetricDatum datum = oldDatum.toBuilder().statisticValues(statisticSet).build(); metricDatumWithKey.datum(datum); } }
final MetricDatum metricDatum; if (datum == null) { metricDatum = MetricDatum.builder().metricName(name).unit(unit) .statisticValues( StatisticSet.builder().maximum(value).minimum(value).sampleCount(1.0).sum(value).build()) .build(); } else { if (!datum.unit().equals(unit)) { throw new IllegalArgumentException("Cannot add to existing metric with different unit"); final StatisticSet oldStatisticSet = datum.statisticValues(); final StatisticSet statisticSet = oldStatisticSet.toBuilder() .maximum(Math.max(value, oldStatisticSet.maximum())) .sum(oldStatisticSet.sum() + value).build(); metricDatum = datum.toBuilder().statisticValues(statisticSet).build();
@Override public void end() { StringBuilder output = new StringBuilder(); output.append("Metrics:\n"); output.append("Dimensions: "); boolean needsComma = false; for (Dimension dimension : getDimensions()) { output.append(String.format("%s[%s: %s]", needsComma ? ", " : "", dimension.name(), dimension.value())); needsComma = true; } output.append("\n"); for (MetricDatum datum : data.values()) { StatisticSet statistics = datum.statisticValues(); output.append(String.format("Name=%25s\tMin=%.2f\tMax=%.2f\tCount=%.2f\tSum=%.2f\tAvg=%.2f\tUnit=%s\n", datum.metricName(), statistics.minimum(), statistics.maximum(), statistics.sampleCount(), statistics.sum(), statistics.sum() / statistics.sampleCount(), datum.unit())); } log.info(output.toString()); } }
/** * @param datum data point */ public CloudWatchMetricKey(MetricDatum datum) { this.dimensions = datum.dimensions(); this.metricName = datum.metricName(); }
@Override @SafeVarargs public final Builder metricData(Consumer<MetricDatum.Builder>... metricData) { metricData(Stream.of(metricData).map(c -> MetricDatum.builder().applyMutation(c).build()) .collect(Collectors.toList())); return this; }
/** * Once we call this method, all MetricDatums added to the scope will be enqueued to the publisher runnable. * We enqueue MetricDatumWithKey because the publisher will aggregate similar metrics (i.e. MetricDatum with the * same metricName) in the background thread. Hence aggregation using MetricDatumWithKey will be especially useful * when aggregating across multiple MetricScopes. */ @Override public void end() { super.end(); final List<MetricDatumWithKey<CloudWatchMetricKey>> dataWithKeys = data.values().stream() .map(metricDatum -> metricDatum.toBuilder().dimensions(getDimensions()).build()) .map(metricDatum -> new MetricDatumWithKey<>(new CloudWatchMetricKey(metricDatum), metricDatum)) .collect(Collectors.toList()); publisher.enqueue(dataWithKeys); }
/** * Returns a string representation of this object. This is useful for testing and debugging. Sensitive data will be * redacted from this string using a placeholder value. */ @Override public String toString() { return ToString.builder("MetricDatum").add("MetricName", metricName()).add("Dimensions", dimensions()) .add("Timestamp", timestamp()).add("Value", value()).add("StatisticValues", statisticValues()) .add("Values", values()).add("Counts", counts()).add("Unit", unitAsString()) .add("StorageResolution", storageResolution()).build(); }
@Override public MetricDatum build() { return new MetricDatum(this); }
@Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (!(obj instanceof MetricDatum)) { return false; } MetricDatum other = (MetricDatum) obj; return Objects.equals(metricName(), other.metricName()) && Objects.equals(dimensions(), other.dimensions()) && Objects.equals(timestamp(), other.timestamp()) && Objects.equals(value(), other.value()) && Objects.equals(statisticValues(), other.statisticValues()) && Objects.equals(values(), other.values()) && Objects.equals(counts(), other.counts()) && Objects.equals(unitAsString(), other.unitAsString()) && Objects.equals(storageResolution(), other.storageResolution()); }
@Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (!(obj instanceof MetricDatum)) { return false; } MetricDatum other = (MetricDatum) obj; return Objects.equals(metricName(), other.metricName()) && Objects.equals(dimensions(), other.dimensions()) && Objects.equals(timestamp(), other.timestamp()) && Objects.equals(value(), other.value()) && Objects.equals(statisticValues(), other.statisticValues()) && Objects.equals(values(), other.values()) && Objects.equals(counts(), other.counts()) && Objects.equals(unitAsString(), other.unitAsString()) && Objects.equals(storageResolution(), other.storageResolution()); }
/** * Returns a string representation of this object. This is useful for testing and debugging. Sensitive data will be * redacted from this string using a placeholder value. */ @Override public String toString() { return ToString.builder("MetricDatum").add("MetricName", metricName()).add("Dimensions", dimensions()) .add("Timestamp", timestamp()).add("Value", value()).add("StatisticValues", statisticValues()) .add("Values", values()).add("Counts", counts()).add("Unit", unitAsString()) .add("StorageResolution", storageResolution()).build(); }
@Override public int hashCode() { int hashCode = 1; hashCode = 31 * hashCode + Objects.hashCode(metricName()); hashCode = 31 * hashCode + Objects.hashCode(dimensions()); hashCode = 31 * hashCode + Objects.hashCode(timestamp()); hashCode = 31 * hashCode + Objects.hashCode(value()); hashCode = 31 * hashCode + Objects.hashCode(statisticValues()); hashCode = 31 * hashCode + Objects.hashCode(values()); hashCode = 31 * hashCode + Objects.hashCode(counts()); hashCode = 31 * hashCode + Objects.hashCode(unitAsString()); hashCode = 31 * hashCode + Objects.hashCode(storageResolution()); return hashCode; }