com.codahale.metrics.SlidingTimeWindowReservoir Java Examples

The following examples show how to use com.codahale.metrics.SlidingTimeWindowReservoir. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MetricsConfigTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void testCustomReservoir() throws Exception {
  System.setProperty("timer.reservoir", UniformReservoir.class.getName());
  System.setProperty("histogram.size", "2048");
  System.setProperty("histogram.window", "600");
  System.setProperty("histogram.reservoir", SlidingTimeWindowReservoir.class.getName());
  NodeConfig cfg = loadNodeConfig();
  SolrMetricManager mgr = new SolrMetricManager(cfg.getSolrResourceLoader(), cfg.getMetricsConfig());
  assertTrue(mgr.getCounterSupplier() instanceof MetricSuppliers.DefaultCounterSupplier);
  assertTrue(mgr.getMeterSupplier() instanceof MetricSuppliers.DefaultMeterSupplier);
  assertTrue(mgr.getTimerSupplier() instanceof MetricSuppliers.DefaultTimerSupplier);
  assertTrue(mgr.getHistogramSupplier() instanceof MetricSuppliers.DefaultHistogramSupplier);
  Reservoir rsv = ((MetricSuppliers.DefaultTimerSupplier)mgr.getTimerSupplier()).getReservoir();
  assertTrue(rsv instanceof UniformReservoir);
  rsv = ((MetricSuppliers.DefaultHistogramSupplier)mgr.getHistogramSupplier()).getReservoir();
  assertTrue(rsv instanceof SlidingTimeWindowReservoir);
}
 
Example #2
Source File: StatsTracker.java    From raml-module-builder with Apache License 2.0 6 votes vote down vote up
/** add a metric (time) to a specific resource - for example
 * type = PostgresClient.get
 * time = single operation time in nanoseconds
 */
public static void addStatElement(String type, long time){
  // unsynchronized but fast check
  if (!registeredStatRequesters.contains(type)) {
    // prevent race condition - registering the same type twice will throw an exception
    synchronized(registeredStatRequesters) {
      // synchronized check
      if (!registeredStatRequesters.contains(type)) {
        METRICS.register(type, new Histogram(new SlidingTimeWindowReservoir(60,
          TimeUnit.SECONDS)));
        registeredStatRequesters.add(type);
      }
    }
  }
  METRICS.histogram(type).update(time/1000000);
}
 
Example #3
Source File: CompareHistogramsWithOtherLibraries.java    From micrometer with Apache License 2.0 5 votes vote down vote up
@Setup(Level.Iteration)
public void setup() {
    registry = new MetricRegistry();
    histogram = registry.histogram("histogram");
    histogramSlidingTimeWindow =
            registry.register("slidingTimeWindowHistogram",
                    new Histogram(new SlidingTimeWindowReservoir(10, TimeUnit.SECONDS)));
    histogramUniform =
            registry.register("uniformHistogram",
                    new Histogram(new UniformReservoir()));
}
 
Example #4
Source File: MetricsConfigurator.java    From datacollector with Apache License 2.0 5 votes vote down vote up
public static Timer createTimer(MetricRegistry metrics, String name, final String pipelineName, final String pipelineRev) {
  return create(
    metrics,
    new Timer(new SlidingTimeWindowReservoir(60, TimeUnit.SECONDS)),
    metricName(name, TIMER_SUFFIX),
    pipelineName,
    pipelineRev
  );
}
 
Example #5
Source File: TaskExecutor.java    From incubator-gobblin with Apache License 2.0 5 votes vote down vote up
/**
 * Constructor used internally.
 */
