prometheus-net

Форк
0
90 строк · 4.2 Кб
1
using System.Diagnostics.Metrics;
2

3
/// <summary>
4
/// Sample custom metrics exported via the .NET Meters API.
5
/// </summary>
6
public static class CustomDotNetMeters
7
{
8
    public static void PublishSampleData()
9
    {
10
        // The meter object is the "container" for all the .NET metrics we will be publishing.
11
        var meter1 = new Meter("Foobar.Wingwang.Dingdong", "vNext");
12

13
        // Example metric: a simple counter.
14
        var counter1 = meter1.CreateCounter<int>("wings-wanged", "wings", "Counts the number of wings that have been wanged.");
15

16
        double nameCount = 1_000_000_000;
17

18
        double HowManyNamesAreThereInTheWorld() => nameCount++;
19

20
        var counter2 = meter1.CreateObservableCounter<double>("all-names", HowManyNamesAreThereInTheWorld, "names", "Count of how many unique names exist in the world up to this point");
21

22
        // Example metric: an observable gauge.
23
        IEnumerable<Measurement<double>> ObserveGrossNestsAll()
24
        {
25
            foreach (var neepitKeepit in Enumerable.Range(1, 10))
26
                yield return new Measurement<double>(Random.Shared.Next(800), new KeyValuePair<string, object?>("beek-beek", "yes"), new KeyValuePair<string, object?>("neepit-keepit", Random.Shared.Next(neepitKeepit)));
27
        }
28

29
        var observableGauge1 = meter1.CreateObservableGauge<double>("gross-nests", ObserveGrossNestsAll, "nests (gross)", "Measures the amount of nests nested (gross).");
30

31
        // Example metric: a histogram.
32
        var histogram1 = meter1.CreateHistogram<byte>("bytes-considered", "bytes", "Informs about all the bytes considered.");
33

34
        // .NET 7: Example metric: an up/down counter.
35
        var upDown1 = meter1.CreateUpDownCounter<int>("water-level", "brick-heights", "Current water level in the tank (measured in visible bricks from the midpoint).");
36

37
        // Example metric: an observable up/down counter.
38
        int sandLevel = 0;
39

40
        int MeasureSandLevel()
41
        {
42
            sandLevel += Random.Shared.Next(-1, 2);
43
            return sandLevel;
44
        }
45

46
        var upDown2 = meter1.CreateObservableUpDownCounter<int>("sand-level", MeasureSandLevel, "chainlinks", "Current sand level in the tank (measured in visible chain links from the midpoint).");
47

48
        // Example high cardinality metric: bytes sent per connection.
49
        var highCardinalityCounter1 = meter1.CreateCounter<long>("bytes-sent", "bytes", "Bytes sent per connection.");
50

51
        var activeConnections = new List<Guid>();
52

53
        // Start with 10 active connections.
54
        foreach (var _ in Enumerable.Range(0, 10))
55
            activeConnections.Add(Guid.NewGuid());
56

57
        // Dummy data generator.
58
        _ = Task.Run(async delegate
59
        {
60
            while (true)
61
            {
62
                if (Random.Shared.Next(10) == 0)
63
                    counter1.Add(1, new KeyValuePair<string, object?>("wing-type", "FlexxWing MaxxFling 3000"));
64

65
                if (Random.Shared.Next(10) == 0)
66
                    counter1.Add(1, new KeyValuePair<string, object?>("wing-type", "SlaxxWing 1.0"), new KeyValuePair<string, object?>("wing-version", "beta"));
67

68
                // is-faulted here conflicts with the static label of the same name and gets overwritten by the static label.
69
                histogram1.Record((byte)(Random.Shared.Next(256)), new KeyValuePair<string, object?>("is-faulted", true), new KeyValuePair<string, object?>("canbus_ver", "1.0"));
70

71
                // .NET 7
72
                upDown1.Add(Random.Shared.Next(-1, 2));
73

74
                // Add some bytes for every active connection.
75
                foreach (var connection in activeConnections)
76
                    highCardinalityCounter1.Add(Random.Shared.Next(10_000_000), new KeyValuePair<string, object?>("connection-id", connection));
77

78
                // Maybe some connection went away, maybe some was added.
79
                // Timeseries that stop receiving updates will disappear from prometheus-net output after a short delay (up to 10 minutes by default).
80
                if (Random.Shared.Next(100) == 0)
81
                {
82
                    activeConnections.RemoveAt(Random.Shared.Next(activeConnections.Count));
83
                    activeConnections.Add(Guid.NewGuid());
84
                }
85

86
                await Task.Delay(100);
87
            }
88
        });
89
    }
90
}
91

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

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

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

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