libcopp  2.2.0
main.cpp
Go to the documentation of this file.
1 // Copyright 2021 atframework
2 
3 #include <iostream>
4 #include <string>
5 
6 #if defined(HAVE_OPENTELEMETRY_CPP) && HAVE_OPENTELEMETRY_CPP
7 # include "opentelemetry/exporters/ostream/span_exporter_factory.h"
8 # include "opentelemetry/sdk/trace/batch_span_processor_factory.h"
9 # include "opentelemetry/sdk/trace/batch_span_processor_options.h"
10 # include "opentelemetry/sdk/trace/tracer_provider_factory.h"
11 # include "opentelemetry/sdk/trace/batch_span_processor.h"
12 # include "opentelemetry/sdk/trace/exporter.h"
13 # include "opentelemetry/trace/provider.h"
14 #endif
15 
16 #if defined(HAVE_PROTOBUF) && HAVE_PROTOBUF
17 
18 // There ares some warnings in prorobuf generated codes, we just disable these warnings
19 # if defined(_MSC_VER)
20 # pragma warning(push)
21 # if ((defined(__cplusplus) && __cplusplus >= 201704L) || (defined(_MSVC_LANG) && _MSVC_LANG >= 201704L))
22 # pragma warning(disable : 4996)
23 # pragma warning(disable : 4309)
24 # if _MSC_VER >= 1922
25 # pragma warning(disable : 5054)
26 # endif
27 # endif
28 
29 # if _MSC_VER < 1910
30 # pragma warning(disable : 4800)
31 # endif
32 # pragma warning(disable : 4244)
33 # pragma warning(disable : 4251)
34 # pragma warning(disable : 4267)
35 # endif
36 
37 # if defined(__GNUC__) && !defined(__clang__) && !defined(__apple_build_version__)
38 # if (__GNUC__ * 100 + __GNUC_MINOR__ * 10) >= 460
39 # pragma GCC diagnostic push
40 # endif
41 # pragma GCC diagnostic ignored "-Wunused-parameter"
42 # pragma GCC diagnostic ignored "-Wtype-limits"
43 # elif defined(__clang__) || defined(__apple_build_version__)
44 # pragma clang diagnostic push
45 # pragma clang diagnostic ignored "-Wunused-parameter"
46 # pragma clang diagnostic ignored "-Wtype-limits"
47 # endif
48 
49 # include "test_pb.pb.h"
50 
51 # if defined(__GNUC__) && !defined(__clang__) && !defined(__apple_build_version__)
52 # if (__GNUC__ * 100 + __GNUC_MINOR__ * 10) >= 460
53 # pragma GCC diagnostic pop
54 # endif
55 # elif defined(__clang__) || defined(__apple_build_version__)
56 # pragma clang diagnostic pop
57 # endif
58 
59 # if defined(_MSC_VER)
60 # pragma warning(pop)
61 # endif
62 
63 #endif
64 
65 #if defined(HAVE_OPENTELEMETRY_CPP) && HAVE_OPENTELEMETRY_CPP
66 
67 constexpr int kOpentelemetryNumSpans = 3;
68 
69 namespace {
70 
71 static void OpentelemetryInitTracer() {
72  auto exporter = opentelemetry::exporter::trace::OStreamSpanExporterFactory::Create();
73 
74  // CONFIGURE BATCH SPAN PROCESSOR PARAMETERS
75 
76  opentelemetry::sdk::trace::BatchSpanProcessorOptions options;
77  // We make the queue size `kOpentelemetryNumSpans`*2+5 because when the queue is half full, a preemptive notif
78  // is sent to start an export call, which we want to avoid in this simple example.
79  options.max_queue_size = kOpentelemetryNumSpans * 2 + 1;
80  // Time interval (in ms) between two consecutive exports.
81  options.schedule_delay_millis = std::chrono::milliseconds(3000);
82  // We export `kOpentelemetryNumSpans` after every `schedule_delay_millis` milliseconds.
83  options.max_export_batch_size = kOpentelemetryNumSpans;
84 
85  auto processor = opentelemetry::sdk::trace::BatchSpanProcessorFactory::Create(std::move(exporter), options);
86 
87  auto provider = opentelemetry::sdk::trace::TracerProviderFactory::Create(std::move(processor));
88  // Set the global trace provider.
89  opentelemetry::trace::Provider::SetTracerProvider(std::move(provider));
90 }
91 
92 opentelemetry::nostd::shared_ptr<opentelemetry::trace::Tracer> GetTracer() {
93  auto provider = opentelemetry::trace::Provider::GetTracerProvider();
94  return provider->GetTracer("cmake-toolset-test");
95 }
96 
97 static void OpentelemetryStartAndEndSpans() {
98  for (int i = 1; i <= kOpentelemetryNumSpans; ++i) {
99  std::string msg = "cmake-toolset-test-batch: ";
100  msg += static_cast<char>(i + '0');
101  GetTracer()->StartSpan(msg);
102  }
103 }
104 
105 static void OpentelemetryTest() {
106  OpentelemetryInitTracer();
107  OpentelemetryStartAndEndSpans();
108  OpentelemetryStartAndEndSpans();
109 
110  // Create enough spans to let BatchSpanProcessor to submit spans
111  OpentelemetryStartAndEndSpans();
112  // We should see the spans exported by OStreamSpanExporter here
113 }
114 
115 } // namespace
116 #endif
117 
118 int main() {
119 #if defined(HAVE_PROTOBUF) && HAVE_PROTOBUF
120  // Test protobuf
121  cmake_toolset::test_message msg;
122  msg.set_i32(123);
123  msg.set_i64(123000);
124  msg.set_str("Hello World!");
125 
126  std::cout << msg.DebugString() << std::endl;
127 #else
128  std::cout << "Hello World!" << std::endl;
129 #endif
130 #if defined(HAVE_OPENTELEMETRY_CPP) && HAVE_OPENTELEMETRY_CPP
131  // Test opentelemetry
132  OpentelemetryTest();
133 #endif
134  return 0;
135 }
int main()
Definition: main.cpp:118