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 1, 2024
1 parent a920898 commit b7a73b4
Show file tree
Hide file tree
Showing 5 changed files with 227 additions and 0 deletions.
31 changes: 31 additions & 0 deletions exporters/memory/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,37 @@

package(default_visibility = ["//visibility:public"])

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 = [
"//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",
"@com_google_googletest//:gtest_main",
],
)

cc_library(
name = "in_memory_span_data",
hdrs = [
Expand Down
27 changes: 27 additions & 0 deletions exporters/memory/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,25 @@ 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.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
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,6 +51,8 @@ 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_exporter_test
test/in_memory_metric_exporter_test.cc)

target_link_libraries(
in_memory_span_data_test ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT}
Expand All @@ -45,6 +63,11 @@ if(BUILD_TESTING)
${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 +76,8 @@ 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_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,51 @@
// 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/push_metric_exporter.h"
#include "opentelemetry/version.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace exporter
{
namespace memory
{

class InMemoryMetricExporter final : public sdk::metrics::PushMetricExporter
{
using AggregationTemporalityMap =
std::map<sdk::metrics::InstrumentType, sdk::metrics::AggregationTemporality>;

public:
InMemoryMetricExporter(const AggregationTemporalityMap &temporality = {})
: temporality_(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;

const std::vector<sdk::metrics::ResourceMetrics> &GetData() const;

private:
std::vector<sdk::metrics::ResourceMetrics> data_{};
std::atomic<bool> is_shutdown_{false};
AggregationTemporalityMap temporality_;
};

} // namespace memory
} // namespace exporter
OPENTELEMETRY_END_NAMESPACE
56 changes: 56 additions & 0 deletions exporters/memory/src/in_memory_metric_exporter.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#include "opentelemetry/exporters/memory/in_memory_metric_exporter.h"
#include "opentelemetry/sdk/common/global_log_handler.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace exporter
{
namespace memory
{

using sdk::common::ExportResult;
using sdk::metrics::AggregationTemporality;
using sdk::metrics::InstrumentType;
using sdk::metrics::ResourceMetrics;

ExportResult InMemoryMetricExporter::Export(const ResourceMetrics &data) noexcept
{
if (is_shutdown_)
{
OTEL_INTERNAL_LOG_ERROR("[In Memory Metric Exporter] Exporting failed, exporter is shutdown");
return ExportResult::kFailure;
}
data_.push_back(data);
return ExportResult::kSuccess;
}

AggregationTemporality InMemoryMetricExporter::GetAggregationTemporality(
InstrumentType instrument_type) const noexcept
{
auto result = temporality_.find(instrument_type);
return result == std::end(temporality_) //
? AggregationTemporality::kCumulative
: result->second;
}

bool InMemoryMetricExporter::ForceFlush(std::chrono::microseconds /* timeout */) noexcept
{
return true;
}

bool InMemoryMetricExporter::Shutdown(std::chrono::microseconds /* timeout */) noexcept
{
is_shutdown_ = true;
return true;
}

const std::vector<ResourceMetrics> &InMemoryMetricExporter::GetData() const
{
return data_;
}

} // namespace memory
} // namespace exporter
OPENTELEMETRY_END_NAMESPACE
62 changes: 62 additions & 0 deletions exporters/memory/test/in_memory_metric_exporter_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#include "opentelemetry/exporters/memory/in_memory_metric_exporter.h"
#include "opentelemetry/sdk/instrumentationscope/instrumentation_scope.h"
#include "opentelemetry/sdk/metrics/data/metric_data.h"
#include "opentelemetry/sdk/metrics/export/metric_producer.h"
#include "opentelemetry/sdk/metrics/instruments.h"
#include "opentelemetry/sdk/resource/resource.h"

#include <gtest/gtest.h>

using opentelemetry::exporter::memory::InMemoryMetricExporter;
using opentelemetry::sdk::instrumentationscope::InstrumentationScope;
using opentelemetry::sdk::metrics::AggregationTemporality;
using opentelemetry::sdk::metrics::InstrumentType;
using opentelemetry::sdk::metrics::InstrumentValueType;
using opentelemetry::sdk::metrics::MetricData;
using opentelemetry::sdk::metrics::ResourceMetrics;
using opentelemetry::sdk::metrics::ScopeMetrics;
using opentelemetry::sdk::metrics::SumPointData;
using opentelemetry::sdk::resource::Resource;

TEST(InMemoryMetricExporter, Instantiate)
{
InMemoryMetricExporter exporter;

auto &resource = Resource::GetEmpty();
auto instrumentation_scope = InstrumentationScope::Create("test");
MetricData metric_data{
.instrument_descriptor{
.name_ = "test-metric",
.description_ = "test-description",
.unit_ = "test-unit",
.type_ = InstrumentType::kCounter,
.value_type_ = InstrumentValueType::kLong,
},
.aggregation_temporality = AggregationTemporality::kCumulative,
.start_ts{},
.end_ts{},
.point_data_attr_{{
.attributes = {{"hello", "world"}},
.point_data =
SumPointData{
.value_ = 10ll,
.is_monotonic_ = true,
},
}},
};
ResourceMetrics resource_metrics{
&resource,
std::vector<ScopeMetrics>{{
instrumentation_scope.get(),
std::vector<MetricData>{metric_data},
}},
};
exporter.Export(resource_metrics);

auto &data = exporter.GetData();
EXPECT_EQ(data.size(), 1);
EXPECT_EQ(data.begin()->resource_, &resource);
}

0 comments on commit b7a73b4

Please sign in to comment.