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

io.dropwizard.metrics5.jvm.SafeFileDescriptorRatioGauge Maven / Gradle / Ivy

There is a newer version: 2023-22.3
Show newest version
package io.dropwizard.metrics5.jvm;

import com.sun.management.UnixOperatingSystemMXBean;

import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean;

import io.dropwizard.metrics5.RatioGauge;

/**
 * Java 9 compatible implementation of FileDescriptorRatioGauge that doesn't use reflection
 * and is not susceptible to an InaccessibleObjectException
 *
 * The gauge represents a ratio of used to total file descriptors.
 *
 * @author Vasily Vorontsov ([email protected])
 */
public class SafeFileDescriptorRatioGauge extends RatioGauge {
  private final OperatingSystemMXBean os;

  /**
   * Creates a new gauge using the platform OS bean.
   */
  public SafeFileDescriptorRatioGauge() {
    this(ManagementFactory.getOperatingSystemMXBean());
  }

  /**
   * Creates a new gauge using the given OS bean.
   *
   * @param os    an {@link OperatingSystemMXBean}
   */
  public SafeFileDescriptorRatioGauge(OperatingSystemMXBean os) {
    this.os = os;
  }

  /**
   * @return  {@link io.dropwizard.metrics5.RatioGauge.Ratio} of used to total file descriptors.
   */
  protected Ratio getRatio() {
    if (!(this.os instanceof UnixOperatingSystemMXBean)) {
      return Ratio.of(Double.NaN, Double.NaN);
    }
    Long openFds = ((UnixOperatingSystemMXBean)os).getOpenFileDescriptorCount();
    Long maxFds = ((UnixOperatingSystemMXBean)os).getMaxFileDescriptorCount();
    return Ratio.of(openFds.doubleValue(), maxFds.doubleValue());
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy