程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> Metrics.NET step by step,metrics.netstepby

Metrics.NET step by step,metrics.netstepby

編輯:C#入門知識

Metrics.NET step by step,metrics.netstepby


安裝Nuget包

nuget中搜索metrics,如圖:

class Program { static void Main(string[] args) { Metric.Config // Web監視儀表板,提供Metrics.NET度量值圖表 .WithHttpEndpoint("http://localhost:1234/metrics/") // 配置報表輸出 .WithReporting((rc)=>{ // 報表輸出到控制台 rc.WithConsoleReport(TimeSpan.FromSeconds(5)); }); Console.ReadLine(); } }

收集度量值

Gauge

static void GaugeTest()
{
    Metric.Gauge("test.gauge", () => ran.NextDouble() * 1000, Unit.None);
}

static Random ran = new Random(DateTime.Now.TimeOfDay.Milliseconds); static void CounterTest() { var counter = Metric.Counter("test.counter", Unit.Custom("並發")); Action doWork = () => { System.Threading.Thread.Sleep(ran.Next(10, 300)); }; Action idlesse = () => { System.Threading.Thread.Sleep(ran.Next(0, 500)); }; for (var i = 0; i < 20; i++) { Task.Run(() => { while (true) { counter.Increment(); doWork(); counter.Decrement(); idlesse(); } }); } }

static Random ran = new Random(DateTime.Now.TimeOfDay.Milliseconds); static void HistogramTest() { var histogram = Metric.Histogram("test.histogram", Unit.Custom("歲"), SamplingType.LongTerm); Task.Run(() => { while (true) { histogram.Update(ran.Next(10, 80), ran.Next(0, 2) > 0 ? "男" : "女"); System.Threading.Thread.Sleep(TimeSpan.FromSeconds(1)); } }); }

static Random ran = new Random(DateTime.Now.TimeOfDay.Milliseconds); static void MeterTest() { var meter = Metric.Meter("test.meter", Unit.Calls, TimeUnit.Seconds); Action idlesse = () => { System.Threading.Thread.Sleep(ran.Next(20, 50)); }; Task.Run(() => { while(true) { meter.Mark(); idlesse(); } }); }

static Random ran = new Random(DateTime.Now.TimeOfDay.Milliseconds); static void TimerTest() { var timer = Metric.Timer("test.meter", Unit.None, SamplingType.FavourRecent, TimeUnit.Seconds, TimeUnit.Microseconds); Action doWork = () => { System.Threading.Thread.Sleep(ran.Next(10, 300)); }; Action idlesse = () => { System.Threading.Thread.Sleep(ran.Next(0, 500)); }; for (var i = 0; i < 20; i++) { Task.Run(() => { while (true) { timer.Time(doWork); idlesse(); } }); } }

static Random ran = new Random(DateTime.Now.TimeOfDay.Milliseconds); static void HealthCheckTest() { HealthChecks.RegisterHealthCheck("test.healthcheck", () => { return ran.Next(100) < 5 ? HealthCheckResult.Unhealthy() : HealthCheckResult.Healthy(); }); }

健康狀況檢查很簡單,輸出就兩個值:健康(healthy)、不健康(unhealthy)。

度量值類型

非嚴謹關系概覽

{ "Count" : "long" "Items" : [{ "Item" : "string", "Count" : "long", "Percent" : "double" }] }

HistogramValue

{
    "Count" : "long",
    "LastValue" : "double",
    "LastUserValue" : "string",
    "Max" : "double",
    "MaxUserValue" : "string",
    "Mean" : "double",
    "Min" : "double";
    "MinUserValue" : "string",
    "StdDev" : "double",
    "Median" : "double",
    "Percentile75" : "double",
    "Percentile95" : "double",
    "Percentile98" : "double",
    "Percentile99" : "double",
    "Percentile999" : "double",
    "SampleSize" : "int"
}

MeterValue

{
    "Count" : "long",
    "MeanRate" : "double",
    "OneMinuteRate" : "double",
    "FiveMinuteRate" : "double",
    "FifteenMinuteRate" : "double",
    "RateUnit" : "TimeUnit",
    "Items" : [{
        "Item" : "string",
        "Percent" : "double",
        "Value" : {
            "Count" : "long",
            "MeanRate" : "double",
            "OneMinuteRate" : "double",
            "FiveMinuteRate" : "double",
            "FifteenMinuteRate" : "double",
            "RateUnit" : "TimeUnit"
        }
    }]
}

TimerValue

{
    "Rate" : "MeterValue",
    "Histogram" : "HistogramValue",
    "ActiveSessions" : "long"
}

度量值采集方式

Gauge

使用時讀取,無需額外函數支持

Counter

Increase(long value=1, string item=null) : void //增加計數

Decrease(long value=1, string item=null) : void //減少計數

Histogram

Update(double value, string userValue=null) : void

Meter

Mark(long count=1, string item=null) : void

Timer

Record(long duration, TimeUnit unit,  string userValue=null) : void

Timer(Action action, string userValue=null) : void

Timer<T>(Func<T> action, string userValue=null) : T

下載

MetricsDemo.rar

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved