Remove span based examples from console examples. (#792)

This commit is contained in:
Cijo Thomas 2020-07-09 13:33:47 -07:00 committed by GitHub
parent 1c5872ee26
commit db781d2dfd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 33 additions and 186 deletions

View File

@ -40,14 +40,14 @@ namespace Samples
{
Parser.Default.ParseArguments<JaegerOptions, ZipkinOptions, PrometheusOptions, HttpClientOptions, ZPagesOptions, ConsoleOptions, OtlpOptions>(args)
.MapResult(
(JaegerOptions options) => TestJaeger.Run(options.Host, options.Port, options.UseActivitySource),
(ZipkinOptions options) => TestZipkin.Run(options.Uri, options.UseActivitySource),
(JaegerOptions options) => TestJaeger.Run(options.Host, options.Port),
(ZipkinOptions options) => TestZipkin.Run(options.Uri),
(PrometheusOptions options) => TestPrometheus.RunAsync(options.Port, options.PushIntervalInSecs, options.DurationInMins),
(HttpClientOptions options) => TestHttpClient.Run(),
(RedisOptions options) => TestRedis.Run(options.Uri),
(ZPagesOptions options) => TestZPages.Run(),
(ConsoleOptions options) => TestConsoleExporter.Run(options),
(OtlpOptions options) => TestOtlp.Run(options.Endpoint, options.UseActivitySource),
(OtlpOptions options) => TestOtlp.Run(options.Endpoint),
errs => 1);
Console.ReadLine();
@ -64,9 +64,6 @@ namespace Samples
[Option('p', "port", HelpText = "Port of the Jaeger Agent", Default = 6831)]
public int Port { get; set; }
[Option('a', "activity", HelpText = "Set it to true to export ActivitySource data", Default = false)]
public bool UseActivitySource { get; set; }
}
[Verb("zipkin", HelpText = "Specify the options required to test Zipkin exporter")]
@ -74,9 +71,6 @@ namespace Samples
{
[Option('u', "uri", HelpText = "Please specify the uri of Zipkin backend", Required = true)]
public string Uri { get; set; }
[Option('a', "activity", HelpText = "Set it to true to export ActivitySource data", Default = false)]
public bool UseActivitySource { get; set; }
}
[Verb("prometheus", HelpText = "Specify the options required to test Prometheus")]
@ -121,9 +115,6 @@ namespace Samples
{
[Option('e', "endpoint", HelpText = "Target to which the exporter is going to send traces or metrics", Default = "localhost:55680")]
public string Endpoint { get; set; }
[Option('a', "activity", HelpText = "Set it to true to export ActivitySource data", Default = false)]
public bool UseActivitySource { get; set; }
}
#pragma warning restore SA1402 // File may only contain a single type

View File

@ -23,14 +23,9 @@ namespace Samples
{
internal class TestJaeger
{
internal static object Run(string host, int port, bool useActivitySource)
internal static object Run(string host, int port)
{
if (useActivitySource)
{
return RunWithActivity(host, port);
}
return RunWithSdk(host, port);
return RunWithActivity(host, port);
}
internal static object RunWithActivity(string host, int port)
@ -55,67 +50,13 @@ namespace Samples
{
sample.Start();
Console.WriteLine("Sample is running on the background, press ENTER to stop");
Console.WriteLine("Traces are being created and exported" +
"to Jaeger in the background. Use Jaeger to view them." +
"Press ENTER to stop.");
Console.ReadLine();
}
return null;
}
internal static object RunWithSdk(string host, int port)
{
// Create a tracer.
using var tracerFactory = TracerFactory.Create(
builder => builder.UseJaeger(o =>
{
o.ServiceName = "jaeger-test";
o.AgentHost = host;
o.AgentPort = port;
}));
var tracer = tracerFactory.GetTracer("jaeger-test");
// Create a scoped span. It will end automatically when using statement ends
using (tracer.StartActiveSpan("Main", out var span))
{
span.SetAttribute("custom-attribute", 55);
Console.WriteLine("About to do a busy work");
for (int i = 0; i < 10; i++)
{
DoWork(i, tracer);
}
Console.WriteLine("Completed doing busy work");
Console.WriteLine("Press Enter key to exit.");
}
return null;
}
private static void DoWork(int i, Tracer tracer)
{
// Start another span. If another span was already started, it'll use that span as the parent span.
// In this example, the main method already started a span, so that'll be the parent span, and this will be
// a child span.
using (tracer.StartActiveSpan("DoWork", out var span))
{
// Simulate some work.
try
{
Console.WriteLine("Doing busy work");
Thread.Sleep(1000);
}
catch (ArgumentOutOfRangeException e)
{
// Set status upon error
span.Status = Status.Internal.WithDescription(e.ToString());
}
// Annotate our span to capture metadata about our operation
var attributes = new Dictionary<string, object>();
attributes.Add("use", "demo");
attributes.Add("iteration", i);
span.AddEvent(new Event("Invoking DoWork", attributes));
}
}
}
}

