/** * Returns a map of all the counters in the registry and their names. * * @return all the counters in the registry */ public SortedMap<String, Counter> getCounters() { return getCounters(MetricFilter.ALL); }
/** * Get an existing counter or create a new one if the requested one does not yet exist. Creation * is synchronized to assure that only one instance of the counter is created. * @param name name of the counter * @return new Counter, or existing one if it already exists. This will return null if the * metrics have not been initialized. */ public static Counter getOrCreateCounter(String name) { if (self == null) return null; Map<String, Counter> counters = self.registry.getCounters(); Counter counter = counters.get(name); if (counter != null) return counter; // Looks like it doesn't exist. Lock so that two threads don't create it at once. synchronized (Metrics.class) { // Recheck to make sure someone didn't create it while we waited. counters = self.registry.getCounters(); counter = counters.get(name); if (counter != null) return counter; return self.registry.counter(name); } }
/** * Resets all the counters to 0 for testing. */ public static void resetAllCounters() { for (Map.Entry<String, Counter> entry : METRIC_REGISTRY.getCounters().entrySet()) { entry.getValue().dec(entry.getValue().getCount()); } }
private Map<String, Long> getMetricsInternal() { MetricRegistry metricRegistry = MetricsSystem.METRIC_REGISTRY; // Get all counters. Map<String, Counter> counters = metricRegistry.getCounters(); // Only the gauge for cached blocks is retrieved here, other gauges are statistics of // free/used spaces, those statistics can be gotten via other REST apis. String blocksCachedProperty = MetricsSystem.getMetricName(DefaultBlockWorker.Metrics.BLOCKS_CACHED); @SuppressWarnings("unchecked") Gauge<Integer> blocksCached = (Gauge<Integer>) metricRegistry.getGauges().get(blocksCachedProperty); // Get values of the counters and gauges and put them into a metrics map. SortedMap<String, Long> metrics = new TreeMap<>(); for (Map.Entry<String, Counter> counter : counters.entrySet()) { metrics.put(counter.getKey(), counter.getValue().getCount()); } metrics.put(blocksCachedProperty, blocksCached.getValue().longValue()); return metrics; }
private Map<String, Long> getMetricsInternal() { MetricRegistry metricRegistry = MetricsSystem.METRIC_REGISTRY; // Get all counters. Map<String, Counter> counters = metricRegistry.getCounters(); // Only the gauge for pinned files is retrieved here, other gauges are statistics of // free/used // spaces, those statistics can be gotten via other REST apis. String filesPinnedProperty = MetricsSystem.getMetricName(MasterMetrics.FILES_PINNED); @SuppressWarnings("unchecked") Gauge<Integer> filesPinned = (Gauge<Integer>) MetricsSystem.METRIC_REGISTRY.getGauges().get(filesPinnedProperty); // Get values of the counters and gauges and put them into a metrics map. SortedMap<String, Long> metrics = new TreeMap<>(); for (Map.Entry<String, Counter> counter : counters.entrySet()) { metrics.put(counter.getKey(), counter.getValue().getCount()); } metrics.put(filesPinnedProperty, filesPinned.getValue().longValue()); return metrics; }
private static List<Metric> allMetrics(MetricsSystem.InstanceType instanceType) { List<Metric> metrics = new ArrayList<>(); for (Entry<String, Gauge> entry : METRIC_REGISTRY.getGauges().entrySet()) { if (entry.getKey().startsWith(instanceType.toString())) { Object value = entry.getValue().getValue(); if (!(value instanceof Number)) { LOG.warn( "The value of metric {} of type {} is not sent to metrics master," + " only metrics value of number can be collected", entry.getKey(), entry.getValue().getClass().getSimpleName()); continue; } metrics.add(Metric.from(entry.getKey(), ((Number) value).longValue())); } } for (Entry<String, Counter> entry : METRIC_REGISTRY.getCounters().entrySet()) { metrics.add(Metric.from(entry.getKey(), entry.getValue().getCount())); } for (Entry<String, Meter> entry : METRIC_REGISTRY.getMeters().entrySet()) { // TODO(yupeng): From Meter's implementation, getOneMinuteRate can only report at rate of at // least seconds. if the client's duration is too short (i.e. < 1s), then getOneMinuteRate // would return 0 metrics.add(Metric.from(entry.getKey(), entry.getValue().getOneMinuteRate())); } for (Entry<String, Timer> entry : METRIC_REGISTRY.getTimers().entrySet()) { metrics.add(Metric.from(entry.getKey(), entry.getValue().getCount())); } return metrics; }
@Override public void doRun() { final SortedMap<String, ? extends Counting> counters = metricRegistry.getCounters( filterSingleMetric(GlobalMetricNames.OUTPUT_THROUGHPUT) ); final SortedMap<String, ? extends Counting> inputCounters = metricRegistry.getCounters( filterSingleMetric(GlobalMetricNames.INPUT_THROUGHPUT) );
@Override public void getMetrics(GetMetricsPOptions options, StreamObserver<GetMetricsPResponse> responseObserver) { RpcUtils.call(LOG, (RpcUtils.RpcCallableThrowsIOException<GetMetricsPResponse>) () -> { MetricRegistry mr = MetricsSystem.METRIC_REGISTRY; Map<String, alluxio.grpc.MetricValue> metricsMap = new HashMap<>(); for (Map.Entry<String, Counter> entry : mr.getCounters().entrySet()) { metricsMap.put(MetricsSystem.stripInstanceAndHost(entry.getKey()), alluxio.grpc.MetricValue .newBuilder().setLongValue(entry.getValue().getCount()).build()); } for (Map.Entry<String, Gauge> entry : mr.getGauges().entrySet()) { Object value = entry.getValue().getValue(); if (value instanceof Integer) { metricsMap.put(entry.getKey(), alluxio.grpc.MetricValue.newBuilder() .setLongValue(Long.valueOf((Integer) value)).build()); } else if (value instanceof Long) { metricsMap.put(entry.getKey(), alluxio.grpc.MetricValue.newBuilder() .setLongValue(Long.valueOf((Long) value)).build()); } else if (value instanceof Double) { metricsMap.put(entry.getKey(), alluxio.grpc.MetricValue.newBuilder().setDoubleValue((Double) value).build()); } } return GetMetricsPResponse.newBuilder().putAllMetrics(metricsMap).build(); }, "getConfiguration", "options=%s", responseObserver, options); } }
@Override public void report() { // we do not need to lock here, because the dropwizard registry is // internally a concurrent map @SuppressWarnings("rawtypes") final SortedMap<String, com.codahale.metrics.Gauge> gauges = registry.getGauges(); final SortedMap<String, com.codahale.metrics.Counter> counters = registry.getCounters(); final SortedMap<String, com.codahale.metrics.Histogram> histograms = registry.getHistograms(); final SortedMap<String, com.codahale.metrics.Meter> meters = registry.getMeters(); final SortedMap<String, com.codahale.metrics.Timer> timers = registry.getTimers(); this.reporter.report(gauges, counters, histograms, meters, timers); }
public void reportRegistry(MetricRegistry registry) { Map<String, String> tags = Maps.newHashMap(); if (registry instanceof MetricContext) { tags = Maps.transformValues(((MetricContext) registry).getTagMap(), new Function<Object, String>() { @Override public String apply(Object input) { return input.toString(); } }); } report(registry.getGauges(this.filter), registry.getCounters(this.filter), registry.getHistograms(this.filter), registry.getMeters(this.filter), registry.getTimers(this.filter), tags); }
response.setWorkerCapacityFreePercentage(100 - workerCapacityUsedPercentage); Map<String, Counter> counters = mr.getCounters(new MetricFilter() { @Override public boolean matches(String name, Metric metric) { Map<String, Counter> rpcInvocations = mr.getCounters(new MetricFilter() { @Override public boolean matches(String name, Metric metric) {
@GET @Produces("application/json") @Path("/counters") public Map<String, Long> counters() { return (MapX) MapX.fromMap(metrics.getCounters()) .bimap(k -> k, v -> v.getCount()); }
@VisibleForTesting void reportOnce() { // resolve the additional attributes once per report additionalAttributes = additionalAttributesSupplier.get(); if (additionalAttributes == null) { additionalAttributes = Collections.emptyMap(); } metricRegistry.getGauges().forEach(this::reportGauge); metricRegistry.getCounters().forEach(this::reportCounter); metricRegistry.getMeters().forEach(this::reportMeter); metricRegistry.getHistograms().forEach(this::reportHistogram); metricRegistry.getTimers().forEach(this::reportTimer); }
/** * Report the current values of all metrics in the registry. */ public void report() { synchronized (this) { report(registry.getGauges(filter), registry.getCounters(filter), registry.getHistograms(filter), registry.getMeters(filter), registry.getTimers(filter)); } }
private Counter getCounter(String counter) { CodahaleMetrics metrics = (CodahaleMetrics) MetricsFactory.getInstance(); SortedMap<String, Counter> counters = metrics.getMetricRegistry().getCounters(); assertNotNull(counters); return counters.get(counter); }
@Test public void testCount() throws Exception { int runs = 5; for (int i = 0; i < runs; i++) { MetricsFactory.getInstance().incrementCounter("count1"); Counter counter = metricRegistry.getCounters().get("count1"); Assert.assertEquals(i + 1, counter.getCount()); } }
private ExtractorSummary toSummary(Extractor extractor) { final ExtractorMetrics metrics = ExtractorMetrics.create( MetricUtils.buildTimerMap(metricRegistry.getTimers().get(extractor.getCompleteTimerName())), MetricUtils.buildTimerMap(metricRegistry.getTimers().get(extractor.getConditionTimerName())), MetricUtils.buildTimerMap(metricRegistry.getTimers().get(extractor.getExecutionTimerName())), MetricUtils.buildTimerMap(metricRegistry.getTimers().get(extractor.getConverterTimerName())), metricRegistry.getCounters().get(extractor.getConditionHitsCounterName()).getCount(), metricRegistry.getCounters().get(extractor.getConditionMissesCounterName()).getCount()); return ExtractorSummary.create( extractor.getId(), extractor.getTitle(), extractor.getType().toString().toLowerCase(Locale.ENGLISH), extractor.getCursorStrategy().toString().toLowerCase(Locale.ENGLISH), extractor.getSourceField(), extractor.getTargetField(), extractor.getExtractorConfig(), extractor.getCreatorUserId(), extractor.converterConfigMap(), extractor.getConditionType().toString().toLowerCase(Locale.ENGLISH), extractor.getConditionValue(), extractor.getOrder(), extractor.getExceptionCount(), extractor.getConverterExceptionCount(), metrics); }
@Test public void testCompilationQueryMetric() { Timer timer = metricRegistry.getTimers().get(MetricsConstant.HS2_COMPILING_QUERIES); Counter counter = metricRegistry.getCounters() .get(MetricsConstant.ACTIVE_CALLS + MetricsConstant.HS2_COMPILING_QUERIES); assertThat(timer, nullValue()); assertThat(counter, nullValue()); hook.beforeCompile(ctx); timer = metricRegistry.getTimers().get(MetricsConstant.HS2_COMPILING_QUERIES); counter = metricRegistry.getCounters() .get(MetricsConstant.ACTIVE_CALLS + MetricsConstant.HS2_COMPILING_QUERIES); assertThat(timer.getCount(), equalTo(0l)); assertThat(counter.getCount(), equalTo(1l)); hook.afterCompile(ctx, false); timer = metricRegistry.getTimers().get(MetricsConstant.HS2_COMPILING_QUERIES); counter = metricRegistry.getCounters() .get(MetricsConstant.ACTIVE_CALLS + MetricsConstant.HS2_COMPILING_QUERIES); assertThat(timer.getCount(), equalTo(1l)); assertThat(counter.getCount(), equalTo(0l)); }
@Test public void testExecutionQueryMetric() { Timer timer = metricRegistry.getTimers().get(MetricsConstant.HS2_EXECUTING_QUERIES); Counter counter = metricRegistry.getCounters() .get(MetricsConstant.ACTIVE_CALLS + MetricsConstant.HS2_EXECUTING_QUERIES); assertThat(timer, nullValue()); assertThat(counter, nullValue()); hook.beforeExecution(ctx); timer = metricRegistry.getTimers().get(MetricsConstant.HS2_EXECUTING_QUERIES); counter = metricRegistry.getCounters() .get(MetricsConstant.ACTIVE_CALLS + MetricsConstant.HS2_EXECUTING_QUERIES); assertThat(timer.getCount(), equalTo(0l)); assertThat(counter.getCount(), equalTo(1l)); hook.afterExecution(ctx, false); timer = metricRegistry.getTimers().get(MetricsConstant.HS2_EXECUTING_QUERIES); counter = metricRegistry.getCounters() .get(MetricsConstant.ACTIVE_CALLS + MetricsConstant.HS2_EXECUTING_QUERIES); assertThat(timer.getCount(), equalTo(1l)); assertThat(counter.getCount(), equalTo(0l)); }
@Test public void shouldDecorateCheckedSupplier() throws Throwable { // Given the HelloWorldService returns Hello world BDDMockito.given(helloWorldService.returnHelloWorldWithException()).willReturn("Hello world"); // And measure the call with a Timer CheckedFunction0<String> timedSupplier = Timer.decorateCheckedSupplier(timer, helloWorldService::returnHelloWorldWithException); String value = timedSupplier.apply(); assertThat(timer.getMetrics().getNumberOfTotalCalls()).isEqualTo(1); assertThat(timer.getMetrics().getNumberOfSuccessfulCalls()).isEqualTo(1); assertThat(timer.getMetrics().getNumberOfFailedCalls()).isEqualTo(0); assertThat(metricRegistry.getCounters().size()).isEqualTo(2); assertThat(metricRegistry.getTimers().size()).isEqualTo(1); assertThat(value).isEqualTo("Hello world"); // Then the helloWorldService should be invoked 1 time BDDMockito.then(helloWorldService).should(times(1)).returnHelloWorldWithException(); }