Java Code Examples for com.codahale.metrics.MetricRegistry#getGauges()

The following examples show how to use com.codahale.metrics.MetricRegistry#getGauges() . 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: BoundedBlockingRecordQueueTest.java    From incubator-gobblin with Apache License 2.0 6 votes vote down vote up
@Test(dependsOnMethods = "testQueueStats")
public void testRegisterAll() {
  MetricRegistry metricRegistry = new MetricRegistry();
  this.boundedBlockingRecordQueue.stats().get().registerAll(metricRegistry, METRIC_NAME_PREFIX);
  @SuppressWarnings("rawtypes")
  Map<String, Gauge> gauges = metricRegistry.getGauges();
  Assert.assertEquals(gauges.size(), 2);
  Assert.assertEquals(gauges
      .get(MetricRegistry.name(METRIC_NAME_PREFIX, BoundedBlockingRecordQueue.QueueStats.QUEUE_SIZE)).getValue(), 2);
  Assert.assertEquals(gauges
      .get(MetricRegistry.name(METRIC_NAME_PREFIX, BoundedBlockingRecordQueue.QueueStats.FILL_RATIO)).getValue(), 1d);
  Assert.assertEquals(metricRegistry.getMeters().size(), 2);
  Assert.assertEquals(metricRegistry
      .meter(MetricRegistry.name(METRIC_NAME_PREFIX, BoundedBlockingRecordQueue.QueueStats.GET_ATTEMPT_RATE))
      .getCount(), 7);
  Assert.assertEquals(metricRegistry
      .meter(MetricRegistry.name(METRIC_NAME_PREFIX, BoundedBlockingRecordQueue.QueueStats.PUT_ATTEMPT_RATE))
      .getCount(), 8);
}
 
Example 2
Source File: TestMetricAggregationProcessor.java    From datacollector with Apache License 2.0 5 votes vote down vote up
@Test
public void testEvaluateDataRuleRecord() throws StageException, InterruptedException {
  Record dataRuleChangeRecord = AggregatorUtil.createDataRuleChangeRecord(
    TestHelper.createTestDataRuleDefinition("a < b", true, System.currentTimeMillis())
  );
  Thread.sleep(2); // Sleep for 2 seconds that the data rule records get a later timestamp
  List<Record> testDataRuleRecords = TestHelper.createTestDataRuleRecords();
  List<Record> batch = new ArrayList<>();
  batch.add(dataRuleChangeRecord);
  batch.addAll(testDataRuleRecords);
  runner.runProcess(batch);
  MetricRegistry metrics = metricAggregationProcessor.getMetrics();

  SortedMap<String, Counter> counters = metrics.getCounters();
  Assert.assertTrue(counters.containsKey("user.x.matched.counter"));
  Assert.assertEquals(400, counters.get("user.x.matched.counter").getCount());

  Assert.assertTrue(counters.containsKey("user.x.evaluated.counter"));
  Assert.assertEquals(2000, counters.get("user.x.evaluated.counter").getCount());

  // Alert expected as threshold type is count and threshold value is 100
  SortedMap<String, Gauge> gauges = metrics.getGauges();
  Assert.assertTrue(gauges.containsKey("alert.x.gauge"));
  Map<String, Object> gaugeValue = (Map<String, Object>) gauges.get("alert.x.gauge").getValue();
  Assert.assertEquals(400L, gaugeValue.get("currentValue"));

  List<String> alertTexts = (List<String>) gaugeValue.get("alertTexts");
  Assert.assertEquals(1, alertTexts.size());
  Assert.assertEquals("Alert!!", alertTexts.get(0));
}
 
