Skip to content

Commit

Permalink
Fix header only api singletons (open-telemetry#1520) (open-telemetry#…
Browse files Browse the repository at this point in the history
  • Loading branch information
marcalff authored and yxue committed Dec 5, 2022
1 parent 45c8808 commit 9f1dfb1
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ update the semantic convention in instrumentation library is needed.
* [METRICS EXPORTER] Add `OtlpGrpcMetricExporterFactory` and `OtlpHttpMetricExporterFactory`.
[#1606](https://github.com/open-telemetry/opentelemetry-cpp/pull/1606)
* [METRICS EXPORTER] Add `OtlpGrpcClient` [#1606](https://github.com/open-telemetry/opentelemetry-cpp/pull/1606)
* [BUILD] Fix header only api singletons [#1604](https://github.com/open-telemetry/opentelemetry-cpp/pull/1604)

## [1.6.0] 2022-08-15
>>>>>>> 39d6a225 (Upgrade opentelemetry-proto to v0.19.0 (#1579))
Expand Down
60 changes: 60 additions & 0 deletions api/include/opentelemetry/_metrics/provider.h
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
3 changes: 3 additions & 0 deletions api/include/opentelemetry/common/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
# define OPENTELEMETRY_DEPRECATED_MESSAGE(msg)
#endif

<<<<<<< HEAD
// Regex support
#if (__GNUC__ == 4 && (__GNUC_MINOR__ == 8 || __GNUC_MINOR__ == 9))
# define HAVE_WORKING_REGEX 0
Expand All @@ -98,6 +99,8 @@
# define HAVE_WORKING_REGEX 1
#endif

=======
>>>>>>> aa2faba8 (Fix header only api singletons (#1520) (#1604))
/* clang-format off */

/**
Expand Down
12 changes: 12 additions & 0 deletions api/include/opentelemetry/metrics/provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,19 @@

#include <mutex>

<<<<<<< HEAD
#include "opentelemetry/common/macros.h"
#include "opentelemetry/common/spin_lock_mutex.h"
#include "opentelemetry/metrics/meter_provider.h"
#include "opentelemetry/metrics/noop.h"
#include "opentelemetry/nostd/shared_ptr.h"
=======
# include "opentelemetry/common/macros.h"
# include "opentelemetry/common/spin_lock_mutex.h"
# include "opentelemetry/metrics/meter_provider.h"
# include "opentelemetry/metrics/noop.h"
# include "opentelemetry/nostd/shared_ptr.h"
>>>>>>> aa2faba8 (Fix header only api singletons (#1520) (#1604))

OPENTELEMETRY_BEGIN_NAMESPACE
namespace metrics
Expand Down Expand Up @@ -57,3 +65,7 @@ class Provider

} // namespace metrics
OPENTELEMETRY_END_NAMESPACE
<<<<<<< HEAD
=======
#endif
>>>>>>> aa2faba8 (Fix header only api singletons (#1520) (#1604))
19 changes: 19 additions & 0 deletions api/test/singleton/singleton_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,18 @@ void reset_counts()
class MyTracer : public trace::Tracer
{
public:
<<<<<<< HEAD
nostd::shared_ptr<trace::Span> StartSpan(
nostd::string_view name,
const common::KeyValueIterable & /* attributes */,
const trace::SpanContextKeyValueIterable & /* links */,
const trace::StartSpanOptions & /* options */) noexcept override
=======
nostd::shared_ptr<trace::Span> StartSpan(nostd::string_view name,
const common::KeyValueIterable &attributes,
const trace::SpanContextKeyValueIterable &links,
const trace::StartSpanOptions &options) noexcept override
>>>>>>> aa2faba8 (Fix header only api singletons (#1520) (#1604))
{
nostd::shared_ptr<trace::Span> result(new trace::DefaultSpan(trace::SpanContext::GetInvalid()));

Expand Down Expand Up @@ -170,9 +177,15 @@ class MyTracer : public trace::Tracer
return result;
}

<<<<<<< HEAD
void ForceFlushWithMicroseconds(uint64_t /* timeout */) noexcept override {}

void CloseWithMicroseconds(uint64_t /* timeout */) noexcept override {}
=======
void ForceFlushWithMicroseconds(uint64_t timeout) noexcept override {}

void CloseWithMicroseconds(uint64_t timeout) noexcept override {}
>>>>>>> aa2faba8 (Fix header only api singletons (#1520) (#1604))
};

class MyTracerProvider : public trace::TracerProvider
Expand All @@ -184,9 +197,15 @@ class MyTracerProvider : public trace::TracerProvider
return result;
}

<<<<<<< HEAD
nostd::shared_ptr<trace::Tracer> GetTracer(nostd::string_view /* library_name */,
nostd::string_view /* library_version */,
nostd::string_view /* schema_url */) noexcept override
=======
nostd::shared_ptr<trace::Tracer> GetTracer(nostd::string_view library_name,
nostd::string_view library_version,
nostd::string_view schema_url) noexcept override
>>>>>>> aa2faba8 (Fix header only api singletons (#1520) (#1604))
{
nostd::shared_ptr<trace::Tracer> result(new MyTracer());
return result;
Expand Down

0 comments on commit 9f1dfb1

Please sign in to comment.