com.netflix.spectator.api.Statistic Java Examples

The following examples show how to use com.netflix.spectator.api.Statistic. 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: ServoTimerTest.java    From spectator with Apache License 2.0 6 votes vote down vote up
@Test
public void totalOfSquaresManyBigValues() {
  Timer t = newTimer("foo");
  BigInteger sumOfSq = new BigInteger("0");
  for (int i = 0; i < 100000; ++i) {
    final long nanos = TimeUnit.SECONDS.toNanos(i);
    final BigInteger s = new BigInteger("" + nanos);
    final BigInteger s2 = s.multiply(s);
    sumOfSq = sumOfSq.add(s2);
    t.record(i, TimeUnit.SECONDS);
  }
  clock.setWallTime(61000L);

  final double v = Utils.first(t.measure(), Statistic.totalOfSquares).value();

  // Expected :3.3332833335E14
  // Actual   :3.3332833334999825E14
  final double factor = 1e9 * 1e9;
  sumOfSq = sumOfSq.divide(BigInteger.valueOf(60));
  Assertions.assertEquals(sumOfSq.doubleValue() / factor, v, 2.0);
}
 
Example #2
Source File: ConsolidatorTest.java    From spectator with Apache License 2.0 6 votes vote down vote up
@Test
public void avgRandom() {
  Id id = Id.create("test");
  Id measurementId = id.withTag("atlas.dstype", "rate").withTag(Statistic.count);
  ManualClock clock = new ManualClock();

  Counter primary = registry(clock, PRIMARY_STEP).counter(id);
  Counter consolidated = registry(clock, CONSOLIDATED_STEP).counter(id);

  Consolidator consolidator = new Consolidator.Avg(CONSOLIDATED_STEP, MULTIPLE);

  consolidateRandomData(
      measurementId,
      clock,
      consolidator,
      primary::add,
      consolidated::add,
      primary::measure,
      consolidated::measure);
}
 
Example #3
Source File: ConsolidatorTest.java    From spectator with Apache License 2.0 6 votes vote down vote up
@Test
public void maxRandom() {
  Id id = Id.create("test");
  Id measurementId = id.withTag("atlas.dstype", "gauge").withTag(Statistic.max);
  ManualClock clock = new ManualClock();

  Gauge primary = registry(clock, PRIMARY_STEP).maxGauge(id);
  Gauge consolidated = registry(clock, CONSOLIDATED_STEP).maxGauge(id);

  Consolidator consolidator = new Consolidator.Max(CONSOLIDATED_STEP, MULTIPLE);

  consolidateRandomData(
      measurementId,
      clock,
      consolidator,
      primary::set,
      consolidated::set,
      primary::measure,
      consolidated::measure);
}
 
Example #4
Source File: ConsolidatorTest.java    From spectator with Apache License 2.0 6 votes vote down vote up
@Test
public void noneRandom() {
  Id id = Id.create("test");
  Id measurementId = id.withTag("atlas.dstype", "rate").withTag(Statistic.count);
  ManualClock clock = new ManualClock();

  Counter primary = registry(clock, CONSOLIDATED_STEP).counter(id);
  Counter consolidated = registry(clock, CONSOLIDATED_STEP).counter(id);

  Consolidator consolidator = new Consolidator.None();

  consolidateRandomData(
      measurementId,
      clock,
      consolidator,
      primary::add,
      consolidated::add,
      primary::measure,
      consolidated::measure);
}
 
Example #5
Source File: RollupsTest.java    From spectator with Apache License 2.0 6 votes vote down vote up
@Test
public void aggregateGaugesWithNaN() {
  for (int i = 0; i < 10; ++i) {
    double v = (i % 2 == 0) ? i : Double.NaN;
    registry.gauge("test", "i", "" + i).set(v);
  }
  clock.setWallTime(5000);
  List<Measurement> input = registry.measurements().collect(Collectors.toList());
  List<Measurement> aggr = Rollups.aggregate(this::removeIdxTag, input);
  Assertions.assertEquals(1, aggr.size());

  Measurement m = aggr.get(0);
  Id id = registry.createId("test")
      .withTag("atlas.dstype", "gauge")
      .withTag(Statistic.gauge);
  Assertions.assertEquals(id, m.id());
  Assertions.assertEquals(8.0, m.value(), 1e-12);
}
 
