Skip to content

Commit

Permalink
Implement in-memory metric exporter
Browse files Browse the repository at this point in the history
  • Loading branch information
punya committed Sep 5, 2024
1 parent 0d5a387 commit 48aa31a
Show file tree
Hide file tree
Showing 11 changed files with 551 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ Increment the:
* [REMOVAL] Remove build option `WITH_DEPRECATED_SDK_FACTORY`
[#2717](https://github.com/open-telemetry/opentelemetry-cpp/pull/2717)

* [EXPORTER] Add in-memory metric exporter
[#3043](https://github.com/open-telemetry/opentelemetry-cpp/pull/3043)

Breaking changes:

* [REMOVAL] Remove build option `WITH_DEPRECATED_SDK_FACTORY`
Expand Down
98 changes: 97 additions & 1 deletion exporters/memory/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,110 @@
package(default_visibility = ["//visibility:public"])

cc_library(
name = "in_memory_span_data",
name = "in_memory_metric_data",
srcs = [
"src/in_memory_metric_data.cc",
],
hdrs = [
"include/opentelemetry/exporters/memory/in_memory_metric_data.h",
],
strip_include_prefix = "include",
tags = [
"memory",
"test",
],
deps = [
":in_memory_data",
"//sdk/src/metrics",
],
)

cc_test(
name = "in_memory_metric_data_test",
srcs = ["test/in_memory_metric_data_test.cc"],
tags = [
"memory",
"test",
],
deps = [
":in_memory_metric_data",
"@com_google_googletest//:gtest_main",
],
)

cc_library(
name = "in_memory_metric_exporter",
srcs = [
"src/in_memory_metric_exporter.cc",
],
hdrs = [
"include/opentelemetry/exporters/memory/in_memory_metric_exporter.h",
],
strip_include_prefix = "include",
tags = [
"memory",
"test",
],
deps = [
":in_memory_metric_data",
"//sdk/src/metrics",
],
)

cc_library(
name = "in_memory_metric_exporter_factory",
srcs = [
"src/in_memory_metric_exporter_factory.cc",
],
hdrs = [
"include/opentelemetry/exporters/memory/in_memory_metric_exporter_factory.h",
],
strip_include_prefix = "include",
tags = [
"memory",
"test",
],
deps = [
":in_memory_metric_data",
":in_memory_metric_exporter",
"//sdk/src/metrics",
],
)

cc_test(
name = "in_memory_metric_exporter_test",
srcs = ["test/in_memory_metric_exporter_test.cc"],
tags = [
"memory",
"test",
],
deps = [
":in_memory_metric_exporter_factory",
"@com_google_googletest//:gtest_main",
],
)

cc_library(
name = "in_memory_data",
hdrs = [
"include/opentelemetry/exporters/memory/in_memory_data.h",
],
strip_include_prefix = "include",
tags = ["memory"],
deps = [
"//sdk:headers",
],
)

cc_library(
name = "in_memory_span_data",
hdrs = [
"include/opentelemetry/exporters/memory/in_memory_span_data.h",
],
strip_include_prefix = "include",
tags = ["memory"],
deps = [
":in_memory_data",
"//api",
"//sdk/src/resource",
"//sdk/src/trace",
Expand Down
39 changes: 39 additions & 0 deletions exporters/memory/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,27 @@ set_target_version(opentelemetry_exporter_in_memory)
target_link_libraries(opentelemetry_exporter_in_memory
PUBLIC opentelemetry_trace)

add_library(opentelemetry_exporter_in_memory_metric
src/in_memory_metric_exporter_factory.cc
src/in_memory_metric_exporter.cc
src/in_memory_metric_data.cc)

target_include_directories(
opentelemetry_exporter_in_memory_metric
PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>"
"$<INSTALL_INTERFACE:include>")

set_target_properties(opentelemetry_exporter_in_memory_metric
PROPERTIES EXPORT_NAME in_memory_metric_exporter)
set_target_version(opentelemetry_exporter_in_memory_metric)

target_link_libraries(opentelemetry_exporter_in_memory_metric
PUBLIC opentelemetry_metrics)

if(OPENTELEMETRY_INSTALL)
install(
TARGETS opentelemetry_exporter_in_memory
opentelemetry_exporter_in_memory_metric
EXPORT "${PROJECT_NAME}-target"
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
Expand All @@ -35,16 +53,29 @@ 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)
add_executable(in_memory_metric_data_test
test/in_memory_metric_data_test.cc)
add_executable(in_memory_metric_exporter_test
test/in_memory_metric_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_metric_data_test ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT}
opentelemetry_exporter_in_memory_metric opentelemetry_resources)

target_link_libraries(
in_memory_span_exporter_test ${GTEST_BOTH_LIBRARIES}
${CMAKE_THREAD_LIBS_INIT} opentelemetry_exporter_in_memory
opentelemetry_resources)

target_link_libraries(
in_memory_metric_exporter_test ${GTEST_BOTH_LIBRARIES}
${CMAKE_THREAD_LIBS_INIT} opentelemetry_exporter_in_memory_metric
opentelemetry_resources)

gtest_add_tests(
TARGET in_memory_span_data_test
TEST_PREFIX exporter.
Expand All @@ -53,4 +84,12 @@ if(BUILD_TESTING)
TARGET in_memory_span_exporter_test
TEST_PREFIX exporter.
TEST_LIST in_memory_span_exporter_test)
gtest_add_tests(
TARGET in_memory_metric_data_test
TEST_PREFIX exporter.
TEST_LIST in_memory_metric_data_test)
gtest_add_tests(
TARGET in_memory_metric_exporter_test
TEST_PREFIX exporter.
TEST_LIST in_memory_metric_exporter_test)
endif()
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include "opentelemetry/exporters/memory/in_memory_data.h"
#include "opentelemetry/sdk/metrics/export/metric_producer.h"

#include <vector>

OPENTELEMETRY_BEGIN_NAMESPACE
namespace exporter
{
namespace memory
{

/// The abstract base class for types used to store in-memory data backing an
/// InMemoryMetricExporter.
class InMemoryMetricData
{
public:
InMemoryMetricData(){};
InMemoryMetricData(const InMemoryMetricData &) = delete;
InMemoryMetricData(InMemoryMetricData &&) = delete;
InMemoryMetricData &operator=(const InMemoryMetricData &) = delete;
InMemoryMetricData &operator=(InMemoryMetricData &&) = delete;

virtual ~InMemoryMetricData() = default;

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

/// An implementation of InMemoryMetricData that stores full-fidelity data points in a circular
/// buffer. This allows tests to inspect every aspect of exported data, in exchange for a somewhat
/// cumbersome API.
class CircularBufferInMemoryMetricData final : public InMemoryMetricData,
public InMemoryData<sdk::metrics::ResourceMetrics>
{
public:
explicit CircularBufferInMemoryMetricData(size_t buffer_size);
void Add(std::unique_ptr<sdk::metrics::ResourceMetrics> resource_metrics) override;
};

/// An implementation of InMemoryMetricData that stores only the most recent data point in each time
/// series, and allows convenient lookups of time series. This makes simple tests easier to write.
class SimpleAggregateInMemoryMetricData final : public InMemoryMetricData
{
public:
using AttributeToPoint = std::map<opentelemetry::sdk::metrics::PointAttributes,
opentelemetry::sdk::metrics::PointType>;

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

private:
std::map<std::tuple<std::string, std::string>, AttributeToPoint> data_;
};

} // namespace memory
} // namespace exporter
OPENTELEMETRY_END_NAMESPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include <atomic>
#include <vector>

#include "opentelemetry/sdk/metrics/export/metric_producer.h"
#include "opentelemetry/sdk/metrics/instruments.h"
#include "opentelemetry/sdk/metrics/push_metric_exporter.h"
#include "opentelemetry/version.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace exporter
{
namespace memory
{

class InMemoryMetricData;

/// A Push Metric Exporter which accumulates metrics data in memory and allows it to be inspected.
/// It is not thread-safe.
class InMemoryMetricExporter final : public sdk::metrics::PushMetricExporter
{
using AggregationTemporalityMap =
std::map<sdk::metrics::InstrumentType, sdk::metrics::AggregationTemporality>;

public:
/// @param buffer_size a required value that sets the size of the CircularBuffer
/// @param temporality Output temporality as a function of instrument kind.
InMemoryMetricExporter(const std::shared_ptr<InMemoryMetricData> &data,
const sdk::metrics::AggregationTemporalitySelector &temporality);

InMemoryMetricExporter(const InMemoryMetricExporter &) = delete;
InMemoryMetricExporter(const InMemoryMetricExporter &&) = delete;
void operator=(const InMemoryMetricExporter &) = delete;
void operator=(const InMemoryMetricExporter &&) = delete;
~InMemoryMetricExporter() override = default;

sdk::common::ExportResult Export(const sdk::metrics::ResourceMetrics &data) noexcept override;
sdk::metrics::AggregationTemporality GetAggregationTemporality(
sdk::metrics::InstrumentType instrument_type) const noexcept override;
bool ForceFlush(
std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept override;
bool Shutdown(std::chrono::microseconds timeout = std::chrono::microseconds(0)) noexcept override;

private:
std::shared_ptr<InMemoryMetricData> data_;
std::atomic<bool> is_shutdown_{false};
sdk::metrics::AggregationTemporalitySelector temporality_;
};

} // namespace memory
} // namespace exporter
OPENTELEMETRY_END_NAMESPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include <atomic>
#include <vector>

#include "opentelemetry/exporters/memory/in_memory_data.h"
#include "opentelemetry/sdk/metrics/export/metric_producer.h"
#include "opentelemetry/sdk/metrics/instruments.h"
#include "opentelemetry/sdk/metrics/push_metric_exporter.h"
#include "opentelemetry/version.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace exporter
{
namespace memory
{

class InMemoryMetricData;

/// A factory for InMemoryMetricExporter
class InMemoryMetricExporterFactory
{
public:
/// Create a InMemoryMetricExporter with a default buffer size and aggregation
/// temporality selector.
/// @param [out] data the InMemoryMetricData the exporter will write to,
/// for the caller to inspect
/// @param [in] buffer_size number of entries to save in the circular buffer
/// @param [in] temporality output temporality as a function of instrument kind
static std::unique_ptr<opentelemetry::sdk::metrics::PushMetricExporter> Create(
const std::shared_ptr<InMemoryMetricData> &data,
const sdk::metrics::AggregationTemporalitySelector &temporality);

static std::unique_ptr<opentelemetry::sdk::metrics::PushMetricExporter> Create(
const std::shared_ptr<InMemoryMetricData> &data);
};

} // namespace memory
} // namespace exporter
OPENTELEMETRY_END_NAMESPACE
Loading

0 comments on commit 48aa31a

Please sign in to comment.