Skip to content
Merged
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ Increment the:
* [SDK] Support OTEL_SDK_DISABLED environment variable
[#3245](https://github.com/open-telemetry/opentelemetry-cpp/pull/3245)

* [SDK] Add meter scope configurator
[#3268](https://github.com/open-telemetry/opentelemetry-cpp/pull/3268)

Important changes:

* [SDK] Support OTEL_SDK_DISABLED environment variable
Expand Down
4 changes: 4 additions & 0 deletions sdk/include/opentelemetry/sdk/metrics/meter.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "opentelemetry/sdk/instrumentationscope/instrumentation_scope.h"
#include "opentelemetry/sdk/metrics/instrument_metadata_validator.h"
#include "opentelemetry/sdk/metrics/instruments.h"
#include "opentelemetry/sdk/metrics/meter_config.h"
#include "opentelemetry/sdk/metrics/meter_context.h"
#include "opentelemetry/sdk/metrics/state/async_metric_storage.h"
#include "opentelemetry/sdk/resource/resource.h"
Expand Down Expand Up @@ -138,12 +139,15 @@ class Meter final : public opentelemetry::metrics::Meter
// Mapping between instrument-name and Aggregation Storage.
std::unordered_map<std::string, std::shared_ptr<MetricStorage>> storage_registry_;
std::shared_ptr<ObservableRegistry> observable_registry_;
MeterConfig meter_config_;
std::unique_ptr<SyncWritableMetricStorage> RegisterSyncMetricStorage(
InstrumentDescriptor &instrument_descriptor);
std::unique_ptr<AsyncWritableMetricStorage> RegisterAsyncMetricStorage(
InstrumentDescriptor &instrument_descriptor);
opentelemetry::common::SpinLockMutex storage_lock_;

static opentelemetry::metrics::NoopMeter kNoopMeter;

static nostd::shared_ptr<opentelemetry::metrics::ObservableInstrument>
GetNoopObservableInsrument()
{
Expand Down
59 changes: 59 additions & 0 deletions sdk/include/opentelemetry/sdk/metrics/meter_config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include "opentelemetry/version.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace sdk
{
namespace metrics
{
/**
* MeterConfig defines various configurable aspects of a Meter's behavior.
* This class should not be used directly to configure a Meter's behavior, instead a
* ScopeConfigurator should be used to compute the desired MeterConfig which can then be used to
* configure a Meter.
*/
class MeterConfig
{
public:
bool operator==(const MeterConfig &other) const noexcept;

/**
* Returns if the Meter is enabled or disabled. Meters are enabled by default.
* @return a boolean indicating if the Meter is enabled. Defaults to true.
*/
bool IsEnabled() const noexcept;

/**
* Returns a MeterConfig that represents a disabled Meter. A disabled meter behaves like a
* no-op meter.
* @return a static constant MeterConfig that represents a disabled meter.
*/
static MeterConfig Disabled();

/**
* Returns a MeterConfig that represents an enabled Meter.
* @return a static constant MeterConfig that represents an enabled meter.
*/
static MeterConfig Enabled();

/**
* Returns a MeterConfig that represents a Meter configured with the default behavior.
* The default behavior is guided by the OpenTelemetry specification.
* @return a static constant MeterConfig that represents a meter configured with default
* behavior.
*/
static MeterConfig Default();

private:
explicit MeterConfig(const bool disabled = false) : disabled_(disabled) {}
bool disabled_;
static const MeterConfig kDefaultConfig;
static const MeterConfig kDisabledConfig;
};
} // namespace metrics
} // namespace sdk
OPENTELEMETRY_END_NAMESPACE
20 changes: 16 additions & 4 deletions sdk/include/opentelemetry/sdk/metrics/meter_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include "opentelemetry/nostd/function_ref.h"
#include "opentelemetry/nostd/span.h"
#include "opentelemetry/nostd/string_view.h"
#include "opentelemetry/sdk/instrumentationscope/scope_configurator.h"
#include "opentelemetry/sdk/metrics/meter_config.h"
#include "opentelemetry/sdk/metrics/metric_reader.h"
#include "opentelemetry/sdk/metrics/state/metric_collector.h"
#include "opentelemetry/sdk/metrics/view/instrument_selector.h"
Expand Down Expand Up @@ -48,14 +50,17 @@ class MeterContext : public std::enable_shared_from_this<MeterContext>
public:
/**
* Initialize a new meter provider
* @param readers The readers to be configured with meter context.
* @param views The views to be configured with meter context.
* @param resource The resource for this meter context.
*/
MeterContext(
std::unique_ptr<ViewRegistry> views = std::unique_ptr<ViewRegistry>(new ViewRegistry()),
const opentelemetry::sdk::resource::Resource &resource =
opentelemetry::sdk::resource::Resource::Create({})) noexcept;
opentelemetry::sdk::resource::Resource::Create({}),
std::unique_ptr<instrumentationscope::ScopeConfigurator<MeterConfig>> meter_configurator =
std::make_unique<instrumentationscope::ScopeConfigurator<MeterConfig>>(
instrumentationscope::ScopeConfigurator<MeterConfig>::Builder(MeterConfig::Default())
.Build())) noexcept;

/**
* Obtain the resource associated with this meter context.
Expand All @@ -70,13 +75,19 @@ class MeterContext : public std::enable_shared_from_this<MeterContext>
ViewRegistry *GetViewRegistry() const noexcept;

/**
* NOTE - INTERNAL method, can change in future.
* Obtain the ScopeConfigurator with this meter context.
* @return The ScopeConfigurator for this meter context.
*/
const instrumentationscope::ScopeConfigurator<MeterConfig> &GetMeterConfigurator() const noexcept;

/**
* NOTE - INTERNAL method, can change in the future.
* Process callback for each meter in thread-safe manner
*/
bool ForEachMeter(nostd::function_ref<bool(std::shared_ptr<Meter> &meter)> callback) noexcept;

/**
* NOTE - INTERNAL method, can change in future.
* NOTE - INTERNAL method, can change in the future.
* Get the configured meters.
* This method is NOT thread safe, and only called through MeterProvider
*
Expand Down Expand Up @@ -154,6 +165,7 @@ class MeterContext : public std::enable_shared_from_this<MeterContext>
std::vector<std::shared_ptr<CollectorHandle>> collectors_;
std::unique_ptr<ViewRegistry> views_;
opentelemetry::common::SystemTimestamp sdk_start_ts_;
std::unique_ptr<instrumentationscope::ScopeConfigurator<MeterConfig>> meter_configurator_;
std::vector<std::shared_ptr<Meter>> meters_;

#ifdef ENABLE_METRICS_EXEMPLAR_PREVIEW
Expand Down
26 changes: 25 additions & 1 deletion sdk/include/opentelemetry/sdk/metrics/meter_context_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,39 @@ class OPENTELEMETRY_EXPORT MeterContextFactory
{
public:
/**
* Create a MeterContext.
* Create a MeterContext with valid defaults.
* @return A unique pointer to the created MeterContext object.
*/
static std::unique_ptr<MeterContext> Create();

/**
* Create a MeterContext with specified views.
* @param views ViewRegistry containing OpenTelemetry views registered with this meter context.
*/
static std::unique_ptr<MeterContext> Create(std::unique_ptr<ViewRegistry> views);

/**
* Create a MeterContext with specified views and resource.
* @param views ViewRegistry containing OpenTelemetry views registered with this meter context.
* @param resource The OpenTelemetry resource associated with this meter context.
* @return A unique pointer to the created MeterContext object.
*/
static std::unique_ptr<MeterContext> Create(
std::unique_ptr<ViewRegistry> views,
const opentelemetry::sdk::resource::Resource &resource);

/**
* Create a MeterContext with specified views, resource and meter scope configurator.
* @param views ViewRegistry containing OpenTelemetry views registered with this meter context.
* @param resource The OpenTelemetry resource associated with this meter context.
* @param meter_configurator A scope configurator defining the behavior of a meter associated with
* this meter context.
* @return A unique pointer to the created MeterContext object.
*/
static std::unique_ptr<MeterContext> Create(
std::unique_ptr<ViewRegistry> views,
const opentelemetry::sdk::resource::Resource &resource,
std::unique_ptr<instrumentationscope::ScopeConfigurator<MeterConfig>> meter_configurator);
};

} // namespace metrics
Expand Down
13 changes: 11 additions & 2 deletions sdk/include/opentelemetry/sdk/metrics/meter_provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
#include "opentelemetry/sdk/resource/resource.h"
#include "opentelemetry/version.h"