Example #6
Source File: RollupsTest.java    From spectator with Apache License 2.0 6 votes vote down vote up
@Test
public void aggregateGauges() {
  for (int i = 0; i < 10; ++i) {
    registry.gauge("test", "i", "" + i).set(2.0);
  }
  clock.setWallTime(5000);
  List<Measurement> input = registry.measurements().collect(Collectors.toList());
  List<Measurement> aggr = Rollups.aggregate(this::removeIdxTag, input);
  Assertions.assertEquals(1, aggr.size());

  Measurement m = aggr.get(0);
  Id id = registry.createId("test")
      .withTag("atlas.dstype", "gauge")
      .withTag(Statistic.gauge);
  Assertions.assertEquals(id, m.id());
  Assertions.assertEquals(2.0, m.value(), 1e-12);
}
 
Example #7
Source File: RollupsTest.java    From spectator with Apache License 2.0 6 votes vote down vote up
@Test
public void aggregateCounters() {
  for (int i = 0; i < 10; ++i) {
    registry.counter("test", "i", "" + i).increment();
  }
  clock.setWallTime(5000);
  List<Measurement> input = registry.measurements().collect(Collectors.toList());
  List<Measurement> aggr = Rollups.aggregate(this::removeIdxTag, input);
  Assertions.assertEquals(1, aggr.size());

  Measurement m = aggr.get(0);
  Id id = registry.createId("test")
      .withTag("atlas.dstype", "rate")
      .withTag(Statistic.count);
  Assertions.assertEquals(id, m.id());
  Assertions.assertEquals(10.0 / 5.0, m.value(), 1e-12);
}
 
Example #8
Source File: ConsolidatorTest.java    From spectator with Apache License 2.0 6 votes vote down vote up
@Test
public void createFromStatistic() {
  EnumSet<Statistic> counters = EnumSet.of(
      Statistic.count,
      Statistic.totalAmount,
      Statistic.totalTime,
      Statistic.totalOfSquares,
      Statistic.percentile);
  for (Statistic statistic : Statistic.values()) {
    Consolidator consolidator = Consolidator.create(statistic, CONSOLIDATED_STEP, MULTIPLE);
    if (counters.contains(statistic)) {
      Assertions.assertTrue(consolidator instanceof Consolidator.Avg, statistic.name());
    } else {
      Assertions.assertTrue(consolidator instanceof Consolidator.Max, statistic.name());
    }
  }
}
 
Example #9
Source File: LongTaskTimer.java    From spectator with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a timer for tracking long running tasks.
 *
 * @param registry
 *     Registry to use.
 * @param id
 *     Identifier for the metric being registered.
 * @return
 *     Timer instance.
 */
public static LongTaskTimer get(Registry registry, Id id) {
  ConcurrentMap<Id, Object> state = registry.state();
  Object obj = Utils.computeIfAbsent(state, id, i -> {
    LongTaskTimer timer = new LongTaskTimer(registry, id);
    PolledMeter.using(registry)
        .withId(id)
        .withTag(Statistic.activeTasks)
        .monitorValue(timer, LongTaskTimer::activeTasks);
    PolledMeter.using(registry)
        .withId(id)
        .withTag(Statistic.duration)
        .monitorValue(timer, t -> t.duration() / NANOS_PER_SECOND);
    return timer;
  });
  if (!(obj instanceof LongTaskTimer)) {
    Utils.propagateTypeError(registry, id, LongTaskTimer.class, obj.getClass());
    obj = new LongTaskTimer(new NoopRegistry(), id);
  }
  return (LongTaskTimer) obj;
}
 
Example #10
Source File: DoubleDistributionSummary.java    From spectator with Apache License 2.0 6 votes vote down vote up
/**
 * Create a new instance.
 */
DoubleDistributionSummary(Clock clock, Id id, long resetFreq) {
  this.clock = clock;
  this.id = id;
  this.resetFreq = resetFreq;
  lastResetTime = new AtomicLong(clock.wallTime());
  lastUpdateTime = new AtomicLong(clock.wallTime());
  count = new AtomicLong(0L);
  totalAmount = new AtomicLong(ZERO);
  totalOfSquares = new AtomicLong(ZERO);
  max = new AtomicLong(ZERO);
  countId = id.withTag(Statistic.count);
  totalAmountId = id.withTag(Statistic.totalAmount);
  totalOfSquaresId = id.withTag(Statistic.totalOfSquares);
  maxId = id.withTag(Statistic.max);
}
 
