Gauges

可以画出Http Request执行时间的波形图:

actionInfo表示MVC中的Action,即按照action类型来分组

Metric.Context(this.actionInfo.ActionType)
.Gauge(counterName, () => milliseconds, Unit.Custom("Milliseconds"));

Counters

记录总的请求数和并发请求数:

public class WebApiApplication : System.Web.HttpApplication
{
private readonly Counter totalRequestsCounter =
Metric.Context("[Request]").Counter("Total_Requests", Unit.Custom("個"), "request"); private readonly Counter concurrentRequestsCounter =
Metric.Context("[Request]").Counter("Concurrent_Requests", Unit.Custom("個"), "request,concurrent"); /// <summary>
/// 應用程序啟動方法
/// </summary>
protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register); Metric.Config
.WithHttpEndpoint("http://localhost:1234/metrics/");
//Metric.Config.WithHttpEndpoint("http://+:8898/")//外网可以访问
} /// <summary>
/// 開始請求
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Application_BeginRequest(object sender, EventArgs e)
{
totalRequestsCounter.Increment();
concurrentRequestsCounter.Increment();
} /// <summary>
/// 結束請求
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Application_EndRequest(object sender, EventArgs e)
{
concurrentRequestsCounter.Decrement();
}
}

Histograms

度量POST&PUT请求大小的直方图:

public PostAndPutRequestSizeMetric(ActionInfo info)
: base(info)
{
this.histogram = Metric.Context(this.actionInfo.ActionType)
.Histogram(COUNTER_NAME, Unit.Bytes, SamplingType.FavourRecent);
} /// <summary>
/// Constant defining the name of this counter
/// </summary>
public const String COUNTER_NAME = "Post & Put Request Size"; /// <summary>
/// Reference to the performance counter
/// </summary>
private Histogram histogram; public override void OnActionStart()
{
var method = this.actionInfo.HttpMethod.ToUpper();
if (method == "POST" || method == "PUT")
{
histogram.Update(this.actionInfo.ContentLength);
}
}

Histrogram 的度量值不仅仅是计算最大/小值、平均值,方差,他还展现了分位数(如中位数,或者95th分位数),如75%,90%,98%,99%的数据在哪个范围内。

传统上,中位数(或者其他分位数)是在一个完整的数据集中进行计算的,通过对数据的排序,然后取出中间值(或者离结束1%的那个数字,来计算99th分位数)。这种做法是在小数据集,或者是批量计算的系统中,但是在一个高吞吐、低延时的系统中是不合适的。

一个解决方案就是从数据中进行抽样,保存一个少量、易管理的数据集,并且能够反应总体数据流的统计信息。使我们能够简单快速的计算给定分位数的近似值。这种技术称作reservoir sampling。

Meters

Meter度量一系列事件发生的比率:

 public DeltaExceptionsThrownMetric(ActionInfo info)
: base(info)
{
this.deltaExceptionsThrownCounter
= Metric.Context(this.actionInfo.ActionType).Meter(COUNTER_NAME, Unit.Errors, TimeUnit.Seconds);
} /// <summary>
/// Constant defining the name of this counter
/// </summary>
public const String COUNTER_NAME = "Errors"; /// <summary>
/// Reference to the performance counter
/// </summary>
private Meter deltaExceptionsThrownCounter; /// <summary>
/// Method called by the custom action filter after the action completes
/// </summary>
/// <remarks>
/// If exceptionThrown is true, then the Total Exceptions Thrown counter will be
/// incremented by 1
/// </remarks>
public override void OnActionComplete(long elapsedTicks, bool exceptionThrown)
{
if (exceptionThrown)
this.deltaExceptionsThrownCounter.Mark();
}

Meter需要除了Name之外的两个额外的信息,事件类型(enent type)跟比率单位(rate unit)。事件类型简单的描述Meter需要度量的事件类型,在上面的例子中,Meter是度量失败的请求数,所以他的事件类型也叫做“Errors”。比率单位是命名这个比率的单位时间,在上面的例子中,这个Meter是度量每秒钟的失败请求次数,所以他的单位就是秒。这两个参数加起来就是表述这个Meter,描述每秒钟的失败请求数。

Timers

Timer是Histogram跟Meter的一个组合

public TimerForEachRequestMetric(ActionInfo info)
: base(info)
{
String controllerName = this.actionInfo.ControllerName;
String actionName = this.actionInfo.ActionName;
string counterName = string.Format("{0}{1}", controllerName, actionName); this.averageTimeCounter = Metric.Context(this.actionInfo.ActionType)
.Timer(counterName, Unit.Requests, SamplingType.FavourRecent,
TimeUnit.Seconds, TimeUnit.Milliseconds);
} #region Member Variables
private Timer averageTimeCounter;
#endregion /// <summary>
/// Method called by the custom action filter after the action completes
/// </summary>
/// <remarks>
/// This method increments the Average Time per Call counter by the number of ticks
/// the action took to complete and the base counter is incremented by 1 (this is
/// done in the PerfCounterUtil.IncrementTimer() method).
/// </remarks>
/// <param name="elapsedTicks">A long of the number of ticks it took to complete the action</param>
public override void OnActionComplete(long elapsedTicks, bool exceptionThrown)
{
averageTimeCounter.Record(elapsedTicks, TimeUnit.Nanoseconds);
}

