Skip to content

Commit

Permalink
Change data type in InMemoryMetricData to nostd::unique_ptr
Browse files Browse the repository at this point in the history
  • Loading branch information
owent committed Oct 14, 2024
1 parent 9218c4a commit fa463ee
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,24 @@ class InMemoryData
/**
* @param data a required unique pointer to the data to add to the CircularBuffer
*/
void Add(std::unique_ptr<T> data) noexcept { data_.Add(data); }
void Add(nostd::unique_ptr<T> data) noexcept { data_.Add(data); }

/**
* @return Returns a vector of unique pointers containing all the 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<T>> Get() noexcept
std::vector<nostd::unique_ptr<T>> Get() noexcept
{
std::vector<std::unique_ptr<T>> res;
std::vector<nostd::unique_ptr<T>> res;

// Pointer swap is required because the Consume function requires that the
// AtomicUniquePointer be set to null
data_.Consume(
data_.size(), [&](opentelemetry::sdk::common::CircularBufferRange<
opentelemetry::sdk::common::AtomicUniquePtr<T>> range) noexcept {
range.ForEach([&](opentelemetry::sdk::common::AtomicUniquePtr<T> &ptr) noexcept {
std::unique_ptr<T> swap_ptr = nullptr;
nostd::unique_ptr<T> swap_ptr = nullptr;
ptr.Swap(swap_ptr);
res.push_back(std::move(swap_ptr));
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <tuple>

#include "opentelemetry/exporters/memory/in_memory_data.h"
#include "opentelemetry/nostd/unique_ptr.h"
#include "opentelemetry/sdk/metrics/data/metric_data.h"

OPENTELEMETRY_BEGIN_NAMESPACE
Expand Down Expand Up @@ -37,7 +38,7 @@ class InMemoryMetricData
InMemoryMetricData &operator=(const InMemoryMetricData &) = delete;
InMemoryMetricData &operator=(InMemoryMetricData &&) = delete;

virtual void Add(std::unique_ptr<sdk::metrics::ResourceMetrics> resource_metrics) = 0;
virtual void Add(nostd::unique_ptr<sdk::metrics::ResourceMetrics> resource_metrics) = 0;
};

/// An implementation of InMemoryMetricData that stores full-fidelity data points in a circular
Expand All @@ -48,7 +49,7 @@ class CircularBufferInMemoryMetricData final : public InMemoryMetricData,
{
public:
explicit CircularBufferInMemoryMetricData(size_t buffer_size);
void Add(std::unique_ptr<sdk::metrics::ResourceMetrics> resource_metrics) override;
void Add(nostd::unique_ptr<sdk::metrics::ResourceMetrics> resource_metrics) override;
};

/// An implementation of InMemoryMetricData that stores only the most recent data point in each time
Expand All @@ -59,7 +60,7 @@ class SimpleAggregateInMemoryMetricData final : public InMemoryMetricData
using AttributeToPoint = std::map<opentelemetry::sdk::metrics::PointAttributes,
opentelemetry::sdk::metrics::PointType>;

void Add(std::unique_ptr<sdk::metrics::ResourceMetrics> resource_metrics) override;
void Add(nostd::unique_ptr<sdk::metrics::ResourceMetrics> resource_metrics) override;
const AttributeToPoint &Get(const std::string &scope, const std::string &metric);
void Clear();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class InMemorySpanData final : public exporter::memory::InMemoryData<sdk::trace:
: exporter::memory::InMemoryData<sdk::trace::SpanData>(buffer_size)
{}

std::vector<std::unique_ptr<sdk::trace::SpanData>> GetSpans() noexcept { return Get(); }
std::vector<nostd::unique_ptr<sdk::trace::SpanData>> GetSpans() noexcept { return Get(); }
};
} // namespace memory
} // namespace exporter
Expand Down
4 changes: 2 additions & 2 deletions exporters/memory/src/in_memory_metric_data.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ CircularBufferInMemoryMetricData::CircularBufferInMemoryMetricData(size_t buffer
: InMemoryData(buffer_size)
{}

void CircularBufferInMemoryMetricData::Add(std::unique_ptr<ResourceMetrics> resource_metrics)
void CircularBufferInMemoryMetricData::Add(nostd::unique_ptr<ResourceMetrics> resource_metrics)
{
InMemoryData::Add(std::move(resource_metrics));
}

void SimpleAggregateInMemoryMetricData::Add(std::unique_ptr<ResourceMetrics> resource_metrics)
void SimpleAggregateInMemoryMetricData::Add(nostd::unique_ptr<ResourceMetrics> resource_metrics)
{
for (const auto &sm : resource_metrics->scope_metric_data_)
{
Expand Down
2 changes: 1 addition & 1 deletion exporters/memory/src/in_memory_metric_exporter_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ std::unique_ptr<PushMetricExporter> InMemoryMetricExporterFactory::Create(
const std::shared_ptr<InMemoryMetricData> &data,
const AggregationTemporalitySelector &temporality)
{
return opentelemetry::nostd::make_unique<InMemoryMetricExporter>(data, temporality);
return std::unique_ptr<InMemoryMetricExporter>(new InMemoryMetricExporter{data, temporality});
}

} // namespace memory
Expand Down
32 changes: 32 additions & 0 deletions sdk/include/opentelemetry/sdk/common/atomic_unique_ptr.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <atomic>
#include <memory>

#include "opentelemetry/nostd/unique_ptr.h"
#include "opentelemetry/version.h"

OPENTELEMETRY_BEGIN_NAMESPACE
Expand All @@ -25,6 +26,10 @@ class AtomicUniquePtr

explicit AtomicUniquePtr(std::unique_ptr<T> &&other) noexcept : ptr_(other.release()) {}

#if !defined(OPENTELEMETRY_HAVE_STD_UNIQUE_PTR)
explicit AtomicUniquePtr(nostd::unique_ptr<T> &&other) noexcept : ptr_(other.release()) {}
#endif

~AtomicUniquePtr() noexcept { Reset(); }

T &operator*() const noexcept { return *Get(); }
Expand Down Expand Up @@ -66,6 +71,33 @@ class AtomicUniquePtr
*/
void Swap(std::unique_ptr<T> &other) noexcept { other.reset(ptr_.exchange(other.release())); }

#if !defined(OPENTELEMETRY_HAVE_STD_UNIQUE_PTR)
/**
* Atomically swap the pointer only if it's null.
* @param owner the pointer to swap with
* @return true if the swap was successful
*/
bool SwapIfNull(nostd::unique_ptr<T> &owner) noexcept
{
auto ptr = owner.get();
T *expected = nullptr;
auto was_successful = ptr_.compare_exchange_weak(expected, ptr, std::memory_order_release,
std::memory_order_relaxed);
if (was_successful)
{
owner.release();
return true;
}
return false;
}

/**
* Atomically swap the pointer with another.
* @param ptr the pointer to swap with
*/
void Swap(nostd::unique_ptr<T> &other) noexcept { other.reset(ptr_.exchange(other.release())); }
#endif

/**
* Set the pointer to a new value and delete the current value if non-null.
* @param ptr the new pointer value to set
Expand Down

0 comments on commit fa463ee

Please sign in to comment.