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
5 changed files
with
175 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
48 changes: 48 additions & 0 deletions
48
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,48 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#pragma once | ||
|
||
#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 |
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,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 sdk::common::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 |
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,13 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#include "opentelemetry/exporters/memory/in_memory_metric_exporter.h" | ||
|
||
#include <gtest/gtest.h> | ||
|
||
using opentelemetry::exporter::memory::InMemoryMetricExporter; | ||
|
||
TEST(InMemoryMetricExporter, Instantiate) | ||
{ | ||
InMemoryMetricExporter exporter; | ||
} |