REF :http://www.cnblogs.com/mrblue/p/7080242.html

Metrics.Net实践(2)在WEB中应用度量的更多相关文章

  1. Web中的图标

    随着时代的变迁与技术的不断的更新,在当今这个时代,Web中的图标(Icons)不再仅仅是局限于<img>.除了<img>直接调用Icons文件之外,还有Sprites(俗称雪碧 ...

  2. 20145203盖泽双 《网络对抗技术》实践九:Web安全基础实践

    20145203盖泽双 <网络对抗技术>实践九:Web安全基础实践 1.实践目标 1.理解常用网络攻击技术的基本原理. 2.Webgoat下进行相关实验:SQL注入攻击.XSS攻击.CSR ...

  3. 20145203盖泽双 《网络对抗技术》实践八:Web基础

    20145203盖泽双 <网络对抗技术>实践八:Web基础 1.实践目标 (1)编写Web前端--含有表单的HTML代码. (2)编写Web前端--javascipt验证用户名.密码的代码 ...

  4. Jasperreport+ireport 实践操作及web应用

    Jasperreport+ireport 实践操作及web应用   学习完jasperreports+ireport,给我感觉深刻,不仅掌握了报表开发技术,还掌握了怎样在web中生成pdf,xls,r ...

  5. 《SaltStack技术入门与实践》—— 实践案例 <中小型Web架构>3 Memcached配置管理

    实践案例 <中小型Web架构>3 Memcached配置管理 本章节参考<SaltStack技术入门与实践>,感谢该书作者: 刘继伟.沈灿.赵舜东 Memcached介绍 Me ...

  6. 优化Web中的性能

    优化Web中的性能 简介 web的优化就是一场阻止http请求最终访问到数据库的战争. 优化的方式就是加缓存,在各个节点加缓存. web请求的流程及节点 熟悉流程及节点,才能定位性能的问题.而且优化的 ...

  7. 在C#代码中应用Log4Net(四)在Winform和Web中捕获全局异常

    毕竟人不是神,谁写的程序都会有bug,有了bug不可怕,可怕的是出错了,你却不知道错误在哪里.所以我们需要将应用程序中抛出的所有异常都记录起来,不然出了错,找问题就能要了你的命.下面我们主要讨论的是如 ...

  8. 命名空间“System.Web”中不存在类型或命名空间名称“Optimization”(是否缺少程序集引用?)

    今天,在.net4.5,mvc4下新建了个区域,运行起来就报这个错误: 命名空间"System.Web"中不存在类型或命名空间名称"Optimization"( ...

  9. 在web中使用windows控件,实现摄像头功能

    最近做的一个Web版的视频会议项目,需要在网页中播放来自远程摄像头采集的实时视频,我们已经有了播放远程实时视频的使用C#编写的windows控件,如何将其嵌入到网页中去了?这需要使用一种古老的技术,A ...

随机推荐

  1. ThinkPhp输入参数过滤

    I('id',0); // 获取id参数 自动判断get或者post,不存在时返回默认值0 I('post.name','','htmlspecialchars'); //获取$_POST['name ...

  2. profibus总线和profibus dp的区别

    profibus总线和profibus dp的区别:PROFBUS是一种国际性的开放式的现场总线标准,它既可以用于高速并且对于时间苛求的数据传输,也可以用于大范围的复杂通讯场合.PROFBUS-DP是 ...

  3. testNg-build.xml

    <?xml version="1.0" encoding="UTF-8" standalone="no"?> <proje ...

  4. Python模块Scrapy导入出错:ImportError: cannot import name xmlrpc_client

    Mac(OS version: OS X Yosemite 10.10.5)上安装Scrapy模块,使用时出现: from six.moves import xmlrpc_client as xmlr ...

  5. MSTSC 修改端口的简单方法 3389

    1. 3389端口太过危险  最简单的办法是 修改默认端口方法非常简单. 2. win+r 打开运行, 输入 regedit 打开 注册表 3. 在地址栏输入 远程的服务的路径 输入的内容为: 计算机 ...

  6. SqlServer日期时间函数

    -- 判断是否当天,createdate为日期字段 -- ╔════════════════════╗ -- ============================================= ...

  7. logback错误日志发送邮件

    logback 一.介绍 logback是由log4j的作者开发的一个开源日志组件,用以替代log4j. logback由3个部分组成: ♦ logback-core (基础模块) ♦ logback ...

  8. spring 整合 Struts1.X [转]

    这篇博客摘自[http://blog.csdn.net/chendc201/article/details/8464008], 其中也有一些是自己增加的部分 . 第一步, 需要为 Struts 装载 ...

  9. 更新ffmpeg

    今天对公司线上的几台机器做了下ffmpeg的更新,没有什么技术含量,还是简单记录下,做个流水账~哈哈 软件包获取方式 官方网站:https://ffmpeg.org/download.htmlgith ...

  10. Graph And Its Complement CodeForces - 990D(思维构造)

    题意: 图中有n个点,开始有a个连通块,然后连着的边断开,不连的边连上,变为b个连通块,输出原图的邻接矩阵. 解析: 原图中连通块大于1的图,经过上述操作后,一定变成只有1个连通块的图. 若n != ...