Skip to content
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

Fix metrics context circular reference #1535

Merged
merged 16 commits into from
Aug 11, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions sdk/include/opentelemetry/sdk/metrics/meter.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Meter final : public opentelemetry::metrics::Meter
public:
/** Construct a new Meter with the given pipeline. */
explicit Meter(
std::shared_ptr<sdk::metrics::MeterContext> meter_context,
std::weak_ptr<sdk::metrics::MeterContext> meter_context,
std::unique_ptr<opentelemetry::sdk::instrumentationscope::InstrumentationScope> scope =
opentelemetry::sdk::instrumentationscope::InstrumentationScope::Create("")) noexcept;

Expand Down Expand Up @@ -122,7 +122,7 @@ class Meter final : public opentelemetry::metrics::Meter
// order of declaration is important here - instrumentation scope should destroy after
// meter-context.
std::unique_ptr<sdk::instrumentationscope::InstrumentationScope> scope_;
std::shared_ptr<sdk::metrics::MeterContext> meter_context_;
std::weak_ptr<sdk::metrics::MeterContext> meter_context_;
// Mapping between instrument-name and Aggregation Storage.
std::unordered_map<std::string, std::shared_ptr<MetricStorage>> storage_registry_;

Expand All @@ -135,7 +135,8 @@ class Meter final : public opentelemetry::metrics::Meter
void *),
void *state = nullptr)
{
auto view_registry = meter_context_->GetViewRegistry();
auto ctx = meter_context_.lock();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we be checking if the ctx is valid here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense. Shall we log something too?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, I think we should log here if meter context is no more valid.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

auto view_registry = ctx->GetViewRegistry();
auto success = view_registry->FindViews(
instrument_descriptor, *scope_,
[this, &instrument_descriptor, callback, state](const View &view) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class MetricCollector : public MetricProducer, public CollectorHandle
bool Shutdown(std::chrono::microseconds timeout = std::chrono::microseconds::max()) noexcept;

private:
std::shared_ptr<MeterContext> meter_context_;
std::weak_ptr<MeterContext> meter_context_;
std::shared_ptr<MetricReader> metric_reader_;
};
} // namespace metrics
Expand Down
11 changes: 6 additions & 5 deletions sdk/src/metrics/meter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace metrics = opentelemetry::metrics;
namespace nostd = opentelemetry::nostd;

Meter::Meter(
std::shared_ptr<MeterContext> meter_context,
std::weak_ptr<MeterContext> meter_context,
std::unique_ptr<sdk::instrumentationscope::InstrumentationScope> instrumentation_scope) noexcept
: scope_{std::move(instrumentation_scope)}, meter_context_{meter_context}
{}
Expand Down Expand Up @@ -201,7 +201,8 @@ const sdk::instrumentationscope::InstrumentationScope *Meter::GetInstrumentation
std::unique_ptr<WritableMetricStorage> Meter::RegisterMetricStorage(
InstrumentDescriptor &instrument_descriptor)
{
auto view_registry = meter_context_->GetViewRegistry();
auto ctx = meter_context_.lock();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check if ctx is valid here before using?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

auto view_registry = ctx->GetViewRegistry();
std::unique_ptr<WritableMetricStorage> storages(new MultiMetricStorage());

auto success = view_registry->FindViews(
Expand Down Expand Up @@ -238,11 +239,11 @@ std::vector<MetricData> Meter::Collect(CollectorHandle *collector,
opentelemetry::common::SystemTimestamp collect_ts) noexcept
{
std::vector<MetricData> metric_data_list;
auto ctx = meter_context_.lock();
for (auto &metric_storage : storage_registry_)
{
metric_storage.second->Collect(collector, meter_context_->GetCollectors(),
meter_context_->GetSDKStartTime(), collect_ts,
[&metric_data_list](MetricData metric_data) {
metric_storage.second->Collect(collector, ctx->GetCollectors(), ctx->GetSDKStartTime(),
collect_ts, [&metric_data_list](MetricData metric_data) {
metric_data_list.push_back(metric_data);
return true;
});
Expand Down
5 changes: 3 additions & 2 deletions sdk/src/metrics/state/metric_collector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,16 @@ bool MetricCollector::Collect(
nostd::function_ref<bool(ResourceMetrics &metric_data)> callback) noexcept
{
ResourceMetrics resource_metrics;
for (auto &meter : meter_context_->GetMeters())
auto ctx = meter_context_.lock();
for (auto &meter : ctx->GetMeters())
{
auto collection_ts = std::chrono::system_clock::now();
ScopeMetrics scope_metrics;
scope_metrics.metric_data_ = meter->Collect(this, collection_ts);
scope_metrics.scope_ = meter->GetInstrumentationScope();
resource_metrics.scope_metric_data_.push_back(scope_metrics);
}
resource_metrics.resource_ = &meter_context_->GetResource();
resource_metrics.resource_ = &ctx->GetResource();
callback(resource_metrics);
return true;
}
Expand Down