Class MetricsContext


  • public class MetricsContext
    extends java.lang.Object
    Stores 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
      MetricsContext createCopyWithContext​(boolean preserveDimensions)
      Create a copy of the context
      DimensionSet getDefaultDimensions()  
      java.util.List<DimensionSet> getDimensions()
      Get list of all dimensions including default dimensions
      java.lang.String getNamespace()  
      java.lang.Object getProperty​(java.lang.String name)  
      java.time.Instant getTimestamp()  
      boolean hasDefaultDimensions()  
      void putDimension​(java.lang.String dimension, java.lang.String value)
      Add a dimension set with single dimension-value entry to the metric context.
      void putDimension​(DimensionSet dimensionSet)
      Add dimensions to the metric context.
      void putMetadata​(java.lang.String key, java.lang.Object value)
      Add a key-value pair to the metadata
      void putMetric​(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.
      void putMetric​(java.lang.String key, double value, Unit unit)
      Add a metric measurement to the context.
      void putProperty​(java.lang.String name, java.lang.Object value)
      Add a property to this log entry.
      void resetDimensions​(boolean useDefault)
      Reset the dimensions.
      java.util.List<java.lang.String> serialize()
      Serialize the metrics in this context to strings.
      void setDefaultDimensions​(DimensionSet dimensionSet)
      Sets default dimensions for all other dimensions that get added to the context.
      void setDimensions​(boolean useDefault, DimensionSet... dimensionSets)
      Update the dimensions.
      void setDimensions​(DimensionSet... dimensionSets)
      Update the dimensions.
      void setNamespace​(java.lang.String namespace)
      Update the namespace with the parameter.
      void setTimestamp​(java.time.Instant timestamp)
      Update timestamp field in the metadata
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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)
    • 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 InvalidNamespaceException
        Update 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 InvalidMetricException
        Add 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 metric
        value - Value of the metric
        unit - The unit of the metric
        Throws:
        InvalidMetricException - if the metric is invalid
      • putMetric

        public void putMetric​(java.lang.String key,
                              double value)
                       throws InvalidMetricException
        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.
        
         metricContext.putMetric("Count", 10)
         
        Parameters:
        key - Name of the metric
        value - 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 property
        value - 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
      • 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 used
        dimensionSets - 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 key
        value - 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 InvalidTimestampException
        Update 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.JsonProcessingException
        Serialize 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