prometheus-net
90 строк · 4.2 Кб
1using System.Diagnostics.Metrics;2
3/// <summary>
4/// Sample custom metrics exported via the .NET Meters API.
5/// </summary>
6public static class CustomDotNetMeters7{
8public static void PublishSampleData()9{10// The meter object is the "container" for all the .NET metrics we will be publishing.11var meter1 = new Meter("Foobar.Wingwang.Dingdong", "vNext");12
13// Example metric: a simple counter.14var counter1 = meter1.CreateCounter<int>("wings-wanged", "wings", "Counts the number of wings that have been wanged.");15
16double nameCount = 1_000_000_000;17
18double HowManyNamesAreThereInTheWorld() => nameCount++;19
20var 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.23IEnumerable<Measurement<double>> ObserveGrossNestsAll()24{25foreach (var neepitKeepit in Enumerable.Range(1, 10))26yield 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
29var observableGauge1 = meter1.CreateObservableGauge<double>("gross-nests", ObserveGrossNestsAll, "nests (gross)", "Measures the amount of nests nested (gross).");30
31// Example metric: a histogram.32var histogram1 = meter1.CreateHistogram<byte>("bytes-considered", "bytes", "Informs about all the bytes considered.");33
34// .NET 7: Example metric: an up/down counter.35var 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.38int sandLevel = 0;39
40int MeasureSandLevel()41{42sandLevel += Random.Shared.Next(-1, 2);43return sandLevel;44}45
46var 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.49var highCardinalityCounter1 = meter1.CreateCounter<long>("bytes-sent", "bytes", "Bytes sent per connection.");50
51var activeConnections = new List<Guid>();52
53// Start with 10 active connections.54foreach (var _ in Enumerable.Range(0, 10))55activeConnections.Add(Guid.NewGuid());56
57// Dummy data generator.58_ = Task.Run(async delegate59{60while (true)61{62if (Random.Shared.Next(10) == 0)63counter1.Add(1, new KeyValuePair<string, object?>("wing-type", "FlexxWing MaxxFling 3000"));64
65if (Random.Shared.Next(10) == 0)66counter1.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.69histogram1.Record((byte)(Random.Shared.Next(256)), new KeyValuePair<string, object?>("is-faulted", true), new KeyValuePair<string, object?>("canbus_ver", "1.0"));70
71// .NET 772upDown1.Add(Random.Shared.Next(-1, 2));73
74// Add some bytes for every active connection.75foreach (var connection in activeConnections)76highCardinalityCounter1.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).80if (Random.Shared.Next(100) == 0)81{82activeConnections.RemoveAt(Random.Shared.Next(activeConnections.Count));83activeConnections.Add(Guid.NewGuid());84}85
86await Task.Delay(100);87}88});89}90}
91