Example #11
Source File: ServoTimerTest.java    From spectator with Apache License 2.0 6 votes vote down vote up
@Test
public void totalOfSquaresManySmallValues() {
  Timer t = newTimer("foo");
  BigInteger sumOfSq = new BigInteger("0");
  for (int i = 0; i < 100000; ++i) {
    final long nanos = i;
    final BigInteger s = new BigInteger("" + nanos);
    final BigInteger s2 = s.multiply(s);
    sumOfSq = sumOfSq.add(s2);
    t.record(i, TimeUnit.NANOSECONDS);
  }
  clock.setWallTime(61000L);

  final double v = Utils.first(t.measure(), Statistic.totalOfSquares).value();

  final double factor = 1e9 * 1e9;
  sumOfSq = sumOfSq.divide(BigInteger.valueOf(60));
  Assertions.assertEquals(sumOfSq.doubleValue() / factor, v, 1e-12);
}
 
Example #12
Source File: Utils.java    From servicecomb-java-chassis with Apache License 2.0 6 votes vote down vote up
public static MeasurementNode createStageNode(String stage,
    double count,
    double totalTime,
    double max) {
  Id id = registry.createId("id").withTag(Statistic.count);
  Measurement countMeasurement = new Measurement(id.withTag(Statistic.count), 0, count);
  Measurement totalTimeMeasurement = new Measurement(id.withTag(Statistic.totalTime), 0, totalTime);
  Measurement maxMeasurement = new Measurement(id.withTag(Statistic.max), 0, max);

  MeasurementNode stageNode = new MeasurementNode(stage, null);
  stageNode.addChild(Statistic.count.name(), countMeasurement);
  stageNode.addChild(Statistic.totalTime.name(), totalTimeMeasurement);
  stageNode.addChild(Statistic.max.name(), maxMeasurement);

  return stageNode;
}
 
Example #13
Source File: ServoTimerTest.java    From spectator with Apache License 2.0 6 votes vote down vote up
@Test
public void totalOfSquaresOverflow() {
  final long seconds = 10;
  final long nanos = TimeUnit.SECONDS.toNanos(seconds);
  final BigInteger s = new BigInteger("" + nanos);
  final BigInteger s2 = s.multiply(s);

  Timer t = newTimer("foo");
  t.record(seconds, TimeUnit.SECONDS);
  clock.setWallTime(61000L);

  final double v = Utils.first(t.measure(), Statistic.totalOfSquares).value();

  final double factor = 1e9 * 1e9;
  final BigInteger perSec = s2.divide(BigInteger.valueOf(60));
  Assertions.assertEquals(perSec.doubleValue() / factor, v, 1e-12);
}
 
Example #14
Source File: ServoTimer.java    From spectator with Apache License 2.0 6 votes vote down vote up
/** Create a new instance. */
ServoTimer(ServoRegistry r, Id id) {
  super(r.clock());
  this.id = id;
  count = new AtomicLong(0L);
  totalTime = new AtomicLong(0L);

  ServoClock sc = new ServoClock(clock);
  servoCount = new StepCounter(r.toMonitorConfig(id.withTag(Statistic.count), null), sc);
  servoTotal = new StepCounter(r.toMonitorConfig(id.withTag(Statistic.totalTime), null), sc);
  servoTotalOfSquares = new DoubleCounter(
      r.toMonitorConfig(id.withTag(Statistic.totalOfSquares), null), sc);

  // Constructor that takes a clock param is not public
  servoMax = new MaxGauge(r.toMonitorConfig(id.withTag(Statistic.max), null));

  lastUpdated = new AtomicLong(clock.wallTime());
}
 
Example #15
Source File: DefaultPlaceholderTimerTest.java    From spectator with Apache License 2.0 6 votes vote down vote up
@Test
public void testMeasure() {
  Timer timer = factory.timer(factory.createId("testMeasure"));
  timer.record(42, TimeUnit.MILLISECONDS);
  clock.setWallTime(3712345L);
  for (Measurement m : timer.measure()) {
    Assertions.assertEquals(m.timestamp(), 3712345L);
    if (m.id().equals(timer.id().withTag(Statistic.count))) {
      Assertions.assertEquals(1.0, m.value(), 0.1e-12);
    } else if (m.id().equals(timer.id().withTag(Statistic.totalTime))) {
      Assertions.assertEquals(42e6, m.value(), 0.1e-12);
    } else {
      Assertions.fail("unexpected id: " + m.id());
    }
  }
}
 
Example #16
Source File: IntervalCounter.java    From spectator with Apache License 2.0 5 votes vote down vote up
/**
 * Create a new IntervalCounter using the given registry and base id.
 */