Example 3
Source File: Transformers.java    From ache with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("rawtypes")
public static String parse(Object result){
    MetricRegistry registry = (MetricRegistry) result;
    StringBuilder sb = new StringBuilder();

    Map<String, Counter> counters = registry.getCounters();
    for(Map.Entry<String, Counter> c : counters.entrySet()){
        sb.append(c.getKey().replace(".","_")+" "+c.getValue().getCount()+"\n");
    }

    Map<String, Timer> timers = registry.getTimers();
    for(Map.Entry<String, Timer> t : timers.entrySet()){
        sb.append(t.getKey().replace(".","_")+" "+t.getValue().getCount()+"\n");
    }

    Map<String, Gauge> gauges = registry.getGauges();
    for(Map.Entry<String, Gauge> g : gauges.entrySet()){
        sb.append(g.getKey().replace(".","_")+" "+g.getValue().getValue()+"\n");
    }

    Map<String, Histogram> histograms = registry.getHistograms();
    for(Map.Entry<String, Histogram> h : histograms.entrySet()){
        sb.append(h.getKey().replace(".","_")+" "+h.getValue().getCount()+"\n");
    }

    return sb.toString();
}
 
Example 4
Source File: AdaptiveOperationTrackerTest.java    From ambry with Apache License 2.0 5 votes vote down vote up
/**
 * Test that {@link NonBlockingRouterMetrics} can correctly register custom percentiles. An example of metric name is:
 * "com.github.ambry.router.GetOperation.LocalColoLatencyMs.91.0.thPercentile"
 */
@Test
public void customPercentilesMetricsRegistryTest() {
  // test that if custom percentile is not set, no corresponding metrics would be generated.
  MetricRegistry metricRegistry = routerMetrics.getMetricRegistry();
  MetricFilter filter = new MetricFilter() {
    @Override
    public boolean matches(String name, Metric metric) {
      return name.endsWith("thPercentile");
    }
  };
  SortedMap<String, Gauge> gauges = metricRegistry.getGauges(filter);
  assertTrue("No gauges should be created because custom percentile is not set", gauges.isEmpty());
  // test that dedicated gauges are correctly created for custom percentiles.
  String customPercentiles = "0.91,0.97";
  RouterConfig routerConfig = createRouterConfig(false, 1, 1, 6, customPercentiles, true);
  String[] percentileArray = customPercentiles.split(",");
  Arrays.sort(percentileArray);
  List<String> sortedPercentiles = Arrays.stream(percentileArray)
      .map(p -> String.valueOf(Double.parseDouble(p) * 100))
      .collect(Collectors.toList());
  routerMetrics = new NonBlockingRouterMetrics(mockClusterMap, routerConfig);
  gauges = routerMetrics.getMetricRegistry().getGauges(filter);
  // Note that each percentile creates 4 metrics (GetBlobInfo/GetBlob joins LocalColo/CrossColo). So, the total number of
  // metrics should equal to 4 * (# of given custom percentiles)
  assertEquals("The number of custom percentile gauge doesn't match", sortedPercentiles.size() * 4, gauges.size());
  Iterator mapItor = gauges.keySet().iterator();
  Iterator<String> listItor = sortedPercentiles.iterator();
  while (listItor.hasNext()) {
    String gaugeName = (String) mapItor.next();
    String percentileStr = listItor.next();
    assertTrue("The gauge name doesn't match", gaugeName.endsWith(percentileStr + ".thPercentile"));
  }
  // reset router metrics to clean up registered custom percentile metrics
  routerMetrics = new NonBlockingRouterMetrics(mockClusterMap, defaultRouterConfig);
}
 
