.netcore 实践Metrics

一、新建项目:AppMetricsDemo

<PackageReference Include="App.Metrics.Extensions.Middleware" Version="1.2.1" />
<PackageReference Include="App.Metrics.Extensions.Reporting.InfluxDB" Version="1.2.0" />

Startup.cs

using App.Metrics.Extensions.Reporting.InfluxDB;
using App.Metrics.Extensions.Reporting.InfluxDB.Client;
using App.Metrics.Reporting.Interfaces;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace AppMetricsDemo
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            /*Metrics*/
            services.AddMetrics(options =>
            {
                options.GlobalTags.Add("app", "AppMetricsDemo");
                options.GlobalTags.Add("env", "Development");
            }).AddHealthChecks()
            .AddReporting(factory =>
            {
               factory.AddInfluxDb(
                        new InfluxDBReporterSettings
                        {
                            InfluxDbSettings = new InfluxDBSettings("mydemoDb", new Uri("http://192.168.31.133:8086")) {  },
                            ReportInterval = TimeSpan.FromSeconds(5)
                        });
             })
            .AddMetricsMiddleware(options => options.IgnoredHttpStatusCodes = new[] { 404 });

            services.AddRazorPages();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, Microsoft.AspNetCore.Hosting.IApplicationLifetime lifetime)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
            }

            /*Metrics*/
            app.UseMetrics();
            app.UseMetricsReporting(lifetime);

            app.UseStaticFiles();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapRazorPages();
            });
        }
    }
}

二、登录Grafana,安装influxdb插件

.netcore 实践Metrics

 .netcore 实践Metrics

.netcore 实践Metrics .netcore 实践Metrics

.netcore 实践Metrics  

三、登录Grafana,添加InfluxDB的Dashboard仪表盘的JSON文件

.netcore 实践Metrics

 https://grafana.com/dashboards/2125.netcore 实践Metrics

 .netcore 实践Metrics

 

上一篇:VictoriaMetrics:使用vmctl来实现vm-storage向victoria-metrics-prod(单机版)迁移数据


下一篇:k8s prometheus监控指标