forked from open-telemetry/opentelemetry-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes open-telemetry#1405
- Loading branch information
Showing
6 changed files
with
277 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
exporters/memory/include/opentelemetry/exporters/memory/in_memory_metric_exporter.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// 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 | ||
{ | ||
|
||
using InMemoryMetricData = InMemoryData<sdk::metrics::ResourceMetrics>; | ||
|
||
/// 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(size_t buffer_size, | ||
const sdk::metrics::AggregationTemporalitySelector &temporality) | ||
: data_(std::make_shared<InMemoryMetricData>(buffer_size)), 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::shared_ptr<InMemoryMetricData> &GetData() const; | ||
|
||
private: | ||
std::shared_ptr<InMemoryMetricData> data_; | ||
std::atomic<bool> is_shutdown_{false}; | ||
sdk::metrics::AggregationTemporalitySelector temporality_; | ||
}; | ||
|
||
/// A factory for InMemoryMetricExporter | ||
class InMemoryMetricExporterFactory | ||
{ | ||
public: | ||
/// Create a InMemorySpanExporter with a default buffer size and aggregation | ||
/// temporality selector. | ||
/// @param [out] data the InMemorySpanData 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( | ||
std::shared_ptr<InMemoryMetricData> &data, | ||
size_t buffer_size = 100, | ||
const sdk::metrics::AggregationTemporalitySelector &temporality = [](auto) { | ||
return sdk::metrics::AggregationTemporality::kCumulative; | ||
}); | ||
}; | ||
|
||
} // namespace memory | ||
} // namespace exporter | ||
OPENTELEMETRY_END_NAMESPACE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// 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 opentelemetry::sdk::metrics::PushMetricExporter; | ||
using sdk::common::ExportResult; | ||
using sdk::metrics::AggregationTemporality; | ||
using sdk::metrics::AggregationTemporalitySelector; | ||
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_->Add(std::make_unique<ResourceMetrics>(data)); | ||
return ExportResult::kSuccess; | ||
} | ||
|
||
AggregationTemporality InMemoryMetricExporter::GetAggregationTemporality( | ||
InstrumentType instrument_type) const noexcept | ||
{ | ||
return temporality_(instrument_type); | ||
} | ||
|
||
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::shared_ptr<InMemoryMetricData> &InMemoryMetricExporter::GetData() const | ||
{ | ||
return data_; | ||
} | ||
|
||
std::unique_ptr<PushMetricExporter> InMemoryMetricExporterFactory::Create( | ||
std::shared_ptr<InMemoryMetricData> &data, | ||
size_t buffer_size, | ||
const AggregationTemporalitySelector &temporality) | ||
{ | ||
auto result = std::make_unique<InMemoryMetricExporter>(buffer_size, temporality); | ||
data = result->GetData(); | ||
return result; | ||
} | ||
|
||
} // namespace memory | ||
} // namespace exporter | ||
OPENTELEMETRY_END_NAMESPACE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#include "opentelemetry/exporters/memory/in_memory_metric_exporter.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::InMemoryMetricData; | ||
using opentelemetry::exporter::memory::InMemoryMetricExporter; | ||
using opentelemetry::exporter::memory::InMemoryMetricExporterFactory; | ||
using opentelemetry::sdk::common::ExportResult; | ||
using opentelemetry::sdk::metrics::AggregationTemporality; | ||
using opentelemetry::sdk::metrics::InstrumentType; | ||
using opentelemetry::sdk::metrics::PushMetricExporter; | ||
using opentelemetry::sdk::metrics::ResourceMetrics; | ||
using opentelemetry::sdk::metrics::ScopeMetrics; | ||
using opentelemetry::sdk::resource::Resource; | ||
|
||
class InMemoryMetricExporterTest : public ::testing::Test | ||
{ | ||
protected: | ||
InMemoryMetricExporterTest() { exporter_ = InMemoryMetricExporterFactory::Create(data_); } | ||
|
||
std::unique_ptr<PushMetricExporter> exporter_; | ||
std::shared_ptr<InMemoryMetricData> data_; | ||
|
||
Resource resource_ = Resource::GetEmpty(); | ||
ResourceMetrics resource_metrics_{&resource_, std::vector<ScopeMetrics>{}}; | ||
}; | ||
|
||
TEST_F(InMemoryMetricExporterTest, Export) | ||
{ | ||
EXPECT_EQ(exporter_->Export(resource_metrics_), ExportResult::kSuccess); | ||
|
||
auto data = data_->Get(); | ||
EXPECT_EQ(data.size(), 1); | ||
EXPECT_EQ((*data.begin())->resource_, &resource_); | ||
} | ||
|
||
TEST_F(InMemoryMetricExporterTest, ForceFlush) | ||
{ | ||
EXPECT_TRUE(exporter_->ForceFlush()); | ||
} | ||
|
||
TEST_F(InMemoryMetricExporterTest, Shutdown) | ||
{ | ||
EXPECT_TRUE(exporter_->Shutdown()); | ||
EXPECT_EQ(exporter_->Export(resource_metrics_), ExportResult::kFailure); | ||
} | ||
|
||
TEST_F(InMemoryMetricExporterTest, TemporalitySelector) | ||
{ | ||
EXPECT_EQ(exporter_->GetAggregationTemporality(InstrumentType::kCounter), | ||
AggregationTemporality::kCumulative); | ||
} |