public async Task LabelledGauge_replacevalue_replaces_obsoleted_values_and_new_values_are_exposed_correctly() { var metrics = new PrometheusMetrics(); var gauge = metrics.Gauge() .Name("test_gauge_total") .Help("This is the help") .LabelNames("method") .Register(); gauge.Labels("GET").Value = 974; gauge.Labels("POST").Value = 823; gauge.ReplaceMetricValues(new Dictionary <string[], double>() { { new[] { "POST" }, 151 }, { new[] { "HEAD" }, 673 }, }); var memstream = new MemoryStream(); await metrics.Expose(memstream); var lines = UTF8.GetString(memstream.ToArray()).Split('\n'); Assert.Equal(1, lines.Count(s => s == "# HELP test_gauge_total This is the help")); Assert.Equal(1, lines.Count(s => s == "# TYPE test_gauge_total gauge")); Assert.Equal(1, lines.Count(s => Regex.IsMatch(s, "test_gauge_total{method=\"POST\"} 151 [0-9]+"))); Assert.Equal(1, lines.Count(s => Regex.IsMatch(s, "test_gauge_total{method=\"HEAD\"} 673 [0-9]+"))); Assert.Equal(0, lines.Count(s => Regex.IsMatch(s, "test_gauge_total{method=\"GET\"} 974 [0-9]+"))); }
public async Task Gauge_metrics_are_exposed_correctly() { var metrics = new PrometheusMetrics(); var gauge = metrics.Gauge() .Name("test_gauge_total") .Help("This is the help") .Register(); gauge.Value = 9374; var memstream = new MemoryStream(); await metrics.Expose(memstream); var lines = UTF8.GetString(memstream.ToArray()).Split('\n'); Assert.Equal(1, lines.Count(s => s == "# HELP test_gauge_total This is the help")); Assert.Equal(1, lines.Count(s => s == "# TYPE test_gauge_total gauge")); Assert.Equal(1, lines.Count(s => Regex.IsMatch(s, "test_gauge_total 9374 [0-9]+"))); }
public async Task Gauge_metrics_are_exposed_correctly_in_cultures_with_non_point_decimal_separator() { var metrics = new PrometheusMetrics(); var gauge = metrics.Gauge() .Name("test_gauge_total") .Help("Duis aute irure dolor in reprehenderit in voluptate velit esse") .Register(); gauge.Value = 3.14159; var memstream = new MemoryStream(); await metrics.Expose(memstream); var lines = UTF8.GetString(memstream.ToArray()).Split('\n'); Assert.Equal(1, lines.Count(s => s == "# HELP test_gauge_total Duis aute irure dolor in reprehenderit in voluptate velit esse")); Assert.Equal(1, lines.Count(s => s == "# TYPE test_gauge_total gauge")); Assert.Equal(1, lines.Count(s => Regex.IsMatch(s, "test_gauge_total 3.14159 [0-9]+"))); }
public async Task Gauge_metrics_are_exposed_correctly_when_requested_without_timestamp() { var metrics = new PrometheusMetrics(); var gauge = metrics.Gauge() .Name("test_gauge_total") .Help("This is the help") .Register(); gauge.Value = 23525.5; var memstream = new MemoryStream(); await metrics.Expose(memstream, ExposeOptions.NoTimestamp); var lines = UTF8.GetString(memstream.ToArray()).Split('\n'); Assert.Equal(1, lines.Count(s => s == "# HELP test_gauge_total This is the help")); Assert.Equal(1, lines.Count(s => s == "# TYPE test_gauge_total gauge")); Assert.Equal(1, lines.Count(s => Regex.IsMatch(s, "test_gauge_total 23525.5"))); }
public async Task LabelledGauge_metrics_are_exposed_correctly_in_cultures_with_non_point_decimal_separator() { var metrics = new PrometheusMetrics(); var gauge = metrics.Gauge() .Name("test_labelled_gauge_total") .LabelNames("lorem") .Help("Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium") .Register(); gauge.Labels("ipsum").Value = 2.71828; var memstream = new MemoryStream(); await metrics.Expose(memstream); var lines = UTF8.GetString(memstream.ToArray()).Split('\n'); Assert.Equal(1, lines.Count(s => s == "# HELP test_labelled_gauge_total Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium")); Assert.Equal(1, lines.Count(s => s == "# TYPE test_labelled_gauge_total gauge")); Assert.Equal(1, lines.Count(s => Regex.IsMatch(s, "test_labelled_gauge_total{lorem=\"ipsum\"} 2.71828 [0-9]+"))); }
public static void Main(string[] args) { IMetrics metrics = new PrometheusMetrics(); var planningTime = metrics.Gauge() .Name("planning_time").Help("The processing time in seconds").LabelNames("solver").Register(); var planningCount = metrics.Counter() .Name("planning_count").Help("The count of planning").LabelNames("solver").Register(); var totalPlanningCount = metrics.Counter() .Name("plannning_count_total").Help("Total count of plannings").Register(); var totalRunning = metrics.Gauge() .Name("plannning_running_total").Help("Currently running plannings").Register(); var planningStartTime = metrics.Gauge() .Name("planning_start_time").Help("Start time of last planning").Register(); var planningTimeHistogram = metrics.Histogram() .LinearBuckets(0, 300, 10) .Name("planning_duration_times") .Help("Duration of plannings in ms") .Register(); var planningTimeHistogramForSolver = metrics.Histogram() .LinearBuckets(0, 300, 10) .Name("planning_duration_times_for_solver") .Help("Duration of plannings in ms for different solvers") .LabelNames("solver") .Register(); var pgw = new PushGateway( new Uri("http://127.0.0.1:9091"), "example_app", new List <Tuple <string, string> > { Tuple.Create("environment", "test") } ); Task.Run(async() => { var pm = metrics as PrometheusMetrics; var output = Console.OpenStandardOutput(); for (;;) { Console.WriteLine("# Waiting"); await Task.Delay(TimeSpan.FromSeconds(10)); Console.WriteLine("# Exposing"); await pm.Expose(output); Console.WriteLine("# Done"); try { await pgw.PushAsync(pm, "example_app:9000"); } catch (Exception ex) { Console.WriteLine(ex.Message); } } }); foreach (var solver in new[] { "Static", "Dynamic" }) { Task.Run(() => { var rnd = new Random(solver.GetHashCode()); for (;;) { using (planningTime.Labels(solver).Timer()) { totalPlanningCount.Increment(); planningCount.Labels(solver).Increment(); planningStartTime.SetToCurrentTime(); totalRunning.TrackInProgress(() => { var planningSleep = rnd.Next(5000); Thread.Sleep(planningSleep); planningTimeHistogram.Observe(planningSleep); planningTimeHistogramForSolver.Labels(solver).Observe(planningSleep); }); } } }); } Console.ReadLine(); }