com.netflix.spectator.api.Meter Java Examples

The following examples show how to use com.netflix.spectator.api.Meter. 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: PolledMeter.java    From spectator with Apache License 2.0 6 votes vote down vote up
private Iterable<Measurement> measure() {
  Map<Id, Measurement> measurements = new HashMap<>();
  Iterator<Meter> iter = queue.iterator();
  while (iter.hasNext()) {
    Meter meter = iter.next();
    if (meter.hasExpired()) {
      iter.remove();
    } else {
      for (Measurement m : meter.measure()) {
        Measurement prev = measurements.get(m.id());
        if (prev == null) {
          measurements.put(m.id(), m);
        } else {
          double v = prev.value() + m.value();
          measurements.put(prev.id(), new Measurement(prev.id(), prev.timestamp(), v));
        }
      }
    }
  }
  if (queue.isEmpty()) {
    LOGGER.trace("meter [{}] has expired", id);
  }
  return measurements.values();
}
 
Example #3
Source File: MetricsRestPublisher.java    From servicecomb-java-chassis with Apache License 2.0 6 votes vote down vote up
@ApiResponses({
    @ApiResponse(code = 400, response = String.class, message = "illegal request content"),
})
@GET
@Path("/")
public Map<String, Double> measure() {
  Map<String, Double> measurements = new LinkedHashMap<>();
  if (globalRegistry == null) {
    return measurements;
  }

  StringBuilder sb = new StringBuilder();
  for (Registry registry : globalRegistry.getRegistries()) {
    for (Meter meter : registry) {
      meter.measure().forEach(measurement -> {
        String key = idToString(measurement.id(), sb);
        measurements.put(key, measurement.value());
      });
    }
  }

  return measurements;
}
 
Example #4
Source File: DefaultLogPublisher.java    From servicecomb-java-chassis with Apache License 2.0 6 votes vote down vote up
protected void printLog(List<Meter> meters) {
  StringBuilder sb = new StringBuilder();
  sb.append("\n");
  PublishModelFactory factory = new PublishModelFactory(meters);
  DefaultPublishModel model = factory.createDefaultPublishModel();

  printOsLog(factory.getTree(), sb);
  printVertxMetrics(factory.getTree(), sb);
  printThreadPoolMetrics(model, sb);

  printConsumerLog(model, sb);
  printProducerLog(model, sb);
  printEdgeLog(model, sb);

  LOGGER.info(sb.toString());
}
 
Example #5
Source File: TestVertxMetersInitializer.java    From servicecomb-java-chassis with Apache License 2.0 6 votes vote down vote up
@Test
public void init() throws InterruptedException {
  globalRegistry.add(registry);
  vertxMetersInitializer.init(globalRegistry, eventBus, null);
  logPublisher.init(null, eventBus, null);
  VertxUtils
      .blockDeploy(SharedVertxFactory.getSharedVertx(), TestServerVerticle.class, new DeploymentOptions());
  VertxUtils
      .blockDeploy(SharedVertxFactory.getSharedVertx(), TestClientVerticle.class, new DeploymentOptions());

  globalRegistry.poll(1);
  List<Meter> meters = Lists.newArrayList(registry.iterator());
  List<Measurement> measurements = new ArrayList<>();
  for (Meter meter : meters) {
    meter.measure().forEach(measurements::add);
  }

  LogCollector logCollector = new LogCollector();

  testLog(logCollector, meters, measurements, true);
  logCollector.clear();
  testLog(logCollector, meters, measurements, false);

  logCollector.teardown();
}
 
Example #6
Source File: MicrometerRegistryTest.java    From spectator with Apache License 2.0 6 votes vote down vote up
@Test
public void iterator() {
  registry.counter("c");
  registry.timer("t");
  registry.distributionSummary("s");
  registry.gauge("g");

  Set<String> actual = new HashSet<>();
  for (Meter m : registry) {
    Assertions.assertFalse(m.hasExpired());
    actual.add(m.id().name());
  }

  Set<String> expected = new HashSet<>();
  expected.add("c");
  expected.add("t");
  expected.add("s");
  expected.add("g");
  Assertions.assertEquals(expected, actual);
}
 