IntervalCounter(Registry registry, Id id) {
  this.clock = registry.clock();
  this.id = id;
  this.counter = registry.counter(id.withTag(Statistic.count));
  this.lastUpdated = PolledMeter.using(registry)
      .withId(id)
      .withTag(Statistic.duration)
      .monitorValue(new AtomicLong(0L), Functions.age(clock));
}
 
Example #17
Source File: AtlasMaxGaugeTest.java    From spectator with Apache License 2.0 5 votes vote down vote up
private void checkValue(long expected) {
  int count = 0;
  for (Measurement m : gauge.measure()) {
    Assertions.assertEquals(gauge.id().withTags(Statistic.max, DsType.gauge), m.id());
    Assertions.assertEquals(expected, m.value(), 1e-12);
    ++count;
  }
  Assertions.assertEquals(1, count);
}
 
Example #18
Source File: ServoTimerTest.java    From spectator with Apache License 2.0 5 votes vote down vote up
@Test
public void testMeasure() {
  Timer t = newTimer("foo");
  t.record(42, TimeUnit.MILLISECONDS);
  clock.setWallTime(61000L);
  for (Measurement m : t.measure()) {
    Assertions.assertEquals(m.timestamp(), 61000L);
    final double count = Utils.first(t.measure(), Statistic.count).value();
    final double totalTime = Utils.first(t.measure(), Statistic.totalTime).value();
    Assertions.assertEquals(count, 1.0 / 60.0, 0.1e-12);
    Assertions.assertEquals(totalTime, 42e-3 / 60.0, 0.1e-12);
  }
}
 
Example #19
Source File: ServoTimer.java    From spectator with Apache License 2.0 5 votes vote down vote up
@Override public Iterable<Measurement> measure() {
  final long now = clock.wallTime();
  final List<Measurement> ms = new ArrayList<>(2);
  ms.add(newMeasurement(Statistic.count, now, servoCount.getValue(0)));
  ms.add(newMeasurement(Statistic.totalTime, now, getValue(servoTotal, CNV_SECONDS)));
  ms.add(newMeasurement(Statistic.totalOfSquares, now, getValue(servoTotalOfSquares, CNV_SQUARES)));
  ms.add(newMeasurement(Statistic.max, now, getValue(servoMax, CNV_SECONDS)));
  return ms;
}
 
Example #20
Source File: AtlasCounterTest.java    From spectator with Apache License 2.0 5 votes vote down vote up
private void checkValue(double expected) {
  int count = 0;
  for (Measurement m : counter.measure()) {
    Assertions.assertEquals(counter.id().withTags(Statistic.count, DsType.rate), m.id());
    Assertions.assertEquals(expected / 10.0, m.value(), 1e-12);
    Assertions.assertEquals(expected, counter.actualCount(), 1e-12);
    ++count;
  }
  Assertions.assertEquals(1, count);
}
 
Example #21
Source File: ServoDistributionSummary.java    From spectator with Apache License 2.0 5 votes vote down vote up
@Override public Iterable<Measurement> measure() {
  final long now = clock.wallTime();
  final List<Measurement> ms = new ArrayList<>(2);
  ms.add(new Measurement(id.withTag(Statistic.count), now, count.get()));
  ms.add(new Measurement(id.withTag(Statistic.totalAmount), now, totalAmount.get()));
  return ms;
}
 
Example #22
Source File: LongTaskTimer.java    From spectator with Apache License 2.0 5 votes vote down vote up
@Override public Iterable<Measurement> measure() {
  final List<Measurement> ms = new ArrayList<>(2);
  final long now = clock.wallTime();
  final double durationSeconds = duration() / NANOS_PER_SECOND;
  ms.add(new Measurement(id.withTag(Statistic.duration), now, durationSeconds));
  ms.add(new Measurement(id.withTag(Statistic.activeTasks), now, activeTasks()));
  return ms;
}
 
Example #23
Source File: PercentileTimer.java    From spectator with Apache License 2.0 5 votes vote down vote up
private Counter counterFor(int i) {
  Counter c = counters.get(i);
  if (c == null) {
    Id counterId = id.withTags(Statistic.percentile, new BasicTag("percentile", TAG_VALUES[i]));
    c = registry.counter(counterId);
    counters.set(i, c);
  }
  return c;
}
 
