This repository has been archived by the owner on Dec 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
/
TestZipkin.cs
84 lines (74 loc) · 3.15 KB
/
TestZipkin.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
namespace Samples
{
using System;
using System.Collections.Generic;
using System.Threading;
using OpenCensus.Exporter.Zipkin;
using OpenCensus.Trace;
using OpenCensus.Trace.Config;
using OpenCensus.Trace.Sampler;
internal class TestZipkin
{
internal static object Run(string zipkinUri)
{
// 1. Configure exporter to export traces to Zipkin
var exporter = new ZipkinTraceExporter(
new ZipkinTraceExporterOptions()
{
Endpoint = new Uri(zipkinUri),
ServiceName = "tracing-to-zipkin-service",
},
Tracing.ExportComponent);
exporter.Start();
// 2. Configure 100% sample rate for the purposes of the demo
ITraceConfig traceConfig = Tracing.TraceConfig;
ITraceParams currentConfig = traceConfig.ActiveTraceParams;
var newConfig = currentConfig.ToBuilder()
.SetSampler(Samplers.AlwaysSample)
.Build();
traceConfig.UpdateActiveTraceParams(newConfig);
// 3. Tracer is global singleton. You can register it via dependency injection if it exists
// but if not - you can use it as follows:
var tracer = Tracing.Tracer;
// 4. Create a scoped span. It will end automatically when using statement ends
using (var scope = tracer.SpanBuilder("Main").StartScopedSpan())
{
Console.WriteLine("About to do a busy work");
for (int i = 0; i < 10; i++)
{
DoWork(i);
}
}
// 5. Gracefully shutdown the exporter so it'll flush queued traces to Zipkin.
Tracing.ExportComponent.SpanExporter.Dispose();
return null;
}
private static void DoWork(int i)
{
// 6. Get the global singleton Tracer object
ITracer tracer = Tracing.Tracer;
// 7. 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 (OpenCensus.Common.IScope scope = tracer.SpanBuilder("DoWork").StartScopedSpan())
{
// Simulate some work.
ISpan span = tracer.CurrentSpan;
try
{
Console.WriteLine("Doing busy work");
Thread.Sleep(1000);
}
catch (ArgumentOutOfRangeException e)
{
// 6. Set status upon error
span.Status = Status.Internal.WithDescription(e.ToString());
}
// 7. Annotate our span to capture metadata about our operation
var attributes = new Dictionary<string, IAttributeValue>();
attributes.Add("use", AttributeValue.StringAttributeValue("demo"));
span.AddAnnotation("Invoking DoWork", attributes);
}
}
}
}