Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor for new [Gauge|Sum|Summary|Histogram]Metric types and aggregators #2073

Merged
merged 9 commits into from
Jun 15, 2021
36 changes: 16 additions & 20 deletions examples/Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,12 @@ public class Program
/// <param name="args">Arguments from command line.</param>
public static void Main(string[] args)
{
bool prompt = true;

Parser.Default.ParseArguments<JaegerOptions, ZipkinOptions, PrometheusOptions, MetricsOptions, GrpcNetClientOptions, HttpClientOptions, RedisOptions, ZPagesOptions, ConsoleOptions, OpenTelemetryShimOptions, OpenTracingShimOptions, OtlpOptions, InMemoryOptions>(args)
.MapResult(
(JaegerOptions options) => TestJaegerExporter.Run(options.Host, options.Port),
(ZipkinOptions options) => TestZipkinExporter.Run(options.Uri),
(PrometheusOptions options) => TestPrometheusExporter.Run(options.Port, options.PushIntervalInSecs, options.DurationInMins),
(MetricsOptions options) => TestMetrics.Run(options, ref prompt),
(MetricsOptions options) => TestMetrics.Run(options),
(GrpcNetClientOptions options) => TestGrpcNetClient.Run(),
(HttpClientOptions options) => TestHttpClient.Run(),
(RedisOptions options) => TestRedis.Run(options.Uri),
Expand All @@ -60,11 +58,6 @@ public static void Main(string[] args)
(OtlpOptions options) => TestOtlpExporter.Run(options.Endpoint),
(InMemoryOptions options) => TestInMemoryExporter.Run(options),
errs => 1);

if (prompt)
{
System.Console.ReadLine();
}
}
}

Expand Down Expand Up @@ -103,25 +96,28 @@ internal class PrometheusOptions
[Verb("metrics", HelpText = "Specify the options required to test Metrics")]
internal class MetricsOptions
{
[Option('p', "prompt", HelpText = "Prompt for exit", Default = false)]
public bool? Prompt { get; set; }
[Option('g', "Gauge", HelpText = "Include Observable Gauge.", Required = false)]
public bool? FlagGauge { get; set; }

[Option("runtime", Default = 5000, HelpText = "Run time in milliseconds.", Required = false)]
public int RunTime { get; set; }
[Option('u', "UpDownCounter", HelpText = "Include Observable Up/Down Counter.", Required = false)]
public bool? FlagUpDownCounter { get; set; }

[Option("observationPeriodMilliseconds", Default = 100, HelpText = "Observation period.", Required = false)]
public int ObservationPeriodMilliseconds { get; set; }
[Option('c', "Counter", HelpText = "Include Counter.", Required = false)]
public bool? FlagCounter { get; set; }

[Option("collectionPeriodMilliseconds", Default = 500, HelpText = "Collection period.", Required = false)]
public int CollectionPeriodMilliseconds { get; set; }
[Option('h', "Histogram", HelpText = "Include Histogram.", Required = false)]
public bool? FlagHistogram { get; set; }

[Option('o', "runObservable", Default = true, HelpText = "Run observable counters.", Required = false)]
public bool? RunObservable { get; set; }
[Option("defaultCollection", Default = 500, 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('t', "numTasks", Default = 1, HelpText = "Run # of tasks.", Required = false)]
[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. 0 = No Limit", Required = false)]
[Option("maxLoops", Default = 0, HelpText = "Maximum number of loops/iterations per task. (0 = No Limit)", Required = false)]
public int MaxLoops { get; set; }
}

Expand Down
1 change: 1 addition & 0 deletions examples/Console/TestGrpcNetClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ internal static object Run()
}

System.Console.WriteLine("Press Enter key to exit.");
System.Console.ReadLine();

return null;
}
Expand Down
1 change: 1 addition & 0 deletions examples/Console/TestHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ internal static object Run()
}

System.Console.WriteLine("Press Enter key to exit.");
System.Console.ReadLine();

