一、Metrics簡介 應用監控系統Metrics由Metrics.NET+InfluxDB+Grafana組合而成,通過客戶端Metrics.NET在業務代碼中埋點,Metrics.NET會把收集到數據存儲在InfluxDB資料庫中,然後通過Grafana來展示監控數據。其中,InfluxDB服務 ...
1 <add key="AppID" value="150106"/> 2 <add key="Metrics.DBUri" value="http://139.198.13.12:4126/write"/> 3 <add key="Metrics.UserName" value="Arch"/> 4 <add key="Metrics.Password" value="Arch"/> 5 <add key="Metrics.Database" value="ArchDB"/>
1 static void CreateOrder() 2 { 3 try 4 { 5 // 省略關於下單的業務邏輯代碼 6 //...... 7 8 // 分別統計成功下單量和下單金額,統一寫到MetrisKey中 9 MetricsKey.OrderCount.Mark(); 10 if (n % 2 == 1) 11 { 12 MetricsKey.OrderMoneyCount.Mark("BuyerA", n); 13 } 14 else 15 { 16 MetricsKey.OrderMoneyCount.Mark("BuyerB", n); 17 } 18 } 19 catch (Exception) 20 { 21 // 統計失敗下單量,統一寫到MetrisKey中 22 MetricsKey.OrderErrorCount.Mark(); 23 24 // 省略異常處理代碼...... 25 } 26 }
1 private readonly Histogram searchFlightTime = MetricsHelper.Histogram("MetricsDemo.SearchFlightTime", Unit.Custom("ms")); 2 3 static void SearchFlight() 4 { 5 Stopwatch stopwatch = Stopwatch.StartNew(); 6 7 // 模擬關於航班查詢的業務邏輯的代碼 8 Random random = new Random((int)DateTime.Now.Ticks & 0x0000FFFF); 9 var n = Random.Next(100); 10 Thread.Sleep(n); 11 12 stopwatch.Stop(); 13 14 // 統計航班搜索耗時 15 searchFlightTime.Update(stopwatch.ElapsedMilliseconds); 16 }