Example #7
Source File: PrometheusPublisher.java    From servicecomb-java-chassis with Apache License 2.0 6 votes vote down vote up
@Override
public List<MetricFamilySamples> describe() {
  List<MetricFamilySamples> familySamples = new ArrayList<>();
  if (globalRegistry == null) {
    return familySamples;
  }

  List<Sample> samples = new ArrayList<>();
  for (Registry registry : globalRegistry.getRegistries()) {
    for (Meter meter : registry) {
      meter.measure().forEach(measurement -> {
        Sample sample = convertMeasurementToSample(measurement);
        samples.add(sample);
      });
    }
  }

  familySamples.add(new MetricFamilySamples("ServiceComb_Metrics", Type.UNTYPED, "ServiceComb Metrics", samples));

  return familySamples;
}
 
Example #8
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 #9
Source File: MetricsControllerTest.java    From spectator with Apache License 2.0 6 votes vote down vote up
@Test
public void testEncodeCombinedRegistry() {
  // Multiple occurrences of measurements in the same registry
  // (confirm these are handled within the registry itself).
  Measurement measureBXY2 = new Measurement(idBXY, 5, 5.5);
  Meter meterB2 = new TestMeter("ignoreB", measureBXY2);

  DefaultRegistry registry = new DefaultRegistry(clock);
  registry.register(meterB);
  registry.register(meterB2);

  List<TaggedDataPoints> expectedTaggedDataPoints = Arrays.asList(
     new TaggedDataPoints(
           Arrays.asList(new BasicTag("tagA", "X"),
                         new BasicTag("tagB", "Y")),
           Arrays.asList(new DataPoint(clock.wallTime(), 50.5 + 5.5))));

  HashMap<String, MetricValues> expect = new HashMap<>();
  expect.put("idB", new MetricValues("Counter", expectedTaggedDataPoints));

  PolledMeter.update(registry);
  Assertions.assertEquals(expect, controller.encodeRegistry(registry, allowAll));
}
 
Example #10
Source File: MetricsController.java    From spectator with Apache License 2.0 6 votes vote down vote up
/**
 * Determine the type of a meter for reporting purposes.
 *
 * @param registry
 *    Used to provide supplemental information (e.g. to search for the meter).
 *
 * @param meter
 *    The meters whose kind we want to know.
 *
 * @return
 *    A string such as "Counter". If the type cannot be identified as one of
 *    the standard Spectator api interface variants, then the simple class name
 *    is returned.
 */
public static String meterToKind(Registry registry, Meter meter) {
  String kind;
  if (meter instanceof Timer) {
    kind = "Timer";
  } else if (meter instanceof Counter) {
    kind = "Counter";
  } else if (meter instanceof Gauge) {
    kind = "Gauge";
  } else if (meter instanceof DistributionSummary) {
    kind = "DistributionSummary";
  } else {
    kind = meter.getClass().getSimpleName();
  }
  return kind;
}
 
Example #11
Source File: GlobalRegistry.java    From servicecomb-java-chassis with Apache License 2.0 6 votes vote down vote up
public PolledEvent poll(long secondInterval) {
  long msNow = clock.wallTime();
  List<Meter> meters = new ArrayList<>();
  List<Measurement> measurements = new ArrayList<>();
  for (Registry registry : registries) {
    SpectatorUtils.removeExpiredMeters(registry);

    for (Meter meter : registry) {
      if (meter instanceof PeriodMeter) {
        ((PeriodMeter) meter).calcMeasurements(msNow, secondInterval);
      }

      meters.add(meter);
      meter.measure().forEach(measurements::add);
    }
  }

  return new PolledEvent(meters, measurements);
}
 
Example #12
Source File: MetricDescriptorCache.java    From kork with Apache License 2.0 6 votes vote down vote up
/** Convert a Spectator Meter type into a Stackdriver Metric kind. */
public String meterToKind(Registry registry, Meter meter) {
  if (meter instanceof Counter) {
    return "CUMULATIVE";
  }

  if (registry.counters().anyMatch(m -> m.id().equals(meter.id()))) {
    return "CUMULATIVE";
  }

  if (meterIsTimer(registry, meter)) {
    return "CUMULATIVE";
  }

  return "GAUGE";
}
 
