public void AvailableMetrics_ReturnsResults() { Mock <IDatabaseContext> fakeContext = new Mock <IDatabaseContext>(); fakeContext.Setup(x => x.List <Person>()).Returns(new[] { new Person { Id = 1, } }.AsQueryable()); var hub = new MetricsHub(fakeContext.Object, 1); var metrics = hub.AvailableMetrics; Assert.IsTrue(metrics.Any()); }
/// <summary> /// 处理监控事项 /// </summary> /// <param name="services"></param> void MetricsHandle(IServiceCollection services) { var metricsHub = new MetricsHub(); //counter metricsHub.AddCounter("/register", Metrics.CreateCounter("business_register_user", "注册用户数。")); metricsHub.AddCounter("/order", Metrics.CreateCounter("business_order_total", "下单总数。")); metricsHub.AddCounter("/pay", Metrics.CreateCounter("business_pay_total", "支付总数。")); metricsHub.AddCounter("/ship", Metrics.CreateCounter("business_ship_total", "发货总数。")); //gauge var orderGauge = Metrics.CreateGauge("business_order_count", "当前下单数量。"); var payGauge = Metrics.CreateGauge("business_pay_count", "当前支付数量。"); var shipGauge = Metrics.CreateGauge("business_ship_count", "当前发货数据。"); metricsHub.AddGauge("/order", new Dictionary <string, Gauge> { { "+", orderGauge } }); metricsHub.AddGauge("/pay", new Dictionary <string, Gauge> { { "-", orderGauge }, { "+", payGauge } }); metricsHub.AddGauge("/ship", new Dictionary <string, Gauge> { { "+", shipGauge }, { "-", payGauge } }); //summary 百分位数[在一组由小到大的数字中,某个数字大于80%的数字,这个数字就第80个的百分位数] /*0.5-quantile后面是0.05,0.9-quantile后面是0.01,而0.95后面是0.005,而0.99后面是0.001。这些是我们设置的能容忍的误差。0.5-quantile: 0.05意思是允许最后的误差不超过0.05。假设某个0.5-quantile的值为120,由于设置的误差为0.05,所以120代表的真实quantile是(0.45, 0.55)范围内的某个值。 */ var orderSummary = Metrics .CreateSummary("business_order_summary", "10分钟内的订单数量", new SummaryConfiguration { Objectives = new[] { new QuantileEpsilonPair(0.1, 0.05), new QuantileEpsilonPair(0.3, 0.05), new QuantileEpsilonPair(0.5, 0.05), new QuantileEpsilonPair(0.7, 0.05), new QuantileEpsilonPair(0.9, 0.05), } }); metricsHub.AddSummary("/order", orderSummary); //histogram /* * grafana中 histogram_quantile(0.95, rate(business_order_histogram_seconds_bucket[5h])) * 95%的订单金额小于等于这个值 */ var orderHistogram = Metrics.CreateHistogram("business_order_histogram", "订单直方图。", new HistogramConfiguration { //Buckets = Histogram.ExponentialBuckets(start: 1000, factor: 2, count: 5) Buckets = Histogram.LinearBuckets(start: 1000, width: 1000, count: 6) }); metricsHub.AddHistogram("/order", orderHistogram); services.AddSingleton(metricsHub); }