Java Code Examples for com.netflix.spectator.api.Gauge#set()

The following examples show how to use com.netflix.spectator.api.Gauge#set() . 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: DefaultPlaceholderGaugeTest.java    From spectator with Apache License 2.0 6 votes vote down vote up
@Test
public void testIncrement() {
  String[] tagValue = new String[] { "default" };
  Gauge g = factory.gauge(factory.createId("testIncrement",
      Collections.singleton(new TestTagFactory(tagValue))));
  Assertions.assertEquals(Double.NaN, g.value(), 1e-12);
  Assertions.assertEquals("testIncrement:tag=default", g.id().toString());
  g.set(1);
  Assertions.assertEquals(1.0, g.value(), 1e-12);
  g.set(3);
  Assertions.assertEquals(3.0, g.value(), 1e-12);

  tagValue[0] = "value2";
  Assertions.assertEquals("testIncrement:tag=value2", g.id().toString());
  g.set(1);
  Assertions.assertEquals(1.0, g.value(), 1e-12);

  tagValue[0] = "default";
  Assertions.assertEquals("testIncrement:tag=default", g.id().toString());
  g.set(4);
  Assertions.assertEquals(4.0, g.value(), 1e-12);
}
 
Example 2
Source File: JobAndTaskMetrics.java    From titus-control-plane with Apache License 2.0 5 votes vote down vote up
private List<Gauge> updateStateCounters(Id baseId, String state, Histogram.Builder histogramBuilder, List<Gauge> gauges) {
    if (histogramBuilder == null) {
        // Nothing running for this state, reset gauges
        if (gauges != null) {
            gauges.forEach(g -> g.set(0));
        }
        return Collections.emptyList();
    }

    List<Long> counters = histogramBuilder.build().getCounters();

    // First time we have data for this capacity group.
    if (gauges == null) {
        Id id = baseId.withTag("state", state);
        List<Long> valueBounds = HISTOGRAM_DESCRIPTOR.getValueBounds();
        List<Gauge> newGauges = new ArrayList<>();
        for (int i = 0; i <= valueBounds.size(); i++) {
            Gauge newGauge;
            if (i < valueBounds.size()) {
                long delayMs = valueBounds.get(i);
                newGauge = registry.gauge(id.withTag("delay", DateTimeExt.toTimeUnitString(delayMs)));
            } else {
                newGauge = registry.gauge(id.withTag("delay", "Unlimited"));
            }
            newGauge.set(counters.get(i));
            newGauges.add(newGauge);
        }
        return newGauges;
    }

    // Update gauges
    for (int i = 0; i < counters.size(); i++) {
        gauges.get(i).set(counters.get(i));
    }

    return gauges;
}
 
Example 3
Source File: MicrometerRegistryTest.java    From spectator with Apache License 2.0 5 votes vote down vote up
@Test
public void gaugeSet() {
  Gauge g = registry.gauge("foo");
  Assertions.assertTrue(Double.isNaN(g.value()));
  g.set(42.0);
  Assertions.assertEquals(42.0, g.value(), 1e-12);
  g.set(20.0);
  Assertions.assertEquals(20.0, g.value(), 1e-12);
}
 
Example 4
Source File: MicrometerRegistryTest.java    From spectator with Apache License 2.0 5 votes vote down vote up
@Test
public void gaugeMeasure() {
  Gauge g = registry.gauge("foo");
  g.set(42.0);
  int i = 0;
  for (Measurement m : g.measure()) {
    ++i;
    Assertions.assertEquals("foo", m.id().name());
    Assertions.assertEquals(42.0, m.value(), 1e-12);
  }
  Assertions.assertEquals(1, i);
}
 
Example 5
Source File: MicrometerRegistryTest.java    From spectator with Apache License 2.0 5 votes vote down vote up
@Test
public void gaugeGet() {
  Gauge g1 = registry.gauge("foo");
  g1.set(1.0);

  Gauge g2 = (Gauge) registry.get(registry.createId("foo"));
  Assertions.assertEquals(1.0, g2.value(), 1e-12);
}
 
Example 6
Source File: MicrometerRegistryTest.java    From spectator with Apache License 2.0 5 votes vote down vote up
@Test
public void maxGaugeSet() {
  Gauge g = registry.maxGauge("foo");
  Assertions.assertTrue(Double.isNaN(g.value()));
  g.set(42.0);
  g.set(20.0);
  clock.addSeconds(60);
  Assertions.assertEquals(42.0, g.value(), 1e-12);
}
 
