Java Code Examples for com.codahale.metrics.Counter#getCount()

The following examples show how to use com.codahale.metrics.Counter#getCount() . 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: 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 2
Source File: TCPMetricsImpl.java    From vertx-dropwizard-metrics with Apache License 2.0 6 votes vote down vote up
@Override
public void disconnected(Long ctx, SocketAddress remoteAddress) {
  openConnections.dec();
  connections.update(System.nanoTime() - ctx, TimeUnit.NANOSECONDS);

  // On network outage the remoteAddress can be null.
  // Do not report the open-connections when it's null
  if (remoteAddress != null) {
    // Remote address connection metrics
    Counter counter = counter("open-connections", remoteAddress.host());
    counter.dec();
    if (counter.getCount() == 0) {
      remove("open-connections", remoteAddress.host());
    }
  }

  // A little clunky, but it's possible we got here after closed has been called
  if (closed) {
    removeAll();
  }
}
 
Example 3
Source File: MetricsTest.java    From dcos-commons with Apache License 2.0 5 votes vote down vote up
@Test
public void incrementDeclinesShort() {
    Counter counter = Metrics.getRegistry().counter(Metrics.DECLINE_SHORT);
    long val = counter.getCount();
    Metrics.incrementDeclinesShort(5);
    Assert.assertEquals(5, counter.getCount() - val);
}
 
Example 4
Source File: PacketStatsUiMessageHandler.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
public void process(ObjectNode payload) {
    MetricsService service = get(MetricsService.class);
    Map<String, Counter> counters = service.getCounters(filter);
    Counter arpCounter = counters.get("packetStatisticsComponent.arpFeature.arpPC");
    long arpCount = arpCounter.getCount();
    ObjectNode arpJson = objectNode();
    arpJson.put("ArpCounter", arpCount);
    sendMessage(ARP_RESP, arpJson);
}
 
Example 5
Source File: PacketStatsUiMessageHandler.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
public void process(ObjectNode payload) {
    MetricsService service = get(MetricsService.class);
    Map<String, Counter> counters = service.getCounters(filter);
    Counter icmpCounter = counters.get("packetStatisticsComponent.icmpFeature.icmpPC");
    long icmpCount = icmpCounter.getCount();
    ObjectNode icmpJson = objectNode();
    icmpJson.put("IcmpCounter", icmpCount);
    log.info("Received ICMP Request");
    sendMessage(ICMP_RESP, icmpJson);
}
 
Example 6
Source File: TaskState.java    From incubator-gobblin with Apache License 2.0 5 votes vote down vote up
/**
 * Update record-level metrics.
 *
 * @param recordsWritten number of records written by the writer
 * @param branchIndex fork branch index
 *
 * @deprecated see {@link org.apache.gobblin.instrumented.writer.InstrumentedDataWriterBase}.
 */
public synchronized void updateRecordMetrics(long recordsWritten, int branchIndex) {
  TaskMetrics metrics = TaskMetrics.get(this);
  // chopping branch index from metric name
  // String forkBranchId = ForkOperatorUtils.getForkId(this.taskId, branchIndex);
  String forkBranchId = TaskMetrics.taskInstanceRemoved(this.taskId);

  Counter taskRecordCounter = metrics.getCounter(MetricGroup.TASK.name(), forkBranchId, RECORDS);
  long inc = recordsWritten - taskRecordCounter.getCount();
  taskRecordCounter.inc(inc);
  metrics.getMeter(MetricGroup.TASK.name(), forkBranchId, RECORDS_PER_SECOND).mark(inc);
  metrics.getCounter(MetricGroup.JOB.name(), this.jobId, RECORDS).inc(inc);
  metrics.getMeter(MetricGroup.JOB.name(), this.jobId, RECORDS_PER_SECOND).mark(inc);
}
 
Example 7
Source File: MetricRuleEvaluatorHelper.java    From datacollector with Apache License 2.0 5 votes vote down vote up
public static Object getCounterValue(Counter counter, MetricElement metricElement) {
  Object value;
  switch (metricElement) {
    case COUNTER_COUNT:
      value = counter.getCount();
      break;
    default:
      throw new IllegalStateException("Unexpected metric element type " + metricElement);
  }
  return value;
}
 