#include "opentelemetry/sdk/instrumentationscope/scope_configurator.h"
#include "opentelemetry/sdk/metrics/meter.h"

#ifdef ENABLE_METRICS_EXEMPLAR_PREVIEW
# include "opentelemetry/sdk/metrics/exemplar/filter_type.h"
#endif
Expand All @@ -34,13 +37,19 @@ class OPENTELEMETRY_EXPORT MeterProvider final : public opentelemetry::metrics::
{
public:
/**
* Initialize a new meter provider
* Initialize a new meter provider.
* @param views The views for this meter provider
* @param resource The resources for this meter provider.
* @param meter_configurator Provides access to a function that computes the MeterConfig for
* Meters provided by this MeterProvider.
*/
MeterProvider(
std::unique_ptr<ViewRegistry> views = std::unique_ptr<ViewRegistry>(new ViewRegistry()),
const sdk::resource::Resource &resource = sdk::resource::Resource::Create({})) noexcept;
const sdk::resource::Resource &resource = sdk::resource::Resource::Create({}),
std::unique_ptr<instrumentationscope::ScopeConfigurator<MeterConfig>> meter_configurator =
std::make_unique<instrumentationscope::ScopeConfigurator<MeterConfig>>(
instrumentationscope::ScopeConfigurator<MeterConfig>::Builder(MeterConfig::Default())
.Build())) noexcept;

/**
* Initialize a new meter provider with a specified context
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ class OPENTELEMETRY_EXPORT MeterProviderFactory
std::unique_ptr<ViewRegistry> views,
const opentelemetry::sdk::resource::Resource &resource);

static std::unique_ptr<opentelemetry::sdk::metrics::MeterProvider> Create(
std::unique_ptr<ViewRegistry> views,
const opentelemetry::sdk::resource::Resource &resource,
std::unique_ptr<instrumentationscope::ScopeConfigurator<MeterConfig>> meter_configurator);

static std::unique_ptr<opentelemetry::sdk::metrics::MeterProvider> Create(
std::unique_ptr<sdk::metrics::MeterContext> context);
};
Expand Down
1 change: 1 addition & 0 deletions sdk/src/metrics/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ add_library(
meter_provider.cc
meter_provider_factory.cc
meter.cc
meter_config.cc
meter_context.cc
meter_context_factory.cc
metric_reader.cc
Expand Down
Loading
Loading