-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathtutorial.cc
102 lines (86 loc) · 2.94 KB
/
tutorial.cc
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include "text_map_carrier.h"
#include <cassert>
#include <iostream>
#include <unordered_map>
#include <zipkin/opentracing.h>
using namespace zipkin;
using namespace opentracing;
int main() {
ZipkinOtTracerOptions options;
options.service_name = "Tutorial";
auto tracer = makeZipkinOtTracer(options);
assert(tracer);
auto parent_span = tracer->StartSpan("parent");
assert(parent_span);
// Create a child span.
{
auto child_span =
tracer->StartSpan("childA", {ChildOf(&parent_span->context())});
assert(child_span);
// Set a simple tag.
child_span->SetTag("simple tag", 123);
// Set a complex tag.
child_span->SetTag("complex tag",
Values{123, Dictionary{{"abc", 123}, {"xyz", 4.0}}});
// Log simple values.
child_span->Log({{"event", "simple log"}, {"abc", 123}});
// Log complex values.
child_span->Log({{"event", "complex log"},
{"data", Dictionary{{"a", 1}, {"b", Values{1, 2}}}}});
child_span->Finish();
}
// Create a follows from span.
{
auto child_span =
tracer->StartSpan("childB", {FollowsFrom(&parent_span->context())});
// child_span's destructor will finish the span if not done so explicitly.
}
// Use custom timestamps.
{
auto t1 = SystemClock::now();
auto t2 = SteadyClock::now();
auto span = tracer->StartSpan(
"useCustomTimestamps",
{ChildOf(&parent_span->context()), StartTimestamp(t1)});
assert(span);
span->Finish({FinishTimestamp(t2)});
}
// Extract and Inject a span context.
{
std::unordered_map<std::string, std::string> text_map;
TextMapCarrier carrier(text_map);
auto err = tracer->Inject(parent_span->context(), carrier);
assert(err);
auto span_context_maybe = tracer->Extract(carrier);
assert(span_context_maybe);
auto span = tracer->StartSpan("propagationSpan",
{ChildOf(span_context_maybe->get())});
}
// You get an error when trying to extract a corrupt span.
{
std::unordered_map<std::string, std::string> text_map = {
{"x-b3-traceid", "123"}};
TextMapCarrier carrier(text_map);
auto err = tracer->Extract(carrier);
assert(!err);
assert(err.error() == span_context_corrupted_error);
// How to get a readable message from the error.
std::cout << "Example error message: \"" << err.error().message() << "\"\n";
}
// span.kind can be used to control whether Zipkin's cs/cr or sr/ss anotations
// are set.
{
auto client_span = tracer->StartSpan("tutorial.clientSpan");
assert(client_span);
client_span->SetTag("span.kind", "client");
auto server_span = tracer->StartSpan("tutorial.serverSpan",
{ChildOf(&client_span->context())});
assert(server_span);
server_span->SetTag("span.kind", "server");
server_span->Finish();
client_span->Finish();
}
parent_span->Finish();
tracer->Close();
return 0;
}