forked from open-telemetry/opentelemetry-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix header only api singletons (open-telemetry#1520) (open-telemetry#…
- Loading branch information
Showing
5 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
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,60 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#pragma once | ||
#ifdef ENABLE_METRICS_PREVIEW | ||
# include <mutex> | ||
|
||
# include "opentelemetry/_metrics/meter_provider.h" | ||
# include "opentelemetry/_metrics/noop.h" | ||
# include "opentelemetry/common/macros.h" | ||
# include "opentelemetry/common/spin_lock_mutex.h" | ||
# include "opentelemetry/nostd/shared_ptr.h" | ||
|
||
OPENTELEMETRY_BEGIN_NAMESPACE | ||
namespace metrics | ||
{ | ||
/** | ||
* Stores the singleton global MeterProvider. | ||
*/ | ||
class Provider | ||
{ | ||
public: | ||
/** | ||
* Returns the singleton MeterProvider. | ||
* | ||
* By default, a no-op MeterProvider is returned. This will never return a | ||
* nullptr MeterProvider. | ||
*/ | ||
static nostd::shared_ptr<MeterProvider> GetMeterProvider() noexcept | ||
{ | ||
std::lock_guard<common::SpinLockMutex> guard(GetLock()); | ||
return nostd::shared_ptr<MeterProvider>(GetProvider()); | ||
} | ||
|
||
/** | ||
* Changes the singleton MeterProvider. | ||
*/ | ||
static void SetMeterProvider(nostd::shared_ptr<MeterProvider> tp) noexcept | ||
{ | ||
std::lock_guard<common::SpinLockMutex> guard(GetLock()); | ||
GetProvider() = tp; | ||
} | ||
|
||
private: | ||
OPENTELEMETRY_API_SINGLETON static nostd::shared_ptr<MeterProvider> &GetProvider() noexcept | ||
{ | ||
static nostd::shared_ptr<MeterProvider> provider(new NoopMeterProvider); | ||
return provider; | ||
} | ||
|
||
OPENTELEMETRY_API_SINGLETON static common::SpinLockMutex &GetLock() noexcept | ||
{ | ||
static common::SpinLockMutex lock; | ||
return lock; | ||
} | ||
}; | ||
|
||
} // namespace metrics | ||
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