private void registerContainerAppNumMetrics() { metrics.register( "variable.running.application", new Gauge<Integer>() { @Override public Integer getValue() { if (scheduler == null || scheduler.getRootQueueMetrics() == null) { return 0; } else { return scheduler.getRootQueueMetrics().getAppsRunning(); } } }); metrics.register( "variable.running.container", new Gauge<Integer>() { @Override public Integer getValue() { if (scheduler == null || scheduler.getRootQueueMetrics() == null) { return 0; } else { return scheduler.getRootQueueMetrics().getAllocatedContainers(); } } }); }
private void registerJvmMetrics() { // add JVM gauges metrics.register( "variable.jvm.free.memory", new Gauge<Long>() { @Override public Long getValue() { return Runtime.getRuntime().freeMemory(); } }); metrics.register( "variable.jvm.max.memory", new Gauge<Long>() { @Override public Long getValue() { return Runtime.getRuntime().maxMemory(); } }); metrics.register( "variable.jvm.total.memory", new Gauge<Long>() { @Override public Long getValue() { return Runtime.getRuntime().totalMemory(); } }); }
@Override protected MetricRegistry getMetricRegistry() { METRIC_REGISTRY.register(name("jvm", "gc"), new GarbageCollectorMetricSet()); METRIC_REGISTRY.register(name("jvm", "memory"), new MemoryUsageGaugeSet()); METRIC_REGISTRY.register(name("jvm", "thread-states"), new ThreadStatesGaugeSet()); HystrixCodaHaleMetricsPublisher hystrixCodaHaleMetricsPublisher = new HystrixCodaHaleMetricsPublisher(METRIC_REGISTRY); HystrixPlugins.getInstance().registerMetricsPublisher(hystrixCodaHaleMetricsPublisher); return METRIC_REGISTRY; }
private void registerClusterResourceMetrics() { metrics.register( "variable.cluster.allocated.memory", new Gauge<Integer>() { @Override public Integer getValue() { if (scheduler == null || scheduler.getRootQueueMetrics() == null) { return 0; } else { return scheduler.getRootQueueMetrics().getAllocatedMB(); } } }); metrics.register( "variable.cluster.allocated.vcores", new Gauge<Integer>() { @Override public Integer getValue() { if (scheduler == null || scheduler.getRootQueueMetrics() == null) { return 0; } else { return scheduler.getRootQueueMetrics().getAllocatedVirtualCores(); } } }); metrics.register( "variable.cluster.available.memory", new Gauge<Integer>() { @Override public Integer getValue() { if (scheduler == null || scheduler.getRootQueueMetrics() == null) { return 0; } else { return scheduler.getRootQueueMetrics().getAvailableMB(); } } }); metrics.register( "variable.cluster.available.vcores", new Gauge<Integer>() { @Override public Integer getValue() { if (scheduler == null || scheduler.getRootQueueMetrics() == null) { return 0; } else { return scheduler.getRootQueueMetrics().getAvailableVirtualCores(); } } }); }
@PostConstruct public void init() { log.debug("Registering JVM gauges"); metricRegistry.register(PROP_METRIC_REG_JVM_MEMORY, new MemoryUsageGaugeSet()); metricRegistry.register(PROP_METRIC_REG_JVM_GARBAGE, new GarbageCollectorMetricSet()); metricRegistry.register(PROP_METRIC_REG_JVM_THREADS, new ThreadStatesGaugeSet()); metricRegistry.register(PROP_METRIC_REG_JVM_FILES, new FileDescriptorRatioGauge()); metricRegistry.register( PROP_METRIC_REG_JVM_BUFFERS, new BufferPoolMetricSet(ManagementFactory.getPlatformMBeanServer())); if (jHipsterProperties.getMetrics().getJmx().isEnabled()) { log.debug("Initializing Metrics JMX reporting"); JmxReporter jmxReporter = JmxReporter.forRegistry(metricRegistry).build(); jmxReporter.start(); } }
/** * Registers a Source. * * @param source the source to register */ public void registerSource(Source source) { mSources.add(source); try { mMetricRegistry.register(source.getName(), source.getMetricRegistry()); } catch (IllegalArgumentException e) { LOG.warn("Metrics already registered. Exception:" + e.getMessage()); } }
private void registerSchedulerMetrics() { samplerLock.lock(); try { // counters for scheduler operations schedulerAllocateCounter = metrics.counter("counter.scheduler.operation.allocate"); schedulerHandleCounter = metrics.counter("counter.scheduler.operation.handle"); schedulerHandleCounterMap = new HashMap<SchedulerEventType, Counter>(); for (SchedulerEventType e : SchedulerEventType.values()) { Counter counter = metrics.counter("counter.scheduler.operation.handle." + e); schedulerHandleCounterMap.put(e, counter); } // timers for scheduler operations int timeWindowSize = conf.getInt( SLSConfiguration.METRICS_TIMER_WINDOW_SIZE, SLSConfiguration.METRICS_TIMER_WINDOW_SIZE_DEFAULT); schedulerAllocateTimer = new Timer(new SlidingWindowReservoir(timeWindowSize)); schedulerHandleTimer = new Timer(new SlidingWindowReservoir(timeWindowSize)); schedulerHandleTimerMap = new HashMap<SchedulerEventType, Timer>(); for (SchedulerEventType e : SchedulerEventType.values()) { Timer timer = new Timer(new SlidingWindowReservoir(timeWindowSize)); schedulerHandleTimerMap.put(e, timer); } // histogram for scheduler operations (Samplers) schedulerHistogramList = new ArrayList<Histogram>(); histogramTimerMap = new HashMap<Histogram, Timer>(); Histogram schedulerAllocateHistogram = new Histogram(new SlidingWindowReservoir(SAMPLING_SIZE)); metrics.register("sampler.scheduler.operation.allocate.timecost", schedulerAllocateHistogram); schedulerHistogramList.add(schedulerAllocateHistogram); histogramTimerMap.put(schedulerAllocateHistogram, schedulerAllocateTimer); Histogram schedulerHandleHistogram = new Histogram(new SlidingWindowReservoir(SAMPLING_SIZE)); metrics.register("sampler.scheduler.operation.handle.timecost", schedulerHandleHistogram); schedulerHistogramList.add(schedulerHandleHistogram); histogramTimerMap.put(schedulerHandleHistogram, schedulerHandleTimer); for (SchedulerEventType e : SchedulerEventType.values()) { Histogram histogram = new Histogram(new SlidingWindowReservoir(SAMPLING_SIZE)); metrics.register("sampler.scheduler.operation.handle." + e + ".timecost", histogram); schedulerHistogramList.add(histogram); histogramTimerMap.put(histogram, schedulerHandleTimerMap.get(e)); } } finally { samplerLock.unlock(); } }
public CodaHaleMetricsTracker( final String poolName, final PoolStats poolStats, final MetricRegistry registry) { this.poolName = poolName; this.registry = registry; this.connectionObtainTimer = registry.timer(MetricRegistry.name(poolName, "pool", "Wait")); this.connectionUsage = registry.histogram(MetricRegistry.name(poolName, "pool", "Usage")); this.connectionTimeoutMeter = registry.meter(MetricRegistry.name(poolName, "pool", "ConnectionTimeoutRate")); registry.register( MetricRegistry.name(poolName, "pool", "TotalConnections"), new Gauge<Integer>() { @Override public Integer getValue() { return poolStats.getTotalConnections(); } }); registry.register( MetricRegistry.name(poolName, "pool", "IdleConnections"), new Gauge<Integer>() { @Override public Integer getValue() { return poolStats.getIdleConnections(); } }); registry.register( MetricRegistry.name(poolName, "pool", "ActiveConnections"), new Gauge<Integer>() { @Override public Integer getValue() { return poolStats.getActiveConnections(); } }); registry.register( MetricRegistry.name(poolName, "pool", "PendingConnections"), new Gauge<Integer>() { @Override public Integer getValue() { return poolStats.getPendingThreads(); } }); }
// -- Private private void registerAll(String prefix, MetricSet metricSet, MetricRegistry registry) { for (Map.Entry<String, Metric> entry : metricSet.getMetrics().entrySet()) { if (entry.getValue() instanceof MetricSet) { registerAll(prefix + "." + entry.getKey(), (MetricSet) entry.getValue(), registry); } else { registry.register(prefix + "." + entry.getKey(), entry.getValue()); } } }
public static void main(String[] args) { // create some various metrics and update them MetricRegistry registry = new MetricRegistry(); final AtomicInteger gaugeInteger = new AtomicInteger(); registry.register( name("gauge"), new Gauge<Integer>() { @Override public Integer getValue() { return gaugeInteger.get(); } }); final Counter counter = registry.counter(name("counter")); final Histogram histogram = registry.histogram(name("histogram")); final Meter meter = registry.meter(name("meter")); final Timer timer = registry.timer(name("timer")); NewRelicReporter reporter = new NewRelicReporter( registry, "new relic reporter", MetricFilter.ALL, new AllEnabledMetricAttributeFilter(), TimeUnit.SECONDS, TimeUnit.MILLISECONDS, "foo/"); reporter.start(60, TimeUnit.SECONDS); ScheduledExecutorService svc = Executors.newScheduledThreadPool(1); final Random random = new Random(); svc.scheduleAtFixedRate( new Runnable() { @Override public void run() { System.out.println("Updating"); gaugeInteger.incrementAndGet(); counter.inc(); histogram.update(random.nextInt(10)); meter.mark(); timer.update(random.nextInt(10), TimeUnit.MILLISECONDS); } }, 0, 1, TimeUnit.SECONDS); }
protected void updateMetrics( Map<String, Double> metrics, Optional<String> processorName, Map<String, String> tags) { for (Map.Entry<String, Double> entry : metrics.entrySet()) { final String metricName = buildMetricName(processorName, entry.getKey()); logger.debug(metricName + ": " + entry.getValue()); // if metric is not registered yet - register it if (!metricsMap.containsKey(metricName)) { metricsMap.put(metricName, new AtomicDouble(entry.getValue())); metricRegistry.register(metricName, new MetricGauge(metricName, tags)); } // set real time value to metrics map metricsMap.get(metricName).set(entry.getValue()); } }
private Timer timer(String name) { Timer timer = metricRegistry.getTimers().get(name); if (timer != null) { return timer; } try { return metricRegistry.register( name, new Timer(new SlidingTimeWindowReservoir(timerReservoirSeconds.get(), TimeUnit.SECONDS))); } catch (IllegalArgumentException e) { // timer already exists. this happens due to race condition. its fine. return metricRegistry.getTimers().get(name); } }
public InstrumentedQueuedThreadPool(MetricRegistry registry) { super(); registry.register( name(QueuedThreadPool.class, "percent-idle"), new RatioGauge() { @Override protected Ratio getRatio() { return Ratio.of(getIdleThreads(), getThreads()); } }); registry.register( name(QueuedThreadPool.class, "active-threads"), new Gauge<Integer>() { @Override public Integer getValue() { return getThreads(); } }); registry.register( name(QueuedThreadPool.class, "idle-threads"), new Gauge<Integer>() { @Override public Integer getValue() { return getIdleThreads(); } }); registry.register( name(QueuedThreadPool.class, "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 getQueue().size(); } }); }
public void registerGauges(final BlockWorker worker) { mMetricRegistry.register( MetricRegistry.name("CapacityTotal"), new Gauge<Long>() { @Override public Long getValue() { return worker.getStoreMeta().getCapacityBytes(); } }); mMetricRegistry.register( MetricRegistry.name("CapacityUsed"), new Gauge<Long>() { @Override public Long getValue() { return worker.getStoreMeta().getUsedBytes(); } }); mMetricRegistry.register( MetricRegistry.name("CapacityFree"), new Gauge<Long>() { @Override public Long getValue() { return worker.getStoreMeta().getCapacityBytes() - worker.getStoreMeta().getUsedBytes(); } }); mMetricRegistry.register( MetricRegistry.name("BlocksCached"), new Gauge<Integer>() { @Override public Integer getValue() { return worker.getStoreMeta().getNumberOfBlocks(); } }); }
@Override public final void initialize(OpletContext<T, T> context) { super.initialize(context); this.meter = new Meter(); this.gauge = new Gauge<Long>() { @Override public Long getValue() { return System.currentTimeMillis(); } }; MetricRegistry registry = context.getService(MetricRegistry.class); if (registry != null) { registry.register(context.uniquify("testMeter"), meter); registry.register(context.uniquify("testGauge"), gauge); } }
@Test public void testGauge() { System.out.println("******************************* GAUGE *******************************"); gauge = new Gauge<Integer>() { @Override public Integer getValue() { return count++; } }; registry.register("gauge", gauge); try { for (int i = 0; i < ITER_COUNT; i++) { gauge.getValue(); Thread.sleep(SLEEP_MS); } } catch (InterruptedException ex) { Thread.currentThread().interrupt(); } }
@Test public void testGauge() throws Exception { registry.register( name("foo", "bar"), new Gauge<Integer>() { @Override public Integer getValue() { return 1234; } }); reportAndRefresh(); SearchResponse searchResponse = client().prepareSearch(indexWithDate).setTypes("gauge").execute().actionGet(); org.assertj.core.api.Assertions.assertThat(searchResponse.getHits().totalHits()).isEqualTo(1L); Map<String, Object> hit = searchResponse.getHits().getAt(0).sourceAsMap(); assertTimestamp(hit); assertKey(hit, "name", prefix + ".foo.bar"); assertKey(hit, "value", 1234); assertKey(hit, "host", "localhost"); }
/** * Registers a gauge if it has not been registered. * * @param name the gauge name * @param metric the gauge * @param <T> the type */ public static synchronized <T> void registerGaugeIfAbsent(String name, Gauge<T> metric) { if (!METRIC_REGISTRY.getGauges().containsKey(name)) { METRIC_REGISTRY.register(name, metric); } }
public void logState() { Session s = sessions[0]; Cluster cluster = s.getCluster(); LoadBalancingPolicy lbPolicy = cluster.getConfiguration().getPolicies().getLoadBalancingPolicy(); int count = 0; String keyspace = null; for (final Session session : sessions) { SessionManager sessionM = (SessionManager) session; Session.State sessionState = session.getState(); String newKeyspace = session.getLoggedKeyspace(); if (keyspace == null || !keyspace.equals(newKeyspace)) { count = 0; keyspace = newKeyspace; } int sessionKey = count++; logger.info("Host States for Session {}#{}:", keyspace, sessionKey); Collection<Host> hosts = sessionState.getConnectedHosts(); for (final Host host : hosts) { HostConnectionPool pool = sessionM.pools.get(host); HostDistance distance = lbPolicy.distance(host); // Whether or not the host will reconnect while in a suspected state. boolean isReconnectingFromSuspected = host.getInitialReconnectionAttemptFuture() != null && !host.getInitialReconnectionAttemptFuture().isDone(); // Whether or not the host will reconnect while in a down state. boolean isReconnectingFromDown = host.getReconnectionAttemptFuture() != null && !host.getReconnectionAttemptFuture().isDone(); if (pool != null) { String msg = String.format( "\t[%s:%s:%s] version=%s, state=%s, dist=%s, inFlight=%d, openConnections=%d, " + "trashedConnections=%d, reconnectFromSuspected=%s, reconnectFromDown=%s. pool=%s, poolClosed=%s", host.getDatacenter(), host.getRack(), host.getAddress(), host.getCassandraVersion(), host.state, distance, sessionState.getInFlightQueries(host), sessionState.getOpenConnections(host), pool.trash.size(), isReconnectingFromSuspected, isReconnectingFromDown, pool.hashCode(), pool.isClosed()); logger.info(msg); for (Connection connection : pool.connections) { long now = System.currentTimeMillis(); Connection.State state = connection.state.get(); if (connection.isClosed() || connection.isDefunct() || state == Connection.State.TRASHED || state == Connection.State.GONE || connection.maxIdleTime > 0 && connection.maxIdleTime < now) { logger.warn( "\t\t{} defunct?={}, state={}, maxIdleTime={}", connection, connection.isDefunct(), state, new Date(connection.maxIdleTime)); } else { logger.info( "\t\t{} defunct?={}, state={}, maxIdleTime={}", connection, connection.isDefunct(), state, new Date(connection.maxIdleTime)); } } for (Connection connection : pool.trash) { Connection.State state = connection.state.get(); if (connection.isClosed() || connection.isDefunct() || state == Connection.State.OPEN || state == Connection.State.GONE) { logger.warn( "\t\t{} defunct?={}, state={}, maxIdleTime={} [trash]", connection, connection.isDefunct(), state, new Date(connection.maxIdleTime)); } else { logger.info( "\t\t{} defunct?={}, state={}, maxIdleTime={} [trash]", connection, connection.isDefunct(), state, new Date(connection.maxIdleTime)); } } } else { logger.error("Pool is null for {}.", host); } // Register by host / session metrics if not already registered. // Replace periods with underscores (better graphite metric names) in host, also remove // backslashes. // If the host is no longer part of the session, it will return 0. String prefix = "Session." + sessionKey + "." + host.getAddress().toString().replaceAll("\\.", "_").replaceAll("/", "") + "."; String inFlightKey = prefix + "inFlight"; String openConnectionsKey = prefix + "openConnections"; if (!metricRegistry.getMetrics().containsKey(inFlightKey)) { metricRegistry.register( inFlightKey, new CachedGauge<Integer>(1, TimeUnit.SECONDS) { @Override protected Integer loadValue() { Session.State sessionState = session.getState(); if (sessionState.getConnectedHosts().contains(host)) { return sessionState.getInFlightQueries(host); } else { return 0; } } }); } if (!metricRegistry.getMetrics().containsKey(openConnectionsKey)) { metricRegistry.register( openConnectionsKey, new CachedGauge<Integer>(1, TimeUnit.SECONDS) { @Override protected Integer loadValue() { Session.State sessionState = session.getState(); if (sessionState.getConnectedHosts().contains(host)) { return sessionState.getOpenConnections(host); } else { return 0; } } }); } } } ControlConnection connection = cluster.manager.controlConnection; if (connection.isOpen()) { logger.info("Control connection is open to {}.", connection.connectedHost()); } else { logger.warn("Control connection is closed."); } List<Host> queryPlan = Lists.newArrayList(lbPolicy.newQueryPlan(null, new SimpleStatement(""))); if (queryPlan.size() == 0) { logger.warn("Query Plan is empty!"); } else { logger.info("Query Plan: {}", queryPlan); } }
public static <T extends Metric> T register(String name, T metric) throws IllegalArgumentException { return metrics.register(name, metric); }
/** Registers a default set of metrics for the JVM. */ private void registerDefaultMetrics() { metricRegistry.register(JVM_GARBAGE_COLLECTOR_METRICS, new GarbageCollectorMetricSet()); metricRegistry.register(JVM_MEMORY_METRICS, new MemoryUsageGaugeSet()); metricRegistry.register(JVM_THREAD_METRICS, new ThreadStatesGaugeSet()); metricRegistry.register(JVM_FILE_DESCRIPTOR_METRICS, new FileDescriptorRatioGauge()); }