ASP. NET Core API Gateway real-time performance monitoring
Using Influxdb, Grafana
Dockerfile running InfluxDB, Grafana
influxdb:image: influxdb ports: - "8086:8086" - "8083:8083" environment: - INFLUXDB_DB=TogetherAppMetricsDB - INFLUXDB_ADMIN_ENABLED=true - INFLUXDB_ADMIN_USER=admin - INFLUXDB_ADMIN_PASSWORD=admingrafana: image: grafana/grafana ports: - "3000:3000"
Configure Grafana
- Browser Open
<本地ip>:3000
, login with default account
- Add a data source
Click the Add Data Source button in the configuration to enter the INFLUXDB database information you installed
- Click Dashboard Settings Template
App.metrics in API Gateway
- Install the necessary NuGet packages
> App.Metrics> App.Metrics.AspNetCore.Endpoints> App.Metrics.AspNetCore.Reporting> App.Metrics.AspNetCore.Tracking> App.Metrics.Reporting.InfluxDB
- Configureservices Configuration
StartUp.cs
public void ConfigureServices(IServiceCollection services){ ... services.AddOptions(); services.Configure<AppMetricsOptions>(Configuration.GetSection("AppMetrics")); services.AddAppMetrics(Configuration); services.AddOcelot(Configuration);} public void Configure(ILoggerFactory loggerFactory, IApplicationBuilder app, IHostingEnvironment env) { app.UseMetricsAllEndpoints(); app.UseMetricsAllMiddleware(); app.UseOcelot(); }
ServiceCollectionExtensions.cs
public static class servicecollectionextensions{public static Iservicecollection Addappmetrics (This iservicecollectio N Services, iconfiguration configuration) {var options = Services. Buildserviceprovider (). Getrequiredservice<ioptions<appmetricsoptions>> ()?. Value; if (options?. IsOpen = = true) {var uri = new Uri (options). ConnectionString); var metrics = Appmetrics.createdefaultbuilder (). Configuration.configure (opt + = {opt. Addapptag (options. APP); Opt. Addenvtag (options. ENV); }). Report.toinfluxdb (opt + = {opt. Influxdb.baseuri = URI; Opt. Influxdb.database = options. DatabaseName; Opt. Influxdb.username = options. UserName; Opt. Influxdb.password = options. Password; Opt. Httppolicy.backoffperiod = Timespan.fromseconds (30); Opt. Httppolicy.failuresbeforebackoff = 5; Opt. Httppolicy.timeout = Timespan.fromseconds (10); Opt. Flushinterval = Timespan.fromseconds (5); }). Build (); Services. Addmetrics (metrics); Services. Addmetricsreportscheduler (); Services. Addmetricstrackingmiddleware (); Services. Addmetricsendpoints (); } return services; }}public class appmetricsoptions{public bool IsOpen {get; set;} public string DatabaseName {get; set;} public string ConnectionString {get; set;} public string UserName {get; set;} public string Password {get; set;} public string App {get; set;} public string Env {get; set;}}
Final effect
Reference articles
- . NET core microservices based on App.metrics+influxdb+grafana for unified performance monitoring
- ASP. NET Core Real-time performance monitoring
- Influxdb Docker Documentation
- Grafana Official documents
ASP. NET Core API Gateway real-time performance monitoring