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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
|
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
#include <gtest/gtest.h>
#include <stdint.h> // IWYU pragma: keep
#ifdef _WIN32
# include <windows.h>
#else
# include <dlfcn.h>
#endif
#include "component_a.h"
#include "component_b.h"
#include "component_c.h"
#include "component_d.h"
#include "component_e.h"
#include "component_f.h"
#include "opentelemetry/common/key_value_iterable.h"
#include "opentelemetry/nostd/shared_ptr.h"
#include "opentelemetry/nostd/string_view.h"
#include "opentelemetry/trace/default_span.h"
#include "opentelemetry/trace/noop.h"
#include "opentelemetry/trace/provider.h"
#include "opentelemetry/trace/span.h"
#include "opentelemetry/trace/span_context.h"
#include "opentelemetry/trace/span_context_kv_iterable.h"
#include "opentelemetry/trace/span_startoptions.h"
#include "opentelemetry/trace/tracer.h"
#include "opentelemetry/trace/tracer_provider.h"
using namespace opentelemetry;
void do_something()
{
do_something_in_a();
do_something_in_b();
do_something_in_c();
do_something_in_d();
do_something_in_e();
do_something_in_f();
/*
See https://github.com/bazelbuild/bazel/issues/4218
There is no way to set LD_LIBRARY_PATH in bazel,
for dlopen() to find the library.
Verified manually that dlopen("/full/path/to/libcomponent_g.so") works,
and that the test passes in this case.
*/
#ifndef BAZEL_BUILD
/* Call do_something_in_g() */
# ifdef _WIN32
HMODULE component_g = LoadLibraryA("component_g.dll");
# elif defined(__APPLE__)
void *component_g = dlopen("libcomponent_g.dylib", RTLD_NOW);
# else
void *component_g = dlopen("libcomponent_g.so", RTLD_NOW);
# endif
EXPECT_NE(component_g, nullptr);
# ifdef _WIN32
auto *func_g = reinterpret_cast<void (*)()>(GetProcAddress(component_g, "do_something_in_g"));
# else
auto *func_g = reinterpret_cast<void (*)()>(dlsym(component_g, "do_something_in_g"));
# endif
EXPECT_NE(func_g, nullptr);
(*func_g)();
# ifdef _WIN32
FreeLibrary(component_g);
# else
dlclose(component_g);
# endif
/* Call do_something_in_h() */
# ifdef _WIN32
HMODULE component_h = LoadLibraryA("component_h.dll");
# elif defined(__APPLE__)
void *component_h = dlopen("libcomponent_h.dylib", RTLD_NOW);
# else
void *component_h = dlopen("libcomponent_h.so", RTLD_NOW);
# endif
EXPECT_NE(component_h, nullptr);
# ifdef _WIN32
auto *func_h = reinterpret_cast<void (*)()>(GetProcAddress(component_h, "do_something_in_h"));
# else
auto *func_h = reinterpret_cast<void (*)()>(dlsym(component_h, "do_something_in_h"));
# endif
EXPECT_NE(func_h, nullptr);
(*func_h)();
# ifdef _WIN32
FreeLibrary(component_h);
# else
dlclose(component_h);
# endif
#endif /* BAZEL_BUILD */
}
int span_a_lib_count = 0;
int span_a_f1_count = 0;
int span_a_f2_count = 0;
int span_b_lib_count = 0;
int span_b_f1_count = 0;
int span_b_f2_count = 0;
int span_c_lib_count = 0;
int span_c_f1_count = 0;
int span_c_f2_count = 0;
int span_d_lib_count = 0;
int span_d_f1_count = 0;
int span_d_f2_count = 0;
int span_e_lib_count = 0;
int span_e_f1_count = 0;
int span_e_f2_count = 0;
int span_f_lib_count = 0;
int span_f_f1_count = 0;
int span_f_f2_count = 0;
int span_g_lib_count = 0;
int span_g_f1_count = 0;
int span_g_f2_count = 0;
int span_h_lib_count = 0;
int span_h_f1_count = 0;
int span_h_f2_count = 0;
int unknown_span_count = 0;
void reset_counts()
{
span_a_lib_count = 0;
span_a_f1_count = 0;
span_a_f2_count = 0;
span_b_lib_count = 0;
span_b_f1_count = 0;
span_b_f2_count = 0;
span_c_lib_count = 0;
span_c_f1_count = 0;
span_c_f2_count = 0;
span_d_lib_count = 0;
span_d_f1_count = 0;
span_d_f2_count = 0;
span_e_lib_count = 0;
span_e_f1_count = 0;
span_e_f2_count = 0;
span_f_lib_count = 0;
span_f_f1_count = 0;
span_f_f2_count = 0;
span_g_lib_count = 0;
span_g_f1_count = 0;
span_g_f2_count = 0;
span_h_lib_count = 0;
span_h_f1_count = 0;
span_h_f2_count = 0;
unknown_span_count = 0;
}
class MyTracer : public trace::Tracer
{
public:
MyTracer()
{
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
UpdateEnabled(true);
#endif
}
nostd::shared_ptr<trace::Span> StartSpan(
nostd::string_view name,
const common::KeyValueIterable & /* attributes */,
const trace::SpanContextKeyValueIterable & /* links */,
const trace::StartSpanOptions & /* options */) noexcept override
{
nostd::shared_ptr<trace::Span> result(new trace::DefaultSpan(trace::SpanContext::GetInvalid()));
/*
Unit test code, no need to be fancy.
*/
if (name == "A::library")
{
span_a_lib_count++;
}
else if (name == "A::f1")
{
span_a_f1_count++;
}
else if (name == "A::f2")
{
span_a_f2_count++;
}
else if (name == "B::library")
{
span_b_lib_count++;
}
else if (name == "B::f1")
{
span_b_f1_count++;
}
else if (name == "B::f2")
{
span_b_f2_count++;
}
else if (name == "C::library")
{
span_c_lib_count++;
}
else if (name == "C::f1")
{
span_c_f1_count++;
}
else if (name == "C::f2")
{
span_c_f2_count++;
}
else if (name == "D::library")
{
span_d_lib_count++;
}
else if (name == "D::f1")
{
span_d_f1_count++;
}
else if (name == "D::f2")
{
span_d_f2_count++;
}
else if (name == "E::library")
{
span_e_lib_count++;
}
else if (name == "E::f1")
{
span_e_f1_count++;
}
else if (name == "E::f2")
{
span_e_f2_count++;
}
else if (name == "F::library")
{
span_f_lib_count++;
}
else if (name == "F::f1")
{
span_f_f1_count++;
}
else if (name == "F::f2")
{
span_f_f2_count++;
}
else if (name == "G::library")
{
span_g_lib_count++;
}
else if (name == "G::f1")
{
span_g_f1_count++;
}
else if (name == "G::f2")
{
span_g_f2_count++;
}
else if (name == "H::library")
{
span_h_lib_count++;
}
else if (name == "H::f1")
{
span_h_f1_count++;
}
else if (name == "H::f2")
{
span_h_f2_count++;
}
else
{
unknown_span_count++;
}
return result;
}
#if OPENTELEMETRY_ABI_VERSION_NO == 1
void ForceFlushWithMicroseconds(uint64_t /* timeout */) noexcept override {}
void CloseWithMicroseconds(uint64_t /* timeout */) noexcept override {}
#endif /* OPENTELEMETRY_ABI_VERSION_NO */
};
class MyTracerProvider : public trace::TracerProvider
{
public:
static std::shared_ptr<trace::TracerProvider> Create()
{
std::shared_ptr<trace::TracerProvider> result(new MyTracerProvider());
return result;
}
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
nostd::shared_ptr<trace::Tracer> GetTracer(
nostd::string_view /* name */,
nostd::string_view /* version */,
nostd::string_view /* schema_url */,
const common::KeyValueIterable * /* attributes */) noexcept override
{
nostd::shared_ptr<trace::Tracer> result(new MyTracer());
return result;
}
#else
nostd::shared_ptr<trace::Tracer> GetTracer(nostd::string_view /* name */,
nostd::string_view /* version */,
nostd::string_view /* schema_url */) noexcept override
{
nostd::shared_ptr<trace::Tracer> result(new MyTracer());
return result;
}
#endif
};
void setup_otel()
{
std::shared_ptr<opentelemetry::trace::TracerProvider> provider = MyTracerProvider::Create();
// The whole point of this test is to make sure
// that the API singleton behind SetTracerProvider()
// works for all components, static and dynamic.
// Set the global tracer provider
trace_api::Provider::SetTracerProvider(provider);
}
void cleanup_otel()
{
std::shared_ptr<opentelemetry::trace::TracerProvider> provider(
new opentelemetry::trace::NoopTracerProvider());
// Set the global tracer provider
trace_api::Provider::SetTracerProvider(provider);
}
// TODO: Remove once windows api singletons are supported.
// See https://github.com/open-telemetry/opentelemetry-cpp/issues/2534
#ifdef _WIN32
# define RUN_FAILING_WINDOWS_TEST 0
#else
# define RUN_FAILING_WINDOWS_TEST 1
#endif
TEST(SingletonTest, Uniqueness)
{
do_something();
EXPECT_EQ(span_a_lib_count, 0);
EXPECT_EQ(span_a_f1_count, 0);
EXPECT_EQ(span_a_f2_count, 0);
EXPECT_EQ(span_b_lib_count, 0);
EXPECT_EQ(span_b_f1_count, 0);
EXPECT_EQ(span_b_f2_count, 0);
EXPECT_EQ(span_c_lib_count, 0);
EXPECT_EQ(span_c_f1_count, 0);
EXPECT_EQ(span_c_f2_count, 0);
EXPECT_EQ(span_d_lib_count, 0);
EXPECT_EQ(span_d_f1_count, 0);
EXPECT_EQ(span_d_f2_count, 0);
EXPECT_EQ(span_e_lib_count, 0);
EXPECT_EQ(span_e_f1_count, 0);
EXPECT_EQ(span_e_f2_count, 0);
EXPECT_EQ(span_f_lib_count, 0);
EXPECT_EQ(span_f_f1_count, 0);
EXPECT_EQ(span_f_f2_count, 0);
EXPECT_EQ(span_g_lib_count, 0);
EXPECT_EQ(span_g_f1_count, 0);
EXPECT_EQ(span_g_f2_count, 0);
EXPECT_EQ(span_h_lib_count, 0);
EXPECT_EQ(span_h_f1_count, 0);
EXPECT_EQ(span_h_f2_count, 0);
EXPECT_EQ(unknown_span_count, 0);
reset_counts();
setup_otel();
do_something();
EXPECT_EQ(span_a_lib_count, 1);
EXPECT_EQ(span_a_f1_count, 2);
EXPECT_EQ(span_a_f2_count, 1);
EXPECT_EQ(span_b_lib_count, 1);
EXPECT_EQ(span_b_f1_count, 2);
EXPECT_EQ(span_b_f2_count, 1);
#if RUN_FAILING_WINDOWS_TEST
EXPECT_EQ(span_c_lib_count, 1); // Fails with shared libraries on Windows
EXPECT_EQ(span_c_f1_count, 2); // Fails with shared libraries on Windows
EXPECT_EQ(span_c_f2_count, 1); // Fails with shared libraries on Windows
EXPECT_EQ(span_d_lib_count, 1); // Fails with shared libraries on Windows
EXPECT_EQ(span_d_f1_count, 2); // Fails with shared libraries on Windows
EXPECT_EQ(span_d_f2_count, 1); // Fails with shared libraries on Windows
EXPECT_EQ(span_e_lib_count, 1); // Fails with shared libraries on Windows
EXPECT_EQ(span_e_f1_count, 2); // Fails with shared libraries on Windows
EXPECT_EQ(span_e_f2_count, 1); // Fails with shared libraries on Windows
EXPECT_EQ(span_f_lib_count, 1); // Fails with shared libraries on Windows
EXPECT_EQ(span_f_f1_count, 2); // Fails with shared libraries on Windows
EXPECT_EQ(span_f_f2_count, 1); // Fails with shared libraries on Windows
#endif
#ifndef BAZEL_BUILD
# if RUN_FAILING_WINDOWS_TEST
EXPECT_EQ(span_g_lib_count, 1); // Fails with shared libraries on Windows
EXPECT_EQ(span_g_f1_count, 2); // Fails with shared libraries on Windows
EXPECT_EQ(span_g_f2_count, 1); // Fails with shared libraries on Windows
EXPECT_EQ(span_h_lib_count, 1); // Fails with shared libraries on Windows
EXPECT_EQ(span_h_f1_count, 2); // Fails with shared libraries on Windows
EXPECT_EQ(span_h_f2_count, 1); // Fails with shared libraries on Windows
# endif
#endif
EXPECT_EQ(unknown_span_count, 0);
reset_counts();
cleanup_otel();
do_something();
EXPECT_EQ(span_a_lib_count, 0);
EXPECT_EQ(span_a_f1_count, 0);
EXPECT_EQ(span_a_f2_count, 0);
EXPECT_EQ(span_b_lib_count, 0);
EXPECT_EQ(span_b_f1_count, 0);
EXPECT_EQ(span_b_f2_count, 0);
EXPECT_EQ(span_c_lib_count, 0);
EXPECT_EQ(span_c_f1_count, 0);
EXPECT_EQ(span_c_f2_count, 0);
EXPECT_EQ(span_d_lib_count, 0);
EXPECT_EQ(span_d_f1_count, 0);
EXPECT_EQ(span_d_f2_count, 0);
EXPECT_EQ(span_e_lib_count, 0);
EXPECT_EQ(span_e_f1_count, 0);
EXPECT_EQ(span_e_f2_count, 0);
EXPECT_EQ(span_f_lib_count, 0);
EXPECT_EQ(span_f_f1_count, 0);
EXPECT_EQ(span_f_f2_count, 0);
EXPECT_EQ(span_g_lib_count, 0);
EXPECT_EQ(span_g_f1_count, 0);
EXPECT_EQ(span_g_f2_count, 0);
EXPECT_EQ(span_h_lib_count, 0);
EXPECT_EQ(span_h_f1_count, 0);
EXPECT_EQ(span_h_f2_count, 0);
EXPECT_EQ(unknown_span_count, 0);
}
|