@Provides @ManageLifecycle public JvmThreadsMonitor getJvmThreadsMonitor(DataSourceTaskIdHolder dataSourceTaskIdHolder) { Map<String, String[]> dimensions = MonitorsConfig.mapOfDatasourceAndTaskID( dataSourceTaskIdHolder.getDataSource(), dataSourceTaskIdHolder.getTaskId() ); return new JvmThreadsMonitor(dimensions); }
@Override public boolean doMonitor(ServiceEmitter emitter) { ThreadMXBean threadBean = ManagementFactory.getThreadMXBean(); final ServiceMetricEvent.Builder builder = builder(); MonitorUtils.addDimensionsToBuilder(builder, dimensions); // Because between next two calls on ThreadMXBean new threads can be started we can observe some inconsistency // in counters values and finished counter could be even negative int newLiveThreads = threadBean.getThreadCount(); long newStartedThreads = threadBean.getTotalStartedThreadCount(); long startedThreadsDiff = newStartedThreads - lastStartedThreads; emitter.emit(builder.build("jvm/threads/started", startedThreadsDiff)); emitter.emit(builder.build("jvm/threads/finished", lastLiveThreads + startedThreadsDiff - newLiveThreads)); emitter.emit(builder.build("jvm/threads/live", newLiveThreads)); emitter.emit(builder.build("jvm/threads/liveDaemon", threadBean.getDaemonThreadCount())); emitter.emit(builder.build("jvm/threads/livePeak", threadBean.getPeakThreadCount())); threadBean.resetPeakThreadCount(); lastStartedThreads = newStartedThreads; lastLiveThreads = newLiveThreads; return true; } }
@Override public boolean doMonitor(ServiceEmitter emitter) { ThreadMXBean threadBean = ManagementFactory.getThreadMXBean(); final ServiceMetricEvent.Builder builder = builder(); MonitorUtils.addDimensionsToBuilder(builder, dimensions); // Because between next two calls on ThreadMXBean new threads can be started we can observe some inconsistency // in counters values and finished counter could be even negative int newLiveThreads = threadBean.getThreadCount(); long newStartedThreads = threadBean.getTotalStartedThreadCount(); long startedThreadsDiff = newStartedThreads - lastStartedThreads; emitter.emit(builder.build("jvm/threads/started", startedThreadsDiff)); emitter.emit(builder.build("jvm/threads/finished", lastLiveThreads + startedThreadsDiff - newLiveThreads)); emitter.emit(builder.build("jvm/threads/live", newLiveThreads)); emitter.emit(builder.build("jvm/threads/liveDaemon", threadBean.getDaemonThreadCount())); emitter.emit(builder.build("jvm/threads/livePeak", threadBean.getPeakThreadCount())); threadBean.resetPeakThreadCount(); lastStartedThreads = newStartedThreads; lastLiveThreads = newLiveThreads; return true; } }
/** * Creates a JVM monitor, configured with the given dimensions, that gathers all currently available JVM-wide * monitors: {@link JvmMonitor}, {@link JvmCpuMonitor} and {@link JvmThreadsMonitor} (this list may * change in any future release of this library, including a minor release). * * @param dimensions common dimensions to configure the JVM monitor with * @param feed feed for all emitted events * * @return a universally useful JVM-wide monitor */ public static Monitor createCompoundJvmMonitor(Map<String, String[]> dimensions, String feed) { // This list doesn't include SysMonitor because it should probably be run only in one JVM, if several JVMs are // running on the same instance, so most of the time SysMonitor should be configured/set up differently than // "simple" JVM monitors, created below. return and(// Could equally be or(), because all member monitors always return true from their monitor() methods. new JvmMonitor(dimensions, feed), new JvmCpuMonitor(dimensions, feed), new JvmThreadsMonitor(dimensions, feed) ); }
@Provides @ManageLifecycle public JvmThreadsMonitor getJvmThreadsMonitor(DataSourceTaskIdHolder dataSourceTaskIdHolder) { Map<String, String[]> dimensions = MonitorsConfig.mapOfDatasourceAndTaskID( dataSourceTaskIdHolder.getDataSource(), dataSourceTaskIdHolder.getTaskId() ); return new JvmThreadsMonitor(dimensions); }
/** * Creates a JVM monitor, configured with the given dimensions, that gathers all currently available JVM-wide * monitors: {@link JvmMonitor}, {@link JvmCpuMonitor} and {@link JvmThreadsMonitor} (this list may * change in any future release of this library, including a minor release). * * @param dimensions common dimensions to configure the JVM monitor with * @param feed feed for all emitted events * * @return a universally useful JVM-wide monitor */ public static Monitor createCompoundJvmMonitor(Map<String, String[]> dimensions, String feed) { // This list doesn't include SysMonitor because it should probably be run only in one JVM, if several JVMs are // running on the same instance, so most of the time SysMonitor should be configured/set up differently than // "simple" JVM monitors, created below. return and(// Could equally be or(), because all member monitors always return true from their monitor() methods. new JvmMonitor(dimensions, feed), new JvmCpuMonitor(dimensions, feed), new JvmThreadsMonitor(dimensions, feed) ); }