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. 标头 header()函数的用法

    头 (header) 是服务器以 HTTP 协议传 HTML 资料到浏览器前所送出的字串,在标头与 HTML 文件之间尚需空一行分隔. 范例一: 本例使浏览器重定向到 PHP 的官方网站. <? ...

  2. 蜗牛慢慢爬 LeetCode 15. 3Sum [Difficulty: Medium]

    题目 Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all ...

  3. CentOS 7.2修改网卡名称

    #!/bin/bash #Centos7.2修改网卡名称ens33为eth0 #2017/05/19 if [ -f /etc/sysconfig/grub ];then cd cp /etc/sys ...

  4. jQ返回定位插件

    一.jQuery 提供开发者开发插件的几种模式 1.$.extend();     //这个方法是绑定在$上面的.可以通过$直接调用 2.$.fn.方法名     //这个方法是绑定在Dom对象上面的 ...

  5. [微软官网] SQLSERVER 执行页面还原

    执行页面还原 https://docs.microsoft.com/zh-cn/previous-versions/sql/sql-server-2008-r2/ms175168(v=sql.105) ...

  6. docker 建立私有仓库,24.205为镜像仓库所在主机

    一.下载registry #yum install -y python-devel libevent-devel python-pip gcc xz-devel #python-pip install ...

  7. python自动化之web抓取

    ''' 从web抓取数据: webbrowser:是python自带的,打开浏览器获取指定页面. requests:从因特网上下载文件和网页. Beautiful Soup:解析HTML,即网页编写的 ...

  8. zabbix 自定义监控nginx

    zabbix自定义nginx监控项 查看nginx编译安装是否加上该选项,如果没有请重新编译安装 配置nginx.conf vim /usr/local/cpgroup/nginx/conf/vhos ...

  9. Educational Codeforces Round 56 Div. 2 翻车记

    A:签到. B:仅当只有一种字符时无法构成非回文串. #include<iostream> #include<cstdio> #include<cmath> #in ...

  10. UOJ #7 【NOI2014】 购票

    题目链接:购票 这道题我调了好久啊……主要还是因为这种用\(CDQ\)分治来搞斜率优化的题已经很久没写过了……上一次要追溯到去年暑假去了…… 看下面这些东西之前你需要先自己推出斜率优化的式子…… 这道 ...