Class MetricRegistries

java.lang.Object
com.palantir.tritium.metrics.MetricRegistries

public final class MetricRegistries extends Object
Utilities for working with MetricRegistry instances.
  • Method Details

    • createWithHdrHistogramReservoirs

      public static com.codahale.metrics.MetricRegistry createWithHdrHistogramReservoirs()
      Create metric registry which produces timers and histograms backed by high dynamic range histograms and timers, that accumulates internal state forever.
      Returns:
      metric registry
    • createWithSlidingTimeWindowReservoirs

      public static com.codahale.metrics.MetricRegistry createWithSlidingTimeWindowReservoirs(long window, TimeUnit windowUnit)
      Creates a MetricRegistry which produces timers and histograms backed by sliding time window array that store measurements for the specified sliding time window.

      See also:

      Parameters:
      window - window of time
      windowUnit - unit for window
      Returns:
      metric registry
    • createWithLockFreeExponentiallyDecayingReservoirs

      public static com.codahale.metrics.MetricRegistry createWithLockFreeExponentiallyDecayingReservoirs()
      Create a metric registry which produces timers and histograms backed by lock free expoentially decaying reservoirs.
      Returns:
      metric registry
      See Also:
      • LockFreeExponentiallyDecayingReservoir
    • metricsPrefixedBy

      public static com.codahale.metrics.MetricFilter metricsPrefixedBy(@Safe @Safe String prefix)
      Creates a MetricFilter predicate to match metrics with names starting with the specified prefix.
      Parameters:
      prefix - metric name prefix
      Returns:
      metric filter
    • metricsMatching

      public static SortedMap<String, com.codahale.metrics.Metric> metricsMatching(com.codahale.metrics.MetricRegistry metrics, com.codahale.metrics.MetricFilter filter)
      Returns a sorted map of metrics from the specified registry matching the specified filter.
      Parameters:
      metrics - metric registry
      filter - metric filter predicate
      Returns:
      sorted map of metrics
    • registerCache

      @Deprecated public static void registerCache(com.codahale.metrics.MetricRegistry registry, com.google.common.cache.Cache<?,?> cache, @Safe @Safe String name)
      Register specified cache with the given metric registry. Callers should ensure that they have enabled stats recording CacheBuilder.newBuilder().recordStats() otherwise there are no cache metrics to register.
      Parameters:
      registry - metric registry
      cache - cache to instrument
      name - cache name
      Throws:
      IllegalArgumentException - if name is blank
    • registerCache

      @Deprecated public static void registerCache(com.palantir.tritium.metrics.registry.TaggedMetricRegistry registry, com.google.common.cache.Cache<?,?> cache, @Safe @Safe String name)
      Deprecated.
      Do not use Guava caches, they are outperformed by and harder to use than Caffeine caches. Prefer
      invalid reference
      Caffeine#recordStats(Supplier)
      and
      invalid reference
      CacheStats#of(TaggedMetricRegistry, String)
      .
      Register specified cache with the given metric registry. Callers should ensure that they have enabled stats recording CacheBuilder.newBuilder().recordStats() otherwise there are no cache metrics to register.
      Parameters:
      registry - metric registry
      cache - cache to instrument
      name - cache name
      Throws:
      IllegalArgumentException - if name is blank
    • registerGarbageCollection

      public static void registerGarbageCollection(com.palantir.tritium.metrics.registry.TaggedMetricRegistry registry)
      Adds Garbage Collection metrics to the given metric registry.

      Registers gauges

      jvm.gc.count
      and
      jvm.gc.time
      tagged with
      {collector: NAME}
      .
      Parameters:
      registry - metric registry
    • registerMemoryPools

      public static void registerMemoryPools(com.palantir.tritium.metrics.registry.TaggedMetricRegistry registry)
      Adds memory pool metrics to the given metric registry.

      Registers the following metrics, tagged with

      {memoryPool: NAME}
      .
      • jvm.memory.pools.max
      • jvm.memory.pools.used
      • jvm.memory.pools.committed
      • jvm.memory.pools.init
      • jvm.memory.pools.usage
      • jvm.memory.pools.used-after-gc (Only for supported pools)
      Parameters:
      registry - metric registry
    • instrument

      public static ScheduledExecutorService instrument(com.palantir.tritium.metrics.registry.TaggedMetricRegistry registry, ScheduledExecutorService delegate, @Safe @Safe String name)
      Returns an instrumented ScheduledExecutorService that monitors the number of tasks submitted, running, completed and also keeps a Timer for the task duration. Similar to InstrumentedScheduledExecutorService, but produces tagged metrics to the specified TaggedMetricRegistry.
      Parameters:
      registry - tagged metric registry
      delegate - executor service to instrument
      name - executor service name
      Returns:
      instrumented executor service
    • instrument

      public static ExecutorService instrument(com.palantir.tritium.metrics.registry.TaggedMetricRegistry registry, ExecutorService delegate, @Safe @Safe String name)
      Returns an instrumented ExecutorService that monitors the number of tasks submitted, running, completed and also keeps a Timer for the task duration. Similar to InstrumentedExecutorService, but produces tagged metrics to the specified TaggedMetricRegistry.
      Parameters:
      registry - tagged metric registry
      delegate - executor service to instrument
      name - executor service name
      Returns:
      instrumented executor service
    • instrument

      public static ThreadFactory instrument(com.palantir.tritium.metrics.registry.TaggedMetricRegistry registry, ThreadFactory delegate, @Safe @Safe String name)
      Returns an instrumented ThreadFactory that monitors the number of created, running, and terminated threads.
      Parameters:
      registry - tagged metric registry
      delegate - Thread factory to instrument
      name - Thread factory name
      Returns:
      instrumented thread factory
    • instrument

      public static SSLContext instrument(com.palantir.tritium.metrics.registry.TaggedMetricRegistry registry, SSLContext context, @Safe @Safe String name)
      Returns an instrumented SSLContext that monitors handshakes and ciphers. A name may be reused across many contexts.
      Parameters:
      registry - tagged metric registry
      context - ssl context to instrument
      name - context name
      Returns:
      instrumented ssl context
    • instrument

      public static SSLSocketFactory instrument(com.palantir.tritium.metrics.registry.TaggedMetricRegistry registry, SSLSocketFactory factory, @Safe @Safe String name)
      Returns an instrumented SSLSocketFactory that monitors handshakes and ciphers. A name may be reused across many factories.
      Parameters:
      registry - tagged metric registry
      factory - socket factory to instrument
      name - socket factory name
      Returns:
      instrumented socket factory
    • unwrap

      public static SSLEngine unwrap(SSLEngine engine)
      Extracts the wrapped delegate if the input SSLEngine is instrumented, otherwise returns the input. Some libraries (Conscrypt, for example) use instanceof checks and casts to configure specific SSLEngine implementations. In such cases, it may be necessary to unwrap the instrumented instance.
      Parameters:
      engine - Engine to unwrap
      Returns:
      The delegate instrumented engine, or the input if it is not instrumented
    • registerSafe

      public static <T extends com.codahale.metrics.Metric> T registerSafe(com.codahale.metrics.MetricRegistry registry, @Safe @Safe String name, T metric)
      Ensures a Metric is registered to a MetricRegistry with the supplied name. If there is an existing Metric registered to name with the same implemented set of interfaces as metric then it's returned. Otherwise metric is registered and returned.

      This is intended to imitate the semantics of MetricRegistry.counter(String) and should only be used for Metric implementations that can't be registered/created in that manner (because it does not actually guarantee that the registered Metric matches the input metric).

      For example, this may be useful for registering Gauges which might cause issues from being added multiple times to a static MetricRegistry in a unit test

      Throws:
      IllegalArgumentException - if there is already a Metric registered that doesn't implement the same interfaces as metric
    • registerWithReplacement

      public static <T extends com.codahale.metrics.Metric> T registerWithReplacement(com.codahale.metrics.MetricRegistry registry, @Safe @Safe String name, T metric)
    • registerAll

      public static void registerAll(com.palantir.tritium.metrics.registry.TaggedMetricRegistry registry, @Safe @Safe String prefix, com.codahale.metrics.MetricSet metricSet)
      Registers a Dropwizard MetricSet with a Tritium TaggedMetricRegistry. Semantics match calling MetricRegistry.register(String, Metric) with a MetricSet.
      Parameters:
      registry - Target Tritium registry
      prefix - Metric name prefix
      metricSet - Set to register with the tagged registry
    • executor

      @CheckReturnValue public static MetricRegistries.ExecutorInstrumentationBuilderRegistryStage executor()
      Returns a builder for ExecutorService instrumentation.