diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-21 11:54:28 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-21 11:54:28 +0000 |
commit | e6918187568dbd01842d8d1d2c808ce16a894239 (patch) | |
tree | 64f88b554b444a49f656b6c656111a145cbbaa28 /src/jaegertracing/opentelemetry-cpp/exporters/memory | |
parent | Initial commit. (diff) | |
download | ceph-e6918187568dbd01842d8d1d2c808ce16a894239.tar.xz ceph-e6918187568dbd01842d8d1d2c808ce16a894239.zip |
Adding upstream version 18.2.2.upstream/18.2.2
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/jaegertracing/opentelemetry-cpp/exporters/memory')
6 files changed, 334 insertions, 0 deletions
diff --git a/src/jaegertracing/opentelemetry-cpp/exporters/memory/BUILD b/src/jaegertracing/opentelemetry-cpp/exporters/memory/BUILD new file mode 100644 index 000000000..b2dd48347 --- /dev/null +++ b/src/jaegertracing/opentelemetry-cpp/exporters/memory/BUILD @@ -0,0 +1,57 @@ +package(default_visibility = ["//visibility:public"]) + +cc_library( + name = "in_memory_span_data", + hdrs = [ + "include/opentelemetry/exporters/memory/in_memory_span_data.h", + ], + strip_include_prefix = "include", + tags = ["memory"], + deps = [ + "//api", + "//sdk/src/resource", + "//sdk/src/trace", + ], +) + +cc_test( + name = "in_memory_span_data_test", + srcs = ["test/in_memory_span_data_test.cc"], + tags = [ + "memory", + "test", + ], + deps = [ + ":in_memory_span_data", + "@com_google_googletest//:gtest_main", + ], +) + +cc_library( + name = "in_memory_span_exporter", + hdrs = [ + "include/opentelemetry/exporters/memory/in_memory_span_exporter.h", + ], + strip_include_prefix = "include", + tags = [ + "memory", + "test", + ], + deps = [ + ":in_memory_span_data", + "//sdk/src/trace", + ], +) + +cc_test( + name = "in_memory_span_exporter_test", + srcs = ["test/in_memory_span_exporter_test.cc"], + tags = [ + "memory", + "test", + ], + deps = [ + ":in_memory_span_exporter", + "@com_google_googletest//:gtest_main", + ], +) diff --git a/src/jaegertracing/opentelemetry-cpp/exporters/memory/CMakeLists.txt b/src/jaegertracing/opentelemetry-cpp/exporters/memory/CMakeLists.txt new file mode 100644 index 000000000..d489ec160 --- /dev/null +++ b/src/jaegertracing/opentelemetry-cpp/exporters/memory/CMakeLists.txt @@ -0,0 +1,46 @@ +add_library(opentelemetry_exporter_in_memory INTERFACE) + +target_include_directories( + opentelemetry_exporter_in_memory + INTERFACE "$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>" + "$<INSTALL_INTERFACE:include>") + +set_target_properties(opentelemetry_exporter_in_memory + PROPERTIES EXPORT_NAME in_memory_span_exporter) + +install( + TARGETS opentelemetry_exporter_in_memory + EXPORT "${PROJECT_NAME}-target" + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}) + +install( + DIRECTORY include/opentelemetry/exporters/memory + DESTINATION include/opentelemetry/exporters + FILES_MATCHING + PATTERN "*.h") + +if(BUILD_TESTING) + add_executable(in_memory_span_data_test test/in_memory_span_data_test.cc) + add_executable(in_memory_span_exporter_test + test/in_memory_span_exporter_test.cc) + + target_link_libraries( + in_memory_span_data_test ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} + opentelemetry_exporter_in_memory opentelemetry_resources) + + target_link_libraries( + in_memory_span_exporter_test ${GTEST_BOTH_LIBRARIES} + ${CMAKE_THREAD_LIBS_INIT} opentelemetry_exporter_in_memory + opentelemetry_resources) + + gtest_add_tests( + TARGET in_memory_span_data_test + TEST_PREFIX exporter. + TEST_LIST in_memory_span_data_test) + gtest_add_tests( + TARGET in_memory_span_exporter_test + TEST_PREFIX exporter. + TEST_LIST in_memory_span_exporter_test) +endif() diff --git a/src/jaegertracing/opentelemetry-cpp/exporters/memory/include/opentelemetry/exporters/memory/in_memory_span_data.h b/src/jaegertracing/opentelemetry-cpp/exporters/memory/include/opentelemetry/exporters/memory/in_memory_span_data.h new file mode 100644 index 000000000..df6d377b9 --- /dev/null +++ b/src/jaegertracing/opentelemetry-cpp/exporters/memory/include/opentelemetry/exporters/memory/in_memory_span_data.h @@ -0,0 +1,74 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once + +#include "opentelemetry/sdk/common/circular_buffer.h" +#include "opentelemetry/sdk/trace/recordable.h" +#include "opentelemetry/sdk/trace/span_data.h" + +#include <vector> + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace exporter +{ +namespace memory +{ +/** + * A wrapper class holding in memory exporter data + */ +class InMemorySpanData final +{ +public: + /** + * @param buffer_size a required value that sets the size of the CircularBuffer + */ + InMemorySpanData(size_t buffer_size) : spans_received_(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_; +}; +} // namespace memory +} // namespace exporter +OPENTELEMETRY_END_NAMESPACE diff --git a/src/jaegertracing/opentelemetry-cpp/exporters/memory/include/opentelemetry/exporters/memory/in_memory_span_exporter.h b/src/jaegertracing/opentelemetry-cpp/exporters/memory/include/opentelemetry/exporters/memory/in_memory_span_exporter.h new file mode 100644 index 000000000..28b7bc34e --- /dev/null +++ b/src/jaegertracing/opentelemetry-cpp/exporters/memory/include/opentelemetry/exporters/memory/in_memory_span_exporter.h @@ -0,0 +1,100 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once +#include <mutex> +#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_config.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace exporter +{ +namespace memory +{ +const size_t MAX_BUFFER_SIZE = 100; + +/** + * A in memory exporter that switches a flag once a valid recordable was received + * and keeps track of all received spans in memory. + */ +class InMemorySpanExporter final : public opentelemetry::sdk::trace::SpanExporter +{ +public: + /** + * @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)) + {} + + /** + * @return Returns a unique pointer to an empty recordable object + */ + std::unique_ptr<sdk::trace::Recordable> MakeRecordable() noexcept override + { + return std::unique_ptr<sdk::trace::Recordable>(new sdk::trace::SpanData()); + } + + /** + * @param recordables a required span containing unique pointers to the data + * to add to the InMemorySpanData + * @return Returns the result of the operation + */ + sdk::common::ExportResult Export( + const nostd::span<std::unique_ptr<sdk::trace::Recordable>> &recordables) noexcept override + { + if (isShutdown()) + { + OTEL_INTERNAL_LOG_ERROR("[In Memory Span Exporter] Exporting " + << recordables.size() << " span(s) failed, exporter is shutdown"); + return sdk::common::ExportResult::kFailure; + } + for (auto &recordable : recordables) + { + auto span = std::unique_ptr<sdk::trace::SpanData>( + static_cast<sdk::trace::SpanData *>(recordable.release())); + if (span != nullptr) + { + data_->Add(std::move(span)); + } + } + + return sdk::common::ExportResult::kSuccess; + } + + /** + * @param timeout an optional value containing the timeout of the exporter + * note: passing custom timeout values is not currently supported for this exporter + * @return Returns the status of the operation + */ + bool Shutdown( + std::chrono::microseconds timeout = std::chrono::microseconds::max()) noexcept override + { + const std::lock_guard<opentelemetry::common::SpinLockMutex> locked(lock_); + is_shutdown_ = true; + return true; + }; + + /** + * @return Returns a shared pointer to this exporters InMemorySpanData + */ + std::shared_ptr<opentelemetry::exporter::memory::InMemorySpanData> GetData() noexcept + { + return data_; + } + +private: + std::shared_ptr<opentelemetry::exporter::memory::InMemorySpanData> data_; + bool is_shutdown_ = false; + mutable opentelemetry::common::SpinLockMutex lock_; + const bool isShutdown() const noexcept + { + const std::lock_guard<opentelemetry::common::SpinLockMutex> locked(lock_); + return is_shutdown_; + } +}; +} // namespace memory +} // namespace exporter +OPENTELEMETRY_END_NAMESPACE diff --git a/src/jaegertracing/opentelemetry-cpp/exporters/memory/test/in_memory_span_data_test.cc b/src/jaegertracing/opentelemetry-cpp/exporters/memory/test/in_memory_span_data_test.cc new file mode 100644 index 000000000..bc1955026 --- /dev/null +++ b/src/jaegertracing/opentelemetry-cpp/exporters/memory/test/in_memory_span_data_test.cc @@ -0,0 +1,28 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#include "opentelemetry/exporters/memory/in_memory_span_data.h" +#include "opentelemetry/nostd/span.h" +#include "opentelemetry/sdk/trace/span_data.h" + +#include <gtest/gtest.h> + +using opentelemetry::exporter::memory::InMemorySpanData; +using opentelemetry::sdk::trace::Recordable; +using opentelemetry::sdk::trace::SpanData; + +TEST(InMemorySpanData, AddRecordable) +{ + InMemorySpanData data(100); + + ASSERT_EQ(0, data.GetSpans().size()); + + std::unique_ptr<SpanData> spandata(new SpanData()); + + data.Add(std::move(spandata)); + + // Consumes all spans in exporter + ASSERT_EQ(1, data.GetSpans().size()); + + ASSERT_EQ(0, data.GetSpans().size()); +} diff --git a/src/jaegertracing/opentelemetry-cpp/exporters/memory/test/in_memory_span_exporter_test.cc b/src/jaegertracing/opentelemetry-cpp/exporters/memory/test/in_memory_span_exporter_test.cc new file mode 100644 index 000000000..56a0ef779 --- /dev/null +++ b/src/jaegertracing/opentelemetry-cpp/exporters/memory/test/in_memory_span_exporter_test.cc @@ -0,0 +1,29 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#include "opentelemetry/exporters/memory/in_memory_span_exporter.h" +#include "opentelemetry/nostd/span.h" +#include "opentelemetry/sdk/trace/span_data.h" + +#include <gtest/gtest.h> + +using opentelemetry::exporter::memory::InMemorySpanExporter; +using opentelemetry::sdk::trace::Recordable; +using opentelemetry::sdk::trace::SpanData; + +TEST(InMemorySpanExporter, ExportBatch) +{ + InMemorySpanExporter exporter; + + ASSERT_EQ(0, exporter.GetData().get()->GetSpans().size()); + + std::unique_ptr<Recordable> spandata(new SpanData()); + opentelemetry::nostd::span<std::unique_ptr<Recordable>> batch(&spandata, 1); + + exporter.Export(batch); + + ASSERT_EQ(1, exporter.GetData().get()->GetSpans().size()); + + // Consumes all spans in exporter + ASSERT_EQ(0, exporter.GetData().get()->GetSpans().size()); +} |