-
Notifications
You must be signed in to change notification settings - Fork 560
Add a minimal tracer example #86
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f17afbe
Add a simple example for the tracer workflow
6d17ffa
Format
5e9602c
Remove obsolete file
ed6de78
PR comments
63ab25c
Add missing files
e82d837
Bazel fixes
2ca3122
Fix bazel build
ff085c6
Fix CMake build
8251da7
Span timing
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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)); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. std::move(processor) ? |
||
| trace::Provider::SetTracerProvider(provider); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can/should this also be std::moved? |
||
| } | ||
| } // 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 hidden or 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 | ||
|
pyohannes marked this conversation as resolved.
|
||
| { | ||
| 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.