Java 类com.codahale.metrics.RatioGauge 实例源码

项目:modules    文件:MetricRegistryServiceImpl.java   
/**
 * Register a ratio gauge.
 *
 * @param name the name of the gauge
 * @param numerator a function returning a number represents the value of the numerator
 * @param denominator a function returning a number that represents the value of the denominator
 * @param <T> a type of number
 * @return the registered gauge
 */
@Override
public <T extends Number> Gauge<Double> registerRatioGauge(final String name, Supplier<T> numerator, Supplier<T> denominator) {
    com.codahale.metrics.RatioGauge theGauge;
    try {
        theGauge = metricRegistry.register(name, new RatioGauge() {
            @Override
            protected Ratio getRatio() {
                return Ratio.of(numerator.get().doubleValue(), denominator.get().doubleValue());
            }
        });
    } catch (IllegalArgumentException ex) {
        throw new MetricAlreadyExistsException(String.format(EXCEPTION_TEMPLATE, name), ex);
    }
    return new GaugeAdapter<>(theGauge);
}
项目:vertx-dropwizard-metrics    文件:PoolMetricsImpl.java   
public PoolMetricsImpl(MetricRegistry registry, String baseName, int maxSize) {
  super(registry, baseName);
  this.queueSize = counter("queue-size");
  this.queueDelay = timer("queue-delay");
  this.usage = timer("usage");
  this.inUse = counter("in-use");
  if (maxSize > 0) {
    RatioGauge gauge = new RatioGauge() {
      @Override
      protected Ratio getRatio() {
        return Ratio.of(inUse.getCount(), maxSize);
      }
    };
    gauge(gauge, "pool-ratio");
    gauge(() -> maxSize, "max-pool-size");
  }
}
项目:atlas-deer    文件:InstrumentedQueuedThreadPool.java   
private void createUtilizationStats() {
    utilizationHistogram = new Histogram(createReservoir());
    metricRegistry.register(
            name(getName(), "utilization-percentage-histogram"),
            utilizationHistogram
    );
    utilizationGauge = new RatioGauge() {

        @Override
        protected Ratio getRatio() {
            return Ratio.of(getThreads() - getIdleThreads(), getThreads());
        }
    };

    metricRegistry.register(name(getName(), "utilization-percentage"), utilizationGauge);
}
项目:instrumentor    文件:Gauges.java   
public static Gauge<Double> ratioOf(
        final Supplier<? extends Number> numerator,
        final Supplier<? extends Number> denominator
) {
    return new RatioGauge() {
        @Override
        protected Ratio getRatio() {
            return Ratio.of(
                    numerator.get().doubleValue(),
                    denominator.get().doubleValue());
        }
    };
}
项目:xlator    文件:JettyConfig.java   
protected void configureMetrics(QueuedThreadPool pool) {
    // metrics
    metricRegistry.register(name(pool.getName(), "utilization"), new RatioGauge() {
        @Override
        protected Ratio getRatio() {
            return Ratio.of(pool.getThreads() - pool.getIdleThreads(), pool.getThreads());
        }
    });
    metricRegistry.register(name(pool.getName(), "utilization-max"), new RatioGauge() {
        @Override
        protected Ratio getRatio() {
            return Ratio.of(pool.getThreads() - pool.getIdleThreads(), pool.getMaxThreads());
        }
    });
    metricRegistry.register(name(pool.getName(), "size"), new Gauge<Integer>() {
        @Override
        public Integer getValue() {
            return pool.getThreads();
        }
    });
    metricRegistry.register(name(pool.getName(), "jobs"), new Gauge<Integer>() {
        @Override
        public Integer getValue() {
            // This assumes the QueuedThreadPool is using a BlockingArrayQueue or
            // ArrayBlockingQueue for its queue, and is therefore a constant-time operation.
            return pool.getQueueSize();
        }
    });
}
项目:bootique-jetty    文件:InstrumentedQueuedThreadPool.java   
protected double getUtilization() {

        // utilization is:
        //     (all_acceptor_t + all_selector_t + active_request_t) / maxT

        // This is not readily apparent from the Jetty API below. An explanation:
        //     getThreads()                    == all_acceptor_t + all_selector_t + active_request_t + idle_request_t
        // hence
        //     getThreads() - getIdleThreads() == all_acceptor_t + all_selector_t + active_request_t

        return RatioGauge.Ratio.of(getThreads() - getIdleThreads(), getMaxThreads()).getValue();
    }
