Class ContextRegistry

java.lang.Object
io.micrometer.context.ContextRegistry

public class ContextRegistry extends Object
Registry that provides access to, instances of ContextAccessor and ThreadLocalAccessor.

A static instance is available via getInstance(). It is intended to be initialized on startup, and to be aware of all available accessors, as many as possible. The means to control what context gets propagated is in ContextSnapshot, which filters context values by key.

Since:
1.0.0
  • Constructor Details

    • ContextRegistry

      public ContextRegistry()
  • Method Details

    • registerContextAccessor

      public ContextRegistry registerContextAccessor(ContextAccessor<?,?> accessor)
      Register a ContextAccessor. If there is an existing registration of another ContextAccessor that can work with its declared types, an exception is thrown.
    • registerThreadLocalAccessor

      public <V> ContextRegistry registerThreadLocalAccessor(String key, ThreadLocal<V> threadLocal)
      Register a ThreadLocalAccessor for the given ThreadLocal.
      Type Parameters:
      V - the type of value stored in the ThreadLocal
      Parameters:
      key - the key to associate with the ThreadLocal value
      threadLocal - the underlying ThreadLocal
      Returns:
      the same registry instance
    • registerThreadLocalAccessor

      public <V> ContextRegistry registerThreadLocalAccessor(String key, Supplier<V> getSupplier, Consumer<V> setConsumer, Runnable resetTask)
      Register a ThreadLocalAccessor from callbacks.
      Type Parameters:
      V - the type of value stored in the ThreadLocal
      Parameters:
      key - the key to associate with the ThreadLocal value
      getSupplier - callback to use for getting the value
      setConsumer - callback to use for setting the value
      resetTask - callback to use for resetting the value
      Returns:
      the same registry instance
    • registerThreadLocalAccessor

      public ContextRegistry registerThreadLocalAccessor(ThreadLocalAccessor<?> accessor)
      Register a ThreadLocalAccessor. If there is an existing registration with the same key, it is removed first.
      Parameters:
      accessor - the accessor to register
      Returns:
      the same registry instance
    • removeThreadLocalAccessor

      public boolean removeThreadLocalAccessor(String key)
      Parameters:
      key - under which the accessor got registered
      Returns:
      true when accessor got successfully removed
    • removeContextAccessor

      public boolean removeContextAccessor(ContextAccessor<?,?> accessorToRemove)
      Removes a registered ContextAccessor.
      Parameters:
      accessorToRemove - accessor instance to remove
      Returns:
      true when accessor got successfully removed
    • loadContextAccessors

      public ContextRegistry loadContextAccessors()
      Load ContextAccessor implementations through the ServiceLoader mechanism.

      Note that existing registrations of the same ContextAccessor type, if any, are removed first.

    • loadThreadLocalAccessors

      public ContextRegistry loadThreadLocalAccessors()
      Load ThreadLocalAccessor implementations through the ServiceLoader mechanism.

      Note that existing registrations with the same key, if any, are removed first.

    • getContextAccessorForRead

      public ContextAccessor<?,?> getContextAccessorForRead(Object context)
      Find a ContextAccessor that can read the given context.
      Parameters:
      context - the context to read from
      Throws:
      IllegalStateException - if no match is found
    • getContextAccessorForWrite

      public ContextAccessor<?,?> getContextAccessorForWrite(Object context)
      Return a ContextAccessor that can write the given context.
      Parameters:
      context - the context to write to
      Throws:
      IllegalStateException - if no match is found
    • getContextAccessors

      public List<ContextAccessor<?,?>> getContextAccessors()
      Return a read-only list of registered ContextAccessor's.
    • getThreadLocalAccessors

      public List<ThreadLocalAccessor<?>> getThreadLocalAccessors()
      Return a read-only list of registered ThreadLocalAccessor's.
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • getInstance

      public static ContextRegistry getInstance()
      Return a global ContextRegistry instance.

      Note: The global instance should be initialized on startup to ensure it has the ability to propagate to and from different types of context throughout the application. The registry itself is not intended to as a mechanism to control what gets propagated. It is in ContextSnapshot where more fine-grained decisions can be made about which context values to propagate.