Skip to content
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

Create SDK TRACE context, a shared reference to tracer pipelines between Tracer/TracerProvider. Update tests. #590

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from 14 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Increment the:

## [Unreleased]

* Added TracerContext to so Tracer and TracerProvider share SDK configuration and
updates to exporters apply to all tracers [#590](https://github.com/open-telemetry/opentelemetry-cpp/pull/590)
* [EXPORTER] Added Zipkin Exporter. ([#471](https://github.com/open-telemetry/opentelemetry-cpp/pull/471))
* [PROPAGATOR] Added Composite Propagator ([#597](https://github.com/open-telemetry/opentelemetry-cpp/pull/597))

Expand Down
2 changes: 1 addition & 1 deletion api/include/opentelemetry/trace/span_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ class SpanContext final
trace_api::TraceId trace_id_;
trace_api::SpanId span_id_;
trace_api::TraceFlags trace_flags_;
nostd::shared_ptr<trace_api::TraceState> trace_state_;
bool is_remote_ = false;
nostd::shared_ptr<trace_api::TraceState> trace_state_;
};
} // namespace trace
OPENTELEMETRY_END_NAMESPACE
1 change: 0 additions & 1 deletion api/include/opentelemetry/trace/trace_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ class TraceState

std::size_t begin{0};
std::size_t end{0};
bool invalid_header = false;
while (begin < header.size() && ts->num_entries_ < kMaxKeyValuePairs)
{
// find list-member
Expand Down
4 changes: 2 additions & 2 deletions examples/batch/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ void initTracer()
// We export `kNumSpans` after every `schedule_delay_millis` milliseconds.
options.max_export_batch_size = kNumSpans;

auto processor = std::shared_ptr<sdktrace::SpanProcessor>(
auto processor = std::unique_ptr<sdktrace::SpanProcessor>(
new sdktrace::BatchSpanProcessor(std::move(exporter), options));

auto provider = nostd::shared_ptr<opentelemetry::trace::TracerProvider>(
new sdktrace::TracerProvider(processor));
new sdktrace::TracerProvider(std::move(processor)));
// Set the global trace provider.
opentelemetry::trace::Provider::SetTracerProvider(provider);
}
Expand Down
4 changes: 2 additions & 2 deletions examples/multithreaded/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ void initTracer()
{
auto exporter = std::unique_ptr<sdktrace::SpanExporter>(
new opentelemetry::exporter::trace::OStreamSpanExporter);
auto processor = std::shared_ptr<sdktrace::SpanProcessor>(
auto processor = std::unique_ptr<sdktrace::SpanProcessor>(
new sdktrace::SimpleSpanProcessor(std::move(exporter)));
auto provider = nostd::shared_ptr<opentelemetry::trace::TracerProvider>(
new sdktrace::TracerProvider(processor, opentelemetry::sdk::resource::Resource::Create({})));
new sdktrace::TracerProvider(std::move(processor), opentelemetry::sdk::resource::Resource::Create({})));
// Set the global trace provider
opentelemetry::trace::Provider::SetTracerProvider(provider);
}
Expand Down
4 changes: 2 additions & 2 deletions examples/otlp/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ void InitTracer()
// Create OTLP exporter instance
auto exporter = std::unique_ptr<sdktrace::SpanExporter>(new otlp::OtlpExporter(opts));

auto processor = std::shared_ptr<sdktrace::SpanProcessor>(
auto processor = std::unique_ptr<sdktrace::SpanProcessor>(
new sdktrace::SimpleSpanProcessor(std::move(exporter)));
auto provider = nostd::shared_ptr<trace::TracerProvider>(new sdktrace::TracerProvider(processor));
auto provider = nostd::shared_ptr<trace::TracerProvider>(new sdktrace::TracerProvider(std::move(processor)));
// Set the global trace provider
trace::Provider::SetTracerProvider(provider);
}
Expand Down
5 changes: 2 additions & 3 deletions examples/simple/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ void initTracer()
{
auto exporter = std::unique_ptr<sdktrace::SpanExporter>(
new opentelemetry::exporter::trace::OStreamSpanExporter);
auto processor = std::shared_ptr<sdktrace::SpanProcessor>(
auto processor = std::unique_ptr<sdktrace::SpanProcessor>(
new sdktrace::SimpleSpanProcessor(std::move(exporter)));
auto provider = nostd::shared_ptr<opentelemetry::trace::TracerProvider>(
new sdktrace::TracerProvider(processor, opentelemetry::sdk::resource::Resource::Create({}),
std::make_shared<opentelemetry::sdk::trace::AlwaysOnSampler>()));
new sdktrace::TracerProvider(std::move(processor)));

// Set the global trace provider
opentelemetry::trace::Provider::SetTracerProvider(provider);
Expand Down
127 changes: 86 additions & 41 deletions exporters/ostream/test/ostream_span_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ TEST(OStreamSpanExporter, Shutdown)
{
auto exporter = std::unique_ptr<sdktrace::SpanExporter>(
new opentelemetry::exporter::trace::OStreamSpanExporter);
auto processor = std::shared_ptr<sdktrace::SpanProcessor>(
auto processor = std::unique_ptr<sdktrace::SpanProcessor>(
new sdktrace::SimpleSpanProcessor(std::move(exporter)));
auto provider = nostd::shared_ptr<sdktrace::TracerProvider>(
new sdktrace::TracerProvider(std::move(processor)));
auto tracer = provider->GetTracer("test");

auto recordable = processor->MakeRecordable();
recordable->SetName("Test Span");
auto span = tracer->StartSpan("Test Span");

// Create stringstream to redirect to
std::stringstream stdoutOutput;
Expand All @@ -36,8 +38,8 @@ TEST(OStreamSpanExporter, Shutdown)
// Redirect cout to our stringstream buffer
std::cout.rdbuf(stdoutOutput.rdbuf());

EXPECT_TRUE(processor->Shutdown());
processor->OnEnd(std::move(recordable));
EXPECT_TRUE(provider->Shutdown());
span->End();

std::cout.rdbuf(sbuf);

Expand All @@ -49,16 +51,28 @@ TEST(OStreamSpanExporter, PrintDefaultSpan)
{
auto exporter = std::unique_ptr<sdktrace::SpanExporter>(
new opentelemetry::exporter::trace::OStreamSpanExporter);
auto processor = std::shared_ptr<sdktrace::SpanProcessor>(
auto processor = std::unique_ptr<sdktrace::SpanProcessor>(
new sdktrace::SimpleSpanProcessor(std::move(exporter)));
auto provider = nostd::shared_ptr<opentelemetry::trace::TracerProvider>(
new sdktrace::TracerProvider(std::move(processor)));

auto recordable = processor->MakeRecordable();
auto tracer = provider->GetTracer("test");

opentelemetry::core::SystemTimestamp sysone(std::chrono::nanoseconds(1));
opentelemetry::core::SteadyTimestamp one(std::chrono::nanoseconds(1));
opentelemetry::trace::StartSpanOptions startOne;
startOne.start_steady_time = one;
startOne.start_system_time = sysone;
opentelemetry::trace::EndSpanOptions endZero = { one };
auto span = tracer->StartSpan("Test Span", startOne);

constexpr uint8_t trace_id_buf[] = {1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8};
opentelemetry::trace::TraceId t_id(trace_id_buf);
constexpr uint8_t span_id_buf[] = {1, 2, 3, 4, 5, 6, 7, 8};
opentelemetry::trace::SpanId s_id(span_id_buf);

// A hacky solution to reset the IDs for perfect println.
auto& recordable = dynamic_cast<opentelemetry::sdk::trace::Span*>(span.get())->GetRecordablePtr();
recordable->SetIds(t_id, s_id, s_id);

// Create stringstream to redirect to
Expand All @@ -70,17 +84,18 @@ TEST(OStreamSpanExporter, PrintDefaultSpan)
// Redirect cout to our stringstream buffer
std::cout.rdbuf(stdoutOutput.rdbuf());

processor->OnEnd(std::move(recordable));
// Ensure start/duration are zero for toString
span->End(endZero);

std::cout.rdbuf(sbuf);

std::string expectedOutput =
"{\n"
" name : \n"
" name : Test Span\n"
" trace_id : 01020304050607080102030405060708\n"
" span_id : 0102030405060708\n"
" parent_span_id: 0102030405060708\n"
" start : 0\n"
" start : 1\n"
" duration : 0\n"
" description : \n"
" span kind : Internal\n"
Expand All @@ -95,28 +110,38 @@ TEST(OStreamSpanExporter, PrintChangedSpanCout)
{
auto exporter = std::unique_ptr<sdktrace::SpanExporter>(
new opentelemetry::exporter::trace::OStreamSpanExporter);
auto processor = std::shared_ptr<sdktrace::SpanProcessor>(
auto processor = std::unique_ptr<sdktrace::SpanProcessor>(
new sdktrace::SimpleSpanProcessor(std::move(exporter)));
auto provider = nostd::shared_ptr<opentelemetry::trace::TracerProvider>(
new sdktrace::TracerProvider(std::move(processor)));

auto tracer = provider->GetTracer("test");

auto recordable = processor->MakeRecordable();

recordable->SetName("Test Span");
opentelemetry::core::SystemTimestamp now(std::chrono::system_clock::now());
opentelemetry::core::SteadyTimestamp one(std::chrono::nanoseconds(1));
opentelemetry::core::SteadyTimestamp onehundredone(std::chrono::nanoseconds(101));
opentelemetry::trace::StartSpanOptions startOpts;
startOpts.start_steady_time = one;
startOpts.start_system_time = now;
opentelemetry::trace::EndSpanOptions endOpts = { onehundredone };

auto span = tracer->StartSpan("Test Span", startOpts);
span->SetStatus(opentelemetry::trace::StatusCode::kOk, "Test Description");
span->SetAttribute("attr1", "string");


constexpr uint8_t trace_id_buf[] = {1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8};
opentelemetry::trace::TraceId t_id(trace_id_buf);
constexpr uint8_t span_id_buf[] = {1, 2, 3, 4, 5, 6, 7, 8};
opentelemetry::trace::SpanId s_id(span_id_buf);

// A hacky solution to reset the IDs for perfect println.
// All other settings should be against the API.
auto* recordable = dynamic_cast<opentelemetry::sdk::trace::Span*>(span.get())->GetRecordablePtr().get();
recordable->SetIds(t_id, s_id, s_id);

recordable->SetStartTime(now);
recordable->SetDuration(std::chrono::nanoseconds(100));
recordable->SetStatus(opentelemetry::trace::StatusCode::kOk, "Test Description");
recordable->SetSpanKind(opentelemetry::trace::SpanKind::kClient);

recordable->SetAttribute("attr1", "string");

// Create stringstream to redirect to
std::stringstream stdoutOutput;

Expand All @@ -126,7 +151,7 @@ TEST(OStreamSpanExporter, PrintChangedSpanCout)
// Redirect cout to our stringstream buffer
std::cout.rdbuf(stdoutOutput.rdbuf());

processor->OnEnd(std::move(recordable));
span->End(endOpts);

std::cout.rdbuf(sbuf);

Expand Down Expand Up @@ -155,30 +180,39 @@ TEST(OStreamSpanExporter, PrintChangedSpanCerr)
{
auto exporter = std::unique_ptr<sdktrace::SpanExporter>(
new opentelemetry::exporter::trace::OStreamSpanExporter(std::cerr));
auto processor = std::shared_ptr<sdktrace::SpanProcessor>(
auto processor = std::unique_ptr<sdktrace::SpanProcessor>(
new sdktrace::SimpleSpanProcessor(std::move(exporter)));
auto provider = nostd::shared_ptr<sdktrace::TracerProvider>(
new sdktrace::TracerProvider(std::move(processor)));

auto recordable = processor->MakeRecordable();
auto tracer = provider->GetTracer("test");

recordable->SetName("Test Span");
opentelemetry::core::SystemTimestamp now(std::chrono::system_clock::now());
opentelemetry::core::SteadyTimestamp one(std::chrono::nanoseconds(1));
opentelemetry::core::SteadyTimestamp onehundredone(std::chrono::nanoseconds(101));
opentelemetry::trace::StartSpanOptions startOpts;
startOpts.start_steady_time = one;
startOpts.start_system_time = now;
opentelemetry::trace::EndSpanOptions endOpts = { onehundredone };

auto span = tracer->StartSpan("Test Span", startOpts);
span->SetStatus(opentelemetry::trace::StatusCode::kOk, "Test Description");

constexpr uint8_t trace_id_buf[] = {1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8};
opentelemetry::trace::TraceId t_id(trace_id_buf);
constexpr uint8_t span_id_buf[] = {1, 2, 3, 4, 5, 6, 7, 8};
opentelemetry::trace::SpanId s_id(span_id_buf);

// A hacky solution to reset the IDs for perfect println.
// Ideally everything should be set through API.
auto* recordable = dynamic_cast<opentelemetry::sdk::trace::Span*>(span.get())->GetRecordablePtr().get();
recordable->SetIds(t_id, s_id, s_id);

opentelemetry::core::SystemTimestamp now(std::chrono::system_clock::now());

recordable->SetStartTime(now);
recordable->SetDuration(std::chrono::nanoseconds(100));
recordable->SetStatus(opentelemetry::trace::StatusCode::kOk, "Test Description");
recordable->SetSpanKind(opentelemetry::trace::SpanKind::kConsumer);

// Add some data to the span.
std::array<bool, 3> array2 = {false, true, false};
opentelemetry::nostd::span<bool> span2{array2.data(), array2.size()};
recordable->SetAttribute("attr1", span2);
span->SetAttribute("attr1", span2);

// Create stringstream to redirect to
std::stringstream stdcerrOutput;
Expand All @@ -189,7 +223,7 @@ TEST(OStreamSpanExporter, PrintChangedSpanCerr)
// Redirect cout to our stringstream buffer
std::cerr.rdbuf(stdcerrOutput.rdbuf());

processor->OnEnd(std::move(recordable));
span->End(endOpts);

std::cerr.rdbuf(sbuf);

Expand Down Expand Up @@ -218,29 +252,40 @@ TEST(OStreamSpanExporter, PrintChangedSpanClog)
{
auto exporter = std::unique_ptr<sdktrace::SpanExporter>(
new opentelemetry::exporter::trace::OStreamSpanExporter(std::clog));
auto processor = std::shared_ptr<sdktrace::SpanProcessor>(
auto processor = std::unique_ptr<sdktrace::SpanProcessor>(
new sdktrace::SimpleSpanProcessor(std::move(exporter)));
auto provider = nostd::shared_ptr<opentelemetry::trace::TracerProvider>(
new sdktrace::TracerProvider(std::move(processor)));

auto tracer = provider->GetTracer("test");

auto recordable = processor->MakeRecordable();

recordable->SetName("Test Span");
opentelemetry::core::SystemTimestamp now(std::chrono::system_clock::now());
opentelemetry::core::SteadyTimestamp one(std::chrono::nanoseconds(1));
opentelemetry::core::SteadyTimestamp onehundredone(std::chrono::nanoseconds(101));
opentelemetry::trace::StartSpanOptions startOpts;
startOpts.start_steady_time = one;
startOpts.start_system_time = now;
opentelemetry::trace::EndSpanOptions endOpts = { onehundredone };

auto span = tracer->StartSpan("Test Span", startOpts);

constexpr uint8_t trace_id_buf[] = {1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8};
opentelemetry::trace::TraceId t_id(trace_id_buf);
constexpr uint8_t span_id_buf[] = {1, 2, 3, 4, 5, 6, 7, 8};
opentelemetry::trace::SpanId s_id(span_id_buf);

recordable->SetIds(t_id, s_id, s_id);
opentelemetry::core::SystemTimestamp now(std::chrono::system_clock::now());

recordable->SetStartTime(now);
recordable->SetDuration(std::chrono::nanoseconds(100));
recordable->SetStatus(opentelemetry::trace::StatusCode::kOk, "Test Description");
// A hacky solution to reset the IDs for perfect println.
auto* recordable = dynamic_cast<opentelemetry::sdk::trace::Span*>(span.get())->GetRecordablePtr().get();
recordable->SetIds(t_id, s_id, s_id);
recordable->SetSpanKind(opentelemetry::trace::SpanKind::kInternal);

span->SetStatus(opentelemetry::trace::StatusCode::kOk, "Test Description");

std::array<int, 3> array1 = {1, 2, 3};
opentelemetry::nostd::span<int> span1{array1.data(), array1.size()};
recordable->SetAttribute("attr1", span1);
span->SetAttribute("attr1", span1);

// Create stringstream to redirect to
std::stringstream stdclogOutput;
Expand All @@ -251,7 +296,7 @@ TEST(OStreamSpanExporter, PrintChangedSpanClog)
// Redirect cout to our stringstream buffer
std::clog.rdbuf(stdclogOutput.rdbuf());

processor->OnEnd(std::move(recordable));
span->End(endOpts);

std::clog.rdbuf(sbuf);

Expand Down
4 changes: 2 additions & 2 deletions exporters/otlp/test/otlp_exporter_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ TEST_F(OtlpExporterTestPeer, ExportIntegrationTest)

auto exporter = GetExporter(stub_interface);

auto processor = std::shared_ptr<sdk::trace::SpanProcessor>(
auto processor = std::unique_ptr<sdk::trace::SpanProcessor>(
new sdk::trace::SimpleSpanProcessor(std::move(exporter)));
auto provider =
nostd::shared_ptr<trace::TracerProvider>(new sdk::trace::TracerProvider(processor));
nostd::shared_ptr<trace::TracerProvider>(new sdk::trace::TracerProvider(std::move(processor)));
auto tracer = provider->GetTracer("test");

EXPECT_CALL(*mock_stub, Export(_, _, _))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

#include "opentelemetry/ext/zpages/latency_boundaries.h"
#include "opentelemetry/ext/zpages/tracez_data.h"
#include "opentelemetry/ext/zpages/tracez_processor.h"
#include "opentelemetry/ext/zpages/tracez_shared_data.h"
#include "opentelemetry/nostd/span.h"
#include "opentelemetry/nostd/string_view.h"
#include "opentelemetry/sdk/trace/span_data.h"
Expand Down Expand Up @@ -46,10 +46,10 @@ class TracezDataAggregator
/**
* Constructor creates a thread that calls a function to aggregate span data
* at regular intervals.
* @param span_processor is the tracez span processor to be set
* @param shared_data is the tracez shared span data
* @param update_interval the time duration for updating the aggregated data.
*/
TracezDataAggregator(std::shared_ptr<TracezSpanProcessor> span_processor,
TracezDataAggregator(std::shared_ptr<TracezSharedData> shared_data,
milliseconds update_interval = milliseconds(10));

/** Ends the thread set up in the constructor and destroys the object **/
Expand Down Expand Up @@ -136,7 +136,7 @@ class TracezDataAggregator
ThreadsafeSpanData &span_data);

/** Instance of span processor used to collect raw data **/
std::shared_ptr<TracezSpanProcessor> tracez_span_processor_;
std::shared_ptr<TracezSharedData> tracez_shared_data_;

/**
* Tree map with key being the name of the span and value being a unique ptr
Expand Down
Loading