Example 8
Source File: PacketStatsUiMessageHandler.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
public void process(ObjectNode payload) {
    MetricsService service = get(MetricsService.class);
    Map<String, Counter> counters = service.getCounters(filter);
    Counter vlanCounter = counters.get("packetStatisticsComponent.vlanFeature.vlanPC");
    long vlanCount = vlanCounter.getCount();
    ObjectNode vlanJson = objectNode();
    vlanJson.put("VlanCounter", vlanCount);
    log.info("Received VLAN Request");
    sendMessage(VLAN_RESP, vlanJson);
}
 
Example 9
Source File: PacketStatsUiMessageHandler.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
public void process(ObjectNode payload) {
    MetricsService service = get(MetricsService.class);
    Map<String, Counter> counters = service.getCounters(filter);
    Counter lldpCounter = counters.get("packetStatisticsComponent.lldpFeature.lldpPC");
    long lldpCount = lldpCounter.getCount();
    ObjectNode lldpJson = objectNode();
    lldpJson.put("LldpCounter", lldpCount);
    log.info("Received LLDP Request");
    sendMessage(LLDP_RESP, lldpJson);
}
 
Example 10
Source File: PacketStatsUiMessageHandler.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
public void process(ObjectNode payload) {
    MetricsService service = get(MetricsService.class);
    Map<String, Counter> counters = service.getCounters(filter);
    Counter dhcpCounter = counters.get("packetStatisticsComponent.dhcpFeature.dhcpPC");
    long dhcpCount = dhcpCounter.getCount();
    ObjectNode dhcpJson = objectNode();
    dhcpJson.put("DhcpCounter", dhcpCount);
    log.info("Received DHCP Request");
    sendMessage(DHCP_RESP, dhcpJson);
}
 
Example 11
Source File: PacketStatsUiMessageHandler.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
public void process(ObjectNode payload) {
    MetricsService service = get(MetricsService.class);
    Map<String, Counter> counters = service.getCounters(filter);
    Counter igmpCounter =  counters.get("packetStatisticsComponent.igmpFeature.igmpPC");
    long igmpCount = igmpCounter.getCount();
    ObjectNode igmpJson = objectNode();
    igmpJson.put("IgmpCounter", igmpCount);
    log.info("Received IGMP Request");
    sendMessage(IGMP_RESP, igmpJson);
}
 
Example 12
Source File: PacketStatsUiMessageHandler.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
public void process(ObjectNode payload) {
    MetricsService service = get(MetricsService.class);
    Map<String, Counter> counters = service.getCounters(filter);
    Counter unknownCounter =  counters.get("packetStatisticsComponent.unknownFeature.unknownPC");
    long unknownCount = unknownCounter.getCount();
    ObjectNode unknownJson = objectNode();
    unknownJson.put("UnknownCounter", unknownCount);
    log.info("Received UNKNOWN Request");
    sendMessage(UNKNOWN_RESP, unknownJson);
}
 
Example 13
Source File: MetricsTest.java    From dcos-commons with Apache License 2.0 5 votes vote down vote up
@Test
public void incrementRevives() {
    Counter counter = Metrics.getRegistry().counter(Metrics.REVIVES);
    long val = counter.getCount();
    Metrics.incrementRevives();
    Assert.assertEquals(1, counter.getCount() - val);
}
 
Example 14
Source File: MetricsTest.java    From dcos-commons with Apache License 2.0 5 votes vote down vote up
@Test
public void incrementProcessedOffers() {
    Counter counter = Metrics.getRegistry().counter(Metrics.PROCESSED_OFFERS);
    long val = counter.getCount();
    Metrics.incrementProcessedOffers(5);
    Assert.assertEquals(5, counter.getCount() - val);
}
 
