Class MetricsLogger
- java.lang.Object
-
- software.amazon.cloudwatchlogs.emf.logger.MetricsLogger
-
public class MetricsLogger extends java.lang.ObjectA metrics logger. Use this interface to publish logs to CloudWatch Logs and extract metrics to CloudWatch Metrics asynchronously.
-
-
Constructor Summary
Constructors Constructor Description MetricsLogger()MetricsLogger(Environment environment)MetricsLogger(EnvironmentProvider environmentProvider)MetricsLogger(EnvironmentProvider environmentProvider, MetricsContext metricsContext)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidflush()Flushes the current context state to the configured sink.MetricsLoggerputDimensions(DimensionSet dimensions)Adds a dimension.MetricsLoggerputMetadata(java.lang.String key, java.lang.Object value)Add a custom key-value pair to the Metadata object.MetricsLoggerputMetric(java.lang.String key, double value)Put a metric value.MetricsLoggerputMetric(java.lang.String key, double value, Unit unit)Put a metric value.MetricsLoggerputProperty(java.lang.String key, java.lang.Object value)Set a property on the published metrics.MetricsLoggerresetDimensions(boolean useDefault)Clear all custom dimensions on this MetricsLogger instance.MetricsLoggersetDimensions(boolean useDefault, DimensionSet... dimensionSets)Overwrite custom dimensions on this MetricsLogger instance, with an option to preserve default dimensions.MetricsLoggersetDimensions(DimensionSet... dimensionSets)Overwrite all dimensions on this MetricsLogger instance.MetricsLoggersetNamespace(java.lang.String namespace)Set the CloudWatch namespace that metrics should be published to.MetricsLoggersetTimestamp(java.time.Instant timestamp)Set the timestamp to be used for metrics.
-
-
-
Constructor Detail
-
MetricsLogger
public MetricsLogger()
-
MetricsLogger
public MetricsLogger(Environment environment)
-
MetricsLogger
public MetricsLogger(EnvironmentProvider environmentProvider)
-
MetricsLogger
public MetricsLogger(EnvironmentProvider environmentProvider, MetricsContext metricsContext)
-
-
Method Detail
-
flush
public void flush()
Flushes the current context state to the configured sink. TODO: Support flush asynchronously
-
putProperty
public MetricsLogger putProperty(java.lang.String key, java.lang.Object value)
Set a property on the published metrics. This is stored in the emitted log data and you are not charged for this data by CloudWatch Metrics. These values can be values that are useful for searching on, but have too high cardinality to emit as dimensions to CloudWatch Metrics.- Parameters:
key- Property namevalue- Property value- Returns:
- the current logger
-
putDimensions
public MetricsLogger putDimensions(DimensionSet dimensions)
Adds a dimension. This is generally a low cardinality key-value pair that is part of the metric identity. CloudWatch treats each unique combination of dimensions as a separate metric, even if the metrics have the same metric name.- Parameters:
dimensions- the DimensionSet to add- Returns:
- the current logger
- See Also:
- CloudWatch Dimensions
-
setDimensions
public MetricsLogger setDimensions(DimensionSet... dimensionSets)
Overwrite all dimensions on this MetricsLogger instance.- Parameters:
dimensionSets- the dimensionSets to set- Returns:
- the current logger
- See Also:
- CloudWatch Dimensions
-
setDimensions
public MetricsLogger setDimensions(boolean useDefault, DimensionSet... dimensionSets)
Overwrite custom dimensions on this MetricsLogger instance, with an option to preserve default dimensions.- Parameters:
useDefault- indicates whether default dimensions should be useddimensionSets- the dimensionSets to set- Returns:
- the current logger
-
resetDimensions
public MetricsLogger resetDimensions(boolean useDefault)
Clear all custom dimensions on this MetricsLogger instance. Whether default dimensions should be used can be configured by the input parameter.- Parameters:
useDefault- indicates whether default dimensions should be used- Returns:
- the current logger
-
putMetric
public MetricsLogger putMetric(java.lang.String key, double value, Unit unit) throws InvalidMetricException
Put a metric value. This value will be emitted to CloudWatch Metrics asynchronously and does not contribute to your account TPS limits. The value will also be available in your CloudWatch Logs- Parameters:
key- is the name of the metricvalue- is the value of the metricunit- is the unit of the metric value- Returns:
- the current logger
- Throws:
InvalidMetricException- if the metric is invalid
-
putMetric
public MetricsLogger putMetric(java.lang.String key, double value) throws InvalidMetricException
Put a metric value. This value will be emitted to CloudWatch Metrics asynchronously and does not contribute to your account TPS limits. The value will also be available in your CloudWatch Logs- Parameters:
key- the name of the metricvalue- the value of the metric- Returns:
- the current logger
- Throws:
InvalidMetricException- if the metric is invalid
-
putMetadata
public MetricsLogger putMetadata(java.lang.String key, java.lang.Object value)
Add a custom key-value pair to the Metadata object.- Parameters:
key- the name of the keyvalue- the value associated with the key- Returns:
- the current logger
- See Also:
- CloudWatch Metadata
-
setNamespace
public MetricsLogger setNamespace(java.lang.String namespace) throws InvalidNamespaceException
Set the CloudWatch namespace that metrics should be published to.- Parameters:
namespace- the namespace of the logs- Returns:
- the current logger
- Throws:
InvalidNamespaceException- if the namespace is invalid
-
setTimestamp
public MetricsLogger setTimestamp(java.time.Instant timestamp) throws InvalidTimestampException
Set the timestamp to be used for metrics.- Parameters:
timestamp- value of timestamp to be set- Returns:
- the current logger
- Throws:
InvalidTimestampException- if the timestamp is invalid
-
-