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,74 +194,156 @@ 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
221
+ * Unit defaults to NONE
216
222
*
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
223
+ * <p>AggregationType defaults to the default AggregationType of this logger
224
+ *
225
+ * @see MetricsLogger#putMetric(String key, double value, Unit unit, StorageResolution
226
+ * storageResolution, AggregationType aggregationType) putMetric(String key, double value,
227
+ * Unit unit, StorageResolution storageResolution, AggregationType aggregationType)
225
228
*/
226
229
public MetricsLogger putMetric (String key , double value , StorageResolution storageResolution )
227
230
throws InvalidMetricException {
228
- this .putMetric (key , value , Unit .NONE , storageResolution );
231
+ this .putMetric (key , value , Unit .NONE , storageResolution , this . defaultAggregationType );
229
232
return this ;
230
233
}
231
234
232
235
/**
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
+ * Unit defaults to NONE
236
237
*
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
238
+ * <p>StorageResolution defaults to STANDARD
239
+ *
240
+ * <p>AggregationType defaults to the default AggregationType of this logger
241
+ *
242
+ * @see MetricsLogger#putMetric(String key, double value, Unit unit, StorageResolution
243
+ * storageResolution, AggregationType aggregationType) putMetric(String key, double value,
244
+ * Unit unit, StorageResolution storageResolution, AggregationType aggregationType)
242
245
*/
243
246
public MetricsLogger putMetric (String key , double value , Unit unit )
244
247
throws InvalidMetricException {
245
- this .putMetric (key , value , unit , StorageResolution .STANDARD );
248
+ this .putMetric (key , value , unit , StorageResolution .STANDARD , this . defaultAggregationType );
246
249
return this ;
247
250
}
248
251
249
252
/**
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
253
+ * Unit defaults to NONE
254
+ *
255
+ * <p>StorageResolution defaults to STANDARD
256
+ *
257
+ * <p>AggregationType defaults to the default AggregationType of this logger
258
+ *
259
+ * @see MetricsLogger#putMetric(String key, double value, Unit unit, StorageResolution
260
+ * storageResolution, AggregationType aggregationType) putMetric(String key, double value,
261
+ * Unit unit, StorageResolution storageResolution, AggregationType aggregationType)
262
+ */
263
+ public MetricsLogger putMetric (String key , double value ) throws InvalidMetricException {
264
+ this .putMetric (
265
+ key , value , Unit .NONE , StorageResolution .STANDARD , this .defaultAggregationType );
266
+ return this ;
267
+ }
268
+
269
+ /**
270
+ * AggregationType defaults to the default AggregationType of this logger
271
+ *
272
+ * @see MetricsLogger#putMetric(String key, double value, Unit unit, StorageResolution
273
+ * storageResolution, AggregationType aggregationType) putMetric(String key, double value,
274
+ * Unit unit, StorageResolution storageResolution, AggregationType aggregationType)
275
+ */
276
+ public MetricsLogger putMetric (
277
+ String key , double value , Unit unit , StorageResolution storageResolution )
278
+ throws InvalidMetricException {
279
+ this .putMetric (key , value , Unit .NONE , storageResolution , this .defaultAggregationType );
280
+ return this ;
281
+ }
282
+
283
+ /**
284
+ * StorageResolution defaults to STANDARD
285
+ *
286
+ * <p>AggregationType defaults to the default AggregationType of this logger
287
+ *
288
+ * @see MetricsLogger#putMetric(String key, double value, Unit unit, StorageResolution
289
+ * storageResolution, AggregationType aggregationType) putMetric(String key, double value,
290
+ * Unit unit, StorageResolution storageResolution, AggregationType aggregationType)
291
+ */
292
+ public MetricsLogger putMetric (
293
+ String key ,
294
+ double value ,
295
+ StorageResolution storageResolution ,
296
+ AggregationType aggregationType )
297
+ throws InvalidMetricException {
298
+ this .putMetric (key , value , Unit .NONE , storageResolution , aggregationType );
299
+ return this ;
300
+ }
301
+
302
+ /**
303
+ * StorageResolution defaults to STANDARD
304
+ *
305
+ * @see MetricsLogger#putMetric(String key, double value, Unit unit, StorageResolution
306
+ * storageResolution, AggregationType aggregationType) putMetric(String key, double value,
307
+ * Unit unit, StorageResolution storageResolution, AggregationType aggregationType)
308
+ */
309
+ public MetricsLogger putMetric (
310
+ String key , double value , Unit unit , AggregationType aggregationType )
311
+ throws InvalidMetricException {
312
+ this .putMetric (key , value , unit , StorageResolution .STANDARD , aggregationType );
313
+ return this ;
314
+ }
315
+
316
+ /**
317
+ * Unit defaults to NONE
318
+ *
319
+ * <p>StorageResolution defaults to STANDARD
320
+ *
321
+ * @see MetricsLogger#putMetric(String key, double value, Unit unit, StorageResolution
322
+ * storageResolution, AggregationType aggregationType) putMetric(String key, double value,
323
+ * Unit unit, StorageResolution storageResolution, AggregationType aggregationType)
324
+ */
325
+ public MetricsLogger putMetric (String key , double value , AggregationType aggregationType )
326
+ throws InvalidMetricException {
327
+ this .putMetric (key , value , Unit .NONE , StorageResolution .STANDARD , aggregationType );
328
+ return this ;
329
+ }
330
+
331
+ /**
332
+ * Set a metric value, if a metric already has the same key it will be overwitten. This value
333
+ * will be emitted to CloudWatch Metrics asynchronously and does not contribute to your account
334
+ * TPS limits. The value will also be available in your CloudWatch Logs
253
335
*
254
336
* @param key the name of the metric
255
337
* @param value the value of the metric
256
338
* @return the current logger
257
339
* @throws InvalidMetricException if the metric is invalid
258
340
*/
259
- public MetricsLogger putMetric (String key , double value ) throws InvalidMetricException {
260
- this .putMetric (key , value , Unit .NONE , StorageResolution .STANDARD );
261
- return this ;
341
+ public MetricsLogger setMetric (String key , Metric value ) {
342
+ return applyReadLock (
343
+ () -> {
344
+ this .context .setMetric (key , value );
345
+ return this ;
346
+ });
262
347
}
263
348
264
349
/**
0 commit comments