diff --git a/CMakeLists.txt b/CMakeLists.txt index 03ce5739..5842113e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -63,6 +63,8 @@ find_package(nlohmann_json CONFIG REQUIRED) list(APPEND LIBS nlohmann_json) list(APPEND package_deps nlohmann_json) +option(JAEGERTRACING_BUILD_EXAMPLES "Build examples" ON) + option(JAEGERTRACING_COVERAGE "Build with coverage" $ENV{COVERAGE}) option(JAEGERTRACING_BUILD_CROSSDOCK "Build crossdock" $ENV{CROSSDOCK}) cmake_dependent_option( @@ -240,6 +242,11 @@ configure_file( src/jaegertracing/Constants.h @ONLY) +if(JAEGERTRACING_BUILD_EXAMPLES) + add_executable(app examples/App.cpp) + target_link_libraries(app PUBLIC ${JAEGERTRACING_LIB}) +endif() + if(BUILD_TESTING) add_library(testutils src/jaegertracing/testutils/TUDPTransport.cpp diff --git a/README.md b/README.md index d3b0e2b0..76a0b51c 100644 --- a/README.md +++ b/README.md @@ -10,20 +10,15 @@ Please see [CONTRIBUTING.md](CONTRIBUTING.md). ## Generated files This project uses Apache Thrift for wire-format protocol support code -generation. It currently requires exactly Thrift 0.9.2 or 0.9.3. Patches have -been applied to the generated code so it cannot be directly re-generated from -the IDL in the `idl` submodule - -(see https://github.com/jaegertracing/jaeger-client-cpp/issues/45) +generation. It currently requires Thrift 0.11.0. The code can be re-generated with - git submodule init - git submodule update - find idl/thrift/ -type f -name \*.thrift -exec thrift -gen cpp -out src/jaegertracing/thrift-gen {} \; - -but at time of writing (Thrift 0.11.0) the resulting code is invalid due to -https://issues.apache.org/jira/browse/THRIFT-4484. +```bash + $ git submodule update --init + $ find idl/thrift/ -type f -name \*.thrift -exec thrift -gen cpp -out src/jaegertracing/thrift-gen {} \; + $ git apply scripts/thrift-gen.patch +``` ## License @@ -35,4 +30,3 @@ https://issues.apache.org/jira/browse/THRIFT-4484. [cov]: https://codecov.io/gh/jaegertracing/jaeger-client-cpp [ot-img]: https://img.shields.io/badge/OpenTracing--1.0-enabled-blue.svg [ot-url]: http://opentracing.io - diff --git a/examples/App.cpp b/examples/App.cpp new file mode 100644 index 00000000..f7df8a2a --- /dev/null +++ b/examples/App.cpp @@ -0,0 +1,45 @@ +#include + +#include + +#include + +namespace { + +void setUpTracer(const char* configFilePath) +{ + auto configYAML = YAML::LoadFile(configFilePath); + auto config = jaegertracing::Config::parse(configYAML); + auto tracer = jaegertracing::Tracer::make( + "example-service", config, jaegertracing::logging::consoleLogger()); + opentracing::Tracer::InitGlobal( + std::static_pointer_cast(tracer)); +} + +void tracedSubroutine(const std::unique_ptr& parentSpan) +{ + auto span = opentracing::Tracer::Global()->StartSpan( + "tracedSubroutine", { opentracing::ChildOf(&parentSpan->context()) }); +} + +void tracedFunction() +{ + auto span = opentracing::Tracer::Global()->StartSpan("tracedFunction"); + tracedSubroutine(span); +} + +} // anonymous namespace + +int main(int argc, char* argv[]) +{ + if (argc < 2) { + std::cerr << "usage: " << argv[0] << " \n"; + return 1; + } + setUpTracer(argv[1]); + tracedFunction(); + // Not stricly necessary to close tracer, but might flush any buffered + // spans. See more details in opentracing::Tracer::Close() documentation. + opentracing::Tracer::Global()->Close(); + return 0; +} diff --git a/examples/config.yml b/examples/config.yml index 4f1d85f4..340eb511 100644 --- a/examples/config.yml +++ b/examples/config.yml @@ -1,19 +1,6 @@ -service_name: test-service disabled: false -sampler: - type: probabilistic - param: 0.001 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 + logSpans: true +sampler: + type: const + param: 1 diff --git a/src/jaegertracing/Config.h b/src/jaegertracing/Config.h index 943d78b3..75282dbc 100644 --- a/src/jaegertracing/Config.h +++ b/src/jaegertracing/Config.h @@ -32,7 +32,7 @@ class Config { static Config parse(const YAML::Node& configYAML) { - if (!configYAML.IsMap()) { + if (!configYAML.IsDefined() || !configYAML.IsMap()) { return Config(); } diff --git a/src/jaegertracing/TracerFactoryTest.cpp b/src/jaegertracing/TracerFactoryTest.cpp index 0e83bd40..cc6a3e31 100644 --- a/src/jaegertracing/TracerFactoryTest.cpp +++ b/src/jaegertracing/TracerFactoryTest.cpp @@ -14,8 +14,8 @@ * limitations under the License. */ -#include "jaegertracing/Constants.h" #include "jaegertracing/TracerFactory.h" +#include "jaegertracing/Constants.h" #include namespace jaegertracing { @@ -26,10 +26,6 @@ TEST(TracerFactory, testInvalidConfig) "abc: {", R"({ "service_name": {} - })", - R"({ - "service_name": "t", - "badField": 97 })" }; TracerFactory tracerFactory; for (auto&& invalidConfig : invalidConfigTestCases) { diff --git a/src/jaegertracing/baggage/RestrictionsConfig.h b/src/jaegertracing/baggage/RestrictionsConfig.h index 3828f72c..6fe2f673 100644 --- a/src/jaegertracing/baggage/RestrictionsConfig.h +++ b/src/jaegertracing/baggage/RestrictionsConfig.h @@ -33,7 +33,7 @@ class RestrictionsConfig { static RestrictionsConfig parse(const YAML::Node& configYAML) { - if (!configYAML.IsMap()) { + if (!configYAML.IsDefined() || !configYAML.IsMap()) { return RestrictionsConfig(); } diff --git a/src/jaegertracing/propagation/HeadersConfig.h b/src/jaegertracing/propagation/HeadersConfig.h index 537fed28..4ae21355 100644 --- a/src/jaegertracing/propagation/HeadersConfig.h +++ b/src/jaegertracing/propagation/HeadersConfig.h @@ -30,7 +30,7 @@ class HeadersConfig { static HeadersConfig parse(const YAML::Node& configYAML) { - if (!configYAML.IsMap()) { + if (!configYAML.IsDefined() || !configYAML.IsMap()) { return HeadersConfig(); } diff --git a/src/jaegertracing/reporters/Config.h b/src/jaegertracing/reporters/Config.h index ee5663c4..40b19019 100644 --- a/src/jaegertracing/reporters/Config.h +++ b/src/jaegertracing/reporters/Config.h @@ -49,7 +49,7 @@ class Config { static Config parse(const YAML::Node& configYAML) { - if (!configYAML.IsMap()) { + if (!configYAML.IsDefined() || !configYAML.IsMap()) { return Config(); } diff --git a/src/jaegertracing/samplers/Config.h b/src/jaegertracing/samplers/Config.h index f43b9608..3fe55c79 100644 --- a/src/jaegertracing/samplers/Config.h +++ b/src/jaegertracing/samplers/Config.h @@ -54,7 +54,7 @@ class Config { static Config parse(const YAML::Node& configYAML) { - if (!configYAML.IsMap()) { + if (!configYAML.IsDefined() || !configYAML.IsMap()) { return Config(); }