com.netflix.spectator.api.Measurement Java Examples

The following examples show how to use com.netflix.spectator.api.Measurement. 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: StackdriverWriter.java    From kork with Apache License 2.0 6 votes vote down vote up
/** Add a TimeSeries for each appropriate meter measurement. */
void addMeterToTimeSeries(Registry registry, Meter meter, List<TimeSeries> tsList) {
  Iterable<Measurement> measurements = meter.measure();
  boolean applyFilter = true;

  if (cache.meterIsTimer(registry, meter)) {
    measurements = transformTimerMeasurements(measurements);
    applyFilter = false;
  }
  for (Measurement measurement : measurements) {
    if (applyFilter && !measurementFilter.test(measurement)) {
      continue;
    }

    String descriptorType = cache.idToDescriptorType(measurement.id());
    tsList.add(measurementToTimeSeries(descriptorType, registry, meter, measurement));
  }
}
 
Example #2
Source File: CloudEyeFilePublisher.java    From servicecomb-samples with Apache License 2.0 6 votes vote down vote up
protected void logMeasurement(Measurement measurement, long now) {
  String metricKey = generateMetricKey(measurement);

  MDC.put("fileName", filePrefix + "." + metricKey + ".dat");

  CloudEyeMetricModel metricModel = new CloudEyeMetricModel();
  metricModel.setNode(hostName);
  metricModel.setTimestamp(now);
  metricModel.getDynamicValue().put(metricKey, String.valueOf(measurement.value()));

  CloudEyeModel model = new CloudEyeModel();
  model.setPlugin_id(filePrefix);
  model.setMetric(metricModel);

  try {
    CLOUD_EYE_LOGGER.info(JsonUtils.writeValueAsString(model));
  } catch (JsonProcessingException e) {
    LOGGER.error("Failed to write cloud eye log.", e);
  }
}
 
Example #3
Source File: TagMeasurementFilter.java    From spectator with Apache License 2.0 6 votes vote down vote up
/**
 * Implements MeasurementFilter interface.
 */
@SuppressWarnings("PMD.JUnit4TestShouldUseTestAnnotation")
@Override public boolean test(Measurement measurement) {
  Id id = measurement.id();
  if (!stringMatches(id.name(), meterNamePattern)) {
      return false;
  }

  if (tagNamePattern != null || tagValuePattern != null) {
    for (Tag tag : id.tags()) {
      boolean nameOk = stringMatches(tag.key(), tagNamePattern);
      boolean valueOk = stringMatches(tag.value(), tagValuePattern);
      if (nameOk && valueOk) {
        return true;
      }
    }
    return false;
  }

  return true;
}
 
Example #4
Source File: EndpointMeter.java    From servicecomb-java-chassis with Apache License 2.0 6 votes vote down vote up
public void calcMeasurements(List<Measurement> measurements, long msNow, double secondInterval) {
  long connectCount = metric.getConnectCount();
  long disconnectCount = metric.getDisconnectCount();
  long bytesRead = metric.getBytesRead();
  long bytesWritten = metric.getBytesWritten();

  measurements.add(newMeasurement(idConnect, msNow, connectCount - lastConnectCount));
  measurements.add(newMeasurement(idDisconnect, msNow, disconnectCount - lastDisconnectCount));
  measurements.add(newMeasurement(idConnections, msNow, connectCount - disconnectCount));
  measurements.add(newMeasurement(idBytesRead, msNow, (bytesRead - lastBytesRead) / secondInterval));
  measurements.add(newMeasurement(idBytesWritten, msNow, (bytesWritten - lastBytesWritten) / secondInterval));

  this.lastConnectCount = connectCount;
  this.lastDisconnectCount = disconnectCount;
  this.lastBytesRead = bytesRead;
  this.lastBytesWritten = bytesWritten;
}
 
Example #5
Source File: PrototypeMeasurementFilterTest.java    From spectator with Apache License 2.0 6 votes vote down vote up
@Test
public void meterOk() {
    PrototypeMeasurementFilterSpecification.MeterFilterSpecification meterSpec
        = new PrototypeMeasurementFilterSpecification.MeterFilterSpecification(
                 Arrays.asList(valueSpecAyBx, valueSpecAzBy));

    PrototypeMeasurementFilterSpecification spec = new PrototypeMeasurementFilterSpecification();
    spec.getInclude().put("counter.+", meterSpec);

    PrototypeMeasurementFilter filter = new PrototypeMeasurementFilter(spec);

    Id idAYX = Id.create("counterA").withTag("tagA", "Y").withTag("tagB", "X");
    Id idBZY = Id.create("counterB").withTag("tagA", "Z").withTag("tagB", "Y");

    Assertions.assertTrue(filter.test(new Measurement(idAYX, 1, 1)));
    Assertions.assertTrue(filter.test(new Measurement(idBZY, 2, 2)));
}
 