Example #13
Source File: AtlasRegistry.java    From spectator with Apache License 2.0 5 votes vote down vote up
/** Collect measurements from all of the meters in the registry. */
synchronized void pollMeters(long t) {
  publishTaskTimer("pollMeters").record(() -> {
    if (t > lastPollTimestamp) {
      MeasurementConsumer consumer = (id, timestamp, value) -> {
        // Update the map for data to go to the Atlas storage layer
        Consolidator consolidator = atlasMeasurements.get(id);
        if (consolidator == null) {
          int multiple = (int) (stepMillis / lwcStepMillis);
          consolidator = Consolidator.create(id, stepMillis, multiple);
          atlasMeasurements.put(id, consolidator);
        }
        consolidator.update(timestamp, value);

        // Update aggregators for streaming
        evaluator.update(id, timestamp, value);
      };
      logger.debug("collecting measurements for time: {}", t);
      publishTaskTimer("pollMeasurements").record(() -> {
        for (Meter meter : this) {
          ((AtlasMeter) meter).measure(consumer);
        }
      });
      lastPollTimestamp = t;
    }
  });
}
 
Example #14
Source File: PolledMeter.java    From spectator with Apache License 2.0 5 votes vote down vote up
/**
 * Provided for backwards compatibility to support the {@link Registry#register(Meter)}
 * method. Use the builder created with {@link #using(Registry)} instead.
 *
 * @deprecated This method only exists to allow for backwards compatibility and should
 * be considered an internal detail. Scheduled to be removed in 2.0.
 */
@Deprecated
public static void monitorMeter(Registry registry, Meter meter) {
  ConcurrentMap<Id, Object> state = registry.state();
  Object c = Utils.computeIfAbsent(state, meter.id(), MeterState::new);
  if (!(c instanceof MeterState)) {
    Utils.propagateTypeError(registry, meter.id(), MeterState.class, c.getClass());
  } else {
    MeterState t = (MeterState) c;
    t.add(meter);
    long delay = registry.config().gaugePollingFrequency().toMillis();
    t.schedule(registry, null, delay);
  }
}
 
Example #15
Source File: MicrometerRegistry.java    From spectator with Apache License 2.0 5 votes vote down vote up
@Override public Meter get(Id id) {
  try {
    return impl.get(id.name())
        .tags(convert(id.tags()))
        .meters()
        .stream()
        .filter(m -> id.equals(convert(m.getId())))
        .map(this::convert)
        .filter(Objects::nonNull)
        .findFirst()
        .orElse(null);
  } catch (MeterNotFoundException e) {
    return null;
  }
}
 
Example #16
Source File: MicrometerRegistry.java    From spectator with Apache License 2.0 5 votes vote down vote up
@Override public Iterator<Meter> iterator() {
  return impl.getMeters()
      .stream()
      .map(this::convert)
      .filter(Objects::nonNull)
      .iterator();
}
 
Example #17
Source File: MicrometerRegistry.java    From spectator with Apache License 2.0 5 votes vote down vote up
private Id convert(io.micrometer.core.instrument.Meter.Id id) {
  List<Tag> tags = id.getTags()
      .stream()
      .map(t -> Tag.of(t.getKey(), t.getValue()))
      .collect(Collectors.toList());
  return Id.create(id.getName()).withTags(tags);
}
 
Example #18
Source File: ThreadPoolMonitorTest.java    From spectator with Apache License 2.0 5 votes vote down vote up
private Meter getMeter(String meterName, String threadPoolName) {
  ThreadPoolMonitor.attach(registry, latchedExecutor, threadPoolName);
  PolledMeter.update(registry);
  final Id id = registry.createId(meterName).withTag(ThreadPoolMonitor.ID_TAG_NAME,
      (threadPoolName == null || threadPoolName.isEmpty()) ? ThreadPoolMonitor.DEFAULT_ID : threadPoolName);
  return registry.get(id);
}
 
Example #19
Source File: ThreadPoolMonitorTest.java    From spectator with Apache License 2.0 5 votes vote down vote up
private void checkIdTagValue(Meter meter, String expectedIdValue) {
  final Iterable<Measurement> measurements = meter.measure();
  final Iterator<Measurement> measurementIterator = measurements.iterator();
  Assertions.assertTrue(measurementIterator.hasNext());

  final Iterator<Tag> tags = measurementIterator.next().id().tags().iterator();
  Assertions.assertTrue(tags.hasNext());
  Tag tag = tags.next();
  Assertions.assertEquals(ThreadPoolMonitor.ID_TAG_NAME, tag.key());
  Assertions.assertEquals(expectedIdValue, tag.value());
}
 