项目:hummingbird-framework    文件:AbstractFog.java   
/**
 * Instantiates a new Abstract fog.
 */
public AbstractFog() {

    MetricsManager.metrics.register(name(this.getClass(), "hit", "ratio"),
            new RatioGauge() {
                @Override
                protected Ratio getRatio() {
                    return Ratio.of(hits.getCount(), timer.getCount());
                }
            });
}
项目:scylla-tools-java    文件:CQLMetrics.java   
public CQLMetrics()
{
    regularStatementsExecuted = Metrics.counter(factory.createMetricName("RegularStatementsExecuted"));
    preparedStatementsExecuted = Metrics.counter(factory.createMetricName("PreparedStatementsExecuted"));
    preparedStatementsEvicted = Metrics.counter(factory.createMetricName("PreparedStatementsEvicted"));

    preparedStatementsCount = Metrics.register(factory.createMetricName("PreparedStatementsCount"), new Gauge<Integer>()
    {
        public Integer getValue()
        {
            return QueryProcessor.preparedStatementsCount();
        }
    });
    preparedStatementsRatio = Metrics.register(factory.createMetricName("PreparedStatementsRatio"), new RatioGauge()
    {
        public Ratio getRatio()
        {
            return Ratio.of(getNumerator(), getDenominator());
        }

        public double getNumerator()
        {
            return preparedStatementsExecuted.getCount();
        }

        public double getDenominator()
        {
            return regularStatementsExecuted.getCount() + preparedStatementsExecuted.getCount();
        }
    });
}
项目:stagemonitor    文件:PooledResourceMetricsRegisterer.java   
public static void registerPooledResource(final PooledResource pooledResource, Metric2Registry registry) {
    MetricName name = pooledResource.getName();
    registry.register(name.withTag("type", "active"), new Gauge<Integer>() {
        @Override
        public Integer getValue() {
            return pooledResource.getPoolNumActive();
        }
    });
    registry.register(name.withTag("type", "count"), new Gauge<Integer>() {
        @Override
        public Integer getValue() {
            return pooledResource.getActualPoolSize();
        }
    });
    registry.register(name.withTag("type", "max"), new Gauge<Integer>() {
        @Override
        public Integer getValue() {
            return pooledResource.getMaxPoolSize();
        }
    });
    if (pooledResource.getNumTasksPending() != null) {
        registry.register(name.withTag("type", "queued"), new Gauge<Integer>() {
            @Override
            public Integer getValue() {
                return pooledResource.getNumTasksPending();
            }
        });
    }
    registry.register(name.withTag("type", "usage"), new RatioGauge() {
        @Override
        protected Ratio getRatio() {
            return Ratio.of(pooledResource.getPoolNumActive() * 100.0, pooledResource.getMaxPoolSize());
        }
    });
}
项目:stagemonitor    文件:EhCacheMetricSet.java   
@Override
public Map<MetricName, Metric> getMetrics() {
    final Map<MetricName, Metric> metrics = new HashMap<MetricName, Metric>();

    metrics.put(name("cache_hit_ratio").tag("cache_name", cacheName).tier("All").build(), new RatioGauge() {
        @Override
        public Ratio getRatio() {
            return cacheUsageListener.getHitRatio1Min();
        }
    });

    metrics.put(name("cache_size_count").tag("cache_name", cacheName).tier("All").build(), new Gauge<Long>() {
        @Override
        public Long getValue() {
            return cache.getLiveCacheStatistics().getSize();
        }
    });

    metrics.put(name("cache_size_bytes").tag("cache_name", cacheName).tier("All").build(), new Gauge<Long>() {
        @Override
        public Long getValue() {
            return cache.getLiveCacheStatistics().getLocalDiskSizeInBytes() +
                    cache.getLiveCacheStatistics().getLocalHeapSizeInBytes() +
                    cache.getLiveCacheStatistics().getLocalOffHeapSizeInBytes();
        }
    });

    return metrics;
}
项目:fili    文件:AbstractBinderFactory.java   
/**
 * Register global gauges.
 */
