All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.flink.runtime.metrics.util.SystemResourcesMetricsInitializer Maven / Gradle / Ivy

The newest version!
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.flink.runtime.metrics.util;

import org.apache.flink.api.common.time.Time;
import org.apache.flink.metrics.Gauge;
import org.apache.flink.metrics.MetricGroup;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import oshi.SystemInfo;
import oshi.hardware.GlobalMemory;
import oshi.hardware.HardwareAbstractionLayer;

/**
 * Utility class to initialize system resource metrics.
 */
public class SystemResourcesMetricsInitializer {
	private static final Logger LOG = LoggerFactory.getLogger(SystemResourcesMetricsInitializer.class);

	public static void instantiateSystemMetrics(MetricGroup metricGroup, Time probeInterval) {
		try {
			MetricGroup system = metricGroup.addGroup("System");

			SystemResourcesCounter systemResourcesCounter = new SystemResourcesCounter(probeInterval);
			systemResourcesCounter.start();

			SystemInfo systemInfo = new SystemInfo();
			HardwareAbstractionLayer hardwareAbstractionLayer = systemInfo.getHardware();

			instantiateMemoryMetrics(system.addGroup("Memory"), hardwareAbstractionLayer.getMemory());
			instantiateSwapMetrics(system.addGroup("Swap"), hardwareAbstractionLayer.getMemory());
			instantiateCPUMetrics(system.addGroup("CPU"), systemResourcesCounter);
			instantiateNetworkMetrics(system.addGroup("Network"), systemResourcesCounter);
		}
		catch (NoClassDefFoundError ex) {
			LOG.warn(
				"Failed to initialize system resource metrics because of missing class definitions." +
				" Did you forget to explicitly add the oshi-core optional dependency?",
				ex);
		}
	}

	private static void instantiateMemoryMetrics(MetricGroup metrics, GlobalMemory memory) {
		metrics.>gauge("Available", memory::getAvailable);
		metrics.>gauge("Total", memory::getTotal);
	}

	private static void instantiateSwapMetrics(MetricGroup metrics, GlobalMemory memory) {
		metrics.>gauge("Used", memory::getSwapUsed);
		metrics.>gauge("Total", memory::getSwapTotal);
	}

	private static void instantiateCPUMetrics(MetricGroup metrics, SystemResourcesCounter usageCounter) {
		metrics.>gauge("Usage", usageCounter::getCpuUsage);
		metrics.>gauge("Idle", usageCounter::getCpuIdle);
		metrics.>gauge("Sys", usageCounter::getCpuSys);
		metrics.>gauge("User", usageCounter::getCpuUser);
		metrics.>gauge("IOWait", usageCounter::getIOWait);
		metrics.>gauge("Nice", usageCounter::getCpuNice);
		metrics.>gauge("Irq", usageCounter::getCpuIrq);
		metrics.>gauge("SoftIrq", usageCounter::getCpuSoftIrq);

		metrics.>gauge("Load1min", usageCounter::getCpuLoad1);
		metrics.>gauge("Load5min", usageCounter::getCpuLoad5);
		metrics.>gauge("Load15min", usageCounter::getCpuLoad15);

		for (int i = 0; i < usageCounter.getProcessorsCount(); i++) {
			final int processor = i;
			metrics.>gauge(
				String.format("UsageCPU%d", processor),
				() -> usageCounter.getCpuUsagePerProcessor(processor));
		}
	}

	private static void instantiateNetworkMetrics(MetricGroup metrics, SystemResourcesCounter usageCounter) {
		for (int i = 0; i < usageCounter.getNetworkInterfaceNames().length; i++) {
			MetricGroup interfaceGroup = metrics.addGroup(usageCounter.getNetworkInterfaceNames()[i]);

			final int interfaceNo = i;
			interfaceGroup.>gauge("ReceiveRate", () -> usageCounter.getReceiveRatePerInterface(interfaceNo));
			interfaceGroup.>gauge("SendRate", () -> usageCounter.getSendRatePerInterface(interfaceNo));
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy