Metrics中怎么监控应用程序的性能

发布时间:2021-08-05 17:25:15 作者:Leah
来源:亿速云 阅读:130

本篇文章为大家展示了Metrics中怎么监控应用程序的性能,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

一 度量类型

Metrics提供5种基本的度量类型:Gauges, Counters, Histograms, Meters和 Timers

Gauge

Gauge是最简单的度量类型,只有一个简单的返回值,他用来记录一些对象或者事物的瞬时值。

比如,我们类型为Gauge的计数器来记录某个服务目前开通的城市个数

Metric.Gauge("Service Cities Count", () => Cities.Count, new Unit("个"));

Counters

Counter是一个简单64位的计数器,他可以增加和减少。

比如我们可以定义两个Counter类型的计数器,用来统计所有服务请求数目,和目前正在处理的请求总数。

/// <summary>
/// keep the total count of the requests/// </summary>private readonly Counter totalRequestsCounter = Metric.Counter("Requests", Unit.Requests);/// <summary>
/// count the current concurrent requests/// </summary>private readonly Counter concurrentRequestsCounter = Metric.Counter("SampleMetrics.ConcurrentRequests", Unit.Requests);

这样,在我们请求处理开始的时候,同时将这两个计数器自增。

this.concurrentRequestsCounter.Increment(); // increment concurrent requests counterthis.totalRequestsCounter.Increment(); // increment total requests counter

当某一个请求处理完成之后,将目前正在处理的请求减一

this.concurrentRequestsCounter.Decrement(); // decrement number of concurrent requests

这种计数器也可以用来统计诸如当前有多少人在线,或者服务器中有多少处于有效期内的session

Meters

Meter是一种只能自增的计数器,通常用来度量一系列事件发生的比率。他提供了平均速率,以及指数平滑平均速率,以及采样后的1分钟,5分钟,15分钟速率。

比如需要统计请求的速率,比如统计平均每分钟内有多少次请求进来。只需要定义一个metric

/// <summary>
/// measure the rate at which requests come in/// </summary>private readonly Meter meter = Metric.Meter("Requests", Unit.Requests,TimeUnit.Seconds);

在处理请求的地方,调用Mark方法即可。

this.meter.Mark(); // signal a new request to the meter

再比如,要测量服务出错的概率,比如每小时出错多少次。可以定义一个metric。

/// <summary>
/// measure the rate of service exception/// </summary>private readonly Meter errorMeter = Metric.Meter("Error", Unit.Errors, TimeUnit.Hours);

这样,在处理请求的时候,如果出现异常了,调用一下errorMeter的Mark方法即可。

this.errorMeter.Mark();// signal a new error to the meter

Histograms

Histrogram是用来度量流数据中Value的分布情况,Histrogram可以计算最大/小值、平均值,方差,分位数(如中位数,或者95th分位数),如75%,90%,98%,99%的数据在哪个范围内。

比如,我们想度量,所有传进来服务的请求参数的长度分布。那么,可以定义一个histogram。

/// <summary>
/// keep a histogram of the input data of our request method 
/// </summary>private readonly Histogram histogramOfData = Metric.Histogram("ResultsExample", Unit.Items);

然后在请求的地方,调用其Update方法来更新值。

this.histogramOfData.Update(request.length, methodName); // update the histogram with the input data

Timer

Timer是Histogram跟Meter的一个组合,比如要统计当前请求的速率和处理时间。

就可以定义一个Timer:

/// <summary>
/// measure the time rate and duration of requests/// </summary>private readonly Timer timer = Metric.Timer("Requests", Unit.Requests);

在使用的时候,调用timer的NewContext即可。

using (this.timer.NewContext(i.ToString())) // measure until disposed{
    ...
}

二 度量数据的输出

收集了这么多数据之后,我们需要把数据时实的动态展示或者保存起来。Metric提供了多种的数据报告接口。包括自带的Metrics.NET.FlotVisualization, 以及输出到专业的系统监控Graphite,输出到开源,分布式,时间序列的中InfluxDB,或者输出到ElasticSearch中。配置起来也非常简单。比如如果要直接在http页面上展现,只需要在初始化的时候,设置合适的EndPoint即可:

Metric.Config
    .WithHttpEndpoint("http://localhost:1234/metrics/")
    .WithAllCounters()
    .WithInternalMetrics()
    .WithReporting(config => config
        .WithConsoleReport(TimeSpan.FromSeconds(30))

然后在浏览器中输入 http://localhost:1234/metrics/,就可以看到各种采集的准实时各种度量信息:

Metrics中怎么监控应用程序的性能

上面自带的性能DashBoard略显简陋。 通常,我们一般会将这些实时采集的数据存储到分布式时序数据库InfluxDB中,然后利用开源的图表控件Grafana来实时展现这些数据,比如,可以制作想下面这样的,动态的性能准实时监控系统:

上述内容就是Metrics中怎么监控应用程序的性能,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. mongodb性能监控
  2. 使用Application Insights监控应用程序性能

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

metrics

上一篇:Salesforce中如何使用HaoIDE插件

下一篇:如何解决某些HTML字符打不出来的问题

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》