Example 15
Source File: MetricsTest.java    From dcos-commons with Apache License 2.0 5 votes vote down vote up
@Test
public void incrementReceivedOffers() {
    Counter counter = Metrics.getRegistry().counter(Metrics.RECEIVED_OFFERS);
    long val = counter.getCount();
    Metrics.incrementReceivedOffers(5);
    Assert.assertEquals(5, counter.getCount() - val);
}
 
Example 16
Source File: StatsCollector.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
public long getRegionKeysRead(final Region region, final boolean reset) {
    final Counter counter = KVMetrics.counter(KVMetricNames.REGION_KEYS_READ, String.valueOf(region.getId()));
    final long value = counter.getCount();
    if (reset) {
        counter.dec(value);
    }
    return value;
}
 
Example 17
Source File: StatsCollector.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
public long getRegionKeysWritten(final Region region, final boolean reset) {
    final Counter counter = KVMetrics.counter(KVMetricNames.REGION_KEYS_WRITTEN, String.valueOf(region.getId()));
    final long value = counter.getCount();
    if (reset) {
        counter.dec(value);
    }
    return value;
}
 
Example 18
Source File: PacketStatsUiMessageHandler.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
public void process(ObjectNode payload) {
    MetricsService service = get(MetricsService.class);
    Map<String, Counter> counters = service.getCounters(filter);
    Counter bsnCounter =  counters.get("packetStatisticsComponent.bsnFeature.bsnPC");
    long bsnCount = bsnCounter.getCount();
    ObjectNode bsnJson = objectNode();
    bsnJson.put("BsnCounter", bsnCount);
    log.info("Received BSN Request");
    sendMessage(BSN_RESP, bsnJson);
}
 
Example 19
Source File: GetBlobOperationTest.java    From ambry with Apache License 2.0 4 votes vote down vote up
/**
 * A past issue with replication logic resulted in the blob size listed in the blob properties reflecting the size
 * of a chunk's content buffer instead of the plaintext size of the entire blob. This issue affects composite blobs
 * and simple encrypted blob. This test tests the router's ability to replace the incorrect blob size field in the
 * blob properties with the inferred correct size.
 * @throws Exception
 */
@Test
public void testBlobSizeReplacement() throws Exception {
  userMetadata = new byte[10];
  random.nextBytes(userMetadata);
  options = new GetBlobOptionsInternal(
      new GetBlobOptionsBuilder().operationType(GetBlobOptions.OperationType.BlobInfo).build(), false,
      routerMetrics.ageAtGet);

  // test simple blob case
  blobSize = maxChunkSize;
  putContent = new byte[blobSize];
  random.nextBytes(putContent);
  blobProperties =
      new BlobProperties(blobSize + 20, "serviceId", "memberId", "contentType", false, Utils.Infinite_Time,
          Utils.getRandomShort(random), Utils.getRandomShort(random), testEncryption, null);
  ByteBuf putContentBuf = PooledByteBufAllocator.DEFAULT.heapBuffer(blobSize);
  putContentBuf.writeBytes(putContent);
  doDirectPut(BlobType.DataBlob, putContentBuf.retainedDuplicate());
  putContentBuf.release();
  Counter sizeMismatchCounter = (testEncryption ? routerMetrics.simpleEncryptedBlobSizeMismatchCount
      : routerMetrics.simpleUnencryptedBlobSizeMismatchCount);
  long startCount = sizeMismatchCounter.getCount();
  getAndAssertSuccess();
  long endCount = sizeMismatchCounter.getCount();
  Assert.assertEquals("Wrong number of blob size mismatches", 1, endCount - startCount);

  // test composite blob case
  int numChunks = 3;
  blobSize = maxChunkSize;
  List<StoreKey> storeKeys = new ArrayList<>(numChunks);
  for (int i = 0; i < numChunks; i++) {
    doPut();
    storeKeys.add(blobId);
  }
  blobSize = maxChunkSize * numChunks;
  ByteBuffer metadataContent = MetadataContentSerDe.serializeMetadataContentV2(maxChunkSize, blobSize, storeKeys);
  metadataContent.flip();
  blobProperties =
      new BlobProperties(blobSize - 20, "serviceId", "memberId", "contentType", false, Utils.Infinite_Time,
          Utils.getRandomShort(random), Utils.getRandomShort(random), testEncryption, null);
  ByteBuf metadataContentBuf = PooledByteBufAllocator.DEFAULT.heapBuffer(metadataContent.remaining());
  metadataContentBuf.writeBytes(metadataContent.duplicate());
  doDirectPut(BlobType.MetadataBlob, metadataContentBuf.retainedDuplicate());
  metadataContentBuf.release();
  startCount = routerMetrics.compositeBlobSizeMismatchCount.getCount();
  getAndAssertSuccess();
  endCount = routerMetrics.compositeBlobSizeMismatchCount.getCount();
  Assert.assertEquals("Wrong number of blob size mismatches", 1, endCount - startCount);
}
 
