前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >springboot2增加diskspace指标

springboot2增加diskspace指标

作者头像
code4it
发布2018-09-17 16:09:05
1.6K0
发布2018-09-17 16:09:05
举报
文章被收录于专栏:码匠的流水账码匠的流水账

本文主要研究下如何在springboot2新增一个diskspace指标

disk health indicator

DiskSpaceHealthIndicatorProperties

spring-boot-actuator-autoconfigure-2.0.1.RELEASE-sources.jar!/org/springframework/boot/actuate/autoconfigure/system/DiskSpaceHealthIndicatorProperties.java

代码语言:javascript
复制
/**
 * External configuration properties for {@link DiskSpaceHealthIndicator}.
 *
 * @author Andy Wilkinson
 * @since 1.2.0
 */
@ConfigurationProperties(prefix = "management.health.diskspace")
public class DiskSpaceHealthIndicatorProperties {

    private static final int MEGABYTES = 1024 * 1024;

    private static final int DEFAULT_THRESHOLD = 10 * MEGABYTES;

    /**
     * Path used to compute the available disk space.
     */
    private File path = new File(".");

    /**
     * Minimum disk space, in bytes, that should be available.
     */
    private long threshold = DEFAULT_THRESHOLD;

    public File getPath() {
        return this.path;
    }

    public void setPath(File path) {
        Assert.isTrue(path.exists(), () -> "Path '" + path + "' does not exist");
        Assert.isTrue(path.canRead(), () -> "Path '" + path + "' cannot be read");
        this.path = path;
    }

    public long getThreshold() {
        return this.threshold;
    }

    public void setThreshold(long threshold) {
        Assert.isTrue(threshold >= 0, "threshold must be greater than 0");
        this.threshold = threshold;
    }

}

DiskSpaceHealthIndicator

spring-boot-actuator-2.0.1.RELEASE-sources.jar!/org/springframework/boot/actuate/system/DiskSpaceHealthIndicator.java

代码语言:javascript
复制
/**
 * A {@link HealthIndicator} that checks available disk space and reports a status of
 * {@link Status#DOWN} when it drops below a configurable threshold.
 *
 * @author Mattias Severson
 * @author Andy Wilkinson
 * @since 2.0.0
 */
public class DiskSpaceHealthIndicator extends AbstractHealthIndicator {

    private static final Log logger = LogFactory.getLog(DiskSpaceHealthIndicator.class);

    private final File path;

    private final long threshold;

    /**
     * Create a new {@code DiskSpaceHealthIndicator} instance.
     * @param path the Path used to compute the available disk space
     * @param threshold the minimum disk space that should be available (in bytes)
     */
    public DiskSpaceHealthIndicator(File path, long threshold) {
        super("DiskSpace health check failed");
        this.path = path;
        this.threshold = threshold;
    }

    @Override
    protected void doHealthCheck(Health.Builder builder) throws Exception {
        long diskFreeInBytes = this.path.getUsableSpace();
        if (diskFreeInBytes >= this.threshold) {
            builder.up();
        }
        else {
            logger.warn(String.format(
                    "Free disk space below threshold. "
                            + "Available: %d bytes (threshold: %d bytes)",
                    diskFreeInBytes, this.threshold));
            builder.down();
        }
        builder.withDetail("total", this.path.getTotalSpace())
                .withDetail("free", diskFreeInBytes)
                .withDetail("threshold", this.threshold);
    }

}

metrics

这里我们把disk的health indicator转为metrics

代码语言:javascript
复制
@Component
public class DiskMetrics implements MeterBinder {

    private File rootFilePath;

    public DiskMetrics() {
        this.rootFilePath = new File(".");
    }

    @Override
    public void bindTo(MeterRegistry registry) {
        Gauge.builder("diskspace.total", rootFilePath, c -> c.getTotalSpace())
                .register(registry);
        Gauge.builder("diskspace.free", rootFilePath, c -> c.getFreeSpace())
                .register(registry);
        Gauge.builder("diskspace.usage", rootFilePath, c -> {
            long totalDiskSpace = rootFilePath.getTotalSpace();
            if (totalDiskSpace == 0) {
                return 0.0;
            }

            long usedDiskSpace = totalDiskSpace - rootFilePath.getFreeSpace();
            return Double.valueOf(usedDiskSpace) / totalDiskSpace * 100;
        })
                .register(registry);
    }
}

输出

  • /actuator/metrics { "names": [ "jvm.memory.committed", "jvm.buffer.memory.used", "jvm.gc.memory.allocated", "jvm.memory.used", "jvm.gc.max.data.size", "logback.events", "system.cpu.count", "jvm.memory.max", "jvm.buffer.total.capacity", "jvm.buffer.count", "process.files.max", "jvm.threads.daemon", "process.start.time", "diskspace.usage", "jvm.gc.live.data.size", "process.files.open", "process.cpu.usage", "process.uptime", "system.load.average.1m", "statsd.queue.size", "statsd.queue.capacity", "http.server.requests", "system.cpu.usage", "diskspace.free", "jvm.threads.live", "jvm.classes.loaded", "jvm.classes.unloaded", "jvm.threads.peak", "jvm.gc.pause", "jvm.gc.memory.promoted", "diskspace.total" ] }
  • /actuator/metrics/diskspace.usage { "name": "diskspace.usage", "measurements": [ { "statistic": "VALUE", "value": 96.99886102691765 } ], "availableTags": [] }

小结

springboot2默认把diskspace作为一个healthIndicator,其阈值默认为10M。这里通过自定义micrometer的metrics,新增diskspace相关指标,这样就可以统一通过metrcis进行监控报警。

doc

  • SpringBoot四大神器之Actuator
  • micrometer自定义metrics
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-04-15,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 码匠的流水账 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • disk health indicator
    • DiskSpaceHealthIndicatorProperties
      • DiskSpaceHealthIndicator
      • metrics
      • 小结
      • doc
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档