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 .LIST ;
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,74 +194,177 @@ 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 ();
209
217
}
210
218
}
211
219
212
220
/**
213
- * Put a metric value. This value will be emitted to CloudWatch Metrics asynchronously and does
214
- * not contribute to your account TPS limits. The value will also be available in your
215
- * CloudWatch Logs
216
221
*
217
- * @param key is the name of the metric
218
- * @param value is the value of the metric
219
- * @param storageResolution is the resolution of the metric
220
- * @see <a
221
- * href="https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/publishingMetrics.html#high-resolution-metrics">CloudWatch
222
- * High Resolution Metrics</a>
223
- * @return the current logger
224
- * @throws InvalidMetricException if the metric is invalid
222
+ *
223
+ * <ul>
224
+ * <li>Unit defaults to NONE
225
+ * <li>AggregationType defaults to the default AggregationType of this logger
226
+ * </ul>
227
+ *
228
+ * @see MetricsLogger#putMetric(String key, double value, Unit unit, StorageResolution
229
+ * storageResolution, AggregationType aggregationType) putMetric(String key, double value,
230
+ * Unit unit, StorageResolution storageResolution, AggregationType aggregationType)
225
231
*/
226
232
public MetricsLogger putMetric (String key , double value , StorageResolution storageResolution )
227
233
throws InvalidMetricException {
228
- this .putMetric (key , value , Unit .NONE , storageResolution );
234
+ this .putMetric (key , value , Unit .NONE , storageResolution , this . defaultAggregationType );
229
235
return this ;
230
236
}
231
237
232
238
/**
233
- * Put a metric value. This value will be emitted to CloudWatch Metrics asynchronously and does
234
- * not contribute to your account TPS limits. The value will also be available in your
235
- * CloudWatch Logs
236
239
*
237
- * @param key is the name of the metric
238
- * @param value is the value of the metric
239
- * @param unit is the unit of the metric value
240
- * @return the current logger
241
- * @throws InvalidMetricException if the metric is invalid
240
+ *
241
+ * <ul>
242
+ * <li>StorageResolution defaults to STANDARD
243
+ * <li>AggregationType defaults to the default AggregationType of this logger
244
+ * </ul>
245
+ *
246
+ * @see MetricsLogger#putMetric(String key, double value, Unit unit, StorageResolution
247
+ * storageResolution, AggregationType aggregationType) putMetric(String key, double value,
248
+ * Unit unit, StorageResolution storageResolution, AggregationType aggregationType)
242
249
*/
243
250
public MetricsLogger putMetric (String key , double value , Unit unit )
244
251
throws InvalidMetricException {
245
- this .putMetric (key , value , unit , StorageResolution .STANDARD );
252
+ this .putMetric (key , value , unit , StorageResolution .STANDARD , this . defaultAggregationType );
246
253
return this ;
247
254
}
248
255
249
256
/**
250
- * Put a metric value. This value will be emitted to CloudWatch Metrics asynchronously and does
251
- * not contribute to your account TPS limits. The value will also be available in your
252
- * CloudWatch Logs
257
+ *
258
+ *
259
+ * <ul>
260
+ * <li>StorageResolution defaults to STANDARD
261
+ * <li>Unit defaults to NONE
262
+ * <li>AggregationType defaults to the default AggregationType of this logger
263
+ * </ul>
264
+ *
265
+ * @see MetricsLogger#putMetric(String key, double value, Unit unit, StorageResolution
266
+ * storageResolution, AggregationType aggregationType) putMetric(String key, double value,
267
+ * Unit unit, StorageResolution storageResolution, AggregationType aggregationType)
268
+ */
269
+ public MetricsLogger putMetric (String key , double value ) throws InvalidMetricException {
270
+ this .putMetric (
271
+ key , value , Unit .NONE , StorageResolution .STANDARD , this .defaultAggregationType );
272
+ return this ;
273
+ }
274
+
275
+ /**
276
+ *
277
+ *
278
+ * <ul>
279
+ * <li>AggregationType defaults to the default AggregationType of this logger
280
+ * </ul>
281
+ *
282
+ * @see MetricsLogger#putMetric(String key, double value, Unit unit, StorageResolution
283
+ * storageResolution, AggregationType aggregationType) putMetric(String key, double value,
284
+ * Unit unit, StorageResolution storageResolution, AggregationType aggregationType)
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 , this .defaultAggregationType );
290
+ return this ;
291
+ }
292
+
293
+ /**
294
+ *
295
+ *
296
+ * <ul>
297
+ * <li>StorageResolution defaults to STANDARD
298
+ * <li>AggregationType defaults to the default AggregationType of this logger
299
+ * </ul>
300
+ *
301
+ * @see MetricsLogger#putMetric(String key, double value, Unit unit, StorageResolution
302
+ * storageResolution, AggregationType aggregationType) putMetric(String key, double value,
303
+ * Unit unit, StorageResolution storageResolution, AggregationType aggregationType)
304
+ */
305
+ public MetricsLogger putMetric (
306
+ String key ,
307
+ double value ,
308
+ StorageResolution storageResolution ,
309
+ AggregationType aggregationType )
310
+ throws InvalidMetricException {
311
+ this .putMetric (key , value , Unit .NONE , storageResolution , aggregationType );
312
+ return this ;
313
+ }
314
+
315
+ /**
316
+ *
317
+ *
318
+ * <ul>
319
+ * <li>StorageResolution defaults to STANDARD
320
+ * </ul>
321
+ *
322
+ * @see MetricsLogger#putMetric(String key, double value, Unit unit, StorageResolution
323
+ * storageResolution, AggregationType aggregationType) putMetric(String key, double value,
324
+ * Unit unit, StorageResolution storageResolution, AggregationType aggregationType)
325
+ */
326
+ public MetricsLogger putMetric (
327
+ String key , double value , Unit unit , AggregationType aggregationType )
328
+ throws InvalidMetricException {
329
+ this .putMetric (key , value , unit , StorageResolution .STANDARD , aggregationType );
330
+ return this ;
331
+ }
332
+
333
+ /**
334
+ *
335
+ *
336
+ * <ul>
337
+ * <li>StorageResolution defaults to STANDARD
338
+ * <li>Unit defaults to NONE
339
+ * </ul>
340
+ *
341
+ * @see MetricsLogger#putMetric(String key, double value, Unit unit, StorageResolution
342
+ * storageResolution, AggregationType aggregationType) putMetric(String key, double value,
343
+ * Unit unit, StorageResolution storageResolution, AggregationType aggregationType)
344
+ */
345
+ public MetricsLogger putMetric (String key , double value , AggregationType aggregationType )
346
+ throws InvalidMetricException {
347
+ this .putMetric (key , value , Unit .NONE , StorageResolution .STANDARD , aggregationType );
348
+ return this ;
349
+ }
350
+
351
+ /**
352
+ * Set a metric value, if a metric already has the same key it will be overwitten. This value
353
+ * will be emitted to CloudWatch Metrics asynchronously and does not contribute to your account
354
+ * TPS limits. The value will also be available in your CloudWatch Logs
253
355
*
254
356
* @param key the name of the metric
255
357
* @param value the value of the metric
256
358
* @return the current logger
257
359
* @throws InvalidMetricException if the metric is invalid
258
360
*/
259
- public MetricsLogger putMetric (String key , double value ) throws InvalidMetricException {
260
- this .putMetric (key , value , Unit .NONE , StorageResolution .STANDARD );
261
- return this ;
361
+ public MetricsLogger setMetric (String key , Metric value ) throws InvalidMetricException {
362
+ try {
363
+ this .context .setMetric (key , value );
364
+ return this ;
365
+ } finally {
366
+ rwl .readLock ().unlock ();
367
+ }
262
368
}
263
369
264
370
/**
0 commit comments