Java Code Examples for com.codahale.metrics.Gauge#getValue()

The following examples show how to use com.codahale.metrics.Gauge#getValue() . 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: MetricRuleEvaluatorHelper.java    From datacollector with Apache License 2.0 6 votes vote down vote up
public static Object getGaugeValue(Gauge gauge, MetricElement metricElement) {
  Object value;
  RuntimeStats runtimeStats = (RuntimeStats)gauge.getValue();
  switch (metricElement) {
    case TIME_OF_LAST_RECEIVED_RECORD:
      value =  runtimeStats.getTimeOfLastReceivedRecord();
      break;
    case LAST_BATCH_INPUT_RECORDS_COUNT:
      value =  runtimeStats.getLastBatchInputRecordsCount();
      break;
    case LAST_BATCH_OUTPUT_RECORDS_COUNT:
      value =  runtimeStats.getLastBatchOutputRecordsCount();
      break;
    case LAST_BATCH_ERROR_RECORDS_COUNT:
      value =  runtimeStats.getLastBatchErrorRecordsCount();
      break;
    case LAST_BATCH_ERROR_MESSAGES_COUNT:
      value =  runtimeStats.getLastBatchErrorMessagesCount();
      break;
    default:
      throw new IllegalStateException("Unexpected metric element type " + metricElement);
  }
  return value;
}
 
Example 2
Source File: DropwizardMetricsExporter.java    From dropwizard-prometheus with Apache License 2.0 6 votes vote down vote up
public void writeGauge(String name, Gauge<?> gauge) throws IOException {
    final String sanitizedName = sanitizeMetricName(name);
    writer.writeHelp(sanitizedName, getHelpMessage(name, gauge));
    writer.writeType(sanitizedName, MetricType.GAUGE);

    Object obj = gauge.getValue();
    double value;
    if (obj instanceof Number) {
        value = ((Number) obj).doubleValue();
    } else if (obj instanceof Boolean) {
        value = ((Boolean) obj) ? 1 : 0;
    } else {
        LOG.warn("Invalid type for Gauge {}: {}", name, obj.getClass().getName());
        return;
    }

    writer.writeSample(sanitizedName, emptyMap(), value);
}
 
Example 3
Source File: StatusQueueListener.java    From JuniperBot with GNU General Public License v3.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private static <T> T getMetricValue(Map<String, Metric> metricMap, String name, Function<Object, T> valueExtractor) {
    Metric metric = metricMap.get(name);
    T value = null;
    if (metric instanceof Gauge) {
        Gauge gauge = (Gauge) metric;
        value = (T) gauge.getValue();
    }
    if (metric instanceof Counter) {
        Counter counter = (Counter) metric;
        value = (T) (Long) counter.getCount();
    }
    if (value != null && valueExtractor != null) {
        value = valueExtractor.apply(value);
    }
    return value;
}
 
Example 4
Source File: FileRefUtil.java    From datacollector with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a gauge if it is already not. This is done only once for the stage
 * @param context the {@link com.streamsets.pipeline.api.Stage.Context} of this stage
 */
@SuppressWarnings("unchecked")
public static synchronized void initMetricsIfNeeded(ProtoConfigurableEntity.Context context) {
  Gauge<Map<String, Object>> gauge = context.getGauge(fileStatisticGaugeName(context));
  if(gauge == null) {
    gauge = context.createGauge(fileStatisticGaugeName(context), Comparator.comparing(GAUGE_MAP_ORDERING::get));
    Map<String, Object> gaugeStatistics = gauge.getValue();
    //File name is populated at the MetricEnabledWrapperStream.
    gaugeStatistics.put(FileRefUtil.FILE, "");
    gaugeStatistics.put(FileRefUtil.TRANSFER_THROUGHPUT, 0L);
    gaugeStatistics.put(FileRefUtil.SENT_BYTES, String.format(FileRefUtil.BRACKETED_TEMPLATE, 0, 0));
    gaugeStatistics.put(FileRefUtil.REMAINING_BYTES, 0L);
    gaugeStatistics.put(FileRefUtil.COMPLETED_FILE_COUNT, 0L);
  }

  Meter dataTransferMeter = context.getMeter(FileRefUtil.TRANSFER_THROUGHPUT_METER);
  if (dataTransferMeter == null) {
    context.createMeter(FileRefUtil.TRANSFER_THROUGHPUT_METER);
  }
}
 