Example 5
Source File: StateTransitionMetricsTest.java    From resilience4j with Apache License 2.0 4 votes vote down vote up
private static void circuitBreakerMetricsUsesFirstStateObjectInstance(
    CircuitBreaker circuitBreaker, MetricRegistry metricRegistry) throws Exception {
    SortedMap<String, Gauge> gauges = metricRegistry.getGauges();

    assertThat(circuitBreaker.getState(), equalTo(CircuitBreaker.State.CLOSED));
    assertThat(circuitBreaker.getMetrics().getNumberOfBufferedCalls(), equalTo(0));
    assertThat(circuitBreaker.getMetrics().getNumberOfFailedCalls(), equalTo(0));
    assertThat(circuitBreaker.getMetrics().getNumberOfSuccessfulCalls(), equalTo(0));
    assertThat(gauges.get("resilience4j.circuitbreaker.test.state").getValue(), equalTo(0));
    assertThat(gauges.get("resilience4j.circuitbreaker.test.buffered").getValue(), equalTo(0));
    assertThat(gauges.get("resilience4j.circuitbreaker.test.failed").getValue(), equalTo(0));
    assertThat(gauges.get("resilience4j.circuitbreaker.test.successful").getValue(),
        equalTo(0));

    circuitBreaker.onError(0, TimeUnit.NANOSECONDS, new RuntimeException());

    assertThat(circuitBreaker.getState(), equalTo(CircuitBreaker.State.CLOSED));
    assertThat(circuitBreaker.getMetrics().getNumberOfBufferedCalls(), equalTo(1));
    assertThat(circuitBreaker.getMetrics().getNumberOfFailedCalls(), equalTo(1));
    assertThat(circuitBreaker.getMetrics().getNumberOfSuccessfulCalls(), equalTo(0));
    assertThat(gauges.get("resilience4j.circuitbreaker.test.state").getValue(), equalTo(0));
    assertThat(gauges.get("resilience4j.circuitbreaker.test.buffered").getValue(), equalTo(1));
    assertThat(gauges.get("resilience4j.circuitbreaker.test.failed").getValue(), equalTo(1));
    assertThat(gauges.get("resilience4j.circuitbreaker.test.successful").getValue(),
        equalTo(0));

    for (int i = 0; i < 9; i++) {
        circuitBreaker.onError(0, TimeUnit.NANOSECONDS, new RuntimeException());
    }

    assertThat(circuitBreaker.getState(), equalTo(CircuitBreaker.State.OPEN));
    assertThat(circuitBreaker.getMetrics().getNumberOfBufferedCalls(), equalTo(10));
    assertThat(circuitBreaker.getMetrics().getNumberOfFailedCalls(), equalTo(10));
    assertThat(circuitBreaker.getMetrics().getNumberOfSuccessfulCalls(), equalTo(0));
    assertThat(gauges.get("resilience4j.circuitbreaker.test.state").getValue(), equalTo(1));
    assertThat(gauges.get("resilience4j.circuitbreaker.test.buffered").getValue(), equalTo(10));
    assertThat(gauges.get("resilience4j.circuitbreaker.test.failed").getValue(), equalTo(10));
    assertThat(gauges.get("resilience4j.circuitbreaker.test.successful").getValue(),
        equalTo(0));

    await().atMost(1500, TimeUnit.MILLISECONDS)
        .until(() -> {
            circuitBreaker.tryAcquirePermission();
            return circuitBreaker.getState().equals(CircuitBreaker.State.HALF_OPEN);
        });

    circuitBreaker.onSuccess(0, TimeUnit.NANOSECONDS);
    assertThat(circuitBreaker.getState(), equalTo(CircuitBreaker.State.HALF_OPEN));
    assertThat(circuitBreaker.getMetrics().getNumberOfBufferedCalls(), equalTo(1));
    assertThat(circuitBreaker.getMetrics().getNumberOfFailedCalls(), equalTo(0));
    assertThat(circuitBreaker.getMetrics().getNumberOfSuccessfulCalls(), equalTo(1));

    assertThat(gauges.get("resilience4j.circuitbreaker.test.state").getValue(), equalTo(2));
    assertThat(gauges.get("resilience4j.circuitbreaker.test.buffered").getValue(), equalTo(1));
    assertThat(gauges.get("resilience4j.circuitbreaker.test.failed").getValue(), equalTo(0));
    assertThat(gauges.get("resilience4j.circuitbreaker.test.successful").getValue(),
        equalTo(1));
    circuitBreaker.onSuccess(0, TimeUnit.NANOSECONDS);
    assertThat(circuitBreaker.getState(), equalTo(CircuitBreaker.State.HALF_OPEN));
    assertThat(circuitBreaker.getMetrics().getNumberOfBufferedCalls(), equalTo(2));
    assertThat(circuitBreaker.getMetrics().getNumberOfFailedCalls(), equalTo(0));
    assertThat(circuitBreaker.getMetrics().getNumberOfSuccessfulCalls(), equalTo(2));

    assertThat(gauges.get("resilience4j.circuitbreaker.test.state").getValue(), equalTo(2));
    assertThat(gauges.get("resilience4j.circuitbreaker.test.buffered").getValue(), equalTo(2));
    assertThat(gauges.get("resilience4j.circuitbreaker.test.failed").getValue(), equalTo(0));
    assertThat(gauges.get("resilience4j.circuitbreaker.test.successful").getValue(),
        equalTo(2));
    circuitBreaker.onSuccess(0, TimeUnit.NANOSECONDS);
    assertThat(circuitBreaker.getState(), equalTo(CircuitBreaker.State.CLOSED));
    assertThat(circuitBreaker.getMetrics().getNumberOfBufferedCalls(), equalTo(0));
    assertThat(circuitBreaker.getMetrics().getNumberOfFailedCalls(), equalTo(0));
    assertThat(circuitBreaker.getMetrics().getNumberOfSuccessfulCalls(), equalTo(0));

    assertThat(gauges.get("resilience4j.circuitbreaker.test.state").getValue(), equalTo(0));
    assertThat(gauges.get("resilience4j.circuitbreaker.test.buffered").getValue(), equalTo(0));
    assertThat(gauges.get("resilience4j.circuitbreaker.test.failed").getValue(), equalTo(0));
    assertThat(gauges.get("resilience4j.circuitbreaker.test.successful").getValue(),
        equalTo(0));
}
 