Example 7
Source File: DefaultPlaceholderGaugeTest.java    From spectator with Apache License 2.0 5 votes vote down vote up
@Test
public void testIncrementAmount() {
  String[] tagValue = new String[] { "default" };
  Gauge g = factory.gauge(factory.createId("testIncrementAmount",
      Collections.singleton(new TestTagFactory(tagValue))));

  g.set(42);
  Assertions.assertEquals(42.0, g.value(), 1e-12);

  tagValue[0] = "value2";
  g.set(54);
  Assertions.assertEquals(54.0, g.value(), 1e-12);
}
 
Example 8
Source File: DefaultPlaceholderGaugeTest.java    From spectator with Apache License 2.0 5 votes vote down vote up
private void doMeasurementTest(Gauge g, int expectedValue, long expectedTime) {
  g.set(expectedValue);
  clock.setWallTime(expectedTime);
  List<Measurement> measurements = Utils.toList(g.measure());

  Assertions.assertEquals(1, measurements.size());

  Measurement m = measurements.get(0);
  Assertions.assertEquals(g.id(), m.id());
  Assertions.assertEquals(expectedTime, m.timestamp());
  Assertions.assertEquals(expectedValue, m.value(), 0.1e-12);
}
 
Example 9
Source File: ServoGaugeTest.java    From spectator with Apache License 2.0 5 votes vote down vote up
@Test
public void testInit() {
  Gauge g = newGauge("foo");
  Assertions.assertEquals(g.value(), Double.NaN, 1e-12);
  g.set(1.0);
  Assertions.assertEquals(g.value(), 1.0, 1e-12);
}
 
Example 10
Source File: ServoGaugeTest.java    From spectator with Apache License 2.0 5 votes vote down vote up
@Test
public void testGet() {
  final ServoRegistry r = Servo.newRegistry(clock);
  Gauge g = r.gauge(r.createId("foo"));
  g.set(1.0);

  Assertions.assertEquals(1, r.getMonitors().size());
  Assertions.assertEquals(1.0, (Double) r.getMonitors().get(0).getValue(0), 1e-12);
}
 
Example 11
Source File: ServoGaugeTest.java    From spectator with Apache License 2.0 5 votes vote down vote up
@Test
public void expiration() {
  final long initTime = TimeUnit.MINUTES.toMillis(30);
  final long fifteenMinutes = TimeUnit.MINUTES.toMillis(15);

  // Not expired on init, wait for activity to mark as active
  clock.setWallTime(initTime);
  Gauge g = newGauge("foo");
  Assertions.assertFalse(g.hasExpired());
  g.set(42.0);
  Assertions.assertFalse(g.hasExpired());
  Assertions.assertEquals(g.value(), 42.0, 1e-12);

  // Expires with inactivity
  clock.setWallTime(initTime + fifteenMinutes);
  Assertions.assertFalse(g.hasExpired());

  // Expires with inactivity
  clock.setWallTime(initTime + fifteenMinutes + 1);
  Assertions.assertEquals(g.value(), Double.NaN, 1e-12);
  Assertions.assertTrue(g.hasExpired());

  // Activity brings it back
  g.set(1.0);
  Assertions.assertEquals(g.value(), 1.0, 1e-12);
  Assertions.assertFalse(g.hasExpired());
}
 
Example 12
Source File: ServoGaugeTest.java    From spectator with Apache License 2.0 5 votes vote down vote up
@Test
public void hasGaugeType() {
  final ServoRegistry r = Servo.newRegistry(clock);
  Gauge g = r.gauge(r.createId("foo"));
  g.set(1.0);

  Map<String, String> tags = r.getMonitors().get(0).getConfig().getTags().asMap();
  Assertions.assertEquals("GAUGE", tags.get("type"));
}
 
Example 13
Source File: ServoGaugeTest.java    From spectator with Apache License 2.0 5 votes vote down vote up
@Test
public void measure() {
  final ServoRegistry r = Servo.newRegistry(clock);
  Gauge g = r.gauge(r.createId("foo"));
  g.set(1.0);

  Iterator<Measurement> ms = g.measure().iterator();
  Assertions.assertTrue(ms.hasNext());
  Measurement m = ms.next();
  Assertions.assertFalse(ms.hasNext());
  Assertions.assertEquals("foo", m.id().name());
  Assertions.assertEquals(1.0, 1.0, 1e-12);
}
 
Example 14
Source File: BalancedBucketManager.java    From titus-control-plane with Apache License 2.0 4 votes vote down vote up
private void updateBucketMetrics(int bucket, int bucketSize) {
    Gauge bucketSizeGauge = registry.gauge(bucketSizeId.withTag("bucket", Integer.toString(bucket)));
    bucketSizeGauge.set(bucketSize);
}