-
Notifications
You must be signed in to change notification settings - Fork 440
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
Implement periodic exporting metric reader #1286
Merged
Merged
Changes from 8 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
69 changes: 69 additions & 0 deletions
69
sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader.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,69 @@ | ||
// 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 batch PeriodicExortingMetricReader options. | ||
*/ | ||
|
||
struct PeriodicExportingMetricReaderOptions | ||
{ | ||
|
||
/* The time interval between two consecutive exports. */ | ||
std::chrono::milliseconds schedule_delay_millis = std::chrono::milliseconds(60000); | ||
|
||
/* how long the export can run before it is cancelled. */ | ||
std::chrono::milliseconds export_timeout_millis = std::chrono::milliseconds(30000); | ||
}; | ||
|
||
class PeriodicExportingMetricReader : public MetricReader | ||
{ | ||
|
||
public: | ||
PeriodicExportingMetricReader( | ||
std::unique_ptr<MetricExporter> exporter, | ||
const PeriodicExportingMetricReaderOptions &option, | ||
AggregationTemporality aggregation_temporality = AggregationTemporality::kCummulative); | ||
|
||
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 schedule_delay_millis_; | ||
std::chrono::milliseconds export_timeout_millis_; | ||
|
||
void DoBackgroundWork(); | ||
|
||
/* The background worker thread */ | ||
std::thread worker_thread_; | ||
|
||
/* Synchronization primitives */ | ||
std::condition_variable cv_, force_flush_cv_; | ||
esigo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
std::mutex cv_m_, force_flush_cv_m_, shutdown_m_; | ||
}; | ||
|
||
} // namespace metrics | ||
} // namespace sdk | ||
OPENTELEMETRY_END_NAMESPACE | ||
#endif |
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
90 changes: 90 additions & 0 deletions
90
sdk/src/metrics/export/periodic_exporting_metric_reader.cc
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,90 @@ | ||
// 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)}, | ||
schedule_delay_millis_{option.schedule_delay_millis}, | ||
export_timeout_millis_{option.export_timeout_millis} | ||
{} | ||
|
||
void PeriodicExportingMetricReader::OnInitialized() noexcept | ||
{ | ||
worker_thread_ = std::thread(&PeriodicExportingMetricReader::DoBackgroundWork, this); | ||
} | ||
|
||
void PeriodicExportingMetricReader::DoBackgroundWork() | ||
{ | ||
auto timeout = schedule_delay_millis_; | ||
std::unique_lock<std::mutex> lk(cv_m_); | ||
do | ||
{ | ||
cv_.wait_for(lk, timeout); | ||
if (IsShutdown()) | ||
{ | ||
break; | ||
} | ||
std::atomic<bool> cancel_export_for_timeout{false}; | ||
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); | ||
|
||
} 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 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
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,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; | ||
esigo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; | ||
|
||
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(2000); | ||
options.schedule_delay_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.
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 name
schedule_delay_millis
might be misleading.The suggested name is
export_interval_millis
https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/sdk.md#periodic-exporting-metricreaderThere 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.
For example, if export 1 runs during [T1, T2] and export 2 runs during [T3, T4],
export_interval_millis = T3 - T1
, whileschedule_delay_millis = T3 - T2
.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.
Thanks, changed the variable name to
export_interval_millis
, and also logic to ensure there is exactlyexport_interval_millis
ms difference between two exports (by adjusting the time taken for export operation).