protected final void setupGauges() {
    // Gauges are registered here since they should be configured only once at startup.
    MetricRegistry metricRegistry = MetricRegistryFactory.getRegistry();
    Map<String, Metric> metrics = metricRegistry.getMetrics();

    if (!metrics.containsKey(METER_CACHE_HIT_RATIO)) {
        metricRegistry.register(
                METER_CACHE_HIT_RATIO,
                new RatioGauge() {
                    @Override
                    protected Ratio getRatio() {
                        return CACHE_REQUESTS.getCount() != 0
                                ? Ratio.of(CACHE_HITS.getCount(), CACHE_REQUESTS.getCount())
                                : Ratio.of(0, 1);
                    }
                }
        );
    }

    if (!metrics.containsKey(METER_SPLITS_TOTAL_RATIO)) {
        metricRegistry.register(
                METER_SPLITS_TOTAL_RATIO,
                new RatioGauge() {
                    @Override
                    protected Ratio getRatio() {
                        return QUERY_REQUEST_TOTAL.getCount() != 0
                                ? Ratio.of(SPLIT_QUERIES.getCount(), QUERY_REQUEST_TOTAL.getCount())
                                : Ratio.of(0, 1);
                    }
                }
        );
    }

    if (!metrics.containsKey(METER_SPLITS_RATIO)) {
        metricRegistry.register(
                METER_SPLITS_RATIO,
                new RatioGauge() {
                    @Override
                    protected Ratio getRatio() {
                        return SPLITS.getCount() != 0
                                ? Ratio.of(SPLIT_QUERIES.getCount(), SPLITS.getCount())
                                : Ratio.of(0, 1);
                    }
                }
        );
    }

    if (!metrics.containsKey(JVM_UPTIME)) {
        metricRegistry.register(
                JVM_UPTIME,
                (Gauge<Long>) () -> ManagementFactory.getRuntimeMXBean().getUptime()
        );
    }
}
项目:metrics-okhttp    文件:InstrumentedOkHttpClient.java   
private void instrumentHttpCache() {
  if (cache() == null) return;

  registry.register(metricId("cache-request-count"), new Gauge<Integer>() {
    @Override public Integer getValue() {
      // The number of HTTP requests issued since this cache was created.
      return rawClient.cache().requestCount();
    }
  });
  registry.register(metricId("cache-hit-count"), new Gauge<Integer>() {
    @Override public Integer getValue() {
      // ... the number of those requests that required network use.
      return rawClient.cache().hitCount();
    }
  });
  registry.register(metricId("cache-network-count"), new Gauge<Integer>() {
    @Override public Integer getValue() {
      // ... the number of those requests whose responses were served by the cache.
      return rawClient.cache().networkCount();
    }
  });
  registry.register(metricId("cache-write-success-count"), new Gauge<Integer>() {
    @Override public Integer getValue() {
      return rawClient.cache().writeSuccessCount();
    }
  });
  registry.register(metricId("cache-write-abort-count"), new Gauge<Integer>() {
    @Override public Integer getValue() {
      return rawClient.cache().writeAbortCount();
    }
  });
  final Gauge<Long> currentCacheSize = new Gauge<Long>() {
    @Override public Long getValue() {
      try {
        return rawClient.cache().size();
      } catch (IOException ex) {
        LOG.error(ex.getMessage(), ex);
        return -1L;
      }
    }
  };
  final Gauge<Long> maxCacheSize = new Gauge<Long>() {
    @Override public Long getValue() {
      return rawClient.cache().maxSize();
    }
  };
  registry.register(metricId("cache-current-size"), currentCacheSize);
  registry.register(metricId("cache-max-size"), maxCacheSize);
  registry.register(metricId("cache-size"), new RatioGauge() {
    @Override protected Ratio getRatio() {
      return Ratio.of(currentCacheSize.getValue(), maxCacheSize.getValue());
    }
  });
}
项目:folsom    文件:SemanticFolsomMetrics.java   
public SemanticFolsomMetrics(final SemanticMetricRegistry registry, final MetricId baseMetricId) {

    this.registry = registry;

    this.id = baseMetricId.tagged("what", "memcache-results",
                                  "component", "memcache-client");

    final MetricId meterId = id.tagged("unit", "operations");

    MetricId getId = id.tagged("operation", "get");
    this.gets = registry.timer(getId);
    // successful gets are broken down by whether a result was found in the cache or not.
    // the two meters can be summed to count total number of successes.
    MetricId getMetersId = MetricId.join(getId, meterId);
    this.getHits = registry.meter(getMetersId.tagged("result", "success", "cache-result", "hit"));
    this.getMisses = registry.meter(
            getMetersId.tagged("result", "success", "cache-result", "miss"));
    this.getFailures = registry.meter(getMetersId.tagged("result", "failure"));

    // ratio of cache hits to total attempts
    hitRatio = new RatioGauge() {
      @Override
      protected Ratio getRatio() {
        return Ratio.of(getHits.getFiveMinuteRate(),
            gets.getFiveMinuteRate() + multigetItems.getFiveMinuteRate());
      }
    };
    // overwrite the 'what' as this metric doesn't make sense to be aggregated against any of the
    // other metrics
    registry.register(getId.tagged("what", "memcache-hit-ratio", "unit", "%"), hitRatio);

    MetricId setId = id.tagged("operation", "set");
    this.sets = registry.timer(setId);
    MetricId setMetersId = MetricId.join(setId, meterId);
    this.setSuccesses = registry.meter(setMetersId.tagged("result", "success"));
    this.setFailures = registry.meter(setMetersId.tagged("result", "failure"));

    MetricId multigetId = id.tagged("operation", "multiget");
    this.multigets = registry.timer(multigetId);
    MetricId multigetMetersId = MetricId.join(multigetId, meterId);
    this.multigetSuccesses = registry.meter(multigetMetersId.tagged("result", "success"));
    this.multigetFailures = registry.meter(multigetMetersId.tagged("result", "failure"));

    // doesn't seem useful to export
    this.multigetItems = new Meter();

    MetricId deleteId = id.tagged("operation", "delete");
    this.deletes = registry.timer(deleteId);
    MetricId deleteMetersId = MetricId.join(deleteId, meterId);
    this.deleteSuccesses = registry.meter(deleteMetersId.tagged("result", "success"));
    this.deleteFailures = registry.meter(deleteMetersId.tagged("result", "failure"));

    MetricId incrDecrId = id.tagged("operation", "incr-decr");
    this.incrDecrs = registry.timer(incrDecrId);
    MetricId incrDecrMetersId = MetricId.join(incrDecrId, meterId);
    this.incrDecrSuccesses = registry.meter(incrDecrMetersId.tagged("result", "success"));
    this.incrDecrFailures = registry.meter(incrDecrMetersId.tagged("result", "failure"));

    MetricId touchId = id.tagged("operation", "touch");
    this.touches = registry.timer(touchId);
    MetricId touchMetersId = MetricId.join(touchId, meterId);
    this.touchSuccesses = registry.meter(touchMetersId.tagged("result", "success"));
    this.touchFailures = registry.meter(touchMetersId.tagged("result", "failure"));

    createConnectionCounterGauge();
  }