Example #20
Source File: MetricsController.java    From spectator with Apache License 2.0 5 votes vote down vote up
/**
 * Internal API for encoding a registry that can be encoded as JSON.
 * This is a helper function for the REST endpoint and to test against.
 */
Map<String, MetricValues> encodeRegistry(
      Registry sourceRegistry, Predicate<Measurement> filter) {
  Map<String, MetricValues> metricMap = new HashMap<>();

  /*
   * Flatten the meter measurements into a map of measurements keyed by
   * the name and mapped to the different tag variants.
   */
  for (Meter meter : sourceRegistry) {
    String kind = knownMeterKinds.computeIfAbsent(
                       meter.id(), k -> meterToKind(sourceRegistry, meter));

    for (Measurement measurement : meter.measure()) {
      if (!filter.test(measurement)) {
        continue;
      }
      if (Double.isNaN(measurement.value())) {
        continue;
      }

      String meterName = measurement.id().name();
      MetricValues have = metricMap.get(meterName);
      if (have == null) {
        metricMap.put(meterName, new MetricValues(kind, measurement));
      } else {
        have.addMeasurement(measurement);
      }
    }
  }
  return metricMap;
}
 
Example #21
Source File: MetricsControllerTest.java    From spectator with Apache License 2.0 5 votes vote down vote up
@Test
public void testIgnoreNan() {
  Id id = idB.withTag("tagA", "Z");
  Measurement measure = new Measurement(id, 100, Double.NaN);
  Meter meter = new TestMeter("ignoreZ", measure);

  DefaultRegistry registry = new DefaultRegistry(clock);
  registry.register(meter);

  HashMap<String, MetricValues> expect = new HashMap<>();
  PolledMeter.update(registry);
  Assertions.assertEquals(expect, controller.encodeRegistry(registry, allowAll));
}
 
Example #22
Source File: MetricsControllerTest.java    From spectator with Apache License 2.0 5 votes vote down vote up
@Test
public void testEncodeCompositeRegistry() {
  // Multiple occurrences of measurements in the same registry
  // (confirm these are handled within the registry itself).
  // Here measurements are duplicated but meters have different sets.
  Measurement measureAXY2 = new Measurement(idAXY, 20, 20.20);
  Meter meterA2 = new TestMeter("ignoreA", measureAXY2);

  DefaultRegistry registry = new DefaultRegistry(clock);
  registry.register(meterA);
  registry.register(meterA2);

  List<TaggedDataPoints> expectedDataPoints = Arrays.asList(
      new TaggedDataPoints(
          Arrays.asList(new BasicTag("tagA", "X"), new BasicTag("tagZ", "Z")),
          Arrays.asList(new DataPoint(clock.wallTime(), 13.13))),
      new TaggedDataPoints(
          Arrays.asList(new BasicTag("tagA", "X"), new BasicTag("tagB", "Y")),
          Arrays.asList(new DataPoint(clock.wallTime(), 11.11 + 20.20))),
      new TaggedDataPoints(
          Arrays.asList(new BasicTag("tagA", "Y"), new BasicTag("tagB", "X")),
          Arrays.asList(new DataPoint(clock.wallTime(), 12.12)))
  );

  HashMap<String, MetricValues> expect = new HashMap<>();
  expect.put("idA", new MetricValues("Counter", expectedDataPoints));

  PolledMeter.update(registry);
  Assertions.assertEquals(expect, controller.encodeRegistry(registry, allowAll));
}
 
Example #23
Source File: SpectatorReporterTest.java    From spectator with Apache License 2.0 5 votes vote down vote up
private long getValue(String name) {
  Meter meter = registry.get(registry.createId(name));
  if (meter != null) {
    for (Measurement m : meter.measure()) {
      return (long) m.value();
    }
  }
  return Long.MAX_VALUE;
}
 
Example #24
Source File: ServoRegistryTest.java    From spectator with Apache License 2.0 5 votes vote down vote up
@Test
public void iteratorDoesNotContainNullMeters() {
  Registry dflt = Servo.newRegistry();

  boolean found = false;
  Counter counter = dflt.counter("servo.testCounter");
  for (Meter m : dflt) {
    found = m.id().equals(counter.id());
  }
  Assertions.assertTrue(found, "id could not be found in iterator");
}
 
