Skip to content
This repository has been archived by the owner on Aug 30, 2022. It is now read-only.

Example app #101

Merged
merged 9 commits into from
May 16, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand All @@ -248,8 +255,12 @@ if(BUILD_TESTING)
src/jaegertracing/testutils/TracerUtil.cpp)
target_link_libraries(testutils PUBLIC ${JAEGERTRACING_LIB})

if(BUILD_SHARED_LIBS)
set(dynamic_load_test_src src/jaegertracing/DynamicLoadTest.cpp)
endif()
add_executable(UnitTest
src/jaegertracing/ConfigTest.cpp
${dynamic_load_test_src}
src/jaegertracing/ReferenceTest.cpp
src/jaegertracing/SpanContextTest.cpp
src/jaegertracing/SpanTest.cpp
Expand Down Expand Up @@ -284,9 +295,9 @@ if(BUILD_TESTING)

if(TARGET jaegertracing)
add_executable(DynamicallyLoadTracerTest
src/jaegertracing/DynamicLoadTest.cpp
src/jaegertracing/DynamicallyLoadTracerTest.cpp)
target_include_directories(DynamicallyLoadTracerTest PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/src>)
target_link_libraries(
DynamicallyLoadTracerTest OpenTracing::opentracing-static GTest::main)
Expand Down
18 changes: 6 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

42 changes: 42 additions & 0 deletions examples/App.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <iostream>

#include <yaml-cpp/yaml.h>

#include <jaegertracing/Tracer.h>

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<opentracing::Tracer>(tracer));
}

void tracedSubroutine(const std::unique_ptr<opentracing::Span>& 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] << " <config-yaml-path>\n";
return 1;
}
setUpTracer(argv[1]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't you need to close/flush the tracer before exit?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Luckily, no. At least not for the Jaeger client. My favorite thing about C++ is that most things are auto-closing (formally called RAII). Spans must be auto-closing according to OT C++ spec, but the Tracer::Close() definition is a little more vague (see https://github.com/opentracing/opentracing-cpp/blob/master/include/opentracing/tracer.h#L149-L153). As of now, we aren't using close any differently here than for spans. Might add it just in case I change my mind.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to clarify, when the main function exits, the global tracer shared pointer is destroyed, invoking the Tracer destructor, which invokes Tracer::close on its own. We could manually invoke any of these methods, but there is no need to.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not saying you are wrong, but I'd expect that behavior if you had a locally scoped variable that would get destroyed upon exiting the main scope. That global variables behave the same way is a surprise to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ya it's a bit confusing. Here's a source quoting the standard about it: https://stackoverflow.com/a/2204628/1930331.

tracedFunction();
return 0;
}
21 changes: 4 additions & 17 deletions examples/config.yml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion src/jaegertracing/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Config {

static Config parse(const YAML::Node& configYAML)
{
if (!configYAML.IsMap()) {
if (!configYAML.IsDefined() || !configYAML.IsMap()) {
return Config();
}

Expand Down
2 changes: 1 addition & 1 deletion src/jaegertracing/DynamicLoadTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ TEST(DynamicLoad, invalidVersion)
const void* errorCategory = nullptr;
void* tracerFactory = nullptr;
const auto rcode = OpenTracingMakeTracerFactory(
"1.0.0" /*invalid version*/, &errorCategory, &tracerFactory);
"0.0.0" /*invalid version*/, &errorCategory, &tracerFactory);
ASSERT_EQ(rcode, opentracing::incompatible_library_versions_error.value());
ASSERT_EQ(
errorCategory,
Expand Down
2 changes: 1 addition & 1 deletion src/jaegertracing/baggage/RestrictionsConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class RestrictionsConfig {

static RestrictionsConfig parse(const YAML::Node& configYAML)
{
if (!configYAML.IsMap()) {
if (!configYAML.IsDefined() || !configYAML.IsMap()) {
return RestrictionsConfig();
}

Expand Down
2 changes: 1 addition & 1 deletion src/jaegertracing/propagation/HeadersConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class HeadersConfig {

static HeadersConfig parse(const YAML::Node& configYAML)
{
if (!configYAML.IsMap()) {
if (!configYAML.IsDefined() || !configYAML.IsMap()) {
return HeadersConfig();
}

Expand Down
2 changes: 1 addition & 1 deletion src/jaegertracing/reporters/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class Config {

static Config parse(const YAML::Node& configYAML)
{
if (!configYAML.IsMap()) {
if (!configYAML.IsDefined() || !configYAML.IsMap()) {
return Config();
}

Expand Down
2 changes: 1 addition & 1 deletion src/jaegertracing/samplers/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class Config {

static Config parse(const YAML::Node& configYAML)
{
if (!configYAML.IsMap()) {
if (!configYAML.IsDefined() || !configYAML.IsMap()) {
return Config();
}

Expand Down