Example #6
Source File: ThreadPoolMonitorPublishModelFactory.java    From servicecomb-java-chassis with Apache License 2.0 6 votes vote down vote up
protected void readMeasurement(String name, Setter setter) {
  MeasurementNode node = tree.findChild(name);
  if (node == null) {
    return;
  }

  for (Measurement measurement : node.getMeasurements()) {
    String threadPoolName = Utils.getTagValue(measurement.id(), ThreadPoolMonitor.ID_TAG_NAME);
    if (threadPoolName == null) {
      continue;
    }

    ThreadPoolPublishModel model = threadPools.computeIfAbsent(threadPoolName, tpn -> new ThreadPoolPublishModel());
    setter.set(model, measurement);
  }
}
 
Example #7
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 #8
Source File: MeasurementTree.java    From servicecomb-java-chassis with Apache License 2.0 6 votes vote down vote up
public void from(Iterable<Measurement> measurements, MeasurementGroupConfig groupConfig) {
  for (Measurement measurement : measurements) {
    Id id = measurement.id();
    MeasurementNode node = addChild(id.name(), measurement);

    List<TagFinder> tagFinders = groupConfig.findTagFinders(id.name());
    if (tagFinders == null) {
      continue;
    }

    for (TagFinder tagFinder : tagFinders) {
      Tag tag = tagFinder.find(id.tags());
      if (tag == null) {
        if (tagFinder.skipOnNull()) {
          break;
        }
        throw new IllegalStateException(
            String.format("tag key \"%s\" not exist in %s",
                tagFinder.getTagKey(),
                measurement));
      }

      node = node.addChild(tag.value(), measurement);
    }
  }
}
 
Example #9
Source File: StackdriverWriterTest.java    From kork with Apache License 2.0 6 votes vote down vote up
@Test
public void testAddMeasurementsToTimeSeries() {
  DefaultRegistry testRegistry = new DefaultRegistry(clock);

  long millisA = TimeUnit.MILLISECONDS.convert(1472394975L, TimeUnit.SECONDS);
  long millisB = millisA + 987;
  String timeA = "2016-08-28T14:36:15.000000000Z";
  String timeB = "2016-08-28T14:36:15.987000000Z";
  Meter timerA = testRegistry.timer(idAXY);
  Meter timerB = testRegistry.timer(idBXY);
  Measurement measureAXY = new Measurement(idAXY, millisA, 1);
  Measurement measureBXY = new Measurement(idBXY, millisB, 20.1);

  descriptorRegistrySpy.addExtraTimeSeriesLabel(
      MetricDescriptorCache.INSTANCE_LABEL, INSTANCE_ID);

  Assert.assertEquals(
      makeTimeSeries(descriptorA, idAXY, 1, timeA),
      writer.measurementToTimeSeries(descriptorA.getType(), testRegistry, timerA, measureAXY));
  Assert.assertEquals(
      makeTimeSeries(descriptorB, idBXY, 20.1, timeB),
      writer.measurementToTimeSeries(descriptorB.getType(), testRegistry, timerB, measureBXY));
}
 
Example #10
Source File: PrototypeMeasurementFilterTest.java    From spectator with Apache License 2.0 6 votes vote down vote up
@Test
public void metersExcluded() {
    PrototypeMeasurementFilterSpecification spec = new PrototypeMeasurementFilterSpecification();
    spec.getInclude().put(
            "counter.+",
            new PrototypeMeasurementFilterSpecification.MeterFilterSpecification());
    spec.getExclude().put(
            "counterC",
            new PrototypeMeasurementFilterSpecification.MeterFilterSpecification());
    PrototypeMeasurementFilter filter = new PrototypeMeasurementFilter(spec);

    Id idAYX = Id.create("counterA").withTag("tagA", "Y").withTag("tagB", "X");
    Id idCYX = Id.create("counterC").withTag("tagA", "Y").withTag("tagB", "X");

    Assertions.assertTrue(filter.test(new Measurement(idAYX, 1, 1)));
    Assertions.assertFalse(filter.test(new Measurement(idCYX, 2, 2)));
}
 