Example #25
Source File: ServoRegistryTest.java    From spectator with Apache License 2.0 5 votes vote down vote up
public void globalIterator(Function<Registry, Meter> createMeter) {
  Registry dflt = Servo.newRegistry();
  CompositeRegistry global = Spectator.globalRegistry();
  global.removeAll();
  global.add(dflt);

  boolean found = false;
  Id expected = createMeter.apply(dflt).id();
  for (Meter m : global) {
    found |= m.id().equals(expected);
  }
  Assertions.assertTrue(found, "id for sub-registry could not be found in global iterator");
}
 
Example #26
Source File: MicrometerRegistry.java    From spectator with Apache License 2.0 5 votes vote down vote up
private Meter convert(io.micrometer.core.instrument.Meter meter) {
  Id id = convert(meter.getId());
  if (meter instanceof io.micrometer.core.instrument.Counter) {
    return counter(id);
  } else if (meter instanceof io.micrometer.core.instrument.Timer) {
    return timer(id);
  } else if (meter instanceof io.micrometer.core.instrument.DistributionSummary) {
    return distributionSummary(id);
  } else if (meter instanceof io.micrometer.core.instrument.Gauge) {
    return gauge(id);
  } else {
    return null;
  }
}
 
Example #27
Source File: PublishModelFactory.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
protected MeasurementTree createMeasurementTree(List<Meter> meters) {
  MeasurementGroupConfig groupConfig = createMeasurementGroupConfig();

  MeasurementTree tree = new MeasurementTree();
  tree.from(meters.iterator(), groupConfig);
  return tree;
}
 
Example #28
Source File: TestVertxMetersInitializer.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
private void testLog(LogCollector logCollector, List<Meter> meters, List<Measurement> measurements,
    boolean printDetail) {
  ArchaiusUtils.setProperty(DefaultLogPublisher.ENDPOINTS_CLIENT_DETAIL_ENABLED, String.valueOf(printDetail));
  logPublisher.onPolledEvent(new PolledEvent(meters, measurements));

  StringBuilder sb = new StringBuilder();
  logCollector.getEvents().forEach(event -> sb.append(event.getMessage()).append("\n"));
  String actual = sb.toString();
  int idx = actual.indexOf("vertx:\n");
  actual = actual.substring(idx);

  String expect = "vertx:\n"
      + "  instances:\n"
      + "    name       eventLoopContext-created\n"
      + "    registry   0\n"
      + "    registry-watch 0\n"
      + "    transport  0\n"
      + "  transport:\n"
      + "    client.endpoints:\n"
      + "      connectCount disconnectCount queue         connections send(Bps) receive(Bps) remote\n";
  if (printDetail) {
    expect += String.format(
        "      1            0               0             1           4         21           127.0.0.1:%-5s\n",
        port);
  }
  expect += ""
      + "      1            0               0             1           4         21           (summary)\n"
      + "    server.endpoints:\n"
      + "      connectCount disconnectCount rejectByLimit connections send(Bps) receive(Bps) listen\n"
      + "      1            0               0             1           21        4            0.0.0.0:0\n"
      + "      1            0               0             1           21        4            (summary)\n\n";
  Assert.assertEquals(expect, actual);
}
 
Example #29
Source File: MetricDescriptorCache.java    From kork with Apache License 2.0 5 votes vote down vote up
/** Determine if meter is a Timer or not. */
public boolean meterIsTimer(Registry registry, Meter meter) {
  return idToTimer.computeIfAbsent(
      meter.id(),
      k -> {
        try {
          return registry.timers().anyMatch(m -> m.id().equals(meter.id()));
        } catch (ArrayIndexOutOfBoundsException aoex) {
          // !!! 20160929
          // !!! I dont know if this is a bug or what
          // !!! but the tests all get an array out of bounds calling stream()
          return meter instanceof Timer;
        }
      });
}
 
Example #30
Source File: MetricDescriptorCache.java    From kork with Apache License 2.0 5 votes vote down vote up
/** Determine the Stackdriver Custom Metric Desctiptor Kind to use. */
public String descriptorTypeToKind(String descriptorType, Registry registry, Meter meter) {
  return typeToKind.computeIfAbsent(
      descriptorType,
      k -> {
        return meterToKind(registry, meter);
      });
}