001package io.prometheus.client.exemplars;
002
003/**
004 * Static configuration for Exemplar behavior.
005 */
006public class ExemplarConfig {
007
008  private static volatile boolean enabled = true;
009  private static volatile HistogramExemplarSampler histogramExemplarSampler;
010  private static volatile CounterExemplarSampler counterExemplarSampler;
011
012  static {
013    ExemplarSampler defaultExemplarSampler = new Tracer().initExemplarSampler();
014    counterExemplarSampler = defaultExemplarSampler;
015    histogramExemplarSampler = defaultExemplarSampler;
016  }
017
018  /**
019   * Set the default exemplar sampler for Counters.
020   */
021  public static void setCounterExemplarSampler(CounterExemplarSampler counterExemplarSampler) {
022    ExemplarConfig.counterExemplarSampler = counterExemplarSampler;
023  }
024
025  /**
026   * Set the default exemplar sampler for Histograms.
027   */
028  public static void setHistogramExemplarSampler(HistogramExemplarSampler histogramExemplarSampler) {
029    ExemplarConfig.histogramExemplarSampler = histogramExemplarSampler;
030  }
031
032  /**
033   * Prevent metrics from loading exemplars from an {@link ExemplarSampler} by default.
034   * <p>
035   * You can still enable individual metrics to load exemplars from an {@link ExemplarSampler} by calling the
036   * metric builder's {@code withExemplars()} method, and you can still provide single exemplars explicitly
037   * for individual observations with the {@code ...withExemplar()} methods.
038   */
039  public static void disableExemplars() {
040    enabled = false;
041  }
042
043  /**
044   * Allow metrics to load exemplars from an {@link ExemplarSampler} by default.
045   * <p>
046   * You can still disallow individual metrics to load exemplars from an {@link ExemplarSampler} by calling
047   * the metric builder's {@code withoutExemplars()} method.
048   * <p>
049   * Exemplars are enabled by default. This method is here so that you can temporarily {@link #disableExemplars()}
050   * and then {@link #enableExemplars()} again.
051   */
052  public static void enableExemplars() {
053    enabled = true;
054  }
055
056  /**
057   * @return the {@link CounterExemplarSampler} that is used by default in {@code Counter} metrics.
058   */
059  public static CounterExemplarSampler getCounterExemplarSampler() {
060    return counterExemplarSampler;
061  }
062
063  /**
064   * @return the {@link HistogramExemplarSampler} that is used by default in {@code Histogram} metrics.
065   */
066  public static HistogramExemplarSampler getHistogramExemplarSampler() {
067    return histogramExemplarSampler;
068  }
069
070  /**
071   * @return true by default, false if {@link #disableExemplars()} was called.
072   */
073  public static boolean isExemplarsEnabled() {
074    return enabled;
075  }
076}