Skip to content

Commit

Permalink
Fix tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
jsuereth committed Mar 24, 2021
1 parent eacd67a commit 0356183
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 15 deletions.
9 changes: 5 additions & 4 deletions exporters/ostream/test/ostream_span_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "opentelemetry/trace/provider.h"

#include "opentelemetry/sdk/trace/exporter.h"
#include "opentelemetry/sdk/trace/span.h"

#include "opentelemetry/exporters/ostream/span_exporter.h"

Expand Down Expand Up @@ -72,7 +73,7 @@ TEST(OStreamSpanExporter, PrintDefaultSpan)
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();
auto& recordable = dynamic_cast<opentelemetry::sdk::trace::Span*>(span.get())->GetExportable();
recordable->SetIds(t_id, s_id, s_id);

// Create stringstream to redirect to
Expand Down Expand Up @@ -138,7 +139,7 @@ TEST(OStreamSpanExporter, PrintChangedSpanCout)

// 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();
auto* recordable = dynamic_cast<opentelemetry::sdk::trace::Span*>(span.get())->GetExportable().get();
recordable->SetIds(t_id, s_id, s_id);
recordable->SetSpanKind(opentelemetry::trace::SpanKind::kClient);

Expand Down Expand Up @@ -205,7 +206,7 @@ TEST(OStreamSpanExporter, PrintChangedSpanCerr)

// 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();
auto* recordable = dynamic_cast<opentelemetry::sdk::trace::Span*>(span.get())->GetExportable().get();
recordable->SetIds(t_id, s_id, s_id);
recordable->SetSpanKind(opentelemetry::trace::SpanKind::kConsumer);

Expand Down Expand Up @@ -277,7 +278,7 @@ TEST(OStreamSpanExporter, PrintChangedSpanClog)


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

Expand Down
9 changes: 5 additions & 4 deletions ext/include/opentelemetry/ext/zpages/tracez_processor.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include "opentelemetry/ext/zpages/threadsafe_span_data.h"
#include "opentelemetry/ext/zpages/tracez_shared_data.h"
#include "opentelemetry/sdk/trace/exportable_span.h"
#include "opentelemetry/sdk/trace/processor.h"
#include "opentelemetry/sdk/trace/recordable.h"

Expand All @@ -35,25 +36,25 @@ class TracezSpanProcessor : public opentelemetry::sdk::trace::SpanProcessor
* Create a span recordable, which is span_data
* @return a newly initialized recordable
*/
std::unique_ptr<opentelemetry::sdk::trace::Recordable> MakeRecordable() noexcept override
void RegisterRecordable(opentelemetry::sdk::trace::ExportableSpan& span) noexcept override
{
return std::unique_ptr<opentelemetry::sdk::trace::Recordable>(new ThreadsafeSpanData);
span.RegisterRecordableFor(*this, std::unique_ptr<opentelemetry::sdk::trace::Recordable>(new ThreadsafeSpanData));
}

/*
* OnStart is called when a span starts; the recordable is cast to span_data and added to
* running_spans.
* @param span a recordable for a span that was just started
*/
void OnStart(opentelemetry::sdk::trace::Span &span,
void OnStart(opentelemetry::sdk::trace::ExportableSpan &span,
const opentelemetry::trace::SpanContext &parent_context) noexcept override;

/*
* OnEnd is called when a span ends; that span_data is moved from running_spans to
* completed_spans
* @param span a recordable for a span that was ended
*/
void OnEnd(opentelemetry::sdk::trace::Span &span) noexcept override;
void OnEnd(std::unique_ptr<opentelemetry::sdk::trace::ExportableSpan> &&span) noexcept override;

/*
* For now, does nothing. In the future, it
Expand Down
9 changes: 4 additions & 5 deletions ext/src/zpages/tracez_processor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@ namespace ext
namespace zpages
{

void TracezSpanProcessor::OnStart(opentelemetry::sdk::trace::Span &span,
void TracezSpanProcessor::OnStart(opentelemetry::sdk::trace::ExportableSpan &span,
const opentelemetry::trace::SpanContext &parent_context) noexcept
{
shared_data_->OnStart(static_cast<ThreadsafeSpanData *>(span.GetRecordablePtr().get()));
shared_data_->OnStart(static_cast<ThreadsafeSpanData *>(span.GetRecordableFor(*this).get()));
}

void TracezSpanProcessor::OnEnd(
opentelemetry::sdk::trace::Span &span) noexcept
void TracezSpanProcessor::OnEnd(std::unique_ptr<opentelemetry::sdk::trace::ExportableSpan> &&span) noexcept
{
auto recordable = span.ConsumeRecordable();
auto recordable = span->ReleaseRecordableFor(*this);
if (recordable == nullptr)
return;
shared_data_->OnEnd(std::unique_ptr<ThreadsafeSpanData>(static_cast<ThreadsafeSpanData *>(recordable.release())));
Expand Down
2 changes: 0 additions & 2 deletions ext/test/zpages/tracez_processor_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,6 @@ class TracezProcessor : public ::testing::Test
*/
TEST_F(TracezProcessor, NoSpans)
{
auto recordable = processor->MakeRecordable();

EXPECT_EQ(running.size(), 0);
EXPECT_EQ(completed.size(), 0);
}
Expand Down
2 changes: 2 additions & 0 deletions sdk/include/opentelemetry/sdk/trace/exportable_span.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class ExportableSpan : public Recordable
* Releases ownership of the originally registered recordable.
*/
std::unique_ptr<Recordable> ReleaseRecordableFor(const SpanProcessor& processor) noexcept;
/** Access the recordable attached by a given processor. */
const std::unique_ptr<Recordable>& GetRecordableFor(const SpanProcessor& processor) const noexcept;


Tracer &GetTracer() { return *tracer_; }
Expand Down
2 changes: 2 additions & 0 deletions sdk/include/opentelemetry/sdk/trace/span.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ class Span final : public opentelemetry::trace::Span, public std::enable_shared_

trace_api::SpanContext GetContext() const noexcept override { return *span_context_.get(); }

// For Testing only
std::unique_ptr<ExportableSpan> &GetExportable() noexcept { return exportable_; }
private:
// Returns the recordable, or nullptr if not available.
Recordable* GetRecordable() const {
Expand Down
12 changes: 12 additions & 0 deletions sdk/src/trace/exportable_span.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@ std::unique_ptr<Recordable> ExportableSpan::ReleaseRecordableFor(const SpanProce
return std::unique_ptr<Recordable>(nullptr);
}

const std::unique_ptr<Recordable>& ExportableSpan::GetRecordableFor(const SpanProcessor& processor) const noexcept
{
// TODO - return nullptr ref on failed lookup?
static std::unique_ptr<Recordable> empty(nullptr);
auto i = recordables_.find(MakeKey(processor));
if (i != recordables_.end())
{
return i->second;
}
return empty;
}

void ExportableSpan::RegisterRecordableFor(const SpanProcessor& processor, std::unique_ptr<Recordable> recordable) noexcept
{
// TODO
Expand Down

0 comments on commit 0356183

Please sign in to comment.