Example 5
Source File: UsageStatisticsCollectorImpl.java    From hivemq-community-edition with Apache License 2.0 6 votes vote down vote up
private @Nullable <T> T getGaugeValue(final String metricName) {
    try {
        final SortedMap<String, Gauge> gauges = metricsHolder.getMetricRegistry().getGauges((name, metric) -> metricName.equals(name));
        if (gauges.isEmpty()) {
            return null;
        }

        //we expect a single result here
        final Gauge gauge = gauges.values().iterator().next();

        final T value = (T) gauge.getValue();
        return value;
    } catch (final Exception e) {
        return null;
    }
}
 
Example 6
Source File: GaugeSerializer.java    From watcher with Apache License 2.0 5 votes vote down vote up
@Override
public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException {
	Gauge gauge = (Gauge) object;
	Object value = gauge.getValue();
	SerializeWriter writer = serializer.getWriter();
	if (value == null)
		writer.write("0");
	else
		writer.write(value.toString());
}
 
Example 7
Source File: TopMetricSetTest.java    From rolling-metrics with Apache License 2.0 5 votes vote down vote up
private void checkDescriptions(TopMetricSet metricSet, String name, String... requiredDescriptions) {
    for (int i = 0; i < requiredDescriptions.length; i++) {
        String requiredDescription = requiredDescriptions[i];
        Gauge<String> gauge = (Gauge<String>) metricSet.getMetrics().get(name + "." + i + ".description");
        String description = gauge.getValue();
        assertEquals(requiredDescription, description);
    }
}
 
Example 8
Source File: AlertManagerHelper.java    From datacollector with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public static void updateAlertGauge(Gauge gauge, Object value, RuleDefinition ruleDefinition) {
  Map<String, Object> alertResponse = (Map<String, Object>) gauge.getValue();
  // we keep timestamp of first trigger
  // update current value
  alertResponse.put(EmailConstants.CURRENT_VALUE, value);
  // add new alert text
  List<String> alertTexts = (List<String>) alertResponse.get(EmailConstants.ALERT_TEXTS);
  alertTexts = new ArrayList<>(alertTexts);
  updateAlertText(ruleDefinition, alertTexts);
  alertResponse.put(EmailConstants.ALERT_TEXTS, alertTexts);
}
 
Example 9
Source File: TopMetricSetTest.java    From rolling-metrics with Apache License 2.0 5 votes vote down vote up
private void checkValues(TopMetricSet metricSet, String name, int digitsAfterDecimalPoint, double... requiredLatencies) {
    for (int i = 0; i < requiredLatencies.length; i++) {
        BigDecimal requiredLatency = new BigDecimal(requiredLatencies[i]).setScale(digitsAfterDecimalPoint, RoundingMode.CEILING);
        Gauge<BigDecimal> gauge = (Gauge<BigDecimal>) metricSet.getMetrics().get(name + "." + i + ".latency");
        BigDecimal latency = gauge.getValue();
        assertEquals(requiredLatency, latency);
    }
}
 
Example 10
Source File: TestWholeFileDataGeneratorFactory.java    From datacollector with Apache License 2.0 5 votes vote down vote up
@Test
public void testGaugeOrdering() throws Exception {
  DataGeneratorFactory factory =
      new DataGeneratorFactoryBuilder(context, DataGeneratorFormat.WHOLE_FILE).build();
  factory.getGenerator(new ByteArrayOutputStream());
  Gauge<Map<String, Object>> gauge = context.getGauge(FileRefUtil.fileStatisticGaugeName(context));
  Map<String, Object> map = gauge.getValue();

  LinkedHashSet<String> hashSet = new LinkedHashSet<>();
  //Ordering
  hashSet.add(FileRefUtil.FILE);
  hashSet.add(FileRefUtil.TRANSFER_THROUGHPUT);
  hashSet.add(FileRefUtil.SENT_BYTES);
  hashSet.add(FileRefUtil.REMAINING_BYTES);
  hashSet.add(FileRefUtil.COMPLETED_FILE_COUNT);


  Iterator<String> hashSetKeyIterator = hashSet.iterator();
  Iterator<String> keySetIterator = map.keySet().iterator();

  while (hashSetKeyIterator.hasNext()) {
    Assert.assertEquals(hashSetKeyIterator.next(), keySetIterator.next());
  }

  hashSetKeyIterator = hashSet.iterator();
  Iterator<Map.Entry<String, Object>> entrySetIterator = map.entrySet().iterator();
  while (hashSetKeyIterator.hasNext()) {
    Assert.assertEquals(hashSetKeyIterator.next(), entrySetIterator.next().getKey());
  }
}
 