项目:folsom    文件:SemanticFolsomMetrics.java   
public RatioGauge getHitRatio() {
  return hitRatio;
}
项目:scylla-tools-java    文件:CacheMetrics.java   
/**
 * Create metrics for given cache.
 *
 * @param type Type of Cache to identify metrics.
 * @param cache Cache to measure metrics
 */
public CacheMetrics(String type, final ICache cache)
{
    MetricNameFactory factory = new DefaultNameFactory("Cache", type);

    capacity = Metrics.register(factory.createMetricName("Capacity"), new Gauge<Long>()
    {
        public Long getValue()
        {
            return cache.capacity();
        }
    });
    hits = Metrics.meter(factory.createMetricName("Hits"));
    requests = Metrics.meter(factory.createMetricName("Requests"));
    hitRate = Metrics.register(factory.createMetricName("HitRate"), new RatioGauge()
    {
        @Override
        public Ratio getRatio()
        {
            return Ratio.of(hits.getCount(), requests.getCount());
        }
    });
    oneMinuteHitRate = Metrics.register(factory.createMetricName("OneMinuteHitRate"), new RatioGauge()
    {
        protected Ratio getRatio()
        {
            return Ratio.of(hits.getOneMinuteRate(), requests.getOneMinuteRate());
        }
    });
    fiveMinuteHitRate = Metrics.register(factory.createMetricName("FiveMinuteHitRate"), new RatioGauge()
    {
        protected Ratio getRatio()
        {
            return Ratio.of(hits.getFiveMinuteRate(), requests.getFiveMinuteRate());
        }
    });
    fifteenMinuteHitRate = Metrics.register(factory.createMetricName("FifteenMinuteHitRate"), new RatioGauge()
    {
        protected Ratio getRatio()
        {
            return Ratio.of(hits.getFifteenMinuteRate(), requests.getFifteenMinuteRate());
        }
    });
    size = Metrics.register(factory.createMetricName("Size"), new Gauge<Long>()
    {
        public Long getValue()
        {
            return cache.weightedSize();
        }
    });
    entries = Metrics.register(factory.createMetricName("Entries"), new Gauge<Integer>()
    {
        public Integer getValue()
        {
            return cache.size();
        }
    });
}
项目:floe2    文件:StateSizeMonitor.java   
/**
 * @return the last one minute rate of full to incremental state size ratio.
 */
