-
Notifications
You must be signed in to change notification settings - Fork 415
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for multiple processors (#692)
- Loading branch information
Showing
22 changed files
with
623 additions
and
59 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
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()); | ||
|
||
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.