Skip to content

Commit

Permalink
Fix GlobalLogHandler singleton creation order (#1767)
Browse files Browse the repository at this point in the history
  • Loading branch information
lalitb authored Nov 14, 2022
1 parent 389b84f commit a3e3a90
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 7 deletions.
9 changes: 4 additions & 5 deletions sdk/include/opentelemetry/sdk/common/global_log_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,10 @@ class GlobalLogHandler
OPENTELEMETRY_END_NAMESPACE

/**
* We can not decide the destroying order of signaltons.
* Which means, the destructors of other singletons (GlobalLogHandler,TracerProvider and etc.)
* may be called after destroying of global LogHandler and use OTEL_INTERNAL_LOG_* in it.We can do
* nothing but ignore the log in this situation.
*/
* GlobalLogHandler and TracerProvider/MeterProvider/LoggerProvider are lazy sigletons.
* To ensure that GlobalLogHandler is the first one to be initialized (and so last to be
* destroyed), it is first used inside the constructors of TraceProvider, MeterProvider
* and LoggerProvider for debug logging. */
#define OTEL_INTERNAL_LOG_DISPATCH(level, message, attributes) \
do \
{ \
Expand Down
2 changes: 2 additions & 0 deletions sdk/src/logs/logger_provider.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#ifdef ENABLE_LOGS_PREVIEW

# include "opentelemetry/sdk/logs/logger_provider.h"
# include "opentelemetry/sdk/common/global_log_handler.h"

# include <memory>
# include <mutex>
Expand All @@ -26,6 +27,7 @@ LoggerProvider::LoggerProvider(std::unique_ptr<LogRecordProcessor> &&processor,
std::vector<std::unique_ptr<LogRecordProcessor>> processors;
processors.emplace_back(std::move(processor));
context_ = std::make_shared<sdk::logs::LoggerContext>(std::move(processors), std::move(resource));
OTEL_INTERNAL_LOG_DEBUG("[LoggerProvider] LoggerProvider created.");
}

LoggerProvider::LoggerProvider(std::vector<std::unique_ptr<LogRecordProcessor>> &&processors,
Expand Down
4 changes: 3 additions & 1 deletion sdk/src/metrics/meter_provider.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ MeterProvider::MeterProvider(std::shared_ptr<MeterContext> context) noexcept : c
MeterProvider::MeterProvider(std::unique_ptr<ViewRegistry> views,
sdk::resource::Resource resource) noexcept
: context_(std::make_shared<MeterContext>(std::move(views), resource))
{}
{
OTEL_INTERNAL_LOG_DEBUG("[MeterProvider] MeterProvider created.");
}

nostd::shared_ptr<metrics_api::Meter> MeterProvider::GetMeter(
nostd::string_view name,
Expand Down
5 changes: 4 additions & 1 deletion sdk/src/trace/tracer_provider.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

#include "opentelemetry/sdk/trace/tracer_provider.h"
#include "opentelemetry/sdk/common/global_log_handler.h"
#include "opentelemetry/sdk_config.h"

OPENTELEMETRY_BEGIN_NAMESPACE
Expand All @@ -14,7 +15,9 @@ namespace trace_api = opentelemetry::trace;

TracerProvider::TracerProvider(std::shared_ptr<sdk::trace::TracerContext> context) noexcept
: context_{context}
{}
{
OTEL_INTERNAL_LOG_DEBUG("[TracerProvider] TracerProvider created.");
}

TracerProvider::TracerProvider(std::unique_ptr<SpanProcessor> processor,
resource::Resource resource,
Expand Down

2 comments on commit a3e3a90

@github-actions
Copy link

Choose a reason for hiding this comment

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

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'OpenTelemetry-cpp api Benchmark'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: a3e3a90 Previous: 389b84f Ratio
BM_SpanIdDefaultConstructor 3.6787345866043437 ns/iter 1.6886074718479656 ns/iter 2.18
BM_SpanIdConstructor 3.4986089977614467 ns/iter 1.7219827966435994 ns/iter 2.03

This comment was automatically generated by workflow using github-action-benchmark.

@github-actions
Copy link

Choose a reason for hiding this comment

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

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'OpenTelemetry-cpp sdk Benchmark'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: a3e3a90 Previous: 389b84f Ratio
BM_BaselineBuffer/1 1336207.628250122 ns/iter 486395.5292307118 ns/iter 2.75
BM_BaselineBuffer/2 12533831.596374512 ns/iter 2065244.436264038 ns/iter 6.07

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.