View File

@ -25,47 +25,9 @@ namespace Samples
{
internal static class TestOtlp
{
internal static object Run(string endpoint, bool useActivitySource)
internal static object Run(string endpoint)
{
if (useActivitySource)
{
return RunWithActivitySource(endpoint);
}
return RunWithSdk(endpoint);
}
private static object RunWithSdk(string endpoint)
{
var headers = new Metadata
{
{ "test", "test-header" },
};
using var tracerFactory = TracerFactory.Create(builder => builder
.SetResource(Resources.CreateServiceResource("otlp-test"))
.UseOpenTelemetryProtocolExporter(config =>
{
config.Endpoint = endpoint;
config.Headers = headers;
}));
var tracer = tracerFactory.GetTracer("otlp.test.tracer");
using (tracer.StartActiveSpan("parent", out var parent))
{
tracer.CurrentSpan.SetAttribute("key", 123);
tracer.CurrentSpan.AddEvent("test-event");
using (tracer.StartActiveSpan("child", out var child))
{
child.SetAttribute("key", "value");
}
}
Console.WriteLine("Done... wait for events to arrive to backend!");
Console.ReadLine();
return null;
return RunWithActivitySource(endpoint);
}
private static object RunWithActivitySource(string endpoint)
@ -84,7 +46,9 @@ namespace Samples
{
sample.Start();
Console.WriteLine("Sample is running on the background, press ENTER to stop");
Console.WriteLine("Traces are being created and exported" +
"to OTLP in the background." +
"Press ENTER to stop.");
Console.ReadLine();
}

View File

@ -24,80 +24,31 @@ namespace Samples
{
internal class TestZipkin
{
internal static object Run(string zipkinUri, bool useActivitySource)
internal static object Run(string zipkinUri)
{
if (useActivitySource)
{
// Enable OpenTelemetry for the sources "Samples.SampleServer" and "Samples.SampleClient"
// and use the Zipkin exporter.
using var openTelemetry = OpenTelemetrySdk.EnableOpenTelemetry(
builder => builder
.AddActivitySource("Samples.SampleServer")
.AddActivitySource("Samples.SampleClient")
.UseZipkinActivityExporter(o =>
{
o.ServiceName = "test-zipkin";
o.Endpoint = new Uri(zipkinUri);
}));
using (var sample = new InstrumentationWithActivitySource())
{
sample.Start();
Console.WriteLine("Sample is running on the background, press ENTER to stop");
Console.ReadLine();
}
}
else
{
// Configure exporter to export traces to Zipkin
using (var tracerFactory = TracerFactory.Create(builder => builder
.UseZipkin(o =>
{
o.ServiceName = "test-zipkin";
o.Endpoint = new Uri(zipkinUri);
})))
{
var tracer = tracerFactory.GetTracer("zipkin-test");
// Create a scoped span. It will end automatically when using statement ends
using (tracer.WithSpan(tracer.StartSpan("Main")))
// Enable OpenTelemetry for the sources "Samples.SampleServer" and "Samples.SampleClient"
// and use the Zipkin exporter.
using var openTelemetry = OpenTelemetrySdk.EnableOpenTelemetry(
builder => builder
.AddActivitySource("Samples.SampleServer")
.AddActivitySource("Samples.SampleClient")
.UseZipkinActivityExporter(o =>
{
Console.WriteLine("About to do a busy work");
for (var i = 0; i < 10; i++)
{
DoWork(i, tracer);
}
}
}
o.ServiceName = "test-zipkin";
o.Endpoint = new Uri(zipkinUri);
}));
using (var sample = new InstrumentationWithActivitySource())
{
sample.Start();
Console.WriteLine("Traces are being created and exported" +
"to Zipkin in the background. Use Zipkin to view them." +
"Press ENTER to stop.");
Console.ReadLine();
}
return null;
}
private static void DoWork(int i, Tracer tracer)
{
// Start another span. If another span was already started, it'll use that span as the parent span.
// In this example, the main method already started a span, so that'll be the parent span, and this will be
// a child span.
using (tracer.StartActiveSpan("DoWork", out var span))
{
// Simulate some work.
try
{
Console.WriteLine("Doing busy work");
Thread.Sleep(1000);
}
catch (ArgumentOutOfRangeException e)
{
// Set status upon error
span.Status = Status.Internal.WithDescription(e.ToString());
}
// Annotate our span to capture metadata about our operation
var attributes = new Dictionary<string, object> { { "use", "demo" } };
span.AddEvent(new Event("Invoking DoWork", attributes));
}
}
}
}