return null;
}
Expand Down
80 changes: 61 additions & 19 deletions examples/Console/TestMetrics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,46 @@ namespace Examples.Console
{
internal class TestMetrics
{
internal static object Run(MetricsOptions options, ref bool prompt)
internal static object Run(MetricsOptions options)
{
prompt = options.Prompt.Value;

using var provider = Sdk.CreateMeterProviderBuilder()
.AddSource("TestMeter") // All instruments from this meter are enabled.
.SetObservationPeriod(options.ObservationPeriodMilliseconds)
.SetCollectionPeriod(options.CollectionPeriodMilliseconds)
.AddProcessor(new TagEnrichmentProcessor("newAttrib", "newAttribValue"))
.AddExportProcessor(new MetricConsoleExporter())
.SetDefaultCollectionPeriod(options.DefaultCollectionPeriodMilliseconds)
.AddProcessor(new TagEnrichmentProcessor("resource", "here"))
.AddExportProcessor(new MetricConsoleExporter("A"))
.AddExportProcessor(new MetricConsoleExporter("B"), 5 * options.DefaultCollectionPeriodMilliseconds)
.Build();

using var meter = new Meter("TestMeter", "0.0.1");

var counter = meter.CreateCounter<int>("counter1");
Counter<int> counter = null;
if (options.FlagCounter ?? true)
{
counter = meter.CreateCounter<int>("counter");
}

Histogram<int> histogram = null;
if (options.FlagHistogram ?? true)
{
histogram = meter.CreateHistogram<int>("histogram");
}

if (options.FlagGauge ?? true)
{
var observableCounter = meter.CreateObservableGauge<int>("gauge", () =>
{
return new List<Measurement<int>>()
{
new Measurement<int>(
(int)Process.GetCurrentProcess().PrivateMemorySize64,
new KeyValuePair<string, object>("tag1", "value1")),
};
});
}

if (options.RunObservable ?? true)
if (options.FlagUpDownCounter ?? true)
{
var observableCounter = meter.CreateObservableGauge<int>("CurrentMemoryUsage", () =>
var observableCounter = meter.CreateObservableCounter<int>("updown", () =>
{
return new List<Measurement<int>>()
{
Expand Down Expand Up @@ -76,22 +97,42 @@ internal static object Run(MetricsOptions options, ref bool prompt)
break;
}

counter.Add(10);
histogram?.Record(10);

histogram?.Record(
100,
new KeyValuePair<string, object>("tag1", "value1"));

histogram?.Record(
200,
new KeyValuePair<string, object>("tag1", "value2"),
new KeyValuePair<string, object>("tag2", "value2"));

histogram?.Record(
100,
new KeyValuePair<string, object>("tag1", "value1"));

counter.Add(
histogram?.Record(
200,
new KeyValuePair<string, object>("tag2", "value2"),
new KeyValuePair<string, object>("tag1", "value2"));

counter?.Add(10);

counter?.Add(
100,
new KeyValuePair<string, object>("tag1", "value1"));

counter.Add(
counter?.Add(
200,
new KeyValuePair<string, object>("tag1", "value2"),
new KeyValuePair<string, object>("tag2", "value2"));

counter.Add(
counter?.Add(
100,
new KeyValuePair<string, object>("tag1", "value1"));

counter.Add(
counter?.Add(
200,
new KeyValuePair<string, object>("tag2", "value2"),
new KeyValuePair<string, object>("tag1", "value2"));
Expand All @@ -102,13 +143,14 @@ internal static object Run(MetricsOptions options, ref bool prompt)
}

cts.CancelAfter(options.RunTime);
Task.WaitAll(tasks.ToArray());

if (prompt)
System.Console.WriteLine($"Wait for {options.RunTime} milliseconds.");
while (!cts.IsCancellationRequested)
{
System.Console.WriteLine("Press Enter key to exit.");
Task.Delay(1000).Wait();
}

Task.WaitAll(tasks.ToArray());

return null;
}
}
Expand Down
1 change: 1 addition & 0 deletions examples/Console/TestOTelShimWithConsoleExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ internal static object Run(OpenTelemetryShimOptions options)
}

System.Console.WriteLine("Press Enter key to exit.");
System.Console.ReadLine();

return null;
}
Expand Down
1 change: 1 addition & 0 deletions examples/Console/TestOpenTracingShim.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ internal static object Run(OpenTracingShimOptions options)
}

System.Console.WriteLine("Press Enter key to exit.");
System.Console.ReadLine();

return null;
}
Expand Down
2 changes: 2 additions & 0 deletions examples/Console/TestPrometheusExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ internal static object Run(int port, int pushIntervalInSecs, int totalDurationIn
- targets: ['localhost:9184']
*/
System.Console.WriteLine("Press Enter key to exit.");
System.Console.ReadLine();

return null;
}
}
Expand Down
3 changes: 3 additions & 0 deletions examples/Console/TestRedis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ internal static object Run(string zipkinUri)
}
}

System.Console.Write("Press ENTER to stop.");
System.Console.ReadLine();

return null;
}

Expand Down
85 changes: 0 additions & 85 deletions src/OpenTelemetry/Metrics/Aggregator/LastValueAggregator.cs

This file was deleted.

Loading