springboot2增加diskspace指標(biāo)

本文主要研究下如何在springboot2新增一個diskspace指標(biāo)

disk health indicator

DiskSpaceHealthIndicatorProperties

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

/**
 * 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

/**
 * 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轉(zhuǎn)為metrics

@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": []
}

小結(jié)

springboot2默認(rèn)把diskspace作為一個healthIndicator带膜,其閾值默認(rèn)為10M载荔。這里通過自定義micrometer的metrics,新增diskspace相關(guān)指標(biāo),這樣就可以統(tǒng)一通過metrcis進(jìn)行監(jiān)控報警额各。

doc

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市吧恃,隨后出現(xiàn)的幾起案子虾啦,更是在濱河造成了極大的恐慌痕寓,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,820評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件厂抽,死亡現(xiàn)場離奇詭異需频,居然都是意外死亡,警方通過查閱死者的電腦和手機筷凤,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,648評論 3 399
  • 文/潘曉璐 我一進(jìn)店門昭殉,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人蹂风,你說我怎么就攤上這事』葑模” “怎么了任内?”我有些...
    開封第一講書人閱讀 168,324評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長死嗦。 經(jīng)常有香客問我,道長节腐,這世上最難降的妖魔是什么摘盆? 我笑而不...
    開封第一講書人閱讀 59,714評論 1 297
  • 正文 為了忘掉前任,我火速辦了婚禮孩擂,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己坦弟,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 68,724評論 6 397
  • 文/花漫 我一把揭開白布烙懦。 她就那樣靜靜地躺著赤炒,像睡著了一般。 火紅的嫁衣襯著肌膚如雪莺褒。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,328評論 1 310
  • 那天你辣,我揣著相機與錄音,去河邊找鬼舍哄。 笑死,一個胖子當(dāng)著我的面吹牛弥锄,可吹牛的內(nèi)容都是我干的蟆沫。 我是一名探鬼主播籽暇,決...
    沈念sama閱讀 40,897評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼图仓,長吁一口氣:“原來是場噩夢啊……” “哼但绕!你這毒婦竟也來了救崔?” 一聲冷哼從身側(cè)響起捏顺,我...
    開封第一講書人閱讀 39,804評論 0 276
  • 序言:老撾萬榮一對情侶失蹤幅骄,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后拆座,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,345評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡孕索,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,431評論 3 340
  • 正文 我和宋清朗相戀三年躏碳,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片菇绵。...
    茶點故事閱讀 40,561評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡咬最,死狀恐怖翎嫡,靈堂內(nèi)的尸體忽然破棺而出永乌,到底是詐尸還是另有隱情翁垂,我是刑警寧澤硝桩,帶...
    沈念sama閱讀 36,238評論 5 350
  • 正文 年R本政府宣布,位于F島的核電站啼肩,受9級特大地震影響衙伶,放射性物質(zhì)發(fā)生泄漏祈坠。R本人自食惡果不足惜矢劲,卻給世界環(huán)境...
    茶點故事閱讀 41,928評論 3 334
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望躺同。 院中可真熱鬧丸逸,春花似錦、人聲如沸黄刚。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,417評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽业扒。三九已至检吆,卻和暖如春凶赁,著一層夾襖步出監(jiān)牢的瞬間逆甜,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,528評論 1 272
  • 我被黑心中介騙來泰國打工交煞, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人集嵌。 一個月前我還...
    沈念sama閱讀 48,983評論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像根欧,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子酥泛,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,573評論 2 359

推薦閱讀更多精彩內(nèi)容