30
30
import software .amazon .cloudwatchlogs .emf .exception .InvalidMetricException ;
31
31
import software .amazon .cloudwatchlogs .emf .exception .InvalidNamespaceException ;
32
32
import software .amazon .cloudwatchlogs .emf .exception .InvalidTimestampException ;
33
+ import software .amazon .cloudwatchlogs .emf .model .AggregationType ;
33
34
import software .amazon .cloudwatchlogs .emf .model .DimensionSet ;
35
+ import software .amazon .cloudwatchlogs .emf .model .Metric ;
34
36
import software .amazon .cloudwatchlogs .emf .model .MetricsContext ;
35
37
import software .amazon .cloudwatchlogs .emf .model .StorageResolution ;
36
38
import software .amazon .cloudwatchlogs .emf .model .Unit ;
@@ -45,6 +47,7 @@ public class MetricsLogger {
45
47
private MetricsContext context ;
46
48
private CompletableFuture <Environment > environmentFuture ;
47
49
private EnvironmentProvider environmentProvider ;
50
+ @ Getter @ Setter private volatile AggregationType defaultAggregationType = AggregationType .NONE ;
48
51
/**
49
52
* This lock is used to create an internal sync context for flush() method in multi-threaded
50
53
* situations. Flush() acquires write lock, other methods (accessing mutable shared data with
@@ -191,18 +194,23 @@ public MetricsLogger resetDimensions(boolean useDefault) {
191
194
* @param value is the value of the metric
192
195
* @param unit is the unit of the metric value
193
196
* @param storageResolution is the resolution of the metric
197
+ * @param aggregationType is the aggregation type of the metric
194
198
* @see <a
195
199
* href="https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/publishingMetrics.html#high-resolution-metrics">CloudWatch
196
200
* High Resolution Metrics</a>
197
201
* @return the current logger
198
202
* @throws InvalidMetricException if the metric is invalid
199
203
*/
200
204
public MetricsLogger putMetric (
201
- String key , double value , Unit unit , StorageResolution storageResolution )
205
+ String key ,
206
+ double value ,
207
+ Unit unit ,
208
+ StorageResolution storageResolution ,
209
+ AggregationType aggregationType )
202
210
throws InvalidMetricException {
203
211
rwl .readLock ().lock ();
204
212
try {
205
- this .context .putMetric (key , value , unit , storageResolution );
213
+ this .context .putMetric (key , value , unit , storageResolution , aggregationType );
206
214
return this ;
207
215
} finally {
208
216
rwl .readLock ().unlock ();
@@ -225,7 +233,7 @@ public MetricsLogger putMetric(
225
233
*/
226
234
public MetricsLogger putMetric (String key , double value , StorageResolution storageResolution )
227
235
throws InvalidMetricException {
228
- this .putMetric (key , value , Unit .NONE , storageResolution );
236
+ this .putMetric (key , value , Unit .NONE , storageResolution , defaultAggregationType );
229
237
return this ;
230
238
}
231
239
@@ -242,7 +250,7 @@ public MetricsLogger putMetric(String key, double value, StorageResolution stora
242
250
*/
243
251
public MetricsLogger putMetric (String key , double value , Unit unit )
244
252
throws InvalidMetricException {
245
- this .putMetric (key , value , unit , StorageResolution .STANDARD );
253
+ this .putMetric (key , value , unit , StorageResolution .STANDARD , defaultAggregationType );
246
254
return this ;
247
255
}
248
256
@@ -257,10 +265,111 @@ public MetricsLogger putMetric(String key, double value, Unit unit)
257
265
* @throws InvalidMetricException if the metric is invalid
258
266
*/
259
267
public MetricsLogger putMetric (String key , double value ) throws InvalidMetricException {
260
- this .putMetric (key , value , Unit .NONE , StorageResolution .STANDARD );
268
+ this .putMetric (key , value , Unit .NONE , StorageResolution .STANDARD , defaultAggregationType );
269
+ return this ;
270
+ }
271
+
272
+ /**
273
+ * Put a metric value. This value will be emitted to CloudWatch Metrics asynchronously and does
274
+ * not contribute to your account TPS limits. The value will also be available in your
275
+ * CloudWatch Logs
276
+ *
277
+ * @param key is the name of the metric
278
+ * @param value is the value of the metric
279
+ * @param unit is the unit of the metric value
280
+ * @param storageResolution is the resolution of the metric
281
+ * @see <a
282
+ * href="https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/publishingMetrics.html#high-resolution-metrics">CloudWatch
283
+ * High Resolution Metrics</a>
284
+ * @return the current logger
285
+ * @throws InvalidMetricException if the metric is invalid
286
+ */
287
+ public MetricsLogger putMetric (
288
+ String key , double value , Unit unit , StorageResolution storageResolution )
289
+ throws InvalidMetricException {
290
+ this .putMetric (key , value , Unit .NONE , storageResolution , defaultAggregationType );
291
+ return this ;
292
+ }
293
+
294
+ /**
295
+ * Put a metric value. This value will be emitted to CloudWatch Metrics asynchronously and does
296
+ * not contribute to your account TPS limits. The value will also be available in your
297
+ * CloudWatch Logs
298
+ *
299
+ * @param key is the name of the metric
300
+ * @param value is the value of the metric
301
+ * @param storageResolution is the resolution of the metric
302
+ * @param aggregationType is the aggregation type of the metric
303
+ * @see <a
304
+ * href="https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/publishingMetrics.html#high-resolution-metrics">CloudWatch
305
+ * High Resolution Metrics</a>
306
+ * @return the current logger
307
+ * @throws InvalidMetricException if the metric is invalid
308
+ */
309
+ public MetricsLogger putMetric (
310
+ String key ,
311
+ double value ,
312
+ StorageResolution storageResolution ,
313
+ AggregationType aggregationType )
314
+ throws InvalidMetricException {
315
+ this .putMetric (key , value , Unit .NONE , storageResolution , aggregationType );
316
+ return this ;
317
+ }
318
+
319
+ /**
320
+ * Put a metric value. This value will be emitted to CloudWatch Metrics asynchronously and does
321
+ * not contribute to your account TPS limits. The value will also be available in your
322
+ * CloudWatch Logs
323
+ *
324
+ * @param key is the name of the metric
325
+ * @param value is the value of the metric
326
+ * @param unit is the unit of the metric value
327
+ * @param aggregationType is the aggregation type of the metric
328
+ * @return the current logger
329
+ * @throws InvalidMetricException if the metric is invalid
330
+ */
331
+ public MetricsLogger putMetric (
332
+ String key , double value , Unit unit , AggregationType aggregationType )
333
+ throws InvalidMetricException {
334
+ this .putMetric (key , value , unit , StorageResolution .STANDARD , aggregationType );
335
+ return this ;
336
+ }
337
+
338
+ /**
339
+ * Put a metric value. This value will be emitted to CloudWatch Metrics asynchronously and does
340
+ * not contribute to your account TPS limits. The value will also be available in your
341
+ * CloudWatch Logs
342
+ *
343
+ * @param key the name of the metric
344
+ * @param value the value of the metric
345
+ * @param aggregationType is the aggregation type of the metric
346
+ * @return the current logger
347
+ * @throws InvalidMetricException if the metric is invalid
348
+ */
349
+ public MetricsLogger putMetric (String key , double value , AggregationType aggregationType )
350
+ throws InvalidMetricException {
351
+ this .putMetric (key , value , Unit .NONE , StorageResolution .STANDARD , aggregationType );
261
352
return this ;
262
353
}
263
354
355
+ /**
356
+ * Set a metric value, if a metric already has the same key it will be overwitten. This value
357
+ * will be emitted to CloudWatch Metrics asynchronously and does not contribute to your account
358
+ * TPS limits. The value will also be available in your CloudWatch Logs
359
+ *
360
+ * @param key the name of the metric
361
+ * @param value the value of the metric
362
+ * @return the current logger
363
+ * @throws InvalidMetricException if the metric is invalid
364
+ */
365
+ public MetricsLogger setMetric (String key , Metric value ) {
366
+ return applyReadLock (
367
+ () -> {
368
+ this .context .setMetric (key , value );
369
+ return this ;
370
+ });
371
+ }
372
+
264
373
/**
265
374
* Add a custom key-value pair to the Metadata object.
266
375
*
0 commit comments