prometheus-net

Форк
0
172 строки · 6.9 Кб
1
using Prometheus;
2
using System;
3
using System.Diagnostics;
4
using System.Net.Http;
5
using System.Threading;
6
using System.Threading.Tasks;
7

8
#if NET6_0_OR_GREATER
9
using System.Diagnostics.Metrics;
10
#endif
11

12
namespace tester
13
{
14
    internal class Program
15
    {
16
        private static void Main()
17
        {
18
            // Uncomment this to suppress the default sample metrics.
19
            //Metrics.SuppressDefaultMetrics();
20

21
            // Replace the first line with an appropriate type of tester to run different manual tests.
22
            //var tester = new MetricPusherTester();
23
            //var tester = new KestrelMetricServerTester();
24
            var tester = new AspNetCoreMiddlewareTester();
25
            //var tester = new GrpcMiddlewareTester();
26
            //var tester = new MetricServerTester();
27

28
            // For testing Kestrel metric server with HTTPS, you need at least a self-signed certificate (one included here)
29
            // and the matching domain pointed to 127.0.0.1 (e.g. hardcoded in the PCs hosts file) and you also need to
30
            // import this certificate into your Trusted Root Certification Authorities certificate store to trust it.
31
            //var certificate = new X509Certificate2("prometheus-net.test.pfx", "prometheus-net.test");
32
            //var tester = new KestrelMetricServerTester("prometheus-net.test", certificate);
33

34
            tester.OnStart();
35

36
            var metricServer = tester.InitializeMetricServer();
37
            metricServer?.Start();
38

39
            var counter = Metrics.CreateCounter("myCounter", "help text", new[] { "method", "endpoint" });
40
            counter.WithLabels("GET", "/").Inc();
41
            counter.WithLabels("POST", "/cancel").Inc();
42

43
            Metrics.CreateCounter("always_zero", "This counter is always zero but still needs to be present in the output!");
44

45
            var gauge = Metrics.CreateGauge("gauge", "help text");
46
            gauge.Inc(3.4);
47
            gauge.Dec(2.1);
48
            gauge.Set(5.3);
49

50
            // As the initial value is suppressed and a new one never assigned, this one never shows up in the export.
51
            Metrics.CreateGauge("should_not_show_up", "", new GaugeConfiguration
52
            {
53
                SuppressInitialValue = true
54
            });
55

56
            var hist = Metrics.CreateHistogram("myHistogram", "help text", new HistogramConfiguration
57
            {
58
                Buckets = new[] { 0, 0.2, 0.4, 0.6, 0.8, 0.9 }
59
            });
60
            hist.Observe(0.4);
61

62
            var timedHistogram = Metrics.CreateHistogram("myTimedHistogram", "help text", new HistogramConfiguration
63
            {
64
                Buckets = new[] { 0, 0.2, 0.4, 0.6, 0.8, 0.9 }
65
            });
66

67
            var latestGauge = Metrics.CreateGauge("latestGauge", "Reports the latest cycle time");
68

69
            var summary = Metrics.CreateSummary("mySummary", "help text");
70
            summary.Observe(5.3);
71

72
            // Example implementation of updating values before every collection.
73
            var collectionCount = Metrics.CreateCounter("beforecollect_example", "This counter is incremented before every data collection.");
74

75
            // Synchronous callbacks should be instantaneous, to avoid causing delays in the pipeline.
76
            Metrics.DefaultRegistry.AddBeforeCollectCallback(() => collectionCount.Inc());
77

78
            var googlePageBytes = Metrics.CreateCounter("beforecollect_async_example", "This counter is incremented before every data collection, but asynchronously.");
79

80
            // Callbacks can also be asynchronous. It is fine for these to take a bit more time.
81
            // For example, you can make an asynchronous HTTP request to a remote system in such a callback.
82
            var httpClient = new HttpClient();
83

84
            Metrics.DefaultRegistry.AddBeforeCollectCallback(async (cancel) =>
85
            {
86
                // Probe a remote system.
87
                var response = await httpClient.GetAsync("https://google.com", cancel);
88

89
                // Increase a counter by however many bytes we loaded.
90
                googlePageBytes.Inc(response.Content.Headers.ContentLength ?? 0);
91
            });
92

93
            // Uncomment this to test deliberately causing collections to fail. This should result in 503 responses.
94
            // With MetricPusherTester you might get a 1st push already before it fails but after that it should stop pushing.
95
            //Metrics.DefaultRegistry.AddBeforeCollectCallback(() => throw new ScrapeFailedException());
96

97
#if NETCOREAPP
98
            var diagnosticSourceRegistration = DiagnosticSourceAdapter.StartListening();
99
            var eventCounterRegistration = EventCounterAdapter.StartListening();
100
#endif
101

102
#if NET6_0_OR_GREATER
103
            var meter = new Meter("sample.dotnet.meter", "1.2.3");
104
            var meterCounter = meter.CreateCounter<double>("sample_counter");
105
            var meterGauge = meter.CreateObservableGauge<byte>("sample_gauge", () => 92, "Buckets", "How much cheese is loaded");
106

107
            var meterRegistration = MeterAdapter.StartListening();
108
#endif
109

110
            var cts = new CancellationTokenSource();
111

112
            var random = new Random();
113

114
            // Update metrics on a regular interval until told to stop.
115
            var updateInterval = TimeSpan.FromSeconds(0.5);
116
            var updateTask = Task.Factory.StartNew(async delegate
117
            {
118
                while (!cts.IsCancellationRequested)
119
                {
120
                    using (latestGauge.NewTimer())
121
                    using (timedHistogram.NewTimer())
122
                    {
123
                        var duration = Stopwatch.StartNew();
124

125

126
                        counter.Inc();
127
                        counter.Labels("GET", "/").Inc(2);
128
                        gauge.Set(random.NextDouble() + 2);
129
                        hist.Observe(random.NextDouble());
130
                        summary.Observe(random.NextDouble());
131

132
#if NET6_0_OR_GREATER
133
                        meterCounter.Add(1);
134
#endif
135
                        try
136
                        {
137
                            tester.OnTimeToObserveMetrics();
138
                        }
139
                        catch (Exception ex)
140
                        {
141
                            Console.Error.WriteLine(ex);
142
                        }
143

144
                        var sleepTime = updateInterval - duration.Elapsed;
145

146
                        if (sleepTime > TimeSpan.Zero)
147
                            await Task.Delay(sleepTime, cts.Token);
148
                    }
149
                }
150
            }).Result;
151

152
            Console.WriteLine("Press enter to stop metricServer");
153
            Console.ReadLine();
154

155
            cts.Cancel();
156
            try
157
            {
158
                updateTask.GetAwaiter().GetResult();
159
            }
160
            catch (OperationCanceledException)
161
            {
162
            }
163

164
            metricServer?.StopAsync().GetAwaiter().GetResult();
165

166
            tester.OnEnd();
167

168
            Console.WriteLine("Press enter to stop tester");
169
            Console.ReadLine();
170
        }
171
    }
172
}
173

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

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

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

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