micrometer重点

版权声明:转载请注明链接 https://blog.csdn.net/weixin_38569499/article/details/86740121

    micrometer号称监控界的SLF4J,以极低的开销为基于JVM的程序收集监控指标。

1、注册表Registry:

    Meter是用来收集应用的测量指标的接口。micrometer通过MeterRegistry创建和保存Meter,每个监控系统都会有MeterRegistry的实现。

SimpleMeterRegistry

    SimpleMeterRegistry持有内存中每一个指标的最新值,但是不用于对外提供。在未确定

定义方式:

MeterRegistry registry = new SimpleMeterRegistry();

混合注册

    Micrometer 提供了一个CompositeMeterRegistry可添加多个注册表的程序,支持将metrics标准同时发布到多个监视系统。

CompositeMeterRegistry composite = new CompositeMeterRegistry();

Counter compositeCounter = composite.counter("counter");
compositeCounter.increment(); (1)

SimpleMeterRegistry simple = new SimpleMeterRegistry();
composite.add(simple); (2)

compositeCounter.increment(); (3)

    备注:

    1)CompositeMeterRegistry composite中添加的所有注册表,都具备composite的指标,例如simple中有Counter compositeCounter;

    2)当Counter compositeCounter增加的时候,CompositeMeterRegistry composite中添加的所有注册表中的compositeCounter都会相应地增加。

全局注册

    Micrometer提供了一个静态全局注册表Metrics.globalRegistry和一组静态构建器,用于基于此注册表生成计量表。globalRegistry是一个组合注册表。

class MyComponent {
    Counter featureCounter = Metrics.counter("feature", "region", "test"); (1)

    void feature() {
        featureCounter.increment();
    }

    void feature2(String type) {
        Metrics.counter("feature.2", "type", type).increment(); (2)
    }
}

class MyApplication {
    void start() {
        // wire your monitoring system to global static state
        Metrics.addRegistry(new SimpleMeterRegistry()); (3)
    }
}

    全局注册表中添加的指标会自动添加到所有注册表中。

2、指标

    micrometer的指标由指标名称和键值对组成。指标需要注册到注册表中,通过注册表来进行发布。micrometer的指标分为4类:Counter、Gauge、Timer、Summary。

    一般来说,指标的使用分成4个环节:创建、注册、使用、发布。

1、Counter

    counter是计数器,特征是只增不减。counter通常用来表示业务的请求次数之类的只增不减的指标。通常会使用counter的increment()方法或者increment(int n)方法,分别增加1和n。示例:

MeterRegistry registry = new PrometheusMeterRegistry(PrometheusConfig.DEFAULT);
Counter counter = registry.counter("counter", "tag_key", "tag_value"); 

Flux.interval(Duration.ofMillis(10))
        .doOnEach(d -> {
            if (rand.nextDouble() + 0.1 > 0) { 
                counter.increment(); 
            }
        })
        .blockLast();

2、Gauge

    Gauge是仪表值。顾名思义,Gauge用来记录可以瞬息万变的一个数值,例如CPU利用率、内存利用率等。示例:

//example 1:
List<String> list = registry.gauge("listGauge", Collections.emptyList(), new ArrayList<>(), List::size);
List<String> list2 = registry.gaugeCollectionSize("listSize2", Tags.empty(), new ArrayList<>());
Map<String, Integer> map = registry.gaugeMapSize("mapGauge", Tags.empty(), new HashMap<>());

//example 2:
AtomicInteger n = registry.gauge("numberGauge", new AtomicInteger(0));
n.set(1);
n.set(2);

//example 3:
Gauge gauge = Gauge
    .builder("gauge", myObj, myObj::gaugeValue)
    .description("a description of what this gauge does") // optional
    .tags("region", "test") // optional
    .register(registry);

3、Timer

    Timer是计时器,用来测量短时间的代码块的执行时间的分布。Timer记录代码块的执行时间后,可以对执行时间进行统计,分析记录执行的最大时间、总时间、平均时间、执行完成的总任务等。

    Timer的方法:

void record(long amount, TimeUnit unit);
void record(Duration duration);
double totalTime(TimeUnit unit);

    Timer的使用示例:

Timer timer = Timer
    .builder("my.timer")
    .description("a description of what this timer does") // optional
    .tags("region", "test") // optional
    .register(registry);

4、Summary

    Summary是摘要,用于跟踪事件的分布。micrometer的Summary可以通过prometheus的Summary来理解:prometheus指标

代码示例:

//example 1:
DistributionSummary summary = registry.summary("response.size");

//example 2:
DistributionSummary summary = DistributionSummary
    .builder("response.size")
    .description("a description of what this summary does") // optional
    .baseUnit("bytes") // optional (1)
    .tags("region", "test") // optional
    .scale(100) // optional (2)
    .register(registry);

猜你喜欢

转载自blog.csdn.net/weixin_38569499/article/details/86740121
今日推荐