Example #24
Source File: ServoDistributionSummary.java    From spectator with Apache License 2.0 5 votes vote down vote up
/** Create a new instance. */
ServoDistributionSummary(ServoRegistry r, Id id) {
  this.clock = r.clock();
  this.id = id;
  count = new AtomicLong(0L);
  totalAmount = new AtomicLong(0L);

  servoCount = new StepCounter(r.toMonitorConfig(id.withTag(Statistic.count), null));
  servoTotal = new StepCounter(r.toMonitorConfig(id.withTag(Statistic.totalAmount), null));
  servoTotalOfSquares = new StepCounter(
      r.toMonitorConfig(id.withTag(Statistic.totalOfSquares), null));
  servoMax = new MaxGauge(r.toMonitorConfig(id.withTag(Statistic.max), null));

  lastUpdated = new AtomicLong(clock.wallTime());
}
 
Example #25
Source File: PercentileDistributionSummary.java    From spectator with Apache License 2.0 5 votes vote down vote up
private Counter counterFor(int i) {
  Counter c = counters.get(i);
  if (c == null) {
    Id counterId = id.withTags(Statistic.percentile, new BasicTag("percentile", TAG_VALUES[i]));
    c = registry.counter(counterId);
    counters.set(i, c);
  }
  return c;
}
 
Example #26
Source File: DefaultStat.java    From spectator with Apache License 2.0 5 votes vote down vote up
@Threads(1)
@Benchmark
public void withNameWithTags(Blackhole bh) {
  bh.consume(
      registry.createId(
          baseId.name()).withTag(Statistic.count).withTags(baseId.tags()).withTag(DsType.rate));
}
 
Example #27
Source File: IntervalCounterTest.java    From spectator with Apache License 2.0 5 votes vote down vote up
@Test
public void testMeasure() {
  Registry r = new DefaultRegistry(clock);
  clock.setWallTime(61000L);
  Id id = r.createId("test");
  Counter c = IntervalCounter.get(r, id);

  // all meters should have the correct timestamp
  r.stream().forEach(meter -> {
    for (Measurement m : meter.measure()) {
      Assertions.assertEquals(m.timestamp(), 61000L);
    }
  });

  final List<Measurement> measurements = getAllMeasurements(r);
  final double initAge = Utils.first(measurements, Statistic.duration).value();
  final double initCount = Utils.first(measurements, Statistic.count).value();
  Assertions.assertEquals(61.0, initAge, EPSILON);
  Assertions.assertEquals(0.0, initCount, EPSILON);

  clock.setWallTime(120000L);
  c.increment();
  final List<Measurement> afterMeasurements = getAllMeasurements(r);
  final double afterAge = Utils.first(afterMeasurements, Statistic.duration).value();
  final double afterCount = Utils.first(afterMeasurements, Statistic.count).value();
  Assertions.assertEquals(0.0, afterAge, EPSILON);
  Assertions.assertEquals(1.0, afterCount, EPSILON);
}
 
Example #28
Source File: MicrometerRegistryTest.java    From spectator with Apache License 2.0 5 votes vote down vote up
@Test
public void patternUsingState() {
  LongTaskTimer t = LongTaskTimer.get(registry, registry.createId("foo"));
  long tid = t.start();
  clock.addSeconds(60);
  PolledMeter.update(registry);

  Gauge g = registry.gauge(registry.createId("foo").withTag(Statistic.duration));
  Assertions.assertEquals(60.0, g.value(), 1e-12);

  t.stop(tid);
  PolledMeter.update(registry);
  Assertions.assertEquals(0.0, g.value(), 1e-12);
}
 
Example #29
Source File: SpectatorDistributionSummary.java    From micrometer with Apache License 2.0 5 votes vote down vote up
@Override
public double max() {
    for (Measurement measurement : summary.measure()) {
        if (stream(measurement.id().tags().spliterator(), false)
                .anyMatch(tag -> tag.key().equals("statistic") && tag.value().equals(Statistic.max.toString()))) {
            return measurement.value();
        }
    }

    return Double.NaN;
}
 
Example #30
Source File: PublishUtils.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
public static PerfInfo createPerfInfo(MeasurementNode stageNode) {
  PerfInfo perfInfo = new PerfInfo();
  perfInfo.setTps(stageNode.findChild(Statistic.count.name()).summary());
  perfInfo.setMsTotalTime(stageNode.findChild(Statistic.totalTime.name()).summary() * 1000);
  // when UT with DefaultRegistry, there is no max value
  MeasurementNode maxNode = stageNode.findChild(Statistic.max.name());
  if (maxNode != null) {
    perfInfo.setMsMaxLatency(maxNode.summary() * 1000);
  }
  return perfInfo;
}