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. 基于windowsphone7的控制ppt播放

    最近突然想起了一个学长的一个利用手机控制ppt播放的一个创意,并想将其在windows phone7上实现一下. 经过几天的努力已经可以控制ppt的播放,暂停,上一张,下一张了,并且电脑会将当前ppt ...

  2. Java通用oracle和mysql数据库连接

    Java中oracle数据库连接写一个通用类UBUtil(){} import java.io.InputStream; import java.sql.*; import java.util.Pro ...

  3. 虚拟机VMware中的CentOS字符界面和图形界面切换

    在虚拟机中安装CentOS后展示的界面是图形用户界面,想切换到命令模式,于是查了linux下切换模式的方法,可是按了ctrl+alt+f1.f2....f7都是没用,后来发现是因为按键冲突的原因 问题 ...

  4. Java如何查看死锁

    Java中当我们的开发涉及到多线程的时候,这个时候就很容易遇到死锁问题,刚开始遇到死锁问题的时候,我们很容易觉得莫名其妙,而且定位问题也很困难. 因为涉及到java多线程的时候,有的问题会特别复杂,而 ...

  5. Java 几种调度任务的Timer、ScheduledExecutor、 开源工具包 Quartz、开源工具包 JCronTab

    关于Java中的调度问题,是比较常见的问题,一直没有系统的梳理,现在梳理一下 注意:Quartz的例子 需要在特定的版本上执行,不同的版本使用方法不同,但是总的来说方法大同小异.本例子的版本是1.8 ...

  6. using关键字

    声明导入名称空间 处理实现了IDisposeable的对象,并在作用域末尾调用Dispose方法

  7. todomvc-app

    1.HTML <!doctype html> <html lang="en"> <head> <meta charset="ut ...

  8. Avito Cool Challenge 2018 自闭记

    A:n==2?2:1. #include<iostream> #include<cstdio> #include<cmath> #include<cstdli ...

  9. Huge Mods UVA - 10692(指数循环节)

    题意: 输入正整数a1,a2,a3..an和模m,求a1^a2^...^an mod m 解析: #include <iostream> #include <cstdio> # ...

  10. SpringBoot项目部署进阶

    一.war包部署 通过“云开发”平台初始化的SpringBoot项目默认采用jar形式打包,这也是我们推荐的方式.但是,因为某些原因,软件需求方特别要求用war形式打包,我们该怎么做? 1.项目尚未开 ...