下面列出了com.codahale.metrics.MetricRegistry#removeMatching ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* This method starts a separate thread that listens to key expirations events.
*
* @param sessionManager
*/
@Override
public void setSessionManager(final SessionManager sessionManager) {
this.sessionManager = sessionManager;
MetricRegistry metrics = sessionManager.getMetrics();
if (metrics != null) {
// Cleanup old metrics related to this namespace
metrics.removeMatching(new MetricFilter() {
@Override
public boolean matches(String name, Metric metric) {
return name.startsWith(name(RedisConfiguration.METRIC_PREFIX, "redis"));
}
});
if (sticky) {
failoverMetrics = metrics.meter(name(RedisConfiguration.METRIC_PREFIX, namespace, "redis", "failover"));
}
redis.startMonitoring(metrics);
}
expirationManager.startExpiredSessionsTask(sessionManager);
}
public int unregisterGauges(String registryName, String tagSegment) {
if (tagSegment == null) {
return 0;
}
MetricRegistry registry = registry(registryName);
if (registry == null) return 0;
AtomicInteger removed = new AtomicInteger();
registry.removeMatching((name, metric) -> {
if (metric instanceof GaugeWrapper) {
@SuppressWarnings({"rawtypes"})
GaugeWrapper wrapper = (GaugeWrapper) metric;
boolean toRemove = wrapper.getTag().contains(tagSegment);
if (toRemove) {
removed.incrementAndGet();
}
return toRemove;
}
return false;
});
return removed.get();
}
public static void shutdown(MetricRegistry registry) {
registry.removeMatching((name, metric) -> true);
}
/**
* Register a gauge.
* That is, a number of some sort that gets polled at intervals, and
* reported to Graphite. For example, the number of known modules.
* Call like this:
* DropwizardHelper.registerGauge("moduleCount", () -> modules.size());
*
* @param key The key for the metric to report
* @param g A Gauge with a lambda to get the value
*/
public static void registerGauge(String key, Gauge g) {
try {
MetricRegistry reg = SharedMetricRegistries.getOrCreate("okapi");
reg.removeMatching((String name, Metric metric) -> key.equals(name));
reg.register(key, g);
} catch (Exception e) {
logger.warn("registerGauge {}", e.getMessage(), e);
}
}