[Examples.Console] enable analysis (#6221)
Co-authored-by: Rajkumar Rangaraj <rajrang@microsoft.com>
This commit is contained in:
parent
72f7348b57
commit
f183a6a11e
|
|
@ -1,9 +1,10 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>$(DefaultTargetFrameworkForExampleApps)</TargetFramework>
|
||||
<NoWarn>$(NoWarn),CS0618</NoWarn>
|
||||
<NoWarn>$(NoWarn),CA1812</NoWarn>
|
||||
<AnalysisLevel>latest-all</AnalysisLevel>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ using System.Text;
|
|||
|
||||
namespace Examples.Console;
|
||||
|
||||
internal class InstrumentationWithActivitySource : IDisposable
|
||||
internal sealed class InstrumentationWithActivitySource : IDisposable
|
||||
{
|
||||
private const string RequestPath = "/api/request";
|
||||
private readonly SampleServer server = new();
|
||||
|
|
@ -27,7 +27,7 @@ internal class InstrumentationWithActivitySource : IDisposable
|
|||
this.server.Dispose();
|
||||
}
|
||||
|
||||
private class SampleServer : IDisposable
|
||||
private sealed class SampleServer : IDisposable
|
||||
{
|
||||
private readonly HttpListener listener = new();
|
||||
|
||||
|
|
@ -66,7 +66,7 @@ internal class InstrumentationWithActivitySource : IDisposable
|
|||
}
|
||||
|
||||
activity?.SetTag("request.content", requestContent);
|
||||
activity?.SetTag("request.length", requestContent.Length.ToString());
|
||||
activity?.SetTag("request.length", requestContent.Length);
|
||||
|
||||
var echo = Encoding.UTF8.GetBytes("echo: " + requestContent);
|
||||
context.Response.ContentEncoding = Encoding.UTF8;
|
||||
|
|
@ -88,7 +88,7 @@ internal class InstrumentationWithActivitySource : IDisposable
|
|||
}
|
||||
}
|
||||
|
||||
private class SampleClient : IDisposable
|
||||
private sealed class SampleClient : IDisposable
|
||||
{
|
||||
private CancellationTokenSource? cts;
|
||||
private Task? requestTask;
|
||||
|
|
@ -114,7 +114,7 @@ internal class InstrumentationWithActivitySource : IDisposable
|
|||
count++;
|
||||
|
||||
activity?.AddEvent(new ActivityEvent("PostAsync:Started"));
|
||||
using var response = await client.PostAsync(url, content, cancellationToken).ConfigureAwait(false);
|
||||
using var response = await client.PostAsync(new Uri(url, UriKind.Absolute), content, cancellationToken).ConfigureAwait(false);
|
||||
activity?.AddEvent(new ActivityEvent("PostAsync:Ended"));
|
||||
|
||||
activity?.SetTag("http.status_code", (int)response.StatusCode);
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ namespace Examples.Console;
|
|||
/// <summary>
|
||||
/// Main samples entry point.
|
||||
/// </summary>
|
||||
public class Program
|
||||
internal static class Program
|
||||
{
|
||||
/// <summary>
|
||||
/// Main method - invoke this using command line.
|
||||
|
|
@ -51,21 +51,21 @@ public class Program
|
|||
#pragma warning disable SA1402 // File may only contain a single type
|
||||
|
||||
[Verb("zipkin", HelpText = "Specify the options required to test Zipkin exporter")]
|
||||
internal class ZipkinOptions
|
||||
internal sealed class ZipkinOptions
|
||||
{
|
||||
[Option('u', "uri", HelpText = "Please specify the uri of Zipkin backend", Required = true)]
|
||||
public required string Uri { get; set; }
|
||||
}
|
||||
|
||||
[Verb("prometheus", HelpText = "Specify the options required to test Prometheus")]
|
||||
internal class PrometheusOptions
|
||||
internal sealed class PrometheusOptions
|
||||
{
|
||||
[Option('p', "port", Default = 9464, HelpText = "The port to expose metrics. The endpoint will be http://localhost:port/metrics/ (this is the port from which your Prometheus server scraps metrics from.)", Required = false)]
|
||||
public int Port { get; set; }
|
||||
}
|
||||
|
||||
[Verb("metrics", HelpText = "Specify the options required to test Metrics")]
|
||||
internal class MetricsOptions
|
||||
internal sealed class MetricsOptions
|
||||
{
|
||||
[Option('d', "IsDelta", HelpText = "Export Delta metrics", Required = false, Default = false)]
|
||||
public bool IsDelta { get; set; }
|
||||
|
|
@ -93,32 +93,32 @@ internal class MetricsOptions
|
|||
}
|
||||
|
||||
[Verb("grpc", HelpText = "Specify the options required to test Grpc.Net.Client")]
|
||||
internal class GrpcNetClientOptions
|
||||
internal sealed class GrpcNetClientOptions
|
||||
{
|
||||
}
|
||||
|
||||
[Verb("httpclient", HelpText = "Specify the options required to test HttpClient")]
|
||||
internal class HttpClientOptions
|
||||
internal sealed class HttpClientOptions
|
||||
{
|
||||
}
|
||||
|
||||
[Verb("console", HelpText = "Specify the options required to test console exporter")]
|
||||
internal class ConsoleOptions
|
||||
internal sealed class ConsoleOptions
|
||||
{
|
||||
}
|
||||
|
||||
[Verb("otelshim", HelpText = "Specify the options required to test OpenTelemetry Shim with console exporter")]
|
||||
internal class OpenTelemetryShimOptions
|
||||
internal sealed class OpenTelemetryShimOptions
|
||||
{
|
||||
}
|
||||
|
||||
[Verb("opentracing", HelpText = "Specify the options required to test OpenTracing Shim with console exporter")]
|
||||
internal class OpenTracingShimOptions
|
||||
internal sealed class OpenTracingShimOptions
|
||||
{
|
||||
}
|
||||
|
||||
[Verb("otlp", HelpText = "Specify the options required to test OpenTelemetry Protocol (OTLP)")]
|
||||
internal class OtlpOptions
|
||||
internal sealed class OtlpOptions
|
||||
{
|
||||
[Option('e', "endpoint", HelpText = "Target to which the exporter is going to send traces (default value depends on protocol).", Default = null)]
|
||||
public string? Endpoint { get; set; }
|
||||
|
|
@ -128,7 +128,7 @@ internal class OtlpOptions
|
|||
}
|
||||
|
||||
[Verb("logs", HelpText = "Specify the options required to test Logs")]
|
||||
internal class LogsOptions
|
||||
internal sealed class LogsOptions
|
||||
{
|
||||
[Option("useExporter", Default = "otlp", HelpText = "Options include otlp or console.", Required = false)]
|
||||
public string? UseExporter { get; set; }
|
||||
|
|
@ -147,7 +147,7 @@ internal class LogsOptions
|
|||
}
|
||||
|
||||
[Verb("inmemory", HelpText = "Specify the options required to test InMemory Exporter")]
|
||||
internal class InMemoryOptions
|
||||
internal sealed class InMemoryOptions
|
||||
{
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ using OpenTelemetry.Trace;
|
|||
|
||||
namespace Examples.Console;
|
||||
|
||||
internal class TestConsoleExporter
|
||||
internal sealed class TestConsoleExporter
|
||||
{
|
||||
// To run this example, run the following command from
|
||||
// the reporoot\examples\Console\.
|
||||
|
|
@ -27,21 +27,21 @@ internal class TestConsoleExporter
|
|||
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
|
||||
.AddSource("Samples.SampleClient", "Samples.SampleServer")
|
||||
.ConfigureResource(res => res.AddService("console-test"))
|
||||
#pragma warning disable CA2000 // Dispose objects before losing scope
|
||||
.AddProcessor(new MyProcessor()) // This must be added before ConsoleExporter
|
||||
#pragma warning restore CA2000 // Dispose objects before losing scope
|
||||
.AddConsoleExporter()
|
||||
.Build();
|
||||
|
||||
// The above line is required only in applications
|
||||
// which decide to use OpenTelemetry.
|
||||
using (var sample = new InstrumentationWithActivitySource())
|
||||
{
|
||||
sample.Start();
|
||||
using var sample = new InstrumentationWithActivitySource();
|
||||
sample.Start();
|
||||
|
||||
System.Console.WriteLine("Traces are being created and exported " +
|
||||
"to Console in the background. " +
|
||||
"Press ENTER to stop.");
|
||||
System.Console.ReadLine();
|
||||
}
|
||||
System.Console.WriteLine("Traces are being created and exported " +
|
||||
"to Console in the background. " +
|
||||
"Press ENTER to stop.");
|
||||
System.Console.ReadLine();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -50,7 +50,7 @@ internal class TestConsoleExporter
|
|||
/// An example of custom processor which
|
||||
/// can be used to add more tags to an activity.
|
||||
/// </summary>
|
||||
internal class MyProcessor : BaseProcessor<Activity>
|
||||
internal sealed class MyProcessor : BaseProcessor<Activity>
|
||||
{
|
||||
public override void OnStart(Activity activity)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ using OpenTelemetry.Trace;
|
|||
|
||||
namespace Examples.Console;
|
||||
|
||||
internal class TestGrpcNetClient
|
||||
internal sealed class TestGrpcNetClient
|
||||
{
|
||||
internal static int Run(GrpcNetClientOptions options)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ using OpenTelemetry.Trace;
|
|||
|
||||
namespace Examples.Console;
|
||||
|
||||
internal class TestHttpClient
|
||||
internal sealed class TestHttpClient
|
||||
{
|
||||
// To run this example, run the following command from
|
||||
// the reporoot\examples\Console\.
|
||||
|
|
@ -32,7 +32,7 @@ internal class TestHttpClient
|
|||
using (var parent = source.StartActivity("incoming request", ActivityKind.Server))
|
||||
{
|
||||
using var client = new HttpClient();
|
||||
client.GetStringAsync("http://bing.com").GetAwaiter().GetResult();
|
||||
client.GetStringAsync(new Uri("http://bing.com", UriKind.Absolute)).GetAwaiter().GetResult();
|
||||
}
|
||||
|
||||
System.Console.WriteLine("Press Enter key to exit.");
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ using OpenTelemetry.Trace;
|
|||
|
||||
namespace Examples.Console;
|
||||
|
||||
internal class TestInMemoryExporter
|
||||
internal sealed class TestInMemoryExporter
|
||||
{
|
||||
// To run this example, run the following command from
|
||||
// the reporoot\examples\Console\.
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ using OpenTelemetry.Logs;
|
|||
|
||||
namespace Examples.Console;
|
||||
|
||||
internal class TestLogs
|
||||
internal sealed class TestLogs
|
||||
{
|
||||
internal static int Run(LogsOptions options)
|
||||
{
|
||||
|
|
@ -112,11 +112,11 @@ internal class TestLogs
|
|||
});
|
||||
});
|
||||
|
||||
var logger = loggerFactory.CreateLogger<Program>();
|
||||
using (logger.BeginScope("{city}", "Seattle"))
|
||||
using (logger.BeginScope("{storeType}", "Physical"))
|
||||
var logger = loggerFactory.CreateLogger<TestLogs>();
|
||||
using (logger.BeginCityScope("Seattle"))
|
||||
using (logger.BeginStoreTypeScope("Physical"))
|
||||
{
|
||||
logger.LogInformation("Hello from {name} {price}.", "tomato", 2.99);
|
||||
logger.HelloFrom("tomato", 2.99);
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
|
|
|||
|
|
@ -0,0 +1,19 @@
|
|||
// Copyright The OpenTelemetry Authors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Examples.Console;
|
||||
|
||||
internal static partial class TestLogsExtensions
|
||||
{
|
||||
private static readonly Func<ILogger, string, IDisposable?> CityScope = LoggerMessage.DefineScope<string>("{City}");
|
||||
private static readonly Func<ILogger, string, IDisposable?> StoreTypeScope = LoggerMessage.DefineScope<string>("{StoreType}");
|
||||
|
||||
public static IDisposable? BeginCityScope(this ILogger logger, string city) => CityScope(logger, city);
|
||||
|
||||
public static IDisposable? BeginStoreTypeScope(this ILogger logger, string storeType) => StoreTypeScope(logger, storeType);
|
||||
|
||||
[LoggerMessage(LogLevel.Information, "Hello from {Name} {Price}.")]
|
||||
public static partial void HelloFrom(this ILogger logger, string name, double price);
|
||||
}
|
||||
|
|
@ -10,7 +10,7 @@ using OpenTelemetry.Resources;
|
|||
|
||||
namespace Examples.Console;
|
||||
|
||||
internal class TestMetrics
|
||||
internal sealed class TestMetrics
|
||||
{
|
||||
internal static int Run(MetricsOptions options)
|
||||
{
|
||||
|
|
@ -97,7 +97,7 @@ internal class TestMetrics
|
|||
{
|
||||
return new List<Measurement<int>>()
|
||||
{
|
||||
new Measurement<int>(
|
||||
new(
|
||||
(int)Process.GetCurrentProcess().PrivateMemorySize64,
|
||||
new KeyValuePair<string, object?>("tag1", "value1")),
|
||||
};
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ using OpenTelemetry.Trace;
|
|||
|
||||
namespace Examples.Console;
|
||||
|
||||
internal class TestOTelShimWithConsoleExporter
|
||||
internal sealed class TestOTelShimWithConsoleExporter
|
||||
{
|
||||
internal static int Run(OpenTelemetryShimOptions options)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ using OpenTracing;
|
|||
|
||||
namespace Examples.Console;
|
||||
|
||||
internal class TestOpenTracingShim
|
||||
internal sealed class TestOpenTracingShim
|
||||
{
|
||||
internal static int Run(OpenTracingShimOptions options)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -69,24 +69,22 @@ internal static class TestOtlpExporter
|
|||
|
||||
// The above line is required only in Applications
|
||||
// which decide to use OpenTelemetry.
|
||||
using (var sample = new InstrumentationWithActivitySource())
|
||||
{
|
||||
sample.Start();
|
||||
using var sample = new InstrumentationWithActivitySource();
|
||||
sample.Start();
|
||||
|
||||
System.Console.WriteLine("Traces are being created and exported " +
|
||||
"to the OpenTelemetry Collector in the background. " +
|
||||
"Press ENTER to stop.");
|
||||
System.Console.ReadLine();
|
||||
}
|
||||
System.Console.WriteLine("Traces are being created and exported " +
|
||||
"to the OpenTelemetry Collector in the background. " +
|
||||
"Press ENTER to stop.");
|
||||
System.Console.ReadLine();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
private static OtlpExportProtocol? ToOtlpExportProtocol(string? protocol) =>
|
||||
protocol?.Trim().ToLower() switch
|
||||
protocol?.Trim().ToUpperInvariant() switch
|
||||
{
|
||||
"grpc" => OtlpExportProtocol.Grpc,
|
||||
"http/protobuf" => OtlpExportProtocol.HttpProtobuf,
|
||||
"GRPC" => OtlpExportProtocol.Grpc,
|
||||
"HTTP/PROTOBUF" => OtlpExportProtocol.HttpProtobuf,
|
||||
_ => null,
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,19 +3,18 @@
|
|||
|
||||
using System.Diagnostics;
|
||||
using System.Diagnostics.Metrics;
|
||||
using System.Security.Cryptography;
|
||||
using OpenTelemetry;
|
||||
using OpenTelemetry.Metrics;
|
||||
using OpenTelemetry.Trace;
|
||||
|
||||
namespace Examples.Console;
|
||||
|
||||
internal class TestPrometheusExporter
|
||||
internal sealed class TestPrometheusExporter
|
||||
{
|
||||
private static readonly Meter MyMeter = new("MyMeter");
|
||||
private static readonly Meter MyMeter2 = new("MyMeter2");
|
||||
private static readonly Counter<double> Counter = MyMeter.CreateCounter<double>("myCounter", description: "A counter for demonstration purpose.");
|
||||
private static readonly Histogram<long> MyHistogram = MyMeter.CreateHistogram<long>("myHistogram");
|
||||
private static readonly ThreadLocal<Random> ThreadLocalRandom = new(() => new Random());
|
||||
|
||||
internal static int Run(PrometheusOptions options)
|
||||
{
|
||||
|
|
@ -57,7 +56,7 @@ internal class TestPrometheusExporter
|
|||
{
|
||||
Counter.Add(9.9, new("name", "apple"), new("color", "red"));
|
||||
Counter.Add(99.9, new("name", "lemon"), new("color", "yellow"));
|
||||
MyHistogram.Record(ThreadLocalRandom.Value!.Next(1, 1500), new("tag1", "value1"), new("tag2", "value2"));
|
||||
MyHistogram.Record(RandomNumberGenerator.GetInt32(1, 1500), new("tag1", "value1"), new("tag2", "value2"));
|
||||
Task.Delay(10).Wait();
|
||||
}
|
||||
});
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ using OpenTelemetry.Trace;
|
|||
|
||||
namespace Examples.Console;
|
||||
|
||||
internal class TestZipkinExporter
|
||||
internal sealed class TestZipkinExporter
|
||||
{
|
||||
internal static int Run(ZipkinOptions options)
|
||||
{
|
||||
|
|
@ -33,15 +33,13 @@ internal class TestZipkinExporter
|
|||
})
|
||||
.Build();
|
||||
|
||||
using (var sample = new InstrumentationWithActivitySource())
|
||||
{
|
||||
sample.Start();
|
||||
using var sample = new InstrumentationWithActivitySource();
|
||||
sample.Start();
|
||||
|
||||
System.Console.WriteLine("Traces are being created and exported " +
|
||||
"to Zipkin in the background. Use Zipkin to view them. " +
|
||||
"Press ENTER to stop.");
|
||||
System.Console.ReadLine();
|
||||
}
|
||||
System.Console.WriteLine("Traces are being created and exported " +
|
||||
"to Zipkin in the background. Use Zipkin to view them. " +
|
||||
"Press ENTER to stop.");
|
||||
System.Console.ReadLine();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue