-
Notifications
You must be signed in to change notification settings - Fork 558
Implement periodic exporting metric reader #1286
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
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
8bbc035
add periodic exporting metric reader
lalitb 0023a37
fix class name
lalitb 4a2f69c
fix build
lalitb 0b0a265
Merge branch 'main' into periodic-exporting-reader
lalitb bcce46b
Merge branch 'main' into periodic-exporting-reader
lalitb 81c65b5
tests
lalitb 45d9b52
fix test
lalitb 4ad78cb
Merge branch 'main' into periodic-exporting-reader
lalitb 3e60d96
review comment
lalitb 53cdf0f
Merge branch 'periodic-exporting-reader' of github.com:lalitb/opentel…
lalitb 1ef7d66
comment metrics ostream exporter
lalitb db2a182
Merge branch 'main' into periodic-exporting-reader
lalitb 00337e5
fix review comments
lalitb 405b0c0
fix comment
lalitb 582de6b
review comment - remove unnecessart lock
lalitb 195fe0c
Merge branch 'main' into periodic-exporting-reader
lalitb 144d5a2
conflict fix
lalitb 076ee2d
Merge branch 'main' into periodic-exporting-reader
ThomsonTan 3bbe566
Update sdk/include/opentelemetry/sdk/metrics/export/periodic_exportin…
lalitb ca0bb8c
format
lalitb c1c22b0
Merge branch 'main' into periodic-exporting-reader
ThomsonTan 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
72 changes: 72 additions & 0 deletions
72
sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader.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,72 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| #pragma once | ||
| #ifndef ENABLE_METRICS_PREVIEW | ||
|
|
||
| # include "opentelemetry/sdk/metrics/metric_reader.h" | ||
| # include "opentelemetry/version.h" | ||
|
|
||
| # include <atomic> | ||
| # include <chrono> | ||
| # include <condition_variable> | ||
| # include <thread> | ||
|
|
||
| OPENTELEMETRY_BEGIN_NAMESPACE | ||
| namespace sdk | ||
| { | ||
| namespace metrics | ||
| { | ||
|
|
||
| class MetricExporter; | ||
| /** | ||
| * Struct to hold PeriodicExortingMetricReader options. | ||
| */ | ||
|
|
||
| constexpr std::chrono::milliseconds kExportIntervalMillis = std::chrono::milliseconds(60000); | ||
| constexpr std::chrono::milliseconds kExportTimeOutMillis = std::chrono::milliseconds(30000); | ||
| struct PeriodicExportingMetricReaderOptions | ||
| { | ||
|
|
||
| /* The time interval between two consecutive exports. */ | ||
| std::chrono::milliseconds export_interval_millis = | ||
| std::chrono::milliseconds(kExportIntervalMillis); | ||
|
|
||
| /* how long the export can run before it is cancelled. */ | ||
| std::chrono::milliseconds export_timeout_millis = std::chrono::milliseconds(kExportTimeOutMillis); | ||
| }; | ||
|
|
||
| class PeriodicExportingMetricReader : public MetricReader | ||
| { | ||
|
|
||
| public: | ||
| PeriodicExportingMetricReader( | ||
| std::unique_ptr<MetricExporter> exporter, | ||
| const PeriodicExportingMetricReaderOptions &option, | ||
| AggregationTemporality aggregation_temporality = AggregationTemporality::kCumulative); | ||
|
|
||
| private: | ||
| bool OnForceFlush(std::chrono::microseconds timeout) noexcept override; | ||
|
|
||
| bool OnShutDown(std::chrono::microseconds timeout) noexcept override; | ||
|
|
||
| void OnInitialized() noexcept override; | ||
|
|
||
| std::unique_ptr<MetricExporter> exporter_; | ||
| std::chrono::milliseconds export_interval_millis_; | ||
| std::chrono::milliseconds export_timeout_millis_; | ||
|
|
||
| void DoBackgroundWork(); | ||
|
|
||
| /* The background worker thread */ | ||
| std::thread worker_thread_; | ||
|
|
||
| /* Synchronization primitives */ | ||
| std::condition_variable cv_; | ||
| std::mutex cv_m_; | ||
| }; | ||
|
|
||
| } // namespace metrics | ||
| } // namespace sdk | ||
| 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
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
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
101 changes: 101 additions & 0 deletions
101
sdk/src/metrics/export/periodic_exporting_metric_reader.cc
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,101 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| #ifndef ENABLE_METRICS_PREVIEW | ||
| # include "opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader.h" | ||
| # include "opentelemetry/sdk/common/global_log_handler.h" | ||
| # include "opentelemetry/sdk/metrics/metric_exporter.h" | ||
|
|
||
| # include <chrono> | ||
| # include <future> | ||
|
|
||
| OPENTELEMETRY_BEGIN_NAMESPACE | ||
| namespace sdk | ||
| { | ||
| namespace metrics | ||
| { | ||
|
|
||
| PeriodicExportingMetricReader::PeriodicExportingMetricReader( | ||
| std::unique_ptr<MetricExporter> exporter, | ||
| const PeriodicExportingMetricReaderOptions &option, | ||
| AggregationTemporality aggregation_temporality) | ||
| : MetricReader(aggregation_temporality), | ||
| exporter_{std::move(exporter)}, | ||
| export_interval_millis_{option.export_interval_millis}, | ||
| export_timeout_millis_{option.export_timeout_millis} | ||
| { | ||
| if (export_interval_millis_ <= export_timeout_millis_) | ||
| { | ||
| OTEL_INTERNAL_LOG_WARN( | ||
| "[Periodic Exporting Metric Reader] Invalid configuration: " | ||
| "export_interval_millis_ should be less than export_timeout_millis_, using default values"); | ||
| export_interval_millis_ = kExportIntervalMillis; | ||
| export_timeout_millis_ = kExportTimeOutMillis; | ||
| } | ||
| } | ||
|
|
||
| void PeriodicExportingMetricReader::OnInitialized() noexcept | ||
| { | ||
| worker_thread_ = std::thread(&PeriodicExportingMetricReader::DoBackgroundWork, this); | ||
| } | ||
|
|
||
| void PeriodicExportingMetricReader::DoBackgroundWork() | ||
| { | ||
| std::unique_lock<std::mutex> lk(cv_m_); | ||
| do | ||
| { | ||
| if (IsShutdown()) | ||
| { | ||
| break; | ||
| } | ||
| std::atomic<bool> cancel_export_for_timeout{false}; | ||
| auto start = std::chrono::steady_clock::now(); | ||
| auto future_receive = std::async(std::launch::async, [this, &cancel_export_for_timeout] { | ||
| Collect([this, &cancel_export_for_timeout](MetricData data) { | ||
| if (cancel_export_for_timeout) | ||
| { | ||
| OTEL_INTERNAL_LOG_ERROR( | ||
| "[Periodic Exporting Metric Reader] Collect took longer configured time: " | ||
| << export_timeout_millis_.count() << " ms, and timed out"); | ||
| return false; | ||
| } | ||
| this->exporter_->Export(data); | ||
| return true; | ||
| }); | ||
| }); | ||
| std::future_status status; | ||
| do | ||
| { | ||
| status = future_receive.wait_for(std::chrono::milliseconds(export_timeout_millis_)); | ||
| if (status == std::future_status::timeout) | ||
| { | ||
| cancel_export_for_timeout = true; | ||
| break; | ||
| } | ||
| } while (status != std::future_status::ready); | ||
| auto end = std::chrono::steady_clock::now(); | ||
| auto export_time_ms = std::chrono::duration_cast<std::chrono::milliseconds>(end - start); | ||
| auto remaining_wait_interval_ms = export_interval_millis_ - export_time_ms; | ||
| cv_.wait_for(lk, remaining_wait_interval_ms); | ||
| } while (true); | ||
| } | ||
|
|
||
| bool PeriodicExportingMetricReader::OnForceFlush(std::chrono::microseconds timeout) noexcept | ||
| { | ||
| return exporter_->ForceFlush(timeout); | ||
| } | ||
|
|
||
| bool PeriodicExportingMetricReader::OnShutDown(std::chrono::microseconds timeout) noexcept | ||
| { | ||
| if (worker_thread_.joinable()) | ||
| { | ||
| cv_.notify_one(); | ||
| worker_thread_.join(); | ||
| } | ||
| return exporter_->Shutdown(timeout); | ||
| } | ||
|
|
||
| } // namespace metrics | ||
| } // namespace sdk | ||
| 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
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
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
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,81 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| #ifndef ENABLE_METRICS_PREVIEW | ||
|
|
||
| # include "opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader.h" | ||
| # include "opentelemetry/sdk/metrics/export/metric_producer.h" | ||
| # include "opentelemetry/sdk/metrics/metric_exporter.h" | ||
|
|
||
| # include <gtest/gtest.h> | ||
|
|
||
| using namespace opentelemetry; | ||
| using namespace opentelemetry::sdk::instrumentationlibrary; | ||
| using namespace opentelemetry::sdk::metrics; | ||
|
|
||
| class MockPushMetricExporter : public MetricExporter | ||
| { | ||
| public: | ||
| opentelemetry::sdk::common::ExportResult Export(const MetricData &record) noexcept override | ||
| { | ||
| records_.push_back(record); | ||
| return opentelemetry::sdk::common::ExportResult::kSuccess; | ||
| } | ||
|
|
||
| bool ForceFlush( | ||
| std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept override | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| bool Shutdown(std::chrono::microseconds timeout = std::chrono::microseconds(0)) noexcept override | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| size_t GetDataCount() { return records_.size(); } | ||
|
|
||
| private: | ||
| std::vector<MetricData> records_; | ||
| }; | ||
|
|
||
| class MockMetricProducer : public MetricProducer | ||
| { | ||
| public: | ||
| MockMetricProducer(std::chrono::microseconds sleep_ms = std::chrono::microseconds::zero()) | ||
| : sleep_ms_{sleep_ms}, data_sent_size_(0) | ||
| {} | ||
|
|
||
| bool Collect(nostd::function_ref<bool(MetricData)> callback) noexcept override | ||
| { | ||
| std::this_thread::sleep_for(sleep_ms_); | ||
| data_sent_size_++; | ||
| MetricData data; | ||
| callback(data); | ||
| return true; | ||
| } | ||
|
|
||
| size_t GetDataCount() { return data_sent_size_; } | ||
|
|
||
| private: | ||
| std::chrono::microseconds sleep_ms_; | ||
| size_t data_sent_size_; | ||
| }; | ||
|
|
||
| TEST(PeriodicExporingMetricReader, BasicTests) | ||
| { | ||
| std::unique_ptr<MetricExporter> exporter(new MockPushMetricExporter()); | ||
| PeriodicExportingMetricReaderOptions options; | ||
| options.export_timeout_millis = std::chrono::milliseconds(200); | ||
| options.export_interval_millis = std::chrono::milliseconds(500); | ||
| auto exporter_ptr = exporter.get(); | ||
| PeriodicExportingMetricReader reader(std::move(exporter), options); | ||
| MockMetricProducer producer; | ||
| reader.SetMetricProducer(&producer); | ||
| std::this_thread::sleep_for(std::chrono::milliseconds(2000)); | ||
| reader.Shutdown(); | ||
| EXPECT_EQ(static_cast<MockPushMetricExporter *>(exporter_ptr)->GetDataCount(), | ||
| static_cast<MockMetricProducer *>(&producer)->GetDataCount()); | ||
| } | ||
|
|
||
| #endif |
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.
Uh oh!
There was an error while loading. Please reload this page.