Skip to content

Commit 2d7a128

Browse files
committed
Added Statistic Set Metric Type
1 parent 59e2593 commit 2d7a128

File tree

10 files changed

+696
-28
lines changed

10 files changed

+696
-28
lines changed

src/main/java/software/amazon/cloudwatchlogs/emf/logger/MetricsLogger.java

Lines changed: 95 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import software.amazon.cloudwatchlogs.emf.exception.InvalidMetricException;
3131
import software.amazon.cloudwatchlogs.emf.exception.InvalidNamespaceException;
3232
import software.amazon.cloudwatchlogs.emf.exception.InvalidTimestampException;
33+
import software.amazon.cloudwatchlogs.emf.model.AggregationType;
3334
import software.amazon.cloudwatchlogs.emf.model.DimensionSet;
3435
import software.amazon.cloudwatchlogs.emf.model.MetricsContext;
3536
import software.amazon.cloudwatchlogs.emf.model.StorageResolution;
@@ -45,6 +46,7 @@ public class MetricsLogger {
4546
private MetricsContext context;
4647
private CompletableFuture<Environment> environmentFuture;
4748
private EnvironmentProvider environmentProvider;
49+
@Getter @Setter private volatile AggregationType defaultAggregationType = AggregationType.NONE;
4850
/**
4951
* This lock is used to create an internal sync context for flush() method in multi-threaded
5052
* situations. Flush() acquires write lock, other methods (accessing mutable shared data with
@@ -191,18 +193,23 @@ public MetricsLogger resetDimensions(boolean useDefault) {
191193
* @param value is the value of the metric
192194
* @param unit is the unit of the metric value
193195
* @param storageResolution is the resolution of the metric
196+
* @param aggregationType is the aggregation type of the metric
194197
* @see <a
195198
* href="https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/publishingMetrics.html#high-resolution-metrics">CloudWatch
196199
* High Resolution Metrics</a>
197200
* @return the current logger
198201
* @throws InvalidMetricException if the metric is invalid
199202
*/
200203
public MetricsLogger putMetric(
201-
String key, double value, Unit unit, StorageResolution storageResolution)
204+
String key,
205+
double value,
206+
Unit unit,
207+
StorageResolution storageResolution,
208+
AggregationType aggregationType)
202209
throws InvalidMetricException {
203210
rwl.readLock().lock();
204211
try {
205-
this.context.putMetric(key, value, unit, storageResolution);
212+
this.context.putMetric(key, value, unit, storageResolution, aggregationType);
206213
return this;
207214
} finally {
208215
rwl.readLock().unlock();
@@ -225,7 +232,7 @@ public MetricsLogger putMetric(
225232
*/
226233
public MetricsLogger putMetric(String key, double value, StorageResolution storageResolution)
227234
throws InvalidMetricException {
228-
this.putMetric(key, value, Unit.NONE, storageResolution);
235+
this.putMetric(key, value, Unit.NONE, storageResolution, defaultAggregationType);
229236
return this;
230237
}
231238

@@ -242,7 +249,7 @@ public MetricsLogger putMetric(String key, double value, StorageResolution stora
242249
*/
243250
public MetricsLogger putMetric(String key, double value, Unit unit)
244251
throws InvalidMetricException {
245-
this.putMetric(key, value, unit, StorageResolution.STANDARD);
252+
this.putMetric(key, value, unit, StorageResolution.STANDARD, defaultAggregationType);
246253
return this;
247254
}
248255

@@ -257,7 +264,90 @@ public MetricsLogger putMetric(String key, double value, Unit unit)
257264
* @throws InvalidMetricException if the metric is invalid
258265
*/
259266
public MetricsLogger putMetric(String key, double value) throws InvalidMetricException {
260-
this.putMetric(key, value, Unit.NONE, StorageResolution.STANDARD);
267+
this.putMetric(key, value, Unit.NONE, StorageResolution.STANDARD, defaultAggregationType);
268+
return this;
269+
}
270+
271+
/**
272+
* Put a metric value. This value will be emitted to CloudWatch Metrics asynchronously and does
273+
* not contribute to your account TPS limits. The value will also be available in your
274+
* CloudWatch Logs
275+
*
276+
* @param key is the name of the metric
277+
* @param value is the value of the metric
278+
* @param unit is the unit of the metric value
279+
* @param storageResolution is the resolution of the metric
280+
* @see <a
281+
* href="https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/publishingMetrics.html#high-resolution-metrics">CloudWatch
282+
* High Resolution Metrics</a>
283+
* @return the current logger
284+
* @throws InvalidMetricException if the metric is invalid
285+
*/
286+
public MetricsLogger putMetric(
287+
String key, double value, Unit unit, StorageResolution storageResolution)
288+
throws InvalidMetricException {
289+
this.putMetric(key, value, Unit.NONE, storageResolution, defaultAggregationType);
290+
return this;
291+
}
292+
293+
/**
294+
* Put a metric value. This value will be emitted to CloudWatch Metrics asynchronously and does
295+
* not contribute to your account TPS limits. The value will also be available in your
296+
* CloudWatch Logs
297+
*
298+
* @param key is the name of the metric
299+
* @param value is the value of the metric
300+
* @param storageResolution is the resolution of the metric
301+
* @param aggregationType is the aggregation type of the metric
302+
* @see <a
303+
* href="https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/publishingMetrics.html#high-resolution-metrics">CloudWatch
304+
* High Resolution Metrics</a>
305+
* @return the current logger
306+
* @throws InvalidMetricException if the metric is invalid
307+
*/
308+
public MetricsLogger putMetric(
309+
String key,
310+
double value,
311+
StorageResolution storageResolution,
312+
AggregationType aggregationType)
313+
throws InvalidMetricException {
314+
this.putMetric(key, value, Unit.NONE, storageResolution, aggregationType);
315+
return this;
316+
}
317+
318+
/**
319+
* Put a metric value. This value will be emitted to CloudWatch Metrics asynchronously and does
320+
* not contribute to your account TPS limits. The value will also be available in your
321+
* CloudWatch Logs
322+
*
323+
* @param key is the name of the metric
324+
* @param value is the value of the metric
325+
* @param unit is the unit of the metric value
326+
* @param aggregationType is the aggregation type of the metric
327+
* @return the current logger
328+
* @throws InvalidMetricException if the metric is invalid
329+
*/
330+
public MetricsLogger putMetric(
331+
String key, double value, Unit unit, AggregationType aggregationType)
332+
throws InvalidMetricException {
333+
this.putMetric(key, value, unit, StorageResolution.STANDARD, aggregationType);
334+
return this;
335+
}
336+
337+
/**
338+
* Put a metric value. This value will be emitted to CloudWatch Metrics asynchronously and does
339+
* not contribute to your account TPS limits. The value will also be available in your
340+
* CloudWatch Logs
341+
*
342+
* @param key the name of the metric
343+
* @param value the value of the metric
344+
* @param aggregationType is the aggregation type of the metric
345+
* @return the current logger
346+
* @throws InvalidMetricException if the metric is invalid
347+
*/
348+
public MetricsLogger putMetric(String key, double value, AggregationType aggregationType)
349+
throws InvalidMetricException {
350+
this.putMetric(key, value, Unit.NONE, StorageResolution.STANDARD, aggregationType);
261351
return this;
262352
}
263353

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package software.amazon.cloudwatchlogs.emf.model;
18+
19+
public enum AggregationType {
20+
NONE(60),
21+
STATISTIC_SET(1),
22+
UNKNOWN_TO_SDK_VERSION(-1);
23+
24+
private final int value;
25+
26+
AggregationType(final int newValue) {
27+
value = newValue;
28+
}
29+
30+
public int getValue() {
31+
return this.value;
32+
}
33+
}

src/main/java/software/amazon/cloudwatchlogs/emf/model/Metric.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@ public abstract class Metric<V> {
4141
@JsonProperty("Unit")
4242
@JsonSerialize(using = UnitSerializer.class)
4343
@JsonDeserialize(using = UnitDeserializer.class)
44-
protected Unit unit;
44+
protected Unit unit = Unit.NONE;
4545

4646
@JsonProperty("StorageResolution")
4747
@JsonInclude(
4848
value = JsonInclude.Include.CUSTOM,
4949
valueFilter =
5050
StorageResolutionFilter.class) // Do not serialize when valueFilter is true
5151
@JsonSerialize(using = StorageResolutionSerializer.class)
52-
protected StorageResolution storageResolution;
52+
protected StorageResolution storageResolution = StorageResolution.STANDARD;
5353

5454
@JsonIgnore @Getter protected V values;
5555

src/main/java/software/amazon/cloudwatchlogs/emf/model/MetricDirective.java

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,31 +69,44 @@ void putDimensionSet(DimensionSet dimensionSet) {
6969

7070
// Helper method for testing putMetric()
7171
void putMetric(String key, double value) {
72-
putMetric(key, value, Unit.NONE, StorageResolution.STANDARD);
72+
putMetric(key, value, Unit.NONE, StorageResolution.STANDARD, AggregationType.NONE);
7373
}
7474

7575
// Helper method for testing putMetric()
7676
void putMetric(String key, double value, Unit unit) {
77-
putMetric(key, value, unit, StorageResolution.STANDARD);
77+
putMetric(key, value, unit, StorageResolution.STANDARD, AggregationType.NONE);
7878
}
7979

8080
// Helper method for testing serialization
8181
void putMetric(String key, double value, StorageResolution storageResolution) {
82-
putMetric(key, value, Unit.NONE, storageResolution);
82+
putMetric(key, value, Unit.NONE, storageResolution, AggregationType.NONE);
8383
}
8484

85-
void putMetric(String key, double value, Unit unit, StorageResolution storageResolution) {
85+
void putMetric(
86+
String key,
87+
double value,
88+
Unit unit,
89+
StorageResolution storageResolution,
90+
AggregationType aggregationType) {
8691
metrics.compute(
8792
key,
8893
(k, v) -> {
8994
if (v == null) {
90-
MetricDefinition.MetricDefinitionBuilder builder =
91-
MetricDefinition.builder()
95+
switch (aggregationType) {
96+
case STATISTIC_SET:
97+
return StatisticSet.builder()
9298
.name(k)
9399
.unit(unit)
94100
.storageResolution(storageResolution)
95101
.addValue(value);
96-
return builder;
102+
case NONE:
103+
default:
104+
return MetricDefinition.builder()
105+
.name(k)
106+
.unit(unit)
107+
.storageResolution(storageResolution)
108+
.addValue(value);
109+
}
97110
} else if (v instanceof Metric.MetricBuilder) {
98111
((Metric.MetricBuilder) v).addValue(value);
99112
return v;

0 commit comments

Comments
 (0)