prometheus-net
54 строки · 1.9 Кб
1using Prometheus;2using System;3using System.Net;4using System.Threading.Tasks;5
6// This sample demonstrates how to integrate prometheus-net into a console app (e.g. a worker service).
7//
8// NuGet packages required:
9// * prometheus-net
10
11internal static class Program12{
13public static void Main(string[] args)14{15// Start the metrics server on your preferred port number.16var server = new MetricServer(port: 1234);17
18try19{20// On .NET Framework, starting the server requires either elevation to Administrator or permission configuration.21server.Start();22}23catch (HttpListenerException ex)24{25Console.WriteLine($"Failed to start metric server: {ex.Message}");26Console.WriteLine("You may need to grant permissions to your user account if not running as Administrator:");27Console.WriteLine("netsh http add urlacl url=http://+:1234/metrics user=DOMAIN\\user");28return;29}30
31// Generate some sample data from fake business logic.32var recordsProcessed = Metrics.CreateCounter("sample_records_processed_total", "Total number of records processed.");33
34_ = Task.Run(async delegate35{36while (true)37{38// Pretend to process a record approximately every second, just for changing sample data.39recordsProcessed.Inc();40
41await Task.Delay(TimeSpan.FromSeconds(1));42}43});44
45// Metrics published in this sample:46// * built-in process metrics giving basic information about the .NET runtime (enabled by default)47// * the sample counter defined above48Console.WriteLine("Open http://localhost:1234/metrics in a web browser.");49Console.WriteLine("Press enter to exit.");50Console.ReadLine();51
52server.Stop();53}54}