private TaskExecutor(int taskExecutorThreadPoolSize, int coreRetryThreadPoolSize, long retryIntervalInSeconds,
                     int queuedTaskTimeMaxSize, long queuedTaskTimeMaxAge, int timerWindowSize) {
  Preconditions.checkArgument(taskExecutorThreadPoolSize > 0, "Task executor thread pool size should be positive");
  Preconditions.checkArgument(retryIntervalInSeconds > 0, "Task retry interval should be positive");
  Preconditions.checkArgument(queuedTaskTimeMaxSize > 0, "Queued task time max size should be positive");
  Preconditions.checkArgument(queuedTaskTimeMaxAge > 0, "Queued task time max age should be positive");

  // Currently a fixed-size thread pool is used to execute tasks. We probably need to revisit this later.
  this.taskExecutor = ExecutorsUtils.loggingDecorator(Executors.newScheduledThreadPool(
      taskExecutorThreadPoolSize,
      ExecutorsUtils.newThreadFactory(Optional.of(LOG), Optional.of("TaskExecutor-%d"))));

  this.retryIntervalInSeconds = retryIntervalInSeconds;
  this.queuedTaskTimeMaxSize = queuedTaskTimeMaxSize;
  this.queuedTaskTimeMaxAge = queuedTaskTimeMaxAge;
  this.taskCreateAndRunTimer = new Timer(new SlidingTimeWindowReservoir(timerWindowSize, TimeUnit.MINUTES));

  this.forkExecutor = ExecutorsUtils.loggingDecorator(
      new ThreadPoolExecutor(
          // The core thread pool size is equal to that of the task
          // executor as there's at least one fork per task
          taskExecutorThreadPoolSize,
          // The fork executor thread pool size is essentially unbounded. This is to make sure all forks of
          // a task get a thread to run so all forks of the task are making progress. This is necessary since
          // otherwise the parent task will be blocked if the record queue (bounded) of some fork is full and
          // that fork has not yet started to run because of no available thread. The task cannot proceed in
          // this case because it has to make sure every records go to every forks.
          Integer.MAX_VALUE,
          0L,
          TimeUnit.MILLISECONDS,
          // The work queue is a SynchronousQueue. This essentially forces a new thread to be created for each fork.
          new SynchronousQueue<Runnable>(),
          ExecutorsUtils.newThreadFactory(Optional.of(LOG), Optional.of("ForkExecutor-%d"))));
}
 
Example #6
Source File: InnerHistogram.java    From incubator-gobblin with Apache License 2.0 5 votes vote down vote up
InnerHistogram(MetricContext context, String name, ContextAwareHistogram contextAwareHistogram, long windowSize, TimeUnit unit) {
  super(new SlidingTimeWindowReservoir(windowSize, unit));

  this.name = name;

  Optional<MetricContext> parentContext = context.getParent();
  if (parentContext.isPresent()) {
    this.parentHistogram = Optional.fromNullable(parentContext.get().contextAwareHistogram(name, windowSize, unit));
  } else {
    this.parentHistogram = Optional.absent();
  }

  this.contextAwareHistogram = new WeakReference<>(contextAwareHistogram);
}
 
Example #7
Source File: InnerTimer.java    From incubator-gobblin with Apache License 2.0 5 votes vote down vote up
InnerTimer(MetricContext context, String name, ContextAwareTimer contextAwareTimer, long windowSize, TimeUnit unit) {
  super(new SlidingTimeWindowReservoir(windowSize, unit));
  this.name = name;

  Optional<MetricContext> parentContext = context.getParent();
  if (parentContext.isPresent()) {
    this.parentTimer = Optional.fromNullable(parentContext.get().contextAwareTimer(name, windowSize, unit));
  } else {
    this.parentTimer = Optional.absent();
  }
  this.timer = new WeakReference<>(contextAwareTimer);
}
 
Example #8
Source File: MetricBuilders.java    From kite with Apache License 2.0 5 votes vote down vote up
public Histogram getSlidingTimeWindowHistogram(MetricRegistry registry, String name, final long window,
    final TimeUnit windowUnit) {
  return getOrAdd(registry, name, new MetricBuilder<Histogram>() {
    @Override
    public Histogram newMetric() {
      return new Histogram(new SlidingTimeWindowReservoir(window, windowUnit));
    }
    @Override
    public boolean isInstance(Metric metric) {
      return Histogram.class.isInstance(metric);
    }          
  });
}
 