Example 11
Source File: WavefrontMetricsReporter.java    From dropwizard-wavefront with Apache License 2.0 5 votes vote down vote up
private Object evaluateGauge(Gauge gauge) {
  try {
    return gauge.getValue();
  } catch (RuntimeException e) {
    logger.warn("Error reading gauge", e);
    return "error reading gauge";
  }
}
 
Example 12
Source File: MetricEvent.java    From eagle with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings( {"rawtypes", "unchecked"})
public Builder from(Gauge gauge) {
    Object value = gauge.getValue();
    if (value instanceof Map) {
        Map<? extends String, ?> map = (Map<? extends String, ?>) value;
        this.instance.putAll(map);
    } else {
        this.instance.put("value", value);
    }
    return this;
}
 
Example 13
Source File: NakadiMetricsModule.java    From nakadi with MIT License 5 votes vote down vote up
@Override
public void serialize(final Gauge gauge,
                      final JsonGenerator json,
                      final SerializerProvider provider) throws IOException {
    json.writeStartObject();
    final Object value;
    try {
        value = gauge.getValue();
        json.writeObjectField("value", value);
    } catch (RuntimeException e) {
        json.writeObjectField("error", e.toString());
    }
    json.writeEndObject();
}
 
Example 14
Source File: DropWizardMetrics.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a {@code Metric} collected from {@link Gauge}.
 *
 * @param dropwizardName the metric name.
 * @param gauge the gauge object to collect.
 * @return a {@code Metric}.
 */
@SuppressWarnings("rawtypes")
@Nullable
private Metric collectGauge(String dropwizardName, Gauge gauge) {
  String metricName = DropWizardUtils.generateFullMetricName(dropwizardName, "gauge");
  String metricDescription = DropWizardUtils.generateFullMetricDescription(dropwizardName, gauge);

  // Figure out which gauge instance and call the right method to get value
  Type type;
  Value value;

  Object obj = gauge.getValue();
  if (obj instanceof Number) {
    type = Type.GAUGE_DOUBLE;
    value = Value.doubleValue(((Number) obj).doubleValue());
  } else if (obj instanceof Boolean) {
    type = Type.GAUGE_INT64;
    value = Value.longValue(((Boolean) obj) ? 1 : 0);
  } else {
    // Ignoring Gauge (gauge.getKey()) with unhandled type.
    return null;
  }

  MetricDescriptor metricDescriptor =
      MetricDescriptor.create(
          metricName, metricDescription, DEFAULT_UNIT, type, Collections.<LabelKey>emptyList());
  TimeSeries timeSeries =
      TimeSeries.createWithOnePoint(
          Collections.<LabelValue>emptyList(), Point.create(value, clock.now()), null);
  return Metric.createWithOneTimeSeries(metricDescriptor, timeSeries);
}
 
Example 15
Source File: TestCoordinatorBookKeeper.java    From rubix with Apache License 2.0 5 votes vote down vote up
/**
 * Verify that the worker health status properly expires.
 */
@Test
public void testWorkerHealthMetrics_healthStatusExpired() throws IOException
{
  final FakeTicker ticker = new FakeTicker();
  final int healthStatusExpiry = 1000; // ms
  CacheConfig.setValidationEnabled(conf, true);
  CacheConfig.setHealthStatusExpiry(conf, healthStatusExpiry);

  try (BookKeeperMetrics bookKeeperMetrics = new BookKeeperMetrics(conf, metrics)) {
    final CoordinatorBookKeeper coordinatorBookKeeper = new CoordinatorBookKeeper(conf, bookKeeperMetrics, ticker);
    final Gauge liveWorkerGauge = metrics.getGauges().get(BookKeeperMetrics.HealthMetric.LIVE_WORKER_GAUGE.getMetricName());
    final Gauge cachingValidatedWorkerGauge = metrics.getGauges().get(BookKeeperMetrics.HealthMetric.CACHING_VALIDATED_WORKER_GAUGE.getMetricName());
    final Gauge fileValidatedWorkerGauge = metrics.getGauges().get(BookKeeperMetrics.HealthMetric.FILE_VALIDATED_WORKER_GAUGE.getMetricName());

    coordinatorBookKeeper.handleHeartbeat(TEST_HOSTNAME_WORKER1, TEST_STATUS_ALL_VALIDATED);
    coordinatorBookKeeper.handleHeartbeat(TEST_HOSTNAME_WORKER2, TEST_STATUS_ALL_VALIDATED);

    long workerCount = (long) liveWorkerGauge.getValue();
    long cachingValidationCount = (long) cachingValidatedWorkerGauge.getValue();
    long fileValidationCount = (long) fileValidatedWorkerGauge.getValue();
    assertEquals(workerCount, 2, "Incorrect number of workers reporting heartbeat");
    assertEquals(cachingValidationCount, 2, "Incorrect number of workers have been validated");
    assertEquals(fileValidationCount, 2, "Incorrect number of workers have been validated");

    ticker.advance(healthStatusExpiry, TimeUnit.MILLISECONDS);
    coordinatorBookKeeper.handleHeartbeat(TEST_HOSTNAME_WORKER1, TEST_STATUS_ALL_VALIDATED);

    workerCount = (long) liveWorkerGauge.getValue();
    cachingValidationCount = (long) cachingValidatedWorkerGauge.getValue();
    fileValidationCount = (long) fileValidatedWorkerGauge.getValue();
    assertEquals(workerCount, 1, "Incorrect number of workers reporting heartbeat");
    assertEquals(cachingValidationCount, 1, "Incorrect number of workers have been validated");
    assertEquals(fileValidationCount, 1, "Incorrect number of workers have been validated");
  }
}
 
