Skip to content
This repository was archived by the owner on Dec 18, 2023. It is now read-only.

Commit a02af50

Browse files
Simon ZeltserSergeyKanzhelev
authored andcommitted
Updating the sample for zipkin exporter to follow quickstart at opencensus.io (#87)
* Minor fix - updating zipkin sample so we can execute it from command line * Fix zipkin sample * Updated zipkin sample to follow the quickstart of opencensus.io for other languages * Revert the todo
1 parent 68bd833 commit a02af50

File tree

2 files changed

+65
-19
lines changed

2 files changed

+65
-19
lines changed

src/Samples/Program.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ class StackdriverOptions
1313
[Verb("zipkin", HelpText = "Specify the options required to test Zipkin exporter")]
1414
class ZipkinOptions
1515
{
16+
[Option('u', "uri", HelpText = "Please specify the uri of Zipkin backend", Required = true)]
17+
public string Uri { get; set; }
1618
}
1719

1820
[Verb("appInsights", HelpText = "Specify the options required to test ApplicationInsights")]
@@ -39,7 +41,7 @@ public class Program
3941
/// Main method - invoke this using command line.
4042
/// For example:
4143
///
42-
/// Samples.dll zipkin
44+
/// Samples.dll zipkin http://localhost:9411/api/v2/spans
4345
/// Sample.dll appInsights
4446
/// Sample.dll prometheus
4547
/// </summary>
@@ -48,17 +50,14 @@ public static void Main(string[] args)
4850
{
4951
Parser.Default.ParseArguments<ZipkinOptions, ApplicationInsightsOptions, PrometheusOptions, HttpClientOptions, StackdriverOptions>(args)
5052
.MapResult(
51-
(ZipkinOptions options) => TestZipkin.Run(),
53+
(ZipkinOptions options) => TestZipkin.Run(options.Uri),
5254
(ApplicationInsightsOptions options) => TestApplicationInsights.Run(),
5355
(PrometheusOptions options) => TestPrometheus.Run(),
5456
(HttpClientOptions options) => TestHttpClient.Run(),
5557
(StackdriverOptions options) => TestStackdriver.Run(options.ProjectId),
5658
errs => 1);
57-
58-
// TestZipkin.Run();
59-
// TestApplicationInsights.Run();
60-
// TestPrometheus.Run();
61-
// TestHttpClient.Run();
59+
60+
Console.ReadLine();
6261
}
6362
}
6463
}

src/Samples/TestZipkin.cs

Lines changed: 59 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,84 @@
11
namespace Samples
22
{
33
using System;
4+
using System.Collections.Generic;
45
using System.Threading;
56
using OpenCensus.Exporter.Zipkin;
67
using OpenCensus.Trace;
8+
using OpenCensus.Trace.Config;
79
using OpenCensus.Trace.Sampler;
810

911
internal class TestZipkin
1012
{
11-
private static ITracer tracer = Tracing.Tracer;
12-
13-
internal static object Run()
13+
internal static object Run(string zipkinUri)
1414
{
15-
Console.WriteLine("Hello World!");
16-
15+
// 1. Configure exporter to export traces to Zipkin
1716
var exporter = new ZipkinTraceExporter(
1817
new ZipkinTraceExporterOptions()
1918
{
20-
Endpoint = new Uri("https://zipkin.azurewebsites.net/api/v2/spans"),
21-
ServiceName = typeof(Program).Assembly.GetName().Name,
19+
Endpoint = new Uri(zipkinUri),
20+
ServiceName = "tracing-to-zipkin-service",
2221
},
2322
Tracing.ExportComponent);
2423
exporter.Start();
2524

26-
var span = tracer.SpanBuilder("incoming request").SetSampler(Samplers.AlwaysSample).StartScopedSpan();
25+
// 2. Configure 100% sample rate for the purposes of the demo
26+
ITraceConfig traceConfig = Tracing.TraceConfig;
27+
ITraceParams currentConfig = traceConfig.ActiveTraceParams;
28+
var newConfig = currentConfig.ToBuilder()
29+
.SetSampler(Samplers.AlwaysSample)
30+
.Build();
31+
traceConfig.UpdateActiveTraceParams(newConfig);
32+
33+
// 3. Tracer is global singleton. You can register it via dependency injection if it exists
34+
// but if not - you can use it as follows:
35+
var tracer = Tracing.Tracer;
2736

28-
Thread.Sleep(TimeSpan.FromSeconds(1));
29-
var span2 = tracer.CurrentSpan;
30-
span2.End();
37+
// 4. Create a scoped span. It will end automatically when using statement ends
38+
using (var scope = tracer.SpanBuilder("Main").StartScopedSpan())
39+
{
40+
Console.WriteLine("About to do a busy work");
41+
for (int i = 0; i < 10; i++)
42+
{
43+
DoWork(i);
44+
}
45+
}
3146

32-
Console.ReadLine();
47+
// 5. Gracefully shutdown the exporter so it'll flush queued traces to Zipkin.
48+
Tracing.ExportComponent.SpanExporter.Dispose();
3349

3450
return null;
3551
}
52+
53+
private static void DoWork(int i)
54+
{
55+
// 6. Get the global singleton Tracer object
56+
ITracer tracer = Tracing.Tracer;
57+
58+
// 7. Start another span. If another span was already started, it'll use that span as the parent span.
59+
// In this example, the main method already started a span, so that'll be the parent span, and this will be
60+
// a child span.
61+
using (OpenCensus.Common.IScope scope = tracer.SpanBuilder("DoWork").StartScopedSpan())
62+
{
63+
// Simulate some work.
64+
ISpan span = tracer.CurrentSpan;
65+
66+
try
67+
{
68+
Console.WriteLine("Doing busy work");
69+
Thread.Sleep(1000);
70+
}
71+
catch (ArgumentOutOfRangeException e)
72+
{
73+
// 6. Set status upon error
74+
span.Status = Status.Internal.WithDescription(e.ToString());
75+
}
76+
77+
// 7. Annotate our span to capture metadata about our operation
78+
var attributes = new Dictionary<string, IAttributeValue>();
79+
attributes.Add("use", AttributeValue.StringAttributeValue("demo"));
80+
span.AddAnnotation("Invoking DoWork", attributes);
81+
}
82+
}
3683
}
3784
}

0 commit comments

Comments
 (0)