Example #9
Source File: MetricBuilders.java    From kite with Apache License 2.0 5 votes vote down vote up
public Timer getSlidingTimeWindowTimer(MetricRegistry registry, String name, final long window,
    final TimeUnit windowUnit) {
  return getOrAdd(registry, name, new MetricBuilder<Timer>() {
    @Override
    public Timer newMetric() {
      return new Timer(new SlidingTimeWindowReservoir(window, windowUnit));
    }
    @Override
    public boolean isInstance(Metric metric) {
      return Timer.class.isInstance(metric);
    }          
  });
}
 
Example #10
Source File: MetricSuppliers.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings({"unchecked"})
private static final Reservoir getReservoir(SolrResourceLoader loader, PluginInfo info) {
  if (info == null) {
    return new ExponentiallyDecayingReservoir();
  }
  Clock clk = getClock(info, CLOCK);
  String clazz = ExponentiallyDecayingReservoir.class.getName();
  int size = -1;
  double alpha = -1;
  long window = -1;
  if (info.initArgs != null) {
    if (info.initArgs.get(RESERVOIR) != null) {
      String val = String.valueOf(info.initArgs.get(RESERVOIR)).trim();
      if (!val.isEmpty()) {
        clazz = val;
      }
    }
    Number n = (Number)info.initArgs.get(RESERVOIR_SIZE);
    if (n != null) {
      size = n.intValue();
    }
    n = (Number)info.initArgs.get(RESERVOIR_EDR_ALPHA);
    if (n != null) {
      alpha = n.doubleValue();
    }
    n = (Number)info.initArgs.get(RESERVOIR_WINDOW);
    if (n != null) {
      window = n.longValue();
    }
  }
  if (size <= 0) {
    size = DEFAULT_SIZE;
  }
  if (alpha <= 0) {
    alpha = DEFAULT_ALPHA;
  }
  // special case for core implementations
  if (clazz.equals(EDR_CLAZZ)) {
    return new ExponentiallyDecayingReservoir(size, alpha, clk);
  } else if (clazz.equals(UNI_CLAZZ)) {
    return new UniformReservoir(size);
  } else if (clazz.equals(STW_CLAZZ)) {
    if (window <= 0) {
      window = DEFAULT_WINDOW; // 5 minutes, comparable to EDR
    }
    return new SlidingTimeWindowReservoir(window, TimeUnit.SECONDS);
  } else if (clazz.equals(SW_CLAZZ)) {
    return new SlidingWindowReservoir(size);
  } else { // custom reservoir
    Reservoir reservoir;
    try {
      reservoir = loader.newInstance(clazz, Reservoir.class);
      if (reservoir instanceof PluginInfoInitialized) {
        ((PluginInfoInitialized)reservoir).init(info);
      } else {
        SolrPluginUtils.invokeSetters(reservoir, info.initArgs, true);
      }
      return reservoir;
    } catch (Exception e) {
      log.warn("Error initializing custom Reservoir implementation (will use default): {}", info, e);
      return new ExponentiallyDecayingReservoir(size, alpha, clk);
    }
  }
}
 
Example #11
Source File: SemanticCoreStatistics.java    From ffwd with Apache License 2.0 4 votes vote down vote up
@Override
public Histogram newMetric() {
    return new Histogram(new SlidingTimeWindowReservoir(
        TimeUnit.MINUTES.toMinutes(HISTOGRAM_TTL_MINUTES), TimeUnit.MINUTES));
}
 
Example #12
Source File: ContextAwareHistogram.java    From incubator-gobblin with Apache License 2.0 4 votes vote down vote up
ContextAwareHistogram(MetricContext context, String name, long windowSize, TimeUnit unit) {
  super(new SlidingTimeWindowReservoir(windowSize, unit));
  this.innerHistogram = new InnerHistogram(context, name, this, windowSize, unit);
  this.context = context;
}
 
Example #13
Source File: ContextAwareTimer.java    From incubator-gobblin with Apache License 2.0 4 votes vote down vote up
ContextAwareTimer(MetricContext context, String name, long windowSize, TimeUnit unit) {
  super(new SlidingTimeWindowReservoir(windowSize, unit));
  this.innerTimer = new InnerTimer(context, name, this, windowSize, unit);
  this.context = context;
}