Example 20
Source File: TestCloudRecovery.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Test
// commented 4-Sep-2018 @BadApple(bugUrl="https://issues.apache.org/jira/browse/SOLR-12028") // added 20-Jul-2018
public void leaderRecoverFromLogOnStartupTest() throws Exception {
  AtomicInteger countReplayLog = new AtomicInteger(0);
  TestInjection.skipIndexWriterCommitOnClose = true;
  UpdateLog.testing_logReplayFinishHook = countReplayLog::incrementAndGet;

  CloudSolrClient cloudClient = cluster.getSolrClient();
  cloudClient.add(COLLECTION, sdoc("id", "1"));
  cloudClient.add(COLLECTION, sdoc("id", "2"));
  cloudClient.add(COLLECTION, sdoc("id", "3"));
  cloudClient.add(COLLECTION, sdoc("id", "4"));

  ModifiableSolrParams params = new ModifiableSolrParams();
  params.set("q", "*:*");
  QueryResponse resp = cloudClient.query(COLLECTION, params);
  assertEquals(0, resp.getResults().getNumFound());

  ChaosMonkey.stop(cluster.getJettySolrRunners());

  
  for (JettySolrRunner jettySolrRunner : cluster.getJettySolrRunners()) {
    cluster.waitForJettyToStop(jettySolrRunner);
  }
  assertTrue("Timeout waiting for all not live", ClusterStateUtil.waitForAllReplicasNotLive(cloudClient.getZkStateReader(), 45000));
  ChaosMonkey.start(cluster.getJettySolrRunners());
  
  cluster.waitForAllNodes(30);
  
  assertTrue("Timeout waiting for all live and active", ClusterStateUtil.waitForAllActiveAndLiveReplicas(cloudClient.getZkStateReader(), COLLECTION, 120000));

  resp = cloudClient.query(COLLECTION, params);
  assertEquals(4, resp.getResults().getNumFound());
  // Make sure all nodes is recover from tlog
  if (onlyLeaderIndexes) {
    // Leader election can be kicked off, so 2 tlog replicas will replay its tlog before becoming new leader
    assertTrue( countReplayLog.get() >=2);
  } else {
    assertEquals(4, countReplayLog.get());
  }

  // check metrics
  int replicationCount = 0;
  int errorsCount = 0;
  int skippedCount = 0;
  for (JettySolrRunner jetty : cluster.getJettySolrRunners()) {
    SolrMetricManager manager = jetty.getCoreContainer().getMetricManager();
    List<String> registryNames = manager.registryNames().stream()
        .filter(s -> s.startsWith("solr.core.")).collect(Collectors.toList());
    for (String registry : registryNames) {
      Map<String, Metric> metrics = manager.registry(registry).getMetrics();
      Timer timer = (Timer)metrics.get("REPLICATION.peerSync.time");
      Counter counter = (Counter)metrics.get("REPLICATION.peerSync.errors");
      Counter skipped = (Counter)metrics.get("REPLICATION.peerSync.skipped");
      replicationCount += timer.getCount();
      errorsCount += counter.getCount();
      skippedCount += skipped.getCount();
    }
  }
  if (onlyLeaderIndexes) {
    assertTrue(replicationCount >= 2);
  } else {
    assertEquals(2, replicationCount);
  }
}