Skip to content

Commit

Permalink
Use template class for in-memory data. (open-telemetry#1496)
Browse files Browse the repository at this point in the history
  • Loading branch information
yxue committed Dec 5, 2022
1 parent e8eafd9 commit b3ed81d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 55 deletions.
1 change: 1 addition & 0 deletions exporters/memory/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ cc_test(
cc_library(
name = "in_memory_span_data",
hdrs = [
"include/opentelemetry/exporters/memory/in_memory_data.h",
"include/opentelemetry/exporters/memory/in_memory_span_data.h",
],
strip_include_prefix = "include",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#pragma once

#include "opentelemetry/sdk/common/circular_buffer.h"
#include "opentelemetry/exporters/memory/in_memory_data.h"
#include "opentelemetry/sdk/trace/recordable.h"
#include "opentelemetry/sdk/trace/span_data.h"

Expand All @@ -14,60 +14,17 @@ namespace exporter
{
namespace memory
{
/**
* A wrapper class holding in memory exporter data
*/
class InMemorySpanData final
class InMemorySpanData final : public exporter::memory::InMemoryData<sdk::trace::SpanData>
{
public:
/**
* @param buffer_size a required value that sets the size of the CircularBuffer
*/
InMemorySpanData(size_t buffer_size) : spans_received_(buffer_size) {}
explicit InMemorySpanData(size_t buffer_size)
: exporter::memory::InMemoryData<sdk::trace::SpanData>(buffer_size)
{}

/**
* @param data a required unique pointer to the data to add to the CircularBuffer
*/
void Add(std::unique_ptr<opentelemetry::sdk::trace::SpanData> data) noexcept
{
std::unique_ptr<opentelemetry::sdk::trace::SpanData> span_data(
static_cast<opentelemetry::sdk::trace::SpanData *>(data.release()));
spans_received_.Add(span_data);
}

/**
* @return Returns a vector of unique pointers containing all the span data in the
* CircularBuffer. This operation will empty the Buffer, which is why the data
* is returned as unique pointers
*/
std::vector<std::unique_ptr<opentelemetry::sdk::trace::SpanData>> GetSpans() noexcept
{
std::vector<std::unique_ptr<opentelemetry::sdk::trace::SpanData>> res;

// Pointer swap is required because the Consume function requires that the
// AtomicUniquePointer be set to null
spans_received_.Consume(
spans_received_.size(),
[&](opentelemetry::sdk::common::CircularBufferRange<
opentelemetry::sdk::common::AtomicUniquePtr<opentelemetry::sdk::trace::SpanData>>
range) noexcept {
range.ForEach(
[&](opentelemetry::sdk::common::AtomicUniquePtr<opentelemetry::sdk::trace::SpanData>
&ptr) noexcept {
std::unique_ptr<opentelemetry::sdk::trace::SpanData> swap_ptr =
std::unique_ptr<opentelemetry::sdk::trace::SpanData>(nullptr);
ptr.Swap(swap_ptr);
res.push_back(
std::unique_ptr<opentelemetry::sdk::trace::SpanData>(swap_ptr.release()));
return true;
});
});

return res;
}

private:
opentelemetry::sdk::common::CircularBuffer<opentelemetry::sdk::trace::SpanData> spans_received_;
std::vector<std::unique_ptr<sdk::trace::SpanData>> GetSpans() noexcept { return Get(); }
};
} // namespace memory
} // namespace exporter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "opentelemetry/common/spin_lock_mutex.h"
#include "opentelemetry/exporters/memory/in_memory_span_data.h"
#include "opentelemetry/sdk/trace/exporter.h"
#include "opentelemetry/sdk/trace/span_data.h"
#include "opentelemetry/sdk_config.h"

OPENTELEMETRY_BEGIN_NAMESPACE
Expand All @@ -26,7 +27,7 @@ class InMemorySpanExporter final : public opentelemetry::sdk::trace::SpanExporte
* @param buffer_size an optional value that sets the size of the InMemorySpanData
*/
InMemorySpanExporter(size_t buffer_size = MAX_BUFFER_SIZE)
: data_(new opentelemetry::exporter::memory::InMemorySpanData(buffer_size))
: data_(new InMemorySpanData(buffer_size))
{}

/**
Expand Down Expand Up @@ -80,13 +81,10 @@ class InMemorySpanExporter final : public opentelemetry::sdk::trace::SpanExporte
/**
* @return Returns a shared pointer to this exporters InMemorySpanData
*/
std::shared_ptr<opentelemetry::exporter::memory::InMemorySpanData> GetData() noexcept
{
return data_;
}
std::shared_ptr<InMemorySpanData> GetData() noexcept { return data_; }

private:
std::shared_ptr<opentelemetry::exporter::memory::InMemorySpanData> data_;
std::shared_ptr<InMemorySpanData> data_;
bool is_shutdown_ = false;
mutable opentelemetry::common::SpinLockMutex lock_;
const bool isShutdown() const noexcept
Expand Down

0 comments on commit b3ed81d

Please sign in to comment.