prometheus-net

Форк
0
54 строки · 1.9 Кб
1
using Prometheus;
2
using System;
3
using System.Net;
4
using 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

11
internal static class Program
12
{
13
    public static void Main(string[] args)
14
    {
15
        // Start the metrics server on your preferred port number.
16
        var server = new MetricServer(port: 1234);
17

18
        try
19
        {
20
            // On .NET Framework, starting the server requires either elevation to Administrator or permission configuration.
21
            server.Start();
22
        }
23
        catch (HttpListenerException ex)
24
        {
25
            Console.WriteLine($"Failed to start metric server: {ex.Message}");
26
            Console.WriteLine("You may need to grant permissions to your user account if not running as Administrator:");
27
            Console.WriteLine("netsh http add urlacl url=http://+:1234/metrics user=DOMAIN\\user");
28
            return;
29
        }
30

31
        // Generate some sample data from fake business logic.
32
        var recordsProcessed = Metrics.CreateCounter("sample_records_processed_total", "Total number of records processed.");
33

34
        _ = Task.Run(async delegate
35
        {
36
            while (true)
37
            {
38
                // Pretend to process a record approximately every second, just for changing sample data.
39
                recordsProcessed.Inc();
40

41
                await 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 above
48
        Console.WriteLine("Open http://localhost:1234/metrics in a web browser.");
49
        Console.WriteLine("Press enter to exit.");
50
        Console.ReadLine();
51

52
        server.Stop();
53
    }
54
}

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.