Example 6
Source File: TestMetricAggregationProcessor.java    From datacollector with Apache License 2.0 4 votes vote down vote up
@Test
public void testMetricEvaluation() throws StageException {

  MetricsRuleDefinition metricsRuleDefinition1 = new MetricsRuleDefinition(
    "badRecordsAlertID",
    "High incidence of Bad Records",
    "pipeline.batchErrorRecords.meter",
    MetricType.METER,
    MetricElement.METER_COUNT,
    "${value() > 400}",
    false,
    true,
    System.currentTimeMillis()
  );

  MetricsRuleDefinition metricsRuleDefinition2 = new MetricsRuleDefinition(
    "testMetricAggregation1458001548262",
    "Field Hasher Drops Records",
    "stage.com_streamsets_pipeline_stage_processor_fieldhasher_FieldHasherDProcessor_1.outputRecords.meter",
    MetricType.METER,
    MetricElement.METER_COUNT,
    "${value() < 1800}",
    false,
    true,
    System.currentTimeMillis()
  );

  Record metricRuleChangeRecord1 = AggregatorUtil.createMetricRuleChangeRecord(metricsRuleDefinition1);
  Record metricRuleChangeRecord2 = AggregatorUtil.createMetricRuleChangeRecord(metricsRuleDefinition2);
  Record metricRecord1 = createTestMetricRecord();
  Record metricRecord2 = createTestMetricRecord();
  runner.runProcess(Arrays.asList(metricRuleChangeRecord1, metricRuleChangeRecord2, metricRecord1, metricRecord2));

  // look for alert gauges
  MetricRegistry metrics = metricAggregationProcessor.getMetrics();
  SortedMap<String, Gauge> gauges = metrics.getGauges();
  Assert.assertEquals(2, gauges.size());

  Gauge gauge = gauges.get(AlertsUtil.getAlertGaugeName(metricsRuleDefinition1.getId()) + ".gauge");
  Map<String, Object> alertResponse = (Map<String, Object>) gauge.getValue();
  Assert.assertEquals(500L, alertResponse.get("currentValue"));
  Assert.assertEquals(
      "High incidence of Bad Records",
      ((List<String>)alertResponse.get("alertTexts")).iterator().next()
  );

  gauge = gauges.get(AlertsUtil.getAlertGaugeName(metricsRuleDefinition2.getId()) + ".gauge");
  alertResponse = (Map<String, Object>) gauge.getValue();
  Assert.assertEquals(1500L, alertResponse.get("currentValue"));
  Assert.assertEquals(
      "Field Hasher Drops Records",
      ((List<String>)alertResponse.get("alertTexts")).iterator().next()
  );

}