Metrics.NET step by step

安装Nuget包

nuget中搜索metrics,如图:

image 

配置Metrics

在程序入口处插入配置Metrics的代码。

复制代码
    class Program
    {
        static void Main(string[] args)
        {
            Metric.Config
                // Web监视仪表板,提供Metrics.NET度量值图表,浏览器打开这个地址可以访问一个Metrics.NET内置的页面
                .WithHttpEndpoint("http://localhost:1234/metrics/")
                // 配置报表输出
                .WithReporting((rc)=>{
                    // 报表输出到控制台
                    rc.WithConsoleReport(TimeSpan.FromSeconds(5));
                });
            Console.ReadLine();
        }
    }
复制代码

 

这个时候打开http://localhost:1234/metrics/将会看到一个空白的页面,里面啥也没有,各个度量菜单中也没有任何度量项。

image

收集度量值

Main入口中添加度量项,后面需要分别实现这些度量项。

复制代码
static void Main(string[] args)
{
    Metric.Config
        // Web监视仪表板,提供Metrics.NET度量值图表
        .WithHttpEndpoint("http://localhost:1234/metrics/")
        // 配置报表输出
        .WithReporting((rc) =>
        {
            // 报表输出到控制台
            rc.WithConsoleReport(TimeSpan.FromSeconds(5));
        });

    GaugeTest();
    CounterTest();
    HistogramTest();
    MeterTest();
    TimerTest();
    HealthCheckTest();

    Console.ReadLine();
}
复制代码

Gauge

static void GaugeTest()
{
    Metric.Gauge("test.gauge", () => ran.NextDouble() * 1000, Unit.None);
}

image

最简单的度量,Metrics不做任何统计计算,读取一个即时值。

Counter

复制代码
static Random ran = new Random(DateTime.Now.TimeOfDay.Milliseconds);
static void CounterTest()
{
    var counter = Metric.Counter("test.counter", Unit.Custom("并发"));

    Action doWork = () => { System.Threading.Thread.Sleep(ran.Next(10, 300)); };
    Action idlesse = () => { System.Threading.Thread.Sleep(ran.Next(0, 500)); };
    for (var i = 0; i < 20; i++)
    {
        Task.Run(() =>
        {
            while (true)
            {
                counter.Increment();
                doWork();
                counter.Decrement();
                idlesse();
            }
        });
    }
}
复制代码

 

 

image

示例代码展示如何度量并发数。

Histogram

复制代码
static Random ran = new Random(DateTime.Now.TimeOfDay.Milliseconds);
static void HistogramTest()
{
    var histogram = Metric.Histogram("test.histogram", Unit.Custom(""), SamplingType.LongTerm);
    
            
    Task.Run(() =>
    {
        while (true)
        {
            histogram.Update(ran.Next(10, 80), ran.Next(0, 2) > 0 ? "" : "");
            System.Threading.Thread.Sleep(TimeSpan.FromSeconds(1));
        }
    });
}
复制代码

image

示例代码展示每1秒钟录入一个人的年龄,使用Histogram统计所有录入的人的年龄的最新值,平均值,最大最小值,分位数(75%,95%,98%,99%,99.9%)等。从效果上看,可以认为Histogram是Gauge的复杂(数值统计)版。

Meter

复制代码
static Random ran = new Random(DateTime.Now.TimeOfDay.Milliseconds);
static void MeterTest()
{
    var meter = Metric.Meter("test.meter", Unit.Calls, TimeUnit.Seconds);

    Action idlesse = () => { System.Threading.Thread.Sleep(ran.Next(20, 50)); };
    Task.Run(() => {
        while(true)
        {
            meter.Mark();
            idlesse();
        }
    });
}
复制代码

image

示例代码展示了如何统计某个事件每秒钟发生的次数,可以用来统计qps或tps,除了全部数据的qps,和tps,还提供了最近1分钟、5分钟、15分钟的事件频率。调用里有个时间单位(TimeUnit)参数rateUnit,频率=总次数/总时间,这个rateUnit就是总时间的单位。

Timer

复制代码
static Random ran = new Random(DateTime.Now.TimeOfDay.Milliseconds);
static void TimerTest()
{
    var timer = Metric.Timer("test.meter", Unit.None, SamplingType.FavourRecent, TimeUnit.Seconds, TimeUnit.Microseconds);

    Action doWork = () => { System.Threading.Thread.Sleep(ran.Next(10, 300)); };
    Action idlesse = () => { System.Threading.Thread.Sleep(ran.Next(0, 500)); };
    for (var i = 0; i < 20; i++)
    {
        Task.Run(() =>
        {
            while (true)
            {
                timer.Time(doWork);
                idlesse();
            }
        });
    }
}
复制代码

image

timer是meter和histogram结合,在meter的基础上,统计了每个业务处理的耗时的histogram度量信息,同时记录并发数。创建timer需要两个时间单位(TimeUnit),第一个rateUnit同meter里的含义一样,第二个durationUnit是统计每个业务处理耗时的时间单位。

HealthCheck

 

 

 

 

 

 

 

复制代码
static Random ran = new Random(DateTime.Now.TimeOfDay.Milliseconds);
static void HealthCheckTest()
{
    HealthChecks.RegisterHealthCheck("test.healthcheck", () =>
    {
        return ran.Next(100) < 5 ? HealthCheckResult.Unhealthy() : HealthCheckResult.Healthy();
    });
}
复制代码

健康状况检查很简单,输出就两个值:健康(healthy)、不健康(unhealthy)。

度量值类型

非严谨关系概览

image

GaugeValue : double

CounterValue

复制代码
{
    "Count" : "long"
    "Items" : [{
        "Item" : "string",
        "Count" : "long",
        "Percent" : "double"
    }]
}
复制代码

 

 

 

 

HistogramValue

复制代码
{
    "Count" : "long",
    "LastValue" : "double",
    "LastUserValue" : "string",
    "Max" : "double",
    "MaxUserValue" : "string",
    "Mean" : "double",
    "Min" : "double";
    "MinUserValue" : "string",
    "StdDev" : "double",
    "Median" : "double",
    "Percentile75" : "double",
    "Percentile95" : "double",
    "Percentile98" : "double",
    "Percentile99" : "double",
    "Percentile999" : "double",
    "SampleSize" : "int"
}
复制代码

MeterValue

复制代码
{
    "Count" : "long",
    "MeanRate" : "double",
    "OneMinuteRate" : "double",
    "FiveMinuteRate" : "double",
    "FifteenMinuteRate" : "double",
    "RateUnit" : "TimeUnit",
    "Items" : [{
        "Item" : "string",
        "Percent" : "double",
        "Value" : {
            "Count" : "long",
            "MeanRate" : "double",
            "OneMinuteRate" : "double",
            "FiveMinuteRate" : "double",
            "FifteenMinuteRate" : "double",
            "RateUnit" : "TimeUnit"
        }
    }]
}
复制代码

TimerValue

{
    "Rate" : "MeterValue",
    "Histogram" : "HistogramValue",
    "ActiveSessions" : "long"
}

度量值采集方式

Gauge

使用时读取,无需额外函数支持

 

Counter

Increase(long value=1, string item=null) : void //增加计数

Decrease(long value=1, string item=null) : void //减少计数

Histogram

Update(double value, string userValue=null) : void

Meter

Mark(long count=1, string item=null) : void

Timer

Record(long duration, TimeUnit unit,  string userValue=null) : void

Timer(Action action, string userValue=null) : void

Timer<T>(Func<T> action, string userValue=null) : T

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值