forked from jaegertracing/jaeger-client-cpp
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathApp.cpp
98 lines (77 loc) · 2.8 KB
/
App.cpp
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
#include <iostream>
#include <yaml-cpp/yaml.h>
#include <jaegertracing/Tracer.h>
namespace jaeger_ceph {
void setUpTracer(const char* configFileAddress, const char* serviceToTrace)
{
auto configYAML = YAML::LoadFile(configFileAddress);
auto config = jaegertracing::Config::parse(configYAML);
auto tracer = jaegertracing::Tracer::make(
serviceToTrace, config, jaegertracing::logging::consoleLogger());
opentracing::Tracer::InitGlobal(
std::static_pointer_cast<opentracing::Tracer>(tracer));
}
std::unique_ptr<opentracing::Span>
tracedSubroutine(std::unique_ptr<opentracing::Span>& parentSpan,
const char* subRoutineContext)
{
auto span = opentracing::Tracer::Global()->StartSpan(
subRoutineContext, { opentracing::ChildOf(&parentSpan->context()) });
span->SetTag("simple tag in subroutineCtx", 124);
span->Finish();
return span;
}
std::unique_ptr<opentracing::Span> tracedFunction(const char* funcContext)
{
auto span = opentracing::Tracer::Global()->StartSpan(funcContext);
span->Finish();
return span;
}
std::string inject(std::unique_ptr<opentracing::Span>& span, const char* name)
{
std::stringstream ss;
if (!span) {
auto span = opentracing::Tracer::Global()->StartSpan(name);
}
auto err = opentracing::Tracer::Global()->Inject(span->context(), ss);
assert(err);
return ss.str();
}
void extract(std::unique_ptr<opentracing::Span>& span,
const char* name,
std::string t_meta)
{
std::stringstream ss(t_meta);
// if(!tracer){
// }
// setUpTracer("Extract-service");
auto span_context_maybe = opentracing::Tracer::Global()->Extract(ss);
assert(span_context_maybe);
// Propogation span
auto _span = opentracing::Tracer::Global()->StartSpan(
"propagationSpan", { ChildOf(span_context_maybe->get()) });
auto span1 = std::move(_span);
}
} // namespace jaeger_ceph
int main()
{
/* Remember: to close the span using span->Finish()
* understand tracer and span destruction
*/
jaeger_ceph::setUpTracer("/home/d/config.yml", "setUpTracer");
std::unique_ptr<opentracing::Span> parentSpan =
jaeger_ceph::tracedFunction("funcCtx");
std::unique_ptr<opentracing::Span> carrierSpan =
jaeger_ceph::tracedSubroutine(parentSpan, "subroutineCtx");
std::unique_ptr<opentracing::Span> carrierSpan2 =
jaeger_ceph::tracedSubroutine(carrierSpan, "subroutineCtx2");
std::string t_meta = jaeger_ceph::inject(
parentSpan, "inject_placeholder_const_char_name_of_span");
// std::cout << inject_test;
jaeger_ceph::extract(parentSpan, "extract_span_name", t_meta);
parentSpan->Finish();
carrierSpan->Finish();
carrierSpan2->Finish();
opentracing::Tracer::Global()->Close();
return 0;
}