Example #11
Source File: DoubleDistributionSummary.java    From spectator with Apache License 2.0 6 votes vote down vote up
@Override public Iterable<Measurement> measure() {
  final long now = clock.wallTime();
  final long prev = lastResetTime.get();
  final long delta = now - prev;
  final boolean reset = delta > resetFreq;

  if (reset) {
    lastResetTime.set(now);
  }

  final List<Measurement> ms = new ArrayList<>(3);
  if (delta > 1000L) {
    ms.add(new Measurement(countId, now, toRateLong(count, delta, reset)));
    ms.add(new Measurement(totalAmountId, now, toRateDouble(totalAmount, delta, reset)));
    ms.add(new Measurement(totalOfSquaresId, now, toRateDouble(totalOfSquares, delta, reset)));
    ms.add(new Measurement(maxId, now, toDouble(max, reset)));
  }
  return ms;
}
 
Example #12
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 #13
Source File: CassandraTest.java    From spectator with Apache License 2.0 6 votes vote down vote up
@Test
public void readLatency() throws Exception {
  Registry r = new DefaultRegistry(new ManualClock());
  List<JmxConfig> configs = configs();

  JmxData data = timer("keyspace=test,name=ReadLatency,scope=foo,type=ColumnFamily", 0);
  List<Measurement> ms = measure(r, configs, data);
  Assertions.assertEquals(7, ms.size());
  Assertions.assertEquals(
      50.0e-4,
      Utils.first(ms, "statistic", "percentile_50").value(),
      1e-12);

  data = timer("keyspace=test,name=ReadLatency,scope=foo,type=ColumnFamily", 1);
  ms = measure(r, configs, data);
  Assertions.assertEquals(7, ms.size());
  Assertions.assertEquals(
      50.01e-4,
      Utils.first(ms, "statistic", "percentile_50").value(),
      1e-12);
}
 
Example #14
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 #15
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 #16
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 #17
Source File: AtlasDistributionSummaryTest.java    From spectator with Apache License 2.0 5 votes vote down vote up
private void checkValue(long count, long amount, long square, long max) {
  int num = 0;
  for (Measurement m : dist.measure()) {
    String stat = Utils.getTagValue(m.id(), "statistic");
    DsType ds = "max".equals(stat) ? DsType.gauge : DsType.rate;
    Id expectedId = dist.id().withTag(ds).withTag("statistic", stat);
    Assertions.assertEquals(expectedId, m.id());
    switch (stat) {
      case "count":
        Assertions.assertEquals(count / 10.0, m.value(), 1e-12);
        break;
      case "totalAmount":
        Assertions.assertEquals(amount / 10.0, m.value(), 1e-12);
        break;
      case "totalOfSquares":
        Assertions.assertEquals(square / 10.0, m.value(), 1e-12);
        break;
      case "max":
        Assertions.assertEquals(max, m.value(), 1e-12);
        break;
      default:
        throw new IllegalArgumentException("unknown stat: " + stat);
    }
    Assertions.assertEquals(count, dist.count());
    Assertions.assertEquals(amount, dist.totalAmount());
    ++num;
  }
  Assertions.assertEquals(4, num);
}
 
Example #18
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 #19
Source File: CloudEyeFilePublisher.java    From servicecomb-samples with Apache License 2.0 5 votes vote down vote up
@Subscribe
public void onPolledEvent(PolledEvent event) {
  long now = System.currentTimeMillis();
  for (Meter meter : event.getMeters()) {
    for (Measurement measurement : meter.measure()) {
      logMeasurement(measurement, now);
    }
  }
}
 
Example #20
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 #21
Source File: MeasurementSerializerTest.java    From spectator with Apache License 2.0 5 votes vote down vote up
@Test
public void publishPayloadWithCommonTags() throws Exception {
  Id id = registry.createId("foo");
  Measurement m = new Measurement(id, 42L, 3.0);
  PublishPayload p = new PublishPayload(Collections.singletonMap("a", "b"), Collections.singletonList(m));
  String json = mapper.writeValueAsString(p);
  String tags = "{\"name\":\"foo\",\"atlas.dstype\":\"gauge\"}";
  String mjson = "{\"tags\":" + tags + ",\"timestamp\":42,\"value\":3.0}";
  String expected = "{\"tags\":{\"a\":\"b\"},\"metrics\":[" + mjson + "]}";
  Assertions.assertEquals(expected, json);
}
 
