asp.net core api網關 實時性能監控
阿新 • • 發佈:2018-08-08
seo www. blog 設置 gen 官方文檔 spn env tin
asp.net core api網關 實時性能監控
使用InfluxDB、Grafana
Dockerfile 運行 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=admin grafana: image: grafana/grafana ports: - "3000:3000"
配置 Grafana
- 瀏覽器打開
<本地ip>:3000
,使用默認賬號登錄 - 添加數據源
在Configuration中點擊Add data source按鈕,輸入你安裝的InfluxDB數據庫信息 - 點擊儀表板設置模板
在API網關中App.Metrics
- 安裝必要的nuget包
> App.Metrics > App.Metrics.AspNetCore.Endpoints > App.Metrics.AspNetCore.Reporting > App.Metrics.AspNetCore.Tracking > App.Metrics.Reporting.InfluxDB
- ConfigureServices 配置
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 IServiceCollection 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; }
}
最終效果
參考文章
- .NET Core微服務之基於App.Metrics+InfluxDB+Grafana實現統一性能監控
- ASP.NET Core Real-time Performance Monitoring
- influxdb docker 文檔
- grafana 官方文檔
asp.net core api網關 實時性能監控