forked from open-telemetry/opentelemetry-specification
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a minimal tracer example (open-telemetry#86)
- Loading branch information
Johannes Tax
authored
Jun 8, 2020
1 parent
273a0ae
commit 2973baa
Showing
22 changed files
with
280 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
add_subdirectory(plugin) | ||
add_subdirectory(simple) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
cc_library( | ||
name = "foo_library", | ||
srcs = [ | ||
"foo_library/foo_library.cc", | ||
], | ||
hdrs = [ | ||
"foo_library/foo_library.h", | ||
], | ||
deps = [ | ||
"//api", | ||
], | ||
) | ||
|
||
cc_binary( | ||
name = "example_simple", | ||
srcs = [ | ||
"main.cc", | ||
"stdout_exporter.h", | ||
], | ||
deps = [ | ||
":foo_library", | ||
"//api", | ||
"//sdk/src/trace", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
add_library(foo_library foo_library/foo_library.cc) | ||
target_link_libraries(foo_library ${CMAKE_THREAD_LIBS_INIT} opentelemetry_api) | ||
|
||
add_executable(example_simple main.cc) | ||
target_link_libraries(example_simple ${CMAKE_THREAD_LIBS_INIT} foo_library | ||
opentelemetry_trace) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#include "opentelemetry/trace/provider.h" | ||
|
||
namespace trace = opentelemetry::trace; | ||
namespace nostd = opentelemetry::nostd; | ||
|
||
namespace | ||
{ | ||
nostd::shared_ptr<trace::Tracer> get_tracer() | ||
{ | ||
auto provider = trace::Provider::GetTracerProvider(); | ||
return provider->GetTracer("foo_library"); | ||
} | ||
|
||
void f1() | ||
{ | ||
auto span = get_tracer()->StartSpan("f1"); | ||
} | ||
|
||
void f2() | ||
{ | ||
auto span = get_tracer()->StartSpan("f2"); | ||
|
||
f1(); | ||
f1(); | ||
} | ||
} // namespace | ||
|
||
void foo_library() | ||
{ | ||
auto span = get_tracer()->StartSpan("library"); | ||
|
||
f2(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#pragma once | ||
|
||
void foo_library(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#include "opentelemetry/sdk/trace/simple_processor.h" | ||
#include "opentelemetry/sdk/trace/tracer_provider.h" | ||
#include "opentelemetry/trace/provider.h" | ||
|
||
// Using an exporter that simply dumps span data to stdout. | ||
#include "stdout_exporter.h" | ||
|
||
#include "foo_library/foo_library.h" | ||
|
||
namespace | ||
{ | ||
void initTracer() | ||
{ | ||
auto exporter = std::unique_ptr<sdktrace::SpanExporter>(new StdoutExporter); | ||
auto processor = std::shared_ptr<sdktrace::SpanProcessor>( | ||
new sdktrace::SimpleSpanProcessor(std::move(exporter))); | ||
auto provider = nostd::shared_ptr<trace::TracerProvider>(new sdktrace::TracerProvider(processor)); | ||
trace::Provider::SetTracerProvider(provider); | ||
} | ||
} // namespace | ||
|
||
int main() | ||
{ | ||
// Removing this line will leave OT initialized with the default noop | ||
// tracer, thus being effectively deactivated. | ||
initTracer(); | ||
|
||
foo_library(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#pragma once | ||
|
||
#include "opentelemetry/sdk/trace/exporter.h" | ||
#include "opentelemetry/sdk/trace/span_data.h" | ||
|
||
#include <iostream> | ||
|
||
namespace trace = opentelemetry::trace; | ||
namespace nostd = opentelemetry::nostd; | ||
namespace sdktrace = opentelemetry::sdk::trace; | ||
|
||
class StdoutExporter final : public sdktrace::SpanExporter | ||
{ | ||
std::unique_ptr<sdktrace::Recordable> MakeRecordable() noexcept | ||
{ | ||
return std::unique_ptr<sdktrace::Recordable>(new sdktrace::SpanData); | ||
} | ||
|
||
sdktrace::ExportResult Export( | ||
const nostd::span<std::unique_ptr<sdktrace::Recordable>> &spans) noexcept | ||
{ | ||
for (auto &recordable : spans) | ||
{ | ||
auto span = std::unique_ptr<sdktrace::SpanData>( | ||
static_cast<sdktrace::SpanData *>(recordable.release())); | ||
|
||
if (span != nullptr) | ||
{ | ||
char trace_id[32] = {0}; | ||
char span_id[16] = {0}; | ||
char parent_span_id[16] = {0}; | ||
|
||
span->GetTraceId().ToLowerBase16(trace_id); | ||
span->GetSpanId().ToLowerBase16(span_id); | ||
span->GetParentSpanId().ToLowerBase16(parent_span_id); | ||
|
||
std::cout << "{" | ||
<< "\n name : " << span->GetName() | ||
<< "\n trace_id : " << std::string(trace_id, 32) | ||
<< "\n span_id : " << std::string(span_id, 16) | ||
<< "\n parent_span_id: " << std::string(parent_span_id, 16) | ||
<< "\n start : " << span->GetStartTime().time_since_epoch().count() | ||
<< "\n duration : " << span->GetDuration().count() << "\n}" | ||
<< "\n"; | ||
} | ||
} | ||
|
||
return sdktrace::ExportResult::kSuccess; | ||
} | ||
|
||
void Shutdown(std::chrono::microseconds timeout = std::chrono::microseconds(0)) noexcept {} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.