-
Notifications
You must be signed in to change notification settings - Fork 559
Add the in-memory metrics exporter. #1481
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
28 changes: 28 additions & 0 deletions
28
exporters/memory/include/opentelemetry/exporters/memory/in_memory_metric_data.h
This file contains hidden or 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,28 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| #pragma once | ||
|
|
||
| #include "opentelemetry/exporters/memory/in_memory_data.h" | ||
| #include "opentelemetry/version.h" | ||
|
|
||
| OPENTELEMETRY_BEGIN_NAMESPACE | ||
| namespace exporter | ||
| { | ||
| namespace memory | ||
| { | ||
|
|
||
| class InMemoryMetricData final : public exporter::memory::InMemoryData<sdk::metrics::MetricData> | ||
| { | ||
| public: | ||
| /** | ||
| * @param buffer_size a required value that sets the size of the CircularBuffer | ||
| */ | ||
| explicit InMemoryMetricData(size_t buffer_size) | ||
| : exporter::memory::InMemoryData<sdk::metrics::MetricData>(buffer_size) | ||
| {} | ||
|
|
||
| std::vector<std::unique_ptr<sdk::metrics::MetricData>> GetMetrics() noexcept { return Get(); } | ||
| }; | ||
| } // namespace memory | ||
| } // namespace exporter |
63 changes: 63 additions & 0 deletions
63
exporters/memory/include/opentelemetry/exporters/memory/in_memory_metric_exporter.h
This file contains hidden or 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,63 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| #pragma once | ||
| #ifndef ENABLE_METRICS_PREVIEW | ||
|
|
||
| # include <memory> | ||
| # include <mutex> | ||
| # include "opentelemetry/common/spin_lock_mutex.h" | ||
| # include "opentelemetry/exporters/memory/in_memory_metric_data.h" | ||
| # include "opentelemetry/sdk/common/circular_buffer.h" | ||
| # include "opentelemetry/sdk/metrics/data/metric_data.h" | ||
| # include "opentelemetry/sdk/metrics/instruments.h" | ||
| # include "opentelemetry/sdk/metrics/metric_exporter.h" | ||
| # include "opentelemetry/version.h" | ||
|
|
||
| OPENTELEMETRY_BEGIN_NAMESPACE | ||
| namespace exporter | ||
| { | ||
| namespace metrics | ||
| { | ||
| const size_t MAX_BUFFER_SIZE = 100; | ||
|
|
||
| /** | ||
| * The in-memory metrics exporter exports metrics data to the memory. | ||
| * | ||
| */ | ||
| class InMemoryMetricExporter final : public opentelemetry::sdk::metrics::MetricExporter | ||
| { | ||
| public: | ||
| /** | ||
| * Create an InMemoryMetricExporter. The constructor takes the buffer size as the input and | ||
| * initializes the ring buffer with the given size. | ||
| * | ||
| */ | ||
| explicit InMemoryMetricExporter(size_t buffer_size = MAX_BUFFER_SIZE) | ||
| : data_(new InMemoryMetricData(buffer_size)) | ||
| {} | ||
|
|
||
| sdk::common::ExportResult Export(const sdk::metrics::ResourceMetrics &data) noexcept override; | ||
|
|
||
| bool ForceFlush( | ||
| std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept override; | ||
|
|
||
| bool Shutdown( | ||
| std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept override; | ||
|
|
||
| /** | ||
| * @return Returns a shared pointer to this exporters InMemoryMetricData. | ||
| */ | ||
| inline InMemoryMetricData &GetData() noexcept { return *data_; } | ||
|
|
||
| private: | ||
| bool isShutdown() const noexcept; | ||
| bool is_shutdown_ = false; | ||
| mutable opentelemetry::common::SpinLockMutex lock_; | ||
|
|
||
| std::unique_ptr<InMemoryMetricData> data_ = nullptr; | ||
| }; | ||
| } // namespace metrics | ||
| } // namespace exporter | ||
| OPENTELEMETRY_END_NAMESPACE | ||
| #endif | ||
This file contains hidden or 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,54 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| #ifndef ENABLE_METRICS_PREVIEW | ||
|
|
||
| # include "opentelemetry/exporters/memory/in_memory_metric_exporter.h" | ||
| # include "opentelemetry/exporters/otlp/otlp_metric_utils.h" | ||
| # include "opentelemetry/sdk_config.h" | ||
|
|
||
| OPENTELEMETRY_BEGIN_NAMESPACE | ||
| namespace exporter | ||
| { | ||
| namespace metrics | ||
| { | ||
|
|
||
| sdk::common::ExportResult InMemoryMetricExporter::Export( | ||
| const sdk::metrics::ResourceMetrics &data) noexcept | ||
| { | ||
| if (isShutdown()) | ||
| { | ||
| OTEL_INTERNAL_LOG_ERROR("[InMemory Metric] Exporting " | ||
| << data.instrumentation_info_metric_data_.size() | ||
| << " records(s) failed, exporter is shutdown"); | ||
| return sdk::common::ExportResult::kFailure; | ||
| } | ||
|
|
||
| std::unique_ptr<proto::metrics::v1::ResourceMetrics> resource_metrics( | ||
| new proto::metrics::v1::ResourceMetrics); | ||
| otlp::OtlpMetricUtils::PopulateResourceMetrics(data, resource_metrics.get()); | ||
| data_->Add(std::move(resource_metrics)); | ||
| return sdk::common::ExportResult::kSuccess; | ||
| } | ||
|
|
||
| bool InMemoryMetricExporter::ForceFlush(std::chrono::microseconds timeout) noexcept | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| bool InMemoryMetricExporter::Shutdown(std::chrono::microseconds timeout) noexcept | ||
| { | ||
| const std::lock_guard<opentelemetry::common::SpinLockMutex> locked(lock_); | ||
| is_shutdown_ = true; | ||
| return true; | ||
| } | ||
|
|
||
| bool InMemoryMetricExporter::isShutdown() const noexcept | ||
| { | ||
| const std::lock_guard<opentelemetry::common::SpinLockMutex> locked(lock_); | ||
| return is_shutdown_; | ||
| } | ||
| } // namespace metrics | ||
| } // namespace exporter | ||
| OPENTELEMETRY_END_NAMESPACE | ||
| #endif |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment is outdated, this does not return a shared pointer.