Skip to content

Commit a7f9daf

Browse files
authored
[SDK] Add meter scope configurator (#3268)
1 parent 4b56f63 commit a7f9daf

18 files changed

+664
-19
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ Increment the:
2121
* [SDK] Support OTEL_SDK_DISABLED environment variable
2222
[#3245](https://github.com/open-telemetry/opentelemetry-cpp/pull/3245)
2323

24+
* [SDK] Add meter scope configurator
25+
[#3268](https://github.com/open-telemetry/opentelemetry-cpp/pull/3268)
26+
2427
Important changes:
2528

2629
* [SDK] Support OTEL_SDK_DISABLED environment variable

sdk/include/opentelemetry/sdk/metrics/meter.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "opentelemetry/sdk/instrumentationscope/instrumentation_scope.h"
2020
#include "opentelemetry/sdk/metrics/instrument_metadata_validator.h"
2121
#include "opentelemetry/sdk/metrics/instruments.h"
22+
#include "opentelemetry/sdk/metrics/meter_config.h"
2223
#include "opentelemetry/sdk/metrics/meter_context.h"
2324
#include "opentelemetry/sdk/metrics/state/async_metric_storage.h"
2425
#include "opentelemetry/sdk/resource/resource.h"
@@ -138,12 +139,15 @@ class Meter final : public opentelemetry::metrics::Meter
138139
// Mapping between instrument-name and Aggregation Storage.
139140
std::unordered_map<std::string, std::shared_ptr<MetricStorage>> storage_registry_;
140141
std::shared_ptr<ObservableRegistry> observable_registry_;
142+
MeterConfig meter_config_;
141143
std::unique_ptr<SyncWritableMetricStorage> RegisterSyncMetricStorage(
142144
InstrumentDescriptor &instrument_descriptor);
143145
std::unique_ptr<AsyncWritableMetricStorage> RegisterAsyncMetricStorage(
144146
InstrumentDescriptor &instrument_descriptor);
145147
opentelemetry::common::SpinLockMutex storage_lock_;
146148

149+
static opentelemetry::metrics::NoopMeter kNoopMeter;
150+
147151
static nostd::shared_ptr<opentelemetry::metrics::ObservableInstrument>
148152
GetNoopObservableInsrument()
149153
{
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
#pragma once
5+
6+
#include "opentelemetry/version.h"
7+
8+
OPENTELEMETRY_BEGIN_NAMESPACE
9+
namespace sdk
10+
{
11+
namespace metrics
12+
{
13+
/**
14+
* MeterConfig defines various configurable aspects of a Meter's behavior.
15+
* This class should not be used directly to configure a Meter's behavior, instead a
16+
* ScopeConfigurator should be used to compute the desired MeterConfig which can then be used to
17+
* configure a Meter.
18+
*/
19+
class MeterConfig
20+
{
21+
public:
22+
bool operator==(const MeterConfig &other) const noexcept;
23+
24+
/**
25+
* Returns if the Meter is enabled or disabled. Meters are enabled by default.
26+
* @return a boolean indicating if the Meter is enabled. Defaults to true.
27+
*/
28+
bool IsEnabled() const noexcept;
29+
30+
/**
31+
* Returns a MeterConfig that represents a disabled Meter. A disabled meter behaves like a
32+
* no-op meter.
33+
* @return a static constant MeterConfig that represents a disabled meter.
34+
*/
35+
static MeterConfig Disabled();
36+
37+
/**
38+
* Returns a MeterConfig that represents an enabled Meter.
39+
* @return a static constant MeterConfig that represents an enabled meter.
40+
*/
41+
static MeterConfig Enabled();
42+
43+
/**
44+
* Returns a MeterConfig that represents a Meter configured with the default behavior.
45+
* The default behavior is guided by the OpenTelemetry specification.
46+
* @return a static constant MeterConfig that represents a meter configured with default
47+
* behavior.
48+
*/
49+
static MeterConfig Default();
50+
51+
private:
52+
explicit MeterConfig(const bool disabled = false) : disabled_(disabled) {}
53+
bool disabled_;
54+
static const MeterConfig kDefaultConfig;
55+
static const MeterConfig kDisabledConfig;
56+
};
57+
} // namespace metrics
58+
} // namespace sdk
59+
OPENTELEMETRY_END_NAMESPACE

sdk/include/opentelemetry/sdk/metrics/meter_context.h

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#include "opentelemetry/nostd/function_ref.h"
1414
#include "opentelemetry/nostd/span.h"
1515
#include "opentelemetry/nostd/string_view.h"
16+
#include "opentelemetry/sdk/instrumentationscope/scope_configurator.h"
17+
#include "opentelemetry/sdk/metrics/meter_config.h"
1618
#include "opentelemetry/sdk/metrics/metric_reader.h"
1719
#include "opentelemetry/sdk/metrics/state/metric_collector.h"
1820
#include "opentelemetry/sdk/metrics/view/instrument_selector.h"
@@ -48,14 +50,17 @@ class MeterContext : public std::enable_shared_from_this<MeterContext>
4850
public:
4951
/**
5052
* Initialize a new meter provider
51-
* @param readers The readers to be configured with meter context.
5253
* @param views The views to be configured with meter context.
5354
* @param resource The resource for this meter context.
5455
*/
5556
MeterContext(
5657
std::unique_ptr<ViewRegistry> views = std::unique_ptr<ViewRegistry>(new ViewRegistry()),
5758
const opentelemetry::sdk::resource::Resource &resource =
58-
opentelemetry::sdk::resource::Resource::Create({})) noexcept;
59+
opentelemetry::sdk::resource::Resource::Create({}),
60+
std::unique_ptr<instrumentationscope::ScopeConfigurator<MeterConfig>> meter_configurator =
61+
std::make_unique<instrumentationscope::ScopeConfigurator<MeterConfig>>(
62+
instrumentationscope::ScopeConfigurator<MeterConfig>::Builder(MeterConfig::Default())
63+
.Build())) noexcept;
5964

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

7277
/**
73-
* NOTE - INTERNAL method, can change in future.
78+
* Obtain the ScopeConfigurator with this meter context.
79+
* @return The ScopeConfigurator for this meter context.
80+
*/
81+
const instrumentationscope::ScopeConfigurator<MeterConfig> &GetMeterConfigurator() const noexcept;
82+
83+
/**
84+
* NOTE - INTERNAL method, can change in the future.
7485
* Process callback for each meter in thread-safe manner
7586
*/
7687
bool ForEachMeter(nostd::function_ref<bool(std::shared_ptr<Meter> &meter)> callback) noexcept;
7788

7889
/**
79-
* NOTE - INTERNAL method, can change in future.
90+
* NOTE - INTERNAL method, can change in the future.
8091
* Get the configured meters.
8192
* This method is NOT thread safe, and only called through MeterProvider
8293
*
@@ -154,6 +165,7 @@ class MeterContext : public std::enable_shared_from_this<MeterContext>
154165
std::vector<std::shared_ptr<CollectorHandle>> collectors_;
155166
std::unique_ptr<ViewRegistry> views_;
156167
opentelemetry::common::SystemTimestamp sdk_start_ts_;
168+
std::unique_ptr<instrumentationscope::ScopeConfigurator<MeterConfig>> meter_configurator_;
157169
std::vector<std::shared_ptr<Meter>> meters_;
158170

159171
#ifdef ENABLE_METRICS_EXEMPLAR_PREVIEW

sdk/include/opentelemetry/sdk/metrics/meter_context_factory.h

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,39 @@ class OPENTELEMETRY_EXPORT MeterContextFactory
2323
{
2424
public:
2525
/**
26-
* Create a MeterContext.
26+
* Create a MeterContext with valid defaults.
27+
* @return A unique pointer to the created MeterContext object.
2728
*/
2829
static std::unique_ptr<MeterContext> Create();
2930

31+
/**
32+
* Create a MeterContext with specified views.
33+
* @param views ViewRegistry containing OpenTelemetry views registered with this meter context.
34+
*/
3035
static std::unique_ptr<MeterContext> Create(std::unique_ptr<ViewRegistry> views);
3136

37+
/**
38+
* Create a MeterContext with specified views and resource.
39+
* @param views ViewRegistry containing OpenTelemetry views registered with this meter context.
40+
* @param resource The OpenTelemetry resource associated with this meter context.
41+
* @return A unique pointer to the created MeterContext object.
42+
*/
3243
static std::unique_ptr<MeterContext> Create(
3344
std::unique_ptr<ViewRegistry> views,
3445
const opentelemetry::sdk::resource::Resource &resource);
46+
47+
/**
48+
* Create a MeterContext with specified views, resource and meter scope configurator.
49+
* @param views ViewRegistry containing OpenTelemetry views registered with this meter context.
50+
* @param resource The OpenTelemetry resource associated with this meter context.
51+
* @param meter_configurator A scope configurator defining the behavior of a meter associated with
52+
* this meter context.
53+
* @return A unique pointer to the created MeterContext object.
54+
*/
55+
static std::unique_ptr<MeterContext> Create(
56+
std::unique_ptr<ViewRegistry> views,
57+
const opentelemetry::sdk::resource::Resource &resource,
58+
std::unique_ptr<instrumentationscope::ScopeConfigurator<MeterConfig>> meter_configurator);
3559
};
3660

3761
} // namespace metrics

sdk/include/opentelemetry/sdk/metrics/meter_provider.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
#include "opentelemetry/sdk/resource/resource.h"
2121
#include "opentelemetry/version.h"
2222

23+
#include "opentelemetry/sdk/instrumentationscope/scope_configurator.h"
24+
#include "opentelemetry/sdk/metrics/meter.h"
25+
2326
#ifdef ENABLE_METRICS_EXEMPLAR_PREVIEW
2427
# include "opentelemetry/sdk/metrics/exemplar/filter_type.h"
2528
#endif
@@ -34,13 +37,19 @@ class OPENTELEMETRY_EXPORT MeterProvider final : public opentelemetry::metrics::
3437
{
3538
public:
3639
/**
37-
* Initialize a new meter provider
40+
* Initialize a new meter provider.
3841
* @param views The views for this meter provider
3942
* @param resource The resources for this meter provider.
43+
* @param meter_configurator Provides access to a function that computes the MeterConfig for
44+
* Meters provided by this MeterProvider.
4045
*/
4146
MeterProvider(
4247
std::unique_ptr<ViewRegistry> views = std::unique_ptr<ViewRegistry>(new ViewRegistry()),
43-
const sdk::resource::Resource &resource = sdk::resource::Resource::Create({})) noexcept;
48+
const sdk::resource::Resource &resource = sdk::resource::Resource::Create({}),
49+
std::unique_ptr<instrumentationscope::ScopeConfigurator<MeterConfig>> meter_configurator =
50+
std::make_unique<instrumentationscope::ScopeConfigurator<MeterConfig>>(
51+
instrumentationscope::ScopeConfigurator<MeterConfig>::Builder(MeterConfig::Default())
52+
.Build())) noexcept;
4453

4554
/**
4655
* Initialize a new meter provider with a specified context

sdk/include/opentelemetry/sdk/metrics/meter_provider_factory.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ class OPENTELEMETRY_EXPORT MeterProviderFactory
3030
std::unique_ptr<ViewRegistry> views,
3131
const opentelemetry::sdk::resource::Resource &resource);
3232

33+
static std::unique_ptr<opentelemetry::sdk::metrics::MeterProvider> Create(
34+
std::unique_ptr<ViewRegistry> views,
35+
const opentelemetry::sdk::resource::Resource &resource,
36+
std::unique_ptr<instrumentationscope::ScopeConfigurator<MeterConfig>> meter_configurator);
37+
3338
static std::unique_ptr<opentelemetry::sdk::metrics::MeterProvider> Create(
3439
std::unique_ptr<sdk::metrics::MeterContext> context);
3540
};

sdk/src/metrics/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ add_library(
88
meter_provider.cc
99
meter_provider_factory.cc
1010
meter.cc
11+
meter_config.cc
1112
meter_context.cc
1213
meter_context_factory.cc
1314
metric_reader.cc

0 commit comments

Comments
 (0)