Example 16
Source File: RaftBasicTests.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
public static void testStateMachineMetrics(boolean async,
    MiniRaftCluster cluster, Logger LOG) throws Exception {
  RaftServerImpl leader = waitForLeader(cluster);
  try (final RaftClient client = cluster.createClient()) {

    Assert.assertTrue(leader.isLeader());

    Gauge appliedIndexGauge = getStatemachineGaugeWithName(leader,
        STATEMACHINE_APPLIED_INDEX_GAUGE);
    Gauge smAppliedIndexGauge = getStatemachineGaugeWithName(leader,
        STATEMACHINE_APPLY_COMPLETED_GAUGE);

    long appliedIndexBefore = (Long) appliedIndexGauge.getValue();
    long smAppliedIndexBefore = (Long) smAppliedIndexGauge.getValue();
    checkFollowerCommitLagsLeader(cluster);

    if (async) {
      CompletableFuture<RaftClientReply> replyFuture = client.sendAsync(new SimpleMessage("abc"));
      replyFuture.get();
    } else {
      client.send(new SimpleMessage("abc"));
    }

    long appliedIndexAfter = (Long) appliedIndexGauge.getValue();
    long smAppliedIndexAfter = (Long) smAppliedIndexGauge.getValue();
    checkFollowerCommitLagsLeader(cluster);

    Assert.assertTrue("StateMachine Applied Index not incremented",
        appliedIndexAfter > appliedIndexBefore);
    Assert.assertTrue("StateMachine Apply completed Index not incremented",
        smAppliedIndexAfter > smAppliedIndexBefore);
  }
}
 
Example 17
Source File: MetricsModule.java    From hugegraph with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(Gauge gauge, JsonGenerator json,
                      SerializerProvider provider) throws IOException {
    json.writeStartObject();
    final Object value;
    try {
        value = gauge.getValue();
        json.writeObjectField("value", value);
    } catch (RuntimeException e) {
        json.writeObjectField("error", e.toString());
    }
    json.writeEndObject();
}
 
Example 18
Source File: ClientQueueMemoryLocalPersistenceTest.java    From hivemq-community-edition with Apache License 2.0 5 votes vote down vote up
@Test
public void test_batched_add_retained_discard_over_retained_limit() {
    final ImmutableList.Builder<PUBLISH> publishes1 = ImmutableList.builder();
    final ImmutableList.Builder<PUBLISH> publishes2 = ImmutableList.builder();
    for (int i = 0; i < 10; i++) {
        if(i < 5) {
            publishes1.add(createPublish(1, QoS.AT_LEAST_ONCE, "topic" + i));
        } else {
            publishes2.add(createPublish(1, QoS.AT_LEAST_ONCE, "topic" + i));
        }
    }
    persistence.add("client", false, publishes1.build(), 2, DISCARD, true, 0);

    final Gauge<Long> gauge = metricRegistry.getGauges().get(HiveMQMetrics.QUEUED_MESSAGES_MEMORY_PERSISTENCE_TOTAL_SIZE.name());
    final Long value = gauge.getValue();
    assertTrue(value > 0);

    assertEquals(5, persistence.size("client", false, 0));

    persistence.add("client", false, publishes2.build(), 2, DISCARD, true, 0);

    assertEquals(5, persistence.size("client", false, 0));
    assertEquals(value, gauge.getValue());

    final ImmutableList<PUBLISH> all =
            persistence.readNew("client", false, ImmutableIntArray.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), 10000L, 0);
    assertEquals(5, all.size());
    assertEquals("topic0", all.get(0).getTopic());
    assertEquals("topic1", all.get(1).getTopic());
}
 
