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. Source Insight中的多行注释

    转自:http://www.cnblogs.com/dongzhiquan/archive/2013/03/04/2943448.html 我们经常要对一整段代码进行注释,很多代码编辑器都提供了这样的 ...

  2. 微信小程序组件 滚动导航

    JS data: { // 初始化滑动条数据 menuIndex:0, // 每个菜单的宽度 onlyWidth: 70, // 右侧的margin marginWidth:10, // 菜单总长 m ...

  3. Flyway学习笔记

    Flyway做为database migration开源工具,功能上像是git.svn这种代码版本控制.google搜索database migration,或者针对性更强些搜索database mi ...

  4. Java多线程(二) —— 深入剖析ThreadLocal

    对Java多线程中的ThreadLocal类还不是很了解,所以在此总结一下. 主要参考了http://www.cnblogs.com/dolphin0520/p/3920407.html 中的文章. ...

  5. Java的Bean

    Bean的定义 遵循Sun的Java Bean规范编写的特殊类 Java Bean的规范 类的访问控制权限是public 类提供有一个无参的构造函数 类的属性的访问控制权限是private,通过set ...

  6. npm 镜像修改

    1, 修改 下载仓库为淘宝镜像  npm config set registry http://registry.npm.taobao.org/ 2, 如果要发布自己的镜像需要修改回来  npm co ...

  7. Java 输入/输出 反射

    Java  输入/输出   反射 输入输出和反射 一.数据流的基本概念 流一般分为 ( Input Stream ) 和输出流 ( Output Stream ) 两类,但这种划分并不是绝对的.比如一 ...

  8. SpringMVC框架并发时出现id变成另外一个用户id问题

    今天测试写的代码,出现了在用一个账户登录操作的时候,操作记录的是另外一个id. 经过查找网上的解决方案确认了问题:在controller里面定义了一个userid属性,每次都通过userid传输值.然 ...

  9. linux 批量更改文件名 rename 命令

    rename 的典型应用: # rename $1 $2 $3# $1: 要被取代的關鍵字# $2: 新的關鍵字# $3: 檔名符合這個規則的才取代 # 把 IMG001.jpg, IMG002.jp ...

  10. BZOJ2436 [Noi2011]Noi嘉年华 【dp】

    题目链接 BZOJ2436 题解 看这\(O(n^3)\)的数据范围,可以想到区间\(dp\) 发现同一个会场的活动可以重叠,所以暴力求出\(num[l][r]\)表示离散化后\([l,r]\)的完整 ...