-
Notifications
You must be signed in to change notification settings - Fork 440
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
Support for multiple processors #692
Merged
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
1078d63
draft
lalitb 78740eb
memory leak
lalitb ac1384b
remove example
lalitb 74df87a
add example back
lalitb 544f511
fix example
lalitb 55b7c95
Merge branch 'main' into multi-processor-1
lalitb 95966a9
resolve merge conflict
lalitb 341dbcf
fix conflict
lalitb 0310487
fix example
lalitb 65fb45c
Merge branch 'main' into multi-processor-1
lalitb 3e45766
Update examples/http/tracer_common.hpp
lalitb 33168d6
review comments
lalitb 42933ba
and format
lalitb 62d02cb
Merge branch 'multi-processor-1' of github.com:lalitb/opentelemetry-c…
lalitb ddd4507
fix review comments
lalitb 4c9e269
fix compile issue after merge
lalitb fe19cb2
Update examples/multi_processor/README.md
lalitb 554b51a
Merge branch 'main' into multi-processor-1
lalitb e2b640f
review comment
lalitb 7b504d8
Merge branch 'main' into multi-processor-1
lalitb e2ae973
Merge branch 'main' into multi-processor-1
lalitb 59024e4
Merge branch 'main' into multi-processor-1
lalitb a8456d4
Merge branch 'main' into multi-processor-1
lalitb 74fbead
fix merge conflict
lalitb 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 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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
cc_library( | ||
name = "foo_multi_library", | ||
srcs = [ | ||
"foo_library/foo_library.cc", | ||
], | ||
hdrs = [ | ||
"foo_library/foo_library.h", | ||
], | ||
deps = [ | ||
"//api", | ||
], | ||
) | ||
|
||
cc_binary( | ||
name = "example_multi_processor", | ||
srcs = [ | ||
"main.cc", | ||
], | ||
deps = [ | ||
":foo_multi_library", | ||
"//api", | ||
"//exporters/memory:in_memory_span_exporter", | ||
"//exporters/ostream:ostream_span_exporter", | ||
"//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,10 @@ | ||
include_directories(${CMAKE_SOURCE_DIR}/exporters/ostream/include | ||
${CMAKE_SOURCE_DIR}/exporters/memory/include) | ||
|
||
add_library(foo_multi_library foo_library/foo_library.cc) | ||
target_link_libraries(foo_multi_library opentelemetry_exporter_ostream_span | ||
${CMAKE_THREAD_LIBS_INIT} opentelemetry_api) | ||
|
||
add_executable(example_multi_processor main.cc) | ||
target_link_libraries(example_multi_processor ${CMAKE_THREAD_LIBS_INIT} | ||
foo_multi_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,14 @@ | ||
# Multi Processor Trace Example | ||
|
||
In this example, the application in `main.cc` initializes and registers a tracer | ||
provider from the [OpenTelemetry | ||
SDK](https://github.com/open-telemetry/opentelemetry-cpp). The `TracerProvider` is connected | ||
to two `processor-exporter` pipelines - for exporting simultaneously to `StdoutSpanExporter` | ||
and `InMemorySpanExporter`. The application then | ||
calls a `foo_library` which has been instrumented using the [OpenTelemetry | ||
API](https://github.com/open-telemetry/opentelemetry-cpp/tree/main/api). | ||
Resulting telemetry is directed to stdout through the StdoutSpanExporter, and saved as a | ||
variable (vector of spans) through `InMemorySpanExporter`. | ||
|
||
See [CONTRIBUTING.md](../../CONTRIBUTING.md) for instructions on building and | ||
running the example. |
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,42 @@ | ||
#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"); | ||
auto scope = get_tracer()->WithActiveSpan(span); | ||
|
||
span->End(); | ||
} | ||
|
||
void f2() | ||
{ | ||
auto span = get_tracer()->StartSpan("f2"); | ||
auto scope = get_tracer()->WithActiveSpan(span); | ||
|
||
f1(); | ||
f1(); | ||
|
||
span->End(); | ||
} | ||
} // namespace | ||
|
||
void foo_library() | ||
{ | ||
auto span = get_tracer()->StartSpan("library"); | ||
auto scope = get_tracer()->WithActiveSpan(span); | ||
|
||
f2(); | ||
|
||
span->End(); | ||
} |
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,79 @@ | ||
#include "opentelemetry/sdk/trace/simple_processor.h" | ||
#include "opentelemetry/sdk/trace/tracer_context.h" | ||
#include "opentelemetry/sdk/trace/tracer_provider.h" | ||
#include "opentelemetry/trace/provider.h" | ||
|
||
// Using an exporter that simply dumps span data to stdout. | ||
#include "foo_library/foo_library.h" | ||
#include "opentelemetry/exporters/memory/in_memory_span_exporter.h" | ||
#include "opentelemetry/exporters/ostream/span_exporter.h" | ||
|
||
using opentelemetry::exporter::memory::InMemorySpanExporter; | ||
|
||
InMemorySpanExporter *memory_span_exporter; | ||
|
||
namespace | ||
{ | ||
void initTracer() | ||
{ | ||
auto exporter1 = std::unique_ptr<sdktrace::SpanExporter>( | ||
new opentelemetry::exporter::trace::OStreamSpanExporter); | ||
auto processor1 = std::unique_ptr<sdktrace::SpanProcessor>( | ||
new sdktrace::SimpleSpanProcessor(std::move(exporter1))); | ||
|
||
auto exporter2 = std::unique_ptr<sdktrace::SpanExporter>(new InMemorySpanExporter()); | ||
|
||
// fetch the exporter for dumping data later | ||
memory_span_exporter = dynamic_cast<InMemorySpanExporter *>(exporter2.get()); | ||
lalitb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
auto processor2 = std::unique_ptr<sdktrace::SpanProcessor>( | ||
new sdktrace::SimpleSpanProcessor(std::move(exporter2))); | ||
|
||
auto provider = nostd::shared_ptr<opentelemetry::sdk::trace::TracerProvider>( | ||
new sdktrace::TracerProvider(std::move(processor1))); | ||
provider->AddProcessor(std::move(processor2)); | ||
// Set the global trace provider | ||
opentelemetry::trace::Provider::SetTracerProvider(std::move(provider)); | ||
} | ||
|
||
void dumpSpans(std::vector<std::unique_ptr<opentelemetry::sdk::trace::SpanData>> &spans) | ||
{ | ||
char span_buf[opentelemetry::trace::SpanId::kSize * 2]; | ||
char trace_buf[opentelemetry::trace::TraceId::kSize * 2]; | ||
char parent_span_buf[opentelemetry::trace::SpanId::kSize * 2]; | ||
std::cout << "\nSpans from memory :" << std::endl; | ||
|
||
for (auto &span : spans) | ||
{ | ||
std::cout << "\n\tSpan: " << std::endl; | ||
std::cout << "\t\tName: " << span->GetName() << std::endl; | ||
span->GetSpanId().ToLowerBase16(span_buf); | ||
span->GetTraceId().ToLowerBase16(trace_buf); | ||
span->GetParentSpanId().ToLowerBase16(parent_span_buf); | ||
std::cout << "\t\tTraceId: " << std::string(trace_buf, sizeof(trace_buf)) << std::endl; | ||
std::cout << "\t\tSpanId: " << std::string(span_buf, sizeof(span_buf)) << std::endl; | ||
std::cout << "\t\tParentSpanId: " << std::string(parent_span_buf, sizeof(parent_span_buf)) | ||
<< std::endl; | ||
|
||
std::cout << "\t\tDescription: " << span->GetDescription() << std::endl; | ||
std::cout << "\t\tSpan kind:" | ||
<< static_cast<typename std::underlying_type<opentelemetry::trace::SpanKind>::type>( | ||
span->GetSpanKind()) | ||
<< std::endl; | ||
std::cout << "\t\tSpan Status: " | ||
<< static_cast<typename std::underlying_type<opentelemetry::trace::StatusCode>::type>( | ||
span->GetStatus()) | ||
<< std::endl; | ||
} | ||
} | ||
} // namespace | ||
|
||
int main() | ||
{ | ||
// Removing this line will leave the default noop TracerProvider in place. | ||
initTracer(); | ||
|
||
foo_library(); | ||
auto memory_spans = memory_span_exporter->GetData()->GetSpans(); | ||
dumpSpans(memory_spans); | ||
} |
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should there be a convenience constructor that just takes on processor?
Alternatively, a static helper method to make TracerProvider from a processor?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1.
This is what OTel.NET does https://github.com/open-telemetry/opentelemetry-dotnet/blob/a25741030f05c60c85be102ce7c33f3899290d49/docs/trace/extending-the-sdk/Program.cs#L30.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider taking a look at the other language clients that reached 1.0 stable and see what's the best practice.
One more example from Python https://opentelemetry-python.readthedocs.io/en/latest/getting-started.html.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This specific example is using
TracerContext
. This is how it looks like if we directly useTracerProvider
to add processors:( which would be what application developer be doing most of the times ):
Do we want this for
TracerContext
too ?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have modified the example NOT to use
TracerContext
, instead directly useTracerProvider
to pass processor(s) as an argument to constructor.TracerProvider
constructor takes either of single processor, or vector of processors as an argument as shown in example in above comment ( and in PR description ).