Java Code Examples for com.netflix.spectator.api.patterns.PolledMeter#update()

The following examples show how to use com.netflix.spectator.api.patterns.PolledMeter#update() . 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: 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 2
Source File: SpectatorMetricRegistryTest.java    From concurrency-limits with Apache License 2.0 5 votes vote down vote up
@Test
public void testGuage() {
    DefaultRegistry registry = new DefaultRegistry();
    SpectatorMetricRegistry metricRegistry = new SpectatorMetricRegistry(registry, registry.createId("foo"));
    
    metricRegistry.gauge("bar", () -> 10);
    
    PolledMeter.update(registry);
    
    Assert.assertEquals(10.0, registry.gauge(registry.createId("foo.bar")).value(), 0);
}
 
Example 3
Source File: TestThreadPoolPublishModelFactory.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
@Test
public void createDefaultPublishModel() throws JsonProcessingException {
  new Expectations() {
    {
      threadPoolExecutor.getQueue();
      result = queue;
      queue.size();
      result = 10d;
    }
  };
  new MockUp<ScheduledThreadPoolExecutor>() {
    @Mock
    void delayedExecute(RunnableScheduledFuture<?> task) {

    }
  };
  try {
    ThreadPoolMonitor.attach(registry, threadPoolExecutor, "test");

    PolledMeter.update(registry);
    PublishModelFactory factory = new PublishModelFactory(Lists.newArrayList(registry.iterator()));
    DefaultPublishModel model = factory.createDefaultPublishModel();

    Assert.assertEquals(
        "{\"test\":{\"avgTaskCount\":0.0,\"avgCompletedTaskCount\":0.0,\"currentThreadsBusy\":0,\"maxThreads\":0,\"poolSize\":0,\"corePoolSize\":0,\"queueSize\":10,\"rejected\":\"NaN\"}}",
        JsonUtils.writeValueAsString(model.getThreadPools()));
  } catch (Throwable e) {
    e.printStackTrace();
    Assert.fail("unexpected error happen. " + e.getMessage());
  }
}
 
Example 4
Source File: MultiNodeClusterMemberResolverMetrics.java    From titus-control-plane with Apache License 2.0 5 votes vote down vote up
@Override
public void close() {
    closed = true;
    PolledMeter.update(registry);

    PolledMeter.remove(registry, connectionHealthId);
    PolledMeter.remove(registry, connectionTimeId);
}
 
Example 5
Source File: MultiNodeClusterMemberResolverMetrics.java    From titus-control-plane with Apache License 2.0 5 votes vote down vote up
@Override
public void close() {
    closed = true;
    PolledMeter.update(registry);

    PolledMeter.remove(registry, memberTimeId);
}
 
Example 6
Source File: MultiNodeClusterMemberResolverMetrics.java    From titus-control-plane with Apache License 2.0 5 votes vote down vote up
@Override
public void close() {
    closed = true;
    PolledMeter.update(registry);

    PolledMeter.remove(registry, leaderTimeId);
}
 
Example 7
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 8
Source File: RegistryTest.java    From spectator with Apache License 2.0 5 votes vote down vote up
private void assertLongTaskTimer(Registry r, Id id, long timestamp, int activeTasks, double duration) {
  PolledMeter.update(r);

  Gauge g = r.gauge(id.withTag(Statistic.activeTasks));
  Assertions.assertEquals(timestamp, g.measure().iterator().next().timestamp());
  Assertions.assertEquals(activeTasks, g.value(), 1.0e-12);

  g = r.gauge(id.withTag(Statistic.duration));
  Assertions.assertEquals(timestamp, g.measure().iterator().next().timestamp());
  Assertions.assertEquals(duration, g.value(), 1.0e-12);
}
 
Example 9
Source File: RegistryTest.java    From spectator with Apache License 2.0 5 votes vote down vote up
@Test
public void defaultResetRemovesState() {
  DefaultRegistry r = newRegistry(true, 10000);
  AtomicInteger v = new AtomicInteger();
  PolledMeter.using(r).withName("test").monitorValue(v);
  PolledMeter.update(r);
  Assertions.assertEquals(1, r.stream().count());
  Assertions.assertEquals(1, r.state().size());
  r.reset();
  PolledMeter.update(r);
  Assertions.assertEquals(0, r.stream().count());
  Assertions.assertEquals(0, r.state().size());
}
 
Example 10
Source File: CompositeRegistryTest.java    From spectator with Apache License 2.0 5 votes vote down vote up
@Test
public void testRegister() {
  Registry r = newRegistry(5, true);
  Counter c = new DefaultCounter(clock, r.createId("foo"));
  r.register(c);
  c.increment();
  Assertions.assertEquals(c.count(), 1L);
  r.register(c);
  PolledMeter.update(r);
  Meter meter = r.get(c.id());
  for (Measurement m : meter.measure()) {
    Assertions.assertEquals(m.value(), 2.0, 1e-12);
  }
}
 
Example 11
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 12
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 13
Source File: AtlasMeterRegistryTest.java    From micrometer with Apache License 2.0 4 votes vote down vote up
@Issue("#2094")
@Test
void functionCounter() {
    AtomicLong count = new AtomicLong();

    MockClock clock = new MockClock();
    AtlasMeterRegistry registry = new AtlasMeterRegistry(new AtlasConfig() {
        @Nullable
        @Override
        public String get(String k) {
            return null;
        }

        @Override
        public Duration step() {
            return Duration.ofMinutes(1);
        }

        @Override
        public Duration lwcStep() {
            return step();
        }
    }, clock);
    FunctionCounter.builder("test", count, AtomicLong::doubleValue).register(registry);

    Supplier<Double> valueSupplier = () -> {
        AtlasRegistry r = (AtlasRegistry) registry.getSpectatorRegistry();
        PolledMeter.update(r);
        clock.add(Duration.ofMinutes(1));
        return r.measurements()
                .filter(m -> m.id().name().equals("test"))
                .findFirst()
                .map(Measurement::value)
                .orElse(Double.NaN);
    };

    count.addAndGet(60);
    assertThat(valueSupplier.get()).isEqualTo(1.0);

    count.addAndGet(120);
    assertThat(valueSupplier.get()).isEqualTo(2.0);

    count.addAndGet(90);
    assertThat(valueSupplier.get()).isEqualTo(1.5);
}
 
Example 14
Source File: MetricsRegistryTest.java    From spectator with Apache License 2.0 4 votes vote down vote up
private void assertGaugeValue(
    MetricsRegistry r, MetricRegistry codaRegistry, String name, double expected) {
  PolledMeter.update(r);
  Assertions.assertEquals(expected, (Double) codaRegistry.getGauges().get(name).getValue(), 1e-12);
}
 
Example 15
Source File: RegistryTest.java    From spectator with Apache License 2.0 4 votes vote down vote up
private void assertGaugeValue(Registry r, Id id, double expected) {
  PolledMeter.update(r);
  Assertions.assertEquals(expected, r.gauge(id).value(), 1e-12);
}
 
Example 16
Source File: SpectatorMetricRegistryTest.java    From concurrency-limits with Apache License 2.0 3 votes vote down vote up
@Test
public void testUnregister() {
    DefaultRegistry registry = new DefaultRegistry();
    SpectatorMetricRegistry metricRegistry = new SpectatorMetricRegistry(registry, registry.createId("foo"));
    
    metricRegistry.gauge("bar", () -> 10);
    metricRegistry.gauge("bar", () -> 20);
    
    PolledMeter.update(registry);
    
    Assert.assertEquals(20.0, registry.gauge(registry.createId("foo.bar")).value(), 0);
    
}