Example 19
Source File: TestIndexSearcher.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public void testReopen() throws Exception {

    assertU(adoc("id","1", "v_t","Hello Dude", "v_s1","string1"));
    assertU(adoc("id","2", "v_t","Hello Yonik", "v_s1","string2"));
    assertU(commit());

    SolrQueryRequest sr1 = req("q","foo");
    IndexReader r1 = sr1.getSearcher().getRawReader();

    String sval1 = getStringVal(sr1, "v_s1",0);
    assertEquals("string1", sval1);

    assertU(adoc("id","3", "v_s1","{!literal}"));
    assertU(adoc("id","4", "v_s1","other stuff"));
    assertU(commit());

    SolrQueryRequest sr2 = req("q","foo");
    IndexReader r2 = sr2.getSearcher().getRawReader();

    // make sure the readers share the first segment
    // Didn't work w/ older versions of lucene2.9 going from segment -> multi
    assertEquals(r1.leaves().get(0).reader(), r2.leaves().get(0).reader());

    assertU(adoc("id","5", "v_f","3.14159"));
    assertU(adoc("id","6", "v_f","8983", "v_s1","string6"));
    assertU(commit());

    SolrQueryRequest sr3 = req("q","foo");
    IndexReader r3 = sr3.getSearcher().getRawReader();
    // make sure the readers share segments
    // assertEquals(r1.getLeafReaders()[0], r3.getLeafReaders()[0]);
    assertEquals(r2.leaves().get(0).reader(), r3.leaves().get(0).reader());
    assertEquals(r2.leaves().get(1).reader(), r3.leaves().get(1).reader());

    sr1.close();
    sr2.close();            

    // should currently be 1, but this could change depending on future index management
    int baseRefCount = r3.getRefCount();
    assertEquals(1, baseRefCount);

    Map<String, Metric> metrics = h.getCore().getCoreMetricManager().getRegistry().getMetrics();
    @SuppressWarnings({"unchecked"})
    Gauge<Date> g = (Gauge<Date>)metrics.get("SEARCHER.searcher.registeredAt");
    Date sr3SearcherRegAt = g.getValue();
    assertU(commit()); // nothing has changed
    SolrQueryRequest sr4 = req("q","foo");
    assertSame("nothing changed, searcher should be the same",
               sr3.getSearcher(), sr4.getSearcher());
    assertEquals("nothing changed, searcher should not have been re-registered",
                 sr3SearcherRegAt, g.getValue());
    IndexReader r4 = sr4.getSearcher().getRawReader();

    // force an index change so the registered searcher won't be the one we are testing (and
    // then we should be able to test the refCount going all the way to 0
    assertU(adoc("id","7", "v_f","7574"));
    assertU(commit()); 

    // test that reader didn't change
    assertSame(r3, r4);
    assertEquals(baseRefCount, r4.getRefCount());
    sr3.close();
    assertEquals(baseRefCount, r4.getRefCount());
    sr4.close();
    assertEquals(baseRefCount-1, r4.getRefCount());


    SolrQueryRequest sr5 = req("q","foo");
    IndexReaderContext rCtx5 = sr5.getSearcher().getTopReaderContext();

    assertU(delI("1"));
    assertU(commit());
    SolrQueryRequest sr6 = req("q","foo");
    IndexReaderContext rCtx6 = sr6.getSearcher().getTopReaderContext();
    assertEquals(1, rCtx6.leaves().get(0).reader().numDocs()); // only a single doc left in the first segment
    assertTrue( !rCtx5.leaves().get(0).reader().equals(rCtx6.leaves().get(0).reader()) );  // readers now different

    sr5.close();
    sr6.close();
  }
 
Example 20
Source File: IrisMetricsFormat.java    From arcusplatform with Apache License 2.0 4 votes vote down vote up
public static JsonArray toJson(String name, Gauge<?> gauge, List<TagValue> tags) {
   Object value = gauge.getValue();
   return toJson(name, value, tags);
}