Class Observation.Context

java.lang.Object
io.micrometer.observation.Observation.Context
All Implemented Interfaces:
Observation.ContextView
Direct Known Subclasses:
ObservedAspect.ObservedAspectContext, ReceiverContext, SenderContext
Enclosing interface:
Observation

public static class Observation.Context extends Object implements Observation.ContextView
A mutable holder of data required by an ObservationHandler. When extended you can provide your own, custom information to be processed by the handlers.
Since:
1.10.0
  • Constructor Details

    • Context

      public Context()
  • Method Details

    • getName

      public String getName()
      The observation name.
      Specified by:
      getName in interface Observation.ContextView
      Returns:
      name
    • setName

      public void setName(String name)
      Sets the observation name.
      Parameters:
      name - observation name
    • getContextualName

      public String getContextualName()
      Returns the contextual name. The name that makes sense within the current context (e.g. name derived from HTTP request).
      Specified by:
      getContextualName in interface Observation.ContextView
      Returns:
      contextual name
    • setContextualName

      public void setContextualName(@Nullable String contextualName)
      Sets the contextual name.
      Parameters:
      contextualName - name
    • getParentObservation

      @Nullable public ObservationView getParentObservation()
      Returns the parent ObservationView.
      Specified by:
      getParentObservation in interface Observation.ContextView
      Returns:
      parent observation or null if there was no parent
    • setParentObservation

      public void setParentObservation(@Nullable ObservationView parentObservation)
      Sets the parent ObservationView.
      Parameters:
      parentObservation - parent observation to set
    • getError

      @Nullable public Throwable getError()
      Error that occurred while processing the Observation.
      Specified by:
      getError in interface Observation.ContextView
      Returns:
      error (null if there wasn't any)
    • setError

      public void setError(Throwable error)
      Sets an error that occurred while processing the Observation.
      Parameters:
      error - error
    • put

      public <T> Observation.Context put(Object key, T object)
      Puts an element to the context.
      Type Parameters:
      T - value type
      Parameters:
      key - key
      object - value
      Returns:
      this for chaining
    • get

      @Nullable public <T> T get(Object key)
      Gets an entry from the context. Returns null when entry is not present.
      Specified by:
      get in interface Observation.ContextView
      Type Parameters:
      T - value type
      Parameters:
      key - key
      Returns:
      entry or null if not present
    • remove

      public Object remove(Object key)
      Removes an entry from the context.
      Parameters:
      key - key by which to remove an entry
      Returns:
      the previous value associated with the key, or null if there was no mapping for the key
    • getRequired

      @NonNull public <T> T getRequired(Object key)
      Gets an entry from the context. Throws exception when entry is not present.
      Specified by:
      getRequired in interface Observation.ContextView
      Type Parameters:
      T - value type
      Parameters:
      key - key
      Returns:
      entry
      Throws:
      IllegalArgumentException - if not present
    • containsKey

      public boolean containsKey(Object key)
      Checks if context contains a key.
      Specified by:
      containsKey in interface Observation.ContextView
      Parameters:
      key - key
      Returns:
      true when the context contains the entry with the given key
    • getOrDefault

      public <T> T getOrDefault(Object key, T defaultObject)
      Returns an element or default if not present.
      Specified by:
      getOrDefault in interface Observation.ContextView
      Type Parameters:
      T - value type
      Parameters:
      key - key
      defaultObject - default object to return
      Returns:
      object or default if not present
    • computeIfAbsent

      public <T> T computeIfAbsent(Object key, Function<Object,? extends T> mappingFunction)
      Returns an element or calls a mapping function if entry not present. The function will insert the value to the map.
      Type Parameters:
      T - value type
      Parameters:
      key - key
      mappingFunction - mapping function
      Returns:
      object or one derived from the mapping function if not present
    • clear

      public void clear()
      Clears the entries from the context.
    • addLowCardinalityKeyValue

      public Observation.Context addLowCardinalityKeyValue(io.micrometer.common.KeyValue keyValue)
      Adds a low cardinality key value - those will be appended to those fetched from the ObservationConvention.getLowCardinalityKeyValues(Context) method.
      Parameters:
      keyValue - a key value
      Returns:
      this context
    • addHighCardinalityKeyValue

      public Observation.Context addHighCardinalityKeyValue(io.micrometer.common.KeyValue keyValue)
      Adds a high cardinality key value - those will be appended to those fetched from the ObservationConvention.getHighCardinalityKeyValues(Context) method.
      Parameters:
      keyValue - a key value
      Returns:
      this context
    • removeLowCardinalityKeyValue

      public Observation.Context removeLowCardinalityKeyValue(String keyName)
      Removes a low cardinality key value by looking at its key - those will be removed to those fetched from the ObservationConvention.getLowCardinalityKeyValues(Context) method.
      Parameters:
      keyName - name of the key
      Returns:
      this context
      Since:
      1.10.1
    • removeHighCardinalityKeyValue

      public Observation.Context removeHighCardinalityKeyValue(String keyName)
      Removes a high cardinality key value by looking at its key - those will be removed to those fetched from the ObservationConvention.getHighCardinalityKeyValues(Context) method.
      Parameters:
      keyName - name of the key
      Returns:
      this context
      Since:
      1.10.1
    • addLowCardinalityKeyValues

      public Observation.Context addLowCardinalityKeyValues(io.micrometer.common.KeyValues keyValues)
      Adds multiple low cardinality key values at once.
      Parameters:
      keyValues - collection of key values
      Returns:
      this context
    • addHighCardinalityKeyValues

      public Observation.Context addHighCardinalityKeyValues(io.micrometer.common.KeyValues keyValues)
      Adds multiple high cardinality key values at once.
      Parameters:
      keyValues - collection of key values
      Returns:
      this context
    • removeLowCardinalityKeyValues

      public Observation.Context removeLowCardinalityKeyValues(String... keyNames)
      Removes multiple low cardinality key values at once.
      Parameters:
      keyNames - collection of key names
      Returns:
      this context
      Since:
      1.10.1
    • removeHighCardinalityKeyValues

      public Observation.Context removeHighCardinalityKeyValues(String... keyNames)
      Removes multiple high cardinality key values at once.
      Parameters:
      keyNames - collection of key names
      Returns:
      this context
      Since:
      1.10.1
    • getLowCardinalityKeyValues

      @NonNull public io.micrometer.common.KeyValues getLowCardinalityKeyValues()
      Description copied from interface: Observation.ContextView
      Returns low cardinality key values.
      Specified by:
      getLowCardinalityKeyValues in interface Observation.ContextView
      Returns:
      low cardinality key values
    • getHighCardinalityKeyValues

      @NonNull public io.micrometer.common.KeyValues getHighCardinalityKeyValues()
      Description copied from interface: Observation.ContextView
      Returns high cardinality key values.
      Specified by:
      getHighCardinalityKeyValues in interface Observation.ContextView
      Returns:
      high cardinality key values
    • getLowCardinalityKeyValue

      public io.micrometer.common.KeyValue getLowCardinalityKeyValue(String key)
      Description copied from interface: Observation.ContextView
      Returns a low cardinality key value or null if not present.
      Specified by:
      getLowCardinalityKeyValue in interface Observation.ContextView
      Parameters:
      key - key
      Returns:
      a low cardinality key value or null
    • getHighCardinalityKeyValue

      public io.micrometer.common.KeyValue getHighCardinalityKeyValue(String key)
      Description copied from interface: Observation.ContextView
      Returns a high cardinality key value or null if not present.
      Specified by:
      getHighCardinalityKeyValue in interface Observation.ContextView
      Parameters:
      key - key
      Returns:
      a high cardinality key value or null
    • getAllKeyValues

      @NonNull public io.micrometer.common.KeyValues getAllKeyValues()
      Description copied from interface: Observation.ContextView
      Returns all key values.
      Specified by:
      getAllKeyValues in interface Observation.ContextView
      Returns:
      all key values
    • toString

      public String toString()
      Overrides:
      toString in class Object