]> git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/opentracing-cpp/example/dynamic_load/dynamic_load-example.cpp
buildsys: switch source download to quincy
[ceph.git] / ceph / src / jaegertracing / opentracing-cpp / example / dynamic_load / dynamic_load-example.cpp
1 // Demonstrates how to load a tracer library in at runtime and how to use it
2 // to construct spans. To run it using the mocktracer, invoke with
3 //
4 // TRACER_CONFIG=`mktemp`
5 // echo '{ "output_file": "/dev/stdout" }' > $TRACER_CONFIG
6 // dynamic_load-example /path/to/libopentracing_mocktracer.so $TRACER_CONFIG
7
8 #include <opentracing/dynamic_load.h>
9 #include <cassert>
10 #include <cerrno>
11 #include <fstream>
12 #include <iostream>
13 #include <iterator>
14 #include <string>
15
16 #ifdef _MSC_VER
17 #pragma warning(push)
18 #pragma warning(disable : 4996)
19 #endif
20
21 int main(int argc, char* argv[]) {
22 if (argc != 3) {
23 std::cerr << "Usage: <tracer_library> <tracer_config_file>\n";
24 return -1;
25 }
26
27 // Load the tracer library.
28 std::string error_message;
29 auto handle_maybe =
30 opentracing::DynamicallyLoadTracingLibrary(argv[1], error_message);
31 if (!handle_maybe) {
32 std::cerr << "Failed to load tracer library " << error_message << "\n";
33 return -1;
34 }
35
36 // Read in the tracer's configuration.
37 std::ifstream istream{argv[2]};
38 if (!istream.good()) {
39 std::cerr << "Failed to open tracer config file " << argv[2] << ": "
40 << std::strerror(errno) << "\n";
41 return -1;
42 }
43 std::string tracer_config{std::istreambuf_iterator<char>{istream},
44 std::istreambuf_iterator<char>{}};
45
46 // Construct a tracer.
47 auto& tracer_factory = handle_maybe->tracer_factory();
48 auto tracer_maybe =
49 tracer_factory.MakeTracer(tracer_config.c_str(), error_message);
50 if (!tracer_maybe) {
51 std::cerr << "Failed to create tracer " << error_message << "\n";
52 return -1;
53 }
54 auto& tracer = *tracer_maybe;
55
56 // Use the tracer to create some spans.
57 {
58 auto span_a = tracer->StartSpan("A");
59 assert(span_a != nullptr);
60 span_a->SetTag("abc", 123);
61 auto span_b =
62 tracer->StartSpan("B", {opentracing::ChildOf(&span_a->context())});
63 assert(span_b != nullptr);
64 span_b->SetTag("xyz", 987);
65 }
66
67 tracer->Close();
68 return 0;
69 }