File: main.cc

package info (click to toggle)
opentelemetry-cpp 1.23.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,372 kB
  • sloc: cpp: 96,239; sh: 1,766; makefile: 36; python: 31
file content (154 lines) | stat: -rw-r--r-- 3,888 bytes parent folder | download | duplicates (3)
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

// Include common Trace Provider API and processor
#include "opentelemetry/sdk/trace/simple_processor.h"
#include "opentelemetry/sdk/trace/tracer_provider.h"
#include "opentelemetry/trace/provider.h"

#include "opentelemetry/exporters/etw/etw_tracer_exporter.h"

#include <iostream>
#include <thread>

#include <cstdio>
#ifndef LOG_DEBUG
#  include <mutex>

/**
 * @brief Thread-safe logger routine.
 * @param format
 * @param args
 * @return
 */
static inline int log_vprintf(const char *format, va_list args)
{
  static std::mutex cout_mutex;
  std::lock_guard<std::mutex> lk(cout_mutex);
  return vprintf(format, args);
};

/**
 * @brief Thread-safe logger routine.
 * @param format
 * @param
 * @return
 */
static inline int log_printf(const char *format, ...)
{
  int result;
  va_list ap;
  va_start(ap, format);
  result = log_vprintf(format, ap);
  va_end(ap);
  return result;
};

// Debug macros
#  define LOG_DEBUG(fmt_, ...) log_printf(" " fmt_ "\n", ##__VA_ARGS__)
#  define LOG_TRACE(fmt_, ...) log_printf(" " fmt_ "\n", ##__VA_ARGS__)
#  define LOG_INFO(fmt_, ...) log_printf(" " fmt_ "\n", ##__VA_ARGS__)
#  define LOG_WARN(fmt_, ...) log_printf(" " fmt_ "\n", ##__VA_ARGS__)
#  define LOG_ERROR(fmt_, ...) log_printf(" " fmt_ "\n", ##__VA_ARGS__)
#endif

using namespace OPENTELEMETRY_NAMESPACE;

using namespace opentelemetry::exporter::etw;

// Specify destination ETW Provider Name or GUID.
//
// In Visual Studio:
// - View -> Other Windows -> Diagnostic Events...
// - Click Configure (gear on top).
// - Specify "OpenTelemetry-ETW-TLD" in the list of providers.
//
// Event view shows event flow in realtime.
const char *kGlobalProviderName = "OpenTelemetry-ETW-TLD";

std::string providerName = kGlobalProviderName;

// One provider can be used to associate with different destinations.
exporter::etw::TracerProvider provider;

// Code below is generic and may be reused with any other TracerProvider.
// In ETW provider case - instrumentation name must match destination
// ETW provider name.
nostd::shared_ptr<trace::Tracer> tracer = provider.GetTracer(providerName, "1.0");

// Obtain numeric thread id
static inline uint64_t gettid()
{
  std::stringstream ss;
  ss << std::this_thread::get_id();
  uint64_t id = std::stoull(ss.str());
  return id;
}

// bop gets called by beep.
void bop()
{
  LOG_TRACE("bop worker tid=%u", gettid());
  auto span = tracer->StartSpan("bop");
  span->AddEvent("BopEvent", {{"tid", gettid()}});
  span->SetAttribute("attrib", 2);
  span->End();
}

// beep gets called in its own thread.
// It is running in a thread pool, invoked via lambda by dispatcher.
void beep()
{
  LOG_TRACE("beep worker tid=%u", gettid());
  auto span = tracer->StartSpan("beep");
  span->AddEvent("BeepEvent", {{"tid", gettid()}});
  span->SetAttribute("attrib", 1);
  {
    auto bopScope = tracer->WithActiveSpan(span);
    bop();
  }
  span->End();
}

// Max number of threads to spawn
constexpr int kMaxThreads = 10;

int main(int /*arc*/, char ** /*argv*/)
{
  std::thread pool[kMaxThreads];

  // Main dispatcher span: parent of all child thread spans.
  auto mainSpan = tracer->StartSpan("beep_bop");
  mainSpan->SetAttribute("attrib", 0);

  // Start several threads to perform beep-bop actions.
  LOG_TRACE("beep-boop dispatcher tid=%u", gettid());
  for (auto i = 0; i < kMaxThreads; i++)
  {
    pool[i] = std::thread([&]() {
      // This code runs in its own worker thread.
      auto beepScope = tracer->WithActiveSpan(mainSpan);
      // call beep -> bop
      beep();
    });
  };

  // Join all worker threads in the pool
  for (auto &th : pool)
  {
    try
    {
      if (th.joinable())
        th.join();
    }
    catch (...)
    {
      // thread might have been gracefully terminated already
    }
  }

  // End span
  mainSpan->End();

  return 0;
}