Class MetricsContext
- java.lang.Object
-
- software.amazon.cloudwatchlogs.emf.model.MetricsContext
-
public class MetricsContext extends java.lang.ObjectStores metrics and their associated properties and dimensions.
-
-
Constructor Summary
Constructors Constructor Description MetricsContext()MetricsContext(java.lang.String namespace, java.util.Map<java.lang.String,java.lang.Object> properties, java.util.List<DimensionSet> dimensionSets, DimensionSet defaultDimensionSet)MetricsContext(software.amazon.cloudwatchlogs.emf.model.MetricDirective metricDirective)MetricsContext(software.amazon.cloudwatchlogs.emf.model.RootNode rootNode)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description MetricsContextcreateCopyWithContext(boolean preserveDimensions)Create a copy of the contextDimensionSetgetDefaultDimensions()java.util.List<DimensionSet>getDimensions()Get list of all dimensions including default dimensionsjava.lang.StringgetNamespace()java.lang.ObjectgetProperty(java.lang.String name)java.time.InstantgetTimestamp()booleanhasDefaultDimensions()voidputDimension(java.lang.String dimension, java.lang.String value)Add a dimension set with single dimension-value entry to the metric context.voidputDimension(DimensionSet dimensionSet)Add dimensions to the metric context.voidputMetadata(java.lang.String key, java.lang.Object value)Add a key-value pair to the metadatavoidputMetric(java.lang.String key, double value)Add a metric measurement to the context without a unit Multiple calls using the same key will be stored as an array of scalar values.voidputMetric(java.lang.String key, double value, Unit unit)Add a metric measurement to the context.voidputProperty(java.lang.String name, java.lang.Object value)Add a property to this log entry.voidresetDimensions(boolean useDefault)Reset the dimensions.java.util.List<java.lang.String>serialize()Serialize the metrics in this context to strings.voidsetDefaultDimensions(DimensionSet dimensionSet)Sets default dimensions for all other dimensions that get added to the context.voidsetDimensions(boolean useDefault, DimensionSet... dimensionSets)Update the dimensions.voidsetDimensions(DimensionSet... dimensionSets)Update the dimensions.voidsetNamespace(java.lang.String namespace)Update the namespace with the parameter.voidsetTimestamp(java.time.Instant timestamp)Update timestamp field in the metadata
-
-
-
Constructor Detail
-
MetricsContext
public MetricsContext()
-
MetricsContext
public MetricsContext(software.amazon.cloudwatchlogs.emf.model.RootNode rootNode)
-
MetricsContext
public MetricsContext(software.amazon.cloudwatchlogs.emf.model.MetricDirective metricDirective)
-
MetricsContext
public MetricsContext(java.lang.String namespace, java.util.Map<java.lang.String,java.lang.Object> properties, java.util.List<DimensionSet> dimensionSets, DimensionSet defaultDimensionSet) throws InvalidNamespaceException- Throws:
InvalidNamespaceException
-
-
Method Detail
-
getNamespace
public java.lang.String getNamespace()
- Returns:
- the namespace. If the namespace is not set, it would return a default value.
-
setNamespace
public void setNamespace(java.lang.String namespace) throws InvalidNamespaceExceptionUpdate the namespace with the parameter.- Parameters:
namespace- The new namespace- Throws:
InvalidNamespaceException- if the namespace is invalid
-
getDefaultDimensions
public DimensionSet getDefaultDimensions()
- Returns:
- the default dimension set.
-
setDefaultDimensions
public void setDefaultDimensions(DimensionSet dimensionSet)
Sets default dimensions for all other dimensions that get added to the context. If no custom dimensions are specified, the metrics will be emitted with the defaults. If custom dimensions are specified, they will be prepended with the default dimensions- Parameters:
dimensionSet- the DimensionSet to be the default
-
hasDefaultDimensions
public boolean hasDefaultDimensions()
-
putMetric
public void putMetric(java.lang.String key, double value, Unit unit) throws InvalidMetricExceptionAdd a metric measurement to the context. Multiple calls using the same key will be stored as an array of scalar values.metricContext.putMetric("Latency", 100, Unit.MILLISECONDS)- Parameters:
key- Name of the metricvalue- Value of the metricunit- The unit of the metric- Throws:
InvalidMetricException- if the metric is invalid
-
putMetric
public void putMetric(java.lang.String key, double value) throws InvalidMetricExceptionAdd a metric measurement to the context without a unit Multiple calls using the same key will be stored as an array of scalar values.metricContext.putMetric("Count", 10)- Parameters:
key- Name of the metricvalue- Value of the metric- Throws:
InvalidMetricException- if the metric is invalid
-
putProperty
public void putProperty(java.lang.String name, java.lang.Object value)Add a property to this log entry. Properties are additional values that can be associated with metrics. They will not show up in CloudWatch metrics, but they are searchable in CloudWatch Insights.metricContext.putProperty("Location", 'US')- Parameters:
name- Name of the propertyvalue- Value of the property
-
getProperty
public java.lang.Object getProperty(java.lang.String name)
-
putDimension
public void putDimension(DimensionSet dimensionSet)
Add dimensions to the metric context.metricContext.putDimension(DimensionSet.of("Dim", "Value" ))- Parameters:
dimensionSet- the dimensions set to add
-
putDimension
public void putDimension(java.lang.String dimension, java.lang.String value) throws InvalidDimensionException, DimensionSetExceededExceptionAdd a dimension set with single dimension-value entry to the metric context.metricContext.putDimension("Dim", "Value" )- Parameters:
dimension- the name of the dimensionvalue- the value associated with the dimension- Throws:
InvalidDimensionException- if the dimension is invalidDimensionSetExceededException- if the number of dimensions exceeds the limit
-
getDimensions
public java.util.List<DimensionSet> getDimensions() throws DimensionSetExceededException
Get list of all dimensions including default dimensions- Returns:
- the list of dimensions that has been added, including default dimensions.
- Throws:
DimensionSetExceededException- if the number of dimensions exceeds the limit
-
setDimensions
public void setDimensions(DimensionSet... dimensionSets)
Update the dimensions. This would override default dimensions- Parameters:
dimensionSets- the dimensionSets to be set
-
setDimensions
public void setDimensions(boolean useDefault, DimensionSet... dimensionSets)Update the dimensions. Default dimensions are preserved optionally.- Parameters:
useDefault- indicates whether default dimensions should be useddimensionSets- the dimensionSets to set
-
resetDimensions
public void resetDimensions(boolean useDefault)
Reset the dimensions. This would clear all custom dimensions.- Parameters:
useDefault- indicates whether default dimensions should be used
-
putMetadata
public void putMetadata(java.lang.String key, java.lang.Object value)Add a key-value pair to the metadata- Parameters:
key- the name of the keyvalue- the value associated with the key
-
getTimestamp
public java.time.Instant getTimestamp()
- Returns:
- timestamp field from the metadata.
-
setTimestamp
public void setTimestamp(java.time.Instant timestamp) throws InvalidTimestampExceptionUpdate timestamp field in the metadata- Parameters:
timestamp- value of timestamp to be set- Throws:
InvalidTimestampException- if the timestamp is invalid
-
createCopyWithContext
public MetricsContext createCopyWithContext(boolean preserveDimensions)
Create a copy of the context- Parameters:
preserveDimensions- indicates whether default dimensions should be preserved- Returns:
- Creates an independently flushable context
-
serialize
public java.util.List<java.lang.String> serialize() throws com.fasterxml.jackson.core.JsonProcessingExceptionSerialize the metrics in this context to strings. The EMF backend requires no more than 100 metrics in one log event. If there are more than 100 metrics, we split the metrics into multiple log events.If a metric has more than 100 data points, we also split the metric.
- Returns:
- the serialized strings.
- Throws:
com.fasterxml.jackson.core.JsonProcessingException- if there's any object that cannot be serialized
-
-