|
| 1 | +/* |
| 2 | + * Copyright 2023 NAVER Corp. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.navercorp.pinpoint.profiler.monitor; |
| 17 | + |
| 18 | +import com.google.inject.Inject; |
| 19 | +import com.navercorp.pinpoint.common.profiler.concurrent.PinpointThreadFactory; |
| 20 | +import com.navercorp.pinpoint.common.util.StringUtils; |
| 21 | +import com.navercorp.pinpoint.profiler.context.module.ApplicationName; |
| 22 | +import com.navercorp.pinpoint.profiler.context.monitor.config.DefaultMonitorConfig; |
| 23 | +import com.navercorp.pinpoint.profiler.context.monitor.config.MonitorConfig; |
| 24 | +import org.apache.logging.log4j.LogManager; |
| 25 | +import org.apache.logging.log4j.Logger; |
| 26 | + |
| 27 | +import java.util.ArrayList; |
| 28 | +import java.util.List; |
| 29 | +import java.util.concurrent.ScheduledExecutorService; |
| 30 | +import java.util.concurrent.ScheduledThreadPoolExecutor; |
| 31 | +import java.util.concurrent.TimeUnit; |
| 32 | + |
| 33 | +public class DefaultNetworkStatMonitor implements NetworkStatMonitor { |
| 34 | + private static final long MIN_COLLECTION_INTERVAL_MS = 1000 * 5; |
| 35 | + private static final long MAX_COLLECTION_INTERVAL_MS = 1000 * 10; |
| 36 | + private static final long DEFAULT_COLLECTION_INTERVAL_MS = DefaultMonitorConfig.DEFAULT_NETWORK_METRIC_COLLECTION_INTERVAL_MS; |
| 37 | + |
| 38 | + private final Logger logger = LogManager.getLogger(this.getClass()); |
| 39 | + private final long collectionIntervalMs; |
| 40 | + private final StatMonitorJob statMonitorJob; |
| 41 | + private final ScheduledExecutorService executor = new ScheduledThreadPoolExecutor(1, new PinpointThreadFactory("Pinpoint-hw-stat-monitor", true)); |
| 42 | + private final boolean isNetworkMetricEnable; |
| 43 | + |
| 44 | + @Inject |
| 45 | + public DefaultNetworkStatMonitor(@ApplicationName String applicationName, |
| 46 | + MonitorConfig monitorConfig) { |
| 47 | + long collectionIntervalMs = monitorConfig.getNetworkMetricCollectIntervalMs(); |
| 48 | + |
| 49 | + if (collectionIntervalMs < MIN_COLLECTION_INTERVAL_MS) { |
| 50 | + collectionIntervalMs = DEFAULT_COLLECTION_INTERVAL_MS; |
| 51 | + } |
| 52 | + if (collectionIntervalMs > MAX_COLLECTION_INTERVAL_MS) { |
| 53 | + collectionIntervalMs = DEFAULT_COLLECTION_INTERVAL_MS; |
| 54 | + } |
| 55 | + |
| 56 | + this.collectionIntervalMs = collectionIntervalMs; |
| 57 | + List<Runnable> runnableList = new ArrayList<>(); |
| 58 | + this.isNetworkMetricEnable = monitorConfig.isNetworkMetricEnable(); |
| 59 | + |
| 60 | + if (isNetworkMetricEnable && NetworkMetricCollectingJob.isSupported()) { |
| 61 | + String hostGroupName = monitorConfig.getHostGroupName(); |
| 62 | + if (StringUtils.isEmpty(hostGroupName)) { |
| 63 | + hostGroupName = applicationName; |
| 64 | + } |
| 65 | + Runnable oshiMetricCollectingJob = new NetworkMetricCollectingJob(hostGroupName, |
| 66 | + monitorConfig.getMetricCollectorIP(), monitorConfig.getMetricCollectorPort(), monitorConfig.isProtocolStatsEnable()); |
| 67 | + runnableList.add(oshiMetricCollectingJob); |
| 68 | + } |
| 69 | + this.statMonitorJob = new StatMonitorJob(runnableList); |
| 70 | + |
| 71 | + } |
| 72 | + @Override |
| 73 | + public void start() { |
| 74 | + if (isNetworkMetricEnable) { |
| 75 | + executor.scheduleAtFixedRate(statMonitorJob, this.collectionIntervalMs, this.collectionIntervalMs, TimeUnit.MILLISECONDS); |
| 76 | + logger.info("HW stat monitor started"); |
| 77 | + } else { |
| 78 | + logger.info("HW stat monitor disabled"); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public void stop() { |
| 84 | + if (isNetworkMetricEnable) { |
| 85 | + statMonitorJob.close(); |
| 86 | + executor.shutdown(); |
| 87 | + try { |
| 88 | + executor.awaitTermination(3000, TimeUnit.MILLISECONDS); |
| 89 | + } catch (InterruptedException e) { |
| 90 | + Thread.currentThread().interrupt(); |
| 91 | + } |
| 92 | + logger.info("HW stat monitor stopped"); |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments