From bace2ce5d1d57ebffd71ef8bdbd7282bc375b90b Mon Sep 17 00:00:00 2001 From: Cijo Thomas Date: Mon, 7 Mar 2022 13:35:36 -0800 Subject: [PATCH] Make console examples for metrics simpler (#2981) --- examples/Console/Program.cs | 9 ---- examples/Console/TestMetrics.cs | 96 ++++++++++++--------------------- 2 files changed, 33 insertions(+), 72 deletions(-) diff --git a/examples/Console/Program.cs b/examples/Console/Program.cs index 2c0468c7a..b2eebafce 100644 --- a/examples/Console/Program.cs +++ b/examples/Console/Program.cs @@ -106,15 +106,6 @@ namespace Examples.Console [Option("defaultCollection", Default = 1000, HelpText = "Default collection period in milliseconds.", Required = false)] public int DefaultCollectionPeriodMilliseconds { get; set; } - [Option("runtime", Default = 5000, HelpText = "Run time in milliseconds.", Required = false)] - public int RunTime { get; set; } - - [Option("tasks", Default = 1, HelpText = "Run # of concurrent tasks.", Required = false)] - public int NumTasks { get; set; } - - [Option("maxLoops", Default = 0, HelpText = "Maximum number of loops/iterations per task. (0 = No Limit)", Required = false)] - public int MaxLoops { get; set; } - [Option("useExporter", Default = "console", HelpText = "Options include otlp or console.", Required = false)] public string UseExporter { get; set; } diff --git a/examples/Console/TestMetrics.cs b/examples/Console/TestMetrics.cs index 15abffbde..9a6cd75a6 100644 --- a/examples/Console/TestMetrics.cs +++ b/examples/Console/TestMetrics.cs @@ -18,7 +18,6 @@ using System; using System.Collections.Generic; using System.Diagnostics; using System.Diagnostics.Metrics; -using System.Threading; using System.Threading.Tasks; using OpenTelemetry; using OpenTelemetry.Exporter; @@ -116,81 +115,52 @@ namespace Examples.Console }); } - var cts = new CancellationTokenSource(); - - var tasks = new List(); - - for (int i = 0; i < options.NumTasks; i++) + System.Console.WriteLine("Press any key to exit."); + while (!System.Console.KeyAvailable) { - var taskno = i; + histogram?.Record(10); - tasks.Add(Task.Run(() => - { - System.Console.WriteLine($"Task started {taskno + 1}/{options.NumTasks}."); + histogram?.Record( + 100, + new KeyValuePair("tag1", "value1")); - var loops = 0; + histogram?.Record( + 200, + new KeyValuePair("tag1", "value2"), + new KeyValuePair("tag2", "value2")); - while (!cts.IsCancellationRequested) - { - if (options.MaxLoops > 0 && loops >= options.MaxLoops) - { - break; - } + histogram?.Record( + 100, + new KeyValuePair("tag1", "value1")); - histogram?.Record(10); + histogram?.Record( + 200, + new KeyValuePair("tag2", "value2"), + new KeyValuePair("tag1", "value2")); - histogram?.Record( - 100, - new KeyValuePair("tag1", "value1")); + counter?.Add(10); - histogram?.Record( - 200, - new KeyValuePair("tag1", "value2"), - new KeyValuePair("tag2", "value2")); + counter?.Add( + 100, + new KeyValuePair("tag1", "value1")); - histogram?.Record( - 100, - new KeyValuePair("tag1", "value1")); + counter?.Add( + 200, + new KeyValuePair("tag1", "value2"), + new KeyValuePair("tag2", "value2")); - histogram?.Record( - 200, - new KeyValuePair("tag2", "value2"), - new KeyValuePair("tag1", "value2")); + counter?.Add( + 100, + new KeyValuePair("tag1", "value1")); - counter?.Add(10); + counter?.Add( + 200, + new KeyValuePair("tag2", "value2"), + new KeyValuePair("tag1", "value2")); - counter?.Add( - 100, - new KeyValuePair("tag1", "value1")); - - counter?.Add( - 200, - new KeyValuePair("tag1", "value2"), - new KeyValuePair("tag2", "value2")); - - counter?.Add( - 100, - new KeyValuePair("tag1", "value1")); - - counter?.Add( - 200, - new KeyValuePair("tag2", "value2"), - new KeyValuePair("tag1", "value2")); - - loops++; - } - })); + Task.Delay(500).Wait(); } - cts.CancelAfter(options.RunTime); - System.Console.WriteLine($"Wait for {options.RunTime} milliseconds."); - while (!cts.IsCancellationRequested) - { - Task.Delay(1000).Wait(); - } - - Task.WaitAll(tasks.ToArray()); - return null; } }