Example #22
Source File: ConsolidatorTest.java    From spectator with Apache License 2.0 5 votes vote down vote up
private void consolidateRandomData(
    Id measurementId,
    ManualClock clock,
    Consolidator consolidator,
    DoubleConsumer primary,
    DoubleConsumer consolidated,
    Supplier<Iterable<Measurement>> primaryMeasure,
    Supplier<Iterable<Measurement>> consolidatedMeasure) {

  Random r = new Random(42);

  for (int i = 0; i < 3600; ++i) {
    long t = i * 1000L;
    clock.setWallTime(t);
    int v = r.nextInt(10_000);
    primary.accept(v);
    consolidated.accept(v);
    if (t % PRIMARY_STEP == 0L) {
      for (Measurement m : primaryMeasure.get()) {
        consolidator.update(m);
      }
    }
    if (t % CONSOLIDATED_STEP == 0L) {
      Measurement actual = new Measurement(measurementId, t, consolidator.value(t));
      for (Measurement m : consolidatedMeasure.get()) {
        Assertions.assertEquals(m.id(), actual.id());
        Assertions.assertEquals(m.timestamp(), actual.timestamp());
        Assertions.assertEquals(m.value(), actual.value(), 1e-8);
      }
    }

    // Simulate a gap
    if (i == 968) {
      i += 360;
    }
  }
}
 
Example #23
Source File: LatencyScopeMeter.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
public Measurement createMeasurement(long msNow) {
  long currentTimes = times.longValue();
  long deltaTimes = currentTimes - lastTimes;
  this.lastTimes = currentTimes;

  return new Measurement(scopeId, msNow, deltaTimes);
}
 
Example #24
Source File: DoubleDistributionSummaryTest.java    From spectator with Apache License 2.0 5 votes vote down vote up
@Test
public void testMeasureNotEnoughTime() {
  DoubleDistributionSummary t = newInstance();
  t.record(42.0);
  clock.setWallTime(500L);
  int c = 0;
  for (Measurement m : t.measure()) {
    ++c;
  }
  Assertions.assertEquals(0L, c);
}
 
Example #25
Source File: DataExprTest.java    From spectator with Apache License 2.0 5 votes vote down vote up
private TagsValuePair newTagsValuePair(Measurement m) {
  Map<String, String> tags = new HashMap<>();
  for (Tag t : m.id().tags()) {
    tags.put(t.key(), t.value());
  }
  tags.put("name", m.id().name());
  return new TagsValuePair(tags, m.value());
}
 
Example #26
Source File: JsonUtilsTest.java    From spectator with Apache License 2.0 5 votes vote down vote up
@Test
public void encodeInvalidStatisticAsGauge() throws Exception {
  List<Measurement> ms = new ArrayList<>();
  ms.add(count(42, "test", "statistic", "foo"));
  Map<Id, Delta> values = decode(JsonUtils.encode(Collections.emptyMap(), ms));
  Assertions.assertEquals(1, values.size());
  ms.forEach(m -> {
    Id id = m.id();
    Assertions.assertEquals(values.get(id).op, 10);
  });
}
 
Example #27
Source File: AtlasGaugeTest.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.gauge, DsType.gauge), m.id());
    Assertions.assertEquals(expected, m.value(), 1e-12);
    ++count;
  }
  Assertions.assertEquals(1, count);
}
 
Example #28
Source File: MicrometerRegistryTest.java    From spectator with Apache License 2.0 5 votes vote down vote up
@Test
public void counterMeasure() {
  Counter c = registry.counter("foo");
  c.increment();
  int i = 0;
  for (Measurement m : c.measure()) {
    ++i;
    Assertions.assertEquals("foo", m.id().name());
    Assertions.assertEquals(1.0, m.value(), 1e-12);
  }
  Assertions.assertEquals(1, i);
}
 
Example #29
Source File: NetMeter.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
public void calcMeasurements(List<Measurement> measurements, long msNow, long secondInterval) {
  refreshNet(secondInterval);

  interfaceUsageMap.values().forEach(interfaceUsage -> {
    interfaceUsage.calcMeasurements(measurements, msNow);
  });
}
 
Example #30
Source File: MeasurementNode.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
public double summary() {
  double result = 0;
  for (Measurement measurement : measurements) {
    result += measurement.value();
  }

  return result;
}