[英]How to integrate Prometheus with .NET Core application?
我正在嘗試為我的 C# .NET Core 控制台應用程序集成 Prometheus。 我不是在開發 ASP.NET Core 應用程序。 如何像我們通常為 ASP.NET Core 應用程序所做的那樣將指標數據發送到 prometheus?
在 ASP.NET Core 應用程序中,
打開 Startup.cs 並更新 ConfigureServices 和 Configure 以查看以下內容:
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<MetricReporter>();
services.AddControllers();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
// Other middleware components omitted for brevity
// Make sure these calls are made before the call to UseEndPoints.
app.UseMetricServer();
app.UseMiddleware<ResponseMetricMiddleware>();
app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
}
如何為 .NET Core 控制台應用程序執行此操作?
您可以使用 Prometheus-net 包,它提供了一些用於集成 .Net 和 Prometheus 的有用功能。 根據文檔,您可以為沒有任何可訪問 http 端點的控制台應用程序啟動 kestrel 獨立服務器。 為此,您必須在項目文件中的 Sdk 屬性值末尾添加 .Web。
之后你需要啟動紅隼:
var metricServer = new KestrelMetricServer(port: 1234);
metricServer.Start();
另一種方法是簡單地使用獨立的 Http 處理程序,如下所示:
var metricServer = new MetricServer(port: 1234);
metricServer.Start();
默認配置將在 /metrics URL 上發布指標。
如果您的用戶無權在指定端口上打開 Web 服務器,則MetricServer.Start()
可能會在 Windows 上引發訪問被拒絕異常。 您可以使用netsh
命令授予自己所需的權限:
netsh http add urlacl url=http://+:1234/metrics user=DOMAIN\user
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.