@Override
protected final Ratio getRatio() {
    return RatioGauge.Ratio.of(incrementStateSize.getOneMinuteRate(),
            totalStateSize.getOneMinuteRate());
}
项目:hbase    文件:MetricsConnection.java   
MetricsConnection(final ConnectionImplementation conn) {
  this.scope = conn.toString();
  this.registry = new MetricRegistry();

  this.registry.register(getExecutorPoolName(),
      new RatioGauge() {
        @Override
        protected Ratio getRatio() {
          ThreadPoolExecutor batchPool = (ThreadPoolExecutor) conn.getCurrentBatchPool();
          if (batchPool == null) {
            return Ratio.of(0, 0);
          }
          return Ratio.of(batchPool.getActiveCount(), batchPool.getMaximumPoolSize());
        }
      });
  this.registry.register(getMetaPoolName(),
      new RatioGauge() {
        @Override
        protected Ratio getRatio() {
          ThreadPoolExecutor metaPool = (ThreadPoolExecutor) conn.getCurrentMetaLookupPool();
          if (metaPool == null) {
            return Ratio.of(0, 0);
          }
          return Ratio.of(metaPool.getActiveCount(), metaPool.getMaximumPoolSize());
        }
      });
  this.metaCacheHits = registry.counter(name(this.getClass(), "metaCacheHits", scope));
  this.metaCacheMisses = registry.counter(name(this.getClass(), "metaCacheMisses", scope));
  this.metaCacheNumClearServer = registry.counter(name(this.getClass(),
    "metaCacheNumClearServer", scope));
  this.metaCacheNumClearRegion = registry.counter(name(this.getClass(),
    "metaCacheNumClearRegion", scope));
  this.hedgedReadOps = registry.counter(name(this.getClass(), "hedgedReadOps", scope));
  this.hedgedReadWin = registry.counter(name(this.getClass(), "hedgedReadWin", scope));
  this.getTracker = new CallTracker(this.registry, "Get", scope);
  this.scanTracker = new CallTracker(this.registry, "Scan", scope);
  this.appendTracker = new CallTracker(this.registry, "Mutate", "Append", scope);
  this.deleteTracker = new CallTracker(this.registry, "Mutate", "Delete", scope);
  this.incrementTracker = new CallTracker(this.registry, "Mutate", "Increment", scope);
  this.putTracker = new CallTracker(this.registry, "Mutate", "Put", scope);
  this.multiTracker = new CallTracker(this.registry, "Multi", scope);
  this.runnerStats = new RunnerStats(this.registry);
  this.concurrentCallsPerServerHist = registry.histogram(name(MetricsConnection.class, 
    "concurrentCallsPerServer", scope));

  this.reporter = JmxReporter.forRegistry(this.registry).build();
  this.reporter.start();
}
项目:hbase    文件:TestMetricsConnection.java   
@Test
public void testStaticMetrics() throws IOException {
  final byte[] foo = Bytes.toBytes("foo");
  final RegionSpecifier region = RegionSpecifier.newBuilder()
      .setValue(ByteString.EMPTY)
      .setType(RegionSpecifierType.REGION_NAME)
      .build();
  final int loop = 5;

  for (int i = 0; i < loop; i++) {
    METRICS.updateRpc(
        ClientService.getDescriptor().findMethodByName("Get"),
        GetRequest.getDefaultInstance(),
        MetricsConnection.newCallStats());
    METRICS.updateRpc(
        ClientService.getDescriptor().findMethodByName("Scan"),
        ScanRequest.getDefaultInstance(),
        MetricsConnection.newCallStats());
    METRICS.updateRpc(
        ClientService.getDescriptor().findMethodByName("Multi"),
        MultiRequest.getDefaultInstance(),
        MetricsConnection.newCallStats());
    METRICS.updateRpc(
        ClientService.getDescriptor().findMethodByName("Mutate"),
        MutateRequest.newBuilder()
            .setMutation(ProtobufUtil.toMutation(MutationType.APPEND, new Append(foo)))
            .setRegion(region)
            .build(),
        MetricsConnection.newCallStats());
    METRICS.updateRpc(
        ClientService.getDescriptor().findMethodByName("Mutate"),
        MutateRequest.newBuilder()
            .setMutation(ProtobufUtil.toMutation(MutationType.DELETE, new Delete(foo)))
            .setRegion(region)
            .build(),
        MetricsConnection.newCallStats());
    METRICS.updateRpc(
        ClientService.getDescriptor().findMethodByName("Mutate"),
        MutateRequest.newBuilder()
            .setMutation(ProtobufUtil.toMutation(MutationType.INCREMENT, new Increment(foo)))
            .setRegion(region)
            .build(),
        MetricsConnection.newCallStats());
    METRICS.updateRpc(
        ClientService.getDescriptor().findMethodByName("Mutate"),
        MutateRequest.newBuilder()
            .setMutation(ProtobufUtil.toMutation(MutationType.PUT, new Put(foo)))
            .setRegion(region)
            .build(),
        MetricsConnection.newCallStats());
  }
  for (MetricsConnection.CallTracker t : new MetricsConnection.CallTracker[] {
      METRICS.getTracker, METRICS.scanTracker, METRICS.multiTracker, METRICS.appendTracker,
      METRICS.deleteTracker, METRICS.incrementTracker, METRICS.putTracker
  }) {
    assertEquals("Failed to invoke callTimer on " + t, loop, t.callTimer.getCount());
    assertEquals("Failed to invoke reqHist on " + t, loop, t.reqHist.getCount());
    assertEquals("Failed to invoke respHist on " + t, loop, t.respHist.getCount());
  }
  RatioGauge executorMetrics = (RatioGauge) METRICS.getMetricRegistry()
          .getMetrics().get(METRICS.getExecutorPoolName());
  RatioGauge metaMetrics = (RatioGauge) METRICS.getMetricRegistry()
          .getMetrics().get(METRICS.getMetaPoolName());
  assertEquals(Ratio.of(0, 3).getValue(), executorMetrics.getValue(), 0);
  assertEquals(Double.NaN, metaMetrics.getValue(), 0);
}
项目:monasca-common    文件:InstrumentedThreadPoolExecutor.java   
InstrumentedThreadPoolExecutor(MetricRegistry metricRegistry, String name, int corePoolSize,
    int maximumPoolSize, long keepAliveTime, TimeUnit unit,
    final BlockingQueue<Runnable> workQueue, ThreadFactory factory) {
  super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, factory);
  this.name = name;
  requestRate = metricRegistry.meter(MetricRegistry.name(getClass(), name, "request"));
  rejectedRate = metricRegistry.meter(MetricRegistry.name(getClass(), name, "rejected"));
  executionTimer = metricRegistry.timer(MetricRegistry.name(getClass(), name, "execution"));
  metricRegistry.register(MetricRegistry.name(getClass(), name, "queue.size"),
      new Gauge<Integer>() {
        @Override
        public Integer getValue() {
          return getQueue().size();
        }
      });
  metricRegistry.register(MetricRegistry.name(getClass(), name, "threads.count"),
      new Gauge<Integer>() {
        @Override
        public Integer getValue() {
          return getPoolSize();
        }
      });
  metricRegistry.register(MetricRegistry.name(getClass(), name, "threads.active"),
      new Gauge<Integer>() {
        @Override
        public Integer getValue() {
          return getActiveCount();
        }
      });
  metricRegistry.register(MetricRegistry.name(getClass(), name, "threads.idle"),
      new Gauge<Integer>() {
        @Override
        public Integer getValue() {
          return getPoolSize() - getActiveCount();
        }
      });
  metricRegistry.register(MetricRegistry.name(getClass(), name, "threads.percent-active"),
      new RatioGauge() {
        @Override
        protected Ratio getRatio() {
          return Ratio.of(getPoolSize(), getActiveCount());
        }
      });

  setRejectedExecutionHandler(new RejectedExecutionHandler() {
    @Override
    public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
      rejectedRate.mark();
      if (!workQueue.offer(r))
        log.warn("Thread pool {} rejected work.", InstrumentedThreadPoolExecutor.this.name);
      throw new RejectedExecutionException();
    }
  });
}
项目:stagemonitor    文件:SwapMetricSet.java   
@Override
public Map<MetricName, Metric> getMetrics() {
    Map<MetricName, Metric> metrics = new HashMap<MetricName, Metric>();
    metrics.put(name("swap_usage").type("free").build(), new Gauge<Long>() {
        @Override
        public Long getValue() {
            return getSnapshot().getFree();
        }
    });
    metrics.put(name("swap_usage").type("used").build(), new Gauge<Long>() {
        @Override
        public Long getValue() {
            return getSnapshot().getUsed();
        }
    });
    metrics.put(name("swap_usage").type("total").build(), new Gauge<Long>() {
        @Override
        public Long getValue() {
            return getSnapshot().getTotal();
        }
    });
    metrics.put(name("swap_usage_percent").build(), new RatioGauge() {
        @Override
        protected Ratio getRatio() {
            return Ratio.of(getSnapshot().getUsed(), getSnapshot().getTotal() * 100.0);
        }
    });

    metrics.put(name("swap_pages").type("in").build(), new Gauge<Long>() {
        @Override
        public Long getValue() {
            return getSnapshot().getPageIn();
        }
    });
    metrics.put(name("swap_pages").type("out").build(), new Gauge<Long>() {
        @Override
        public Long getValue() {
            return getSnapshot().getPageOut();
        }
    });
    return metrics;
}