From e5a623401711a4fcf502f5de48ff24f2d86c8c45 Mon Sep 17 00:00:00 2001 From: Craig Ringer Date: Fri, 9 Feb 2018 10:24:26 +0800 Subject: [PATCH] Demonstrate that we lose spans on short runs The RemoteReporter loses spans during short-lived executions (#52). This is a test case not a fix. This variant is on top of my integration branch for my feature branches. This variant is enhanced with arguments to control flush mode and sleep after span finish. Signed-off-by: Craig Ringer --- src/jaegertracing/CMakeLists-UnitTest.cmake | 7 ++ src/jaegertracing/shortlived.cpp | 93 +++++++++++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 src/jaegertracing/shortlived.cpp diff --git a/src/jaegertracing/CMakeLists-UnitTest.cmake b/src/jaegertracing/CMakeLists-UnitTest.cmake index a83a4f9d..f6404bff 100644 --- a/src/jaegertracing/CMakeLists-UnitTest.cmake +++ b/src/jaegertracing/CMakeLists-UnitTest.cmake @@ -89,3 +89,10 @@ if(BUILD_TESTING) endif() endif(BUILD_TESTING) + +add_executable(shortlived "shortlived.cpp") +target_link_libraries(shortlived jaegertracing) +target_include_directories(shortlived PRIVATE + $ + $) + diff --git a/src/jaegertracing/shortlived.cpp b/src/jaegertracing/shortlived.cpp new file mode 100644 index 00000000..d542f40f --- /dev/null +++ b/src/jaegertracing/shortlived.cpp @@ -0,0 +1,93 @@ +#include + +#include +#include +#include +#include + +using std::string; +using std::to_string; +using namespace std::chrono; + +const string cfgstr {R"endyaml( +disabled: false +sampler: + type: const + param: 1 +reporter: + queueSize: 100 + bufferFlushInterval: 10 + logSpans: false + localAgentHostPort: 127.0.0.1:6831 +headers: + jaegerDebugHeader: debug-id + jaegerBaggageHeader: baggage + TraceContextHeaderName: trace-id + traceBaggageHeaderPrefix: "testctx-" +baggage_restrictions: + denyBaggageOnInitializationFailure: false + hostPort: 127.0.0.1:5778 + refreshInterval: 60 +)endyaml"}; + +void usage_die() +{ + std::cerr << "usage: shortlived tag-suffix 0|1 [sleep_s]" << std::endl; + std::cerr << " argument 1: suffix to identify this test run" << std::endl; + std::cerr << " argument 2: 1 to force flush, or 0 for no explicit flush" << std::endl; + std::cerr << " argument 3: seconds to sleep after span close and before exit" << std::endl; + exit(1); +} + +int main(int argc, char *argv[]) +{ + bool flush = false; + int sleep_s = 0; + const auto config = jaegertracing::Config::parse(YAML::Load(cfgstr)); + auto tracer = jaegertracing::Tracer::make("shortlived", config); + + if (argc < 3) + usage_die(); + + + flush = stoi(string(argv[2])); + + if (argc > 3) + sleep_s = stoi(string(argv[3])); + + std::ostringstream opname; + opname << "shortlived-" << argv[1] + << "-" << (flush ? "flush" : "noflush") + << "-sleep" << to_string(sleep_s) << "s"; + + std::cout << "Test with opname \"" << opname.str() << ", flush " << flush << std::endl; + + auto span = tracer->StartSpan(opname.str()); + span->SetTag("flush", flush); + span->SetTag("sleep_s", sleep_s); + span->Finish(); + span.reset(); + + if (flush) + { + std::cout << "flushing... " << std::flush; + auto start = system_clock::now(); + /* This should ensure that all spans reach the server */ + static_cast(tracer.get())->flush(); + auto end = system_clock::now(); + std::cout << " flushed (flush took " << duration_cast(end-start).count() << "ms)" << std::endl; + } + else + std::cout << "not flushing" << std::endl; + + if (sleep_s) + { + std::cout << "sleeping for " << sleep_s << " seconds... " << std::flush; + std::this_thread::sleep_for(seconds(sleep_s)); + std::cout << "done" << std::endl; + } + else + std::cout << "not sleeping before exit " << std::endl; + + tracer->Close(); +}