Skip to content

Commit

Permalink
Fix ThreadPoolMetrics NPE (#14404)
Browse files Browse the repository at this point in the history
Signed-off-by: OneSizeFitQuorum <[email protected]>
  • Loading branch information
OneSizeFitsQuorum authored Dec 12, 2024
1 parent 4be9909 commit 8144e5c
Showing 1 changed file with 77 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,87 +48,102 @@ public synchronized void registerThreadPool(IThreadPoolMBean pool, String name)
notRegisteredPoolMap.put(name, pool);
} else {
registeredPoolMap.put(name, pool);
metricService.createAutoGauge(
SystemMetric.THREAD_POOL_ACTIVE_THREAD_COUNT.toString(),
MetricLevel.IMPORTANT,
registeredPoolMap,
map -> registeredPoolMap.get(name).getActiveCount(),
SystemTag.POOL_NAME.toString(),
name);
metricService.createAutoGauge(
SystemMetric.THREAD_POOL_CORE_SIZE.toString(),
MetricLevel.IMPORTANT,
registeredPoolMap,
map -> registeredPoolMap.get(name).getCorePoolSize(),
SystemTag.POOL_NAME.toString(),
name);
metricService.createAutoGauge(
SystemMetric.THREAD_POOL_WAITING_TASK_COUNT.toString(),
MetricLevel.IMPORTANT,
registeredPoolMap,
map -> registeredPoolMap.get(name).getQueueLength(),
SystemTag.POOL_NAME.toString(),
name);
metricService.createAutoGauge(
SystemMetric.THREAD_POOL_DONE_TASK_COUNT.toString(),
MetricLevel.IMPORTANT,
registeredPoolMap,
map -> registeredPoolMap.get(name).getCompletedTaskCount(),
SystemTag.POOL_NAME.toString(),
name);
metricService.createAutoGauge(
SystemMetric.THREAD_POOL_LARGEST_POOL_SIZE.toString(),
MetricLevel.IMPORTANT,
registeredPoolMap,
map -> registeredPoolMap.get(name).getLargestPoolSize(),
SystemTag.POOL_NAME.toString(),
name);
registerThreadPoolMetrics(name);
}
}

private void registerThreadPoolMetrics(String name) {
metricService.createAutoGauge(
SystemMetric.THREAD_POOL_ACTIVE_THREAD_COUNT.toString(),
MetricLevel.IMPORTANT,
registeredPoolMap,
map -> registeredPoolMap.get(name).getActiveCount(),
SystemTag.POOL_NAME.toString(),
name);
metricService.createAutoGauge(
SystemMetric.THREAD_POOL_CORE_SIZE.toString(),
MetricLevel.IMPORTANT,
registeredPoolMap,
map -> registeredPoolMap.get(name).getCorePoolSize(),
SystemTag.POOL_NAME.toString(),
name);
metricService.createAutoGauge(
SystemMetric.THREAD_POOL_WAITING_TASK_COUNT.toString(),
MetricLevel.IMPORTANT,
registeredPoolMap,
map -> registeredPoolMap.get(name).getQueueLength(),
SystemTag.POOL_NAME.toString(),
name);
metricService.createAutoGauge(
SystemMetric.THREAD_POOL_DONE_TASK_COUNT.toString(),
MetricLevel.IMPORTANT,
registeredPoolMap,
map -> registeredPoolMap.get(name).getCompletedTaskCount(),
SystemTag.POOL_NAME.toString(),
name);
metricService.createAutoGauge(
SystemMetric.THREAD_POOL_LARGEST_POOL_SIZE.toString(),
MetricLevel.IMPORTANT,
registeredPoolMap,
map -> registeredPoolMap.get(name).getLargestPoolSize(),
SystemTag.POOL_NAME.toString(),
name);
}

public synchronized void unRegisterThreadPool(String name) {
if (metricService == null) {
notRegisteredPoolMap.remove(name);
} else {
registeredPoolMap.remove(name);
metricService.remove(
MetricType.GAUGE,
SystemMetric.THREAD_POOL_ACTIVE_THREAD_COUNT.toString(),
SystemTag.POOL_NAME.toString(),
name);
metricService.remove(
MetricType.GAUGE,
SystemMetric.THREAD_POOL_CORE_SIZE.toString(),
SystemTag.POOL_NAME.toString(),
name);
metricService.remove(
MetricType.GAUGE,
SystemMetric.THREAD_POOL_WAITING_TASK_COUNT.toString(),
SystemTag.POOL_NAME.toString(),
name);
metricService.remove(
MetricType.GAUGE,
SystemMetric.THREAD_POOL_DONE_TASK_COUNT.toString(),
SystemTag.POOL_NAME.toString(),
name);
metricService.remove(
MetricType.GAUGE,
SystemMetric.THREAD_POOL_LARGEST_POOL_SIZE.toString(),
SystemTag.POOL_NAME.toString(),
name);
unRegisterThreadPoolMetrics(name);
}
}

private void unRegisterThreadPoolMetrics(String name) {
metricService.remove(
MetricType.GAUGE,
SystemMetric.THREAD_POOL_ACTIVE_THREAD_COUNT.toString(),
SystemTag.POOL_NAME.toString(),
name);
metricService.remove(
MetricType.GAUGE,
SystemMetric.THREAD_POOL_CORE_SIZE.toString(),
SystemTag.POOL_NAME.toString(),
name);
metricService.remove(
MetricType.GAUGE,
SystemMetric.THREAD_POOL_WAITING_TASK_COUNT.toString(),
SystemTag.POOL_NAME.toString(),
name);
metricService.remove(
MetricType.GAUGE,
SystemMetric.THREAD_POOL_DONE_TASK_COUNT.toString(),
SystemTag.POOL_NAME.toString(),
name);
metricService.remove(
MetricType.GAUGE,
SystemMetric.THREAD_POOL_LARGEST_POOL_SIZE.toString(),
SystemTag.POOL_NAME.toString(),
name);
}

@Override
public synchronized void bindTo(AbstractMetricService metricService) {
this.metricService = metricService;
notRegisteredPoolMap.forEach((name, pool) -> registerThreadPool(pool, name));
for (Map.Entry<String, IThreadPoolMBean> entry : notRegisteredPoolMap.entrySet()) {
registerThreadPoolMetrics(entry.getKey());
}
registeredPoolMap.putAll(notRegisteredPoolMap);
notRegisteredPoolMap.clear();
}

@Override
public synchronized void unbindFrom(AbstractMetricService metricService) {
registeredPoolMap.forEach((name, pool) -> unRegisterThreadPool(name));
for (Map.Entry<String, IThreadPoolMBean> entry : registeredPoolMap.entrySet()) {
unRegisterThreadPoolMetrics(entry.getKey());
}
registeredPoolMap.clear();
notRegisteredPoolMap.clear();
}

private static class ThreadPoolMetricsHolder {
Expand Down

0 comments on commit 8144e5c

Please sign in to comment.