BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bdlm_metricsregistry.h
Go to the documentation of this file.
1/// @file bdlm_metricsregistry.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdlm_metricsregistry.h -*-C++-*-
8
9#ifndef INCLUDED_BDLM_METRICSREGISTRY
10#define INCLUDED_BDLM_METRICSREGISTRY
11
12#include <bsls_ident.h>
13BSLS_IDENT("$Id: $")
14
15/// @defgroup bdlm_metricsregistry bdlm_metricsregistry
16/// @brief Provide a transferable registry of metric registrations.
17/// @addtogroup bdl
18/// @{
19/// @addtogroup bdlm
20/// @{
21/// @addtogroup bdlm_metricsregistry
22/// @{
23///
24/// <h1> Outline </h1>
25/// * <a href="#bdlm_metricsregistry-purpose"> Purpose</a>
26/// * <a href="#bdlm_metricsregistry-classes"> Classes </a>
27/// * <a href="#bdlm_metricsregistry-description"> Description </a>
28/// * <a href="#bdlm_metricsregistry-thread-safety"> Thread Safety </a>
29/// * <a href="#bdlm_metricsregistry-usage"> Usage </a>
30/// * <a href="#bdlm_metricsregistry-example-1-using-bdlm-metricsregistry"> Example 1: Using bdlm::MetricsRegistry </a>
31///
32/// # Purpose {#bdlm_metricsregistry-purpose}
33/// Provide a transferable registry of metric registrations.
34///
35/// # Classes {#bdlm_metricsregistry-classes}
36///
37/// - bdlm::MetricsRegistry: transferable registry of metric registrations
38/// - bdlm::MetricsRegistryRegistrationHandle: registration handle providing RAII
39///
40/// # Description {#bdlm_metricsregistry-description}
41/// This component contains a mechanism, `bdlm::MetricsRegistry`,
42/// that provides a registry of metrics that is transferable to implementations
43/// of the `bdlm::MetricsAdapter` protocol. `bdlm`, as a low-level metrics
44/// facility, does not directly manage schedulers to collect metrics values or
45/// publishers to publish those value. Instead `bdlm` is designed to allow
46/// applications to plug in different high-level feature-rich metrics collection
47/// and publication frameworks (without requiring a library dependency on those
48/// frameworks). A `bdlm::MetricsRegistry` effectively serves as a proxy for a
49/// higher-level metrics collection system implementing the `MetricsAdapter`
50/// protocol -- it keeps track of registered metrics allowing a higher-level
51/// metrics subsystem to be installed (by calling 'setMetricsAdapter) at any
52/// time, either before or after a metric is registered.
53///
54/// A singleton instance of `MetricsRegistry` is available from the
55/// `defaultInstance` class method. This component also provides a registration
56/// handle class, `bdlm::MetricsRegistryRegistrationHandle`, that provides RAII
57/// semantics for metric registration.
58///
59/// ## Thread Safety {#bdlm_metricsregistry-thread-safety}
60///
61///
62/// The class `bdlm::MetricsRegistry` is *fully thread-safe* (see
63/// {@ref bsldoc_glossary |Fully Thread-Safe}), and
64/// `bdlm::MetricsRegistryRegistrationHandle` is *minimally thread-safe* (see
65/// {@ref bsldoc_glossary |Minimally Thread-Safe}).
66///
67/// ## Usage {#bdlm_metricsregistry-usage}
68///
69///
70/// This section illustrates intended use of this component.
71///
72/// ### Example 1: Using bdlm::MetricsRegistry {#bdlm_metricsregistry-example-1-using-bdlm-metricsregistry}
73///
74///
75/// This example demonstrates the initialization and usage of the
76/// `bdlm::MetricsRegistry` object, allowing for registering metric callback
77/// functions with the `bdlm` monitoring system.
78///
79/// First, we declare a class that provides some metric for the `bdlm`
80/// monitoring system:
81/// @code
82/// class LowLevelFacility {
83/// // PRIVATE DATA
84/// bdlm::MetricsRegistryRegistrationHandle d_metricHandle;
85/// public:
86/// // CREATORS
87/// explicit LowLevelFacility(bdlm::MetricsRegistry& metricsRegistry =
88/// bdlm::MetricsRegistry::defaultInstance());
89///
90/// // ACCESSORS
91/// int someMetric() const
92/// {
93/// return 0; // just a stub
94/// }
95/// };
96/// @endcode
97/// Next, we provide a metric function to be used during callback registration:
98/// @code
99/// void metricCallback(bdlm::Metric *value, const LowLevelFacility *object)
100/// {
101/// *value = bdlm::Metric::Gauge(object->someMetric());
102/// }
103/// @endcode
104/// Here is the constructor definition that registers the collection callback:
105/// @code
106/// /// Construct a `bdlm::MetricsDescriptor` object to be used when
107/// /// registering the callback function:
108/// LowLevelFacility::LowLevelFacility(bdlm::MetricsRegistry& metricsRegistry)
109/// {
110/// bdlm::MetricDescriptor descriptor("bdlm",
111/// "example",
112/// 1,
113/// "bdlmmetricsregistry",
114/// "bmr",
115/// "identifier");
116///
117/// // Register the collection callback:
118/// metricsRegistry.registerCollectionCallback(
119/// &d_metricHandle,
120/// descriptor,
121/// bdlf::BindUtil::bind(&metricCallback,
122/// bdlf::PlaceHolders::_1,
123/// this));
124/// assert(d_metricHandle.isRegistered());
125/// }
126/// @endcode
127/// Notice that the compiler-supplied destructor is sufficient because the
128/// `d_metricHandle` will deregister the metric on destruction.
129///
130/// Now, we construct a `bdlm::MetricsRegistry` object with a test allocator:
131/// @code
132/// bslma::TestAllocator ta;
133/// bdlm::MetricsRegistry registry(&ta);
134/// assert(registry.numRegisteredCollectionCallbacks() == 0);
135/// @endcode
136/// Then, we create the object and pass the constructed `bdlm::MetricsRegistry`
137/// object there:
138/// @code
139/// {
140/// LowLevelFacility facility(registry);
141/// assert(registry.numRegisteredCollectionCallbacks() == 1);
142/// @endcode
143/// If we don't provide a `bdlm::MetricsRegistry` object explicitly, the default
144/// global instance will be used.
145///
146/// Finally, the callback is removed the monitoring system by the destructor of
147/// `facility` object:
148/// @code
149/// } // 'facility.d_metricHandle.unregister()' is called here
150/// assert(registry.numRegisteredCollectionCallbacks() == 0);
151/// @endcode
152/// @}
153/** @} */
154/** @} */
155
156/** @addtogroup bdl
157 * @{
158 */
159/** @addtogroup bdlm
160 * @{
161 */
162/** @addtogroup bdlm_metricsregistry
163 * @{
164 */
165
166#include <bdlm_metricdescriptor.h>
167#include <bdlm_metricsadapter.h>
168
169#include <bslalg_swaputil.h>
170
171#include <bslma_allocator.h>
173
174#include <bslmf_allocatorargt.h>
175#include <bslmf_movableref.h>
177
178#include <bslstl_sharedptr.h>
179
180#include <bsls_keyword.h>
181
182
183namespace bdlm {
184
185class MetricsRegistryRegistrationHandle;
186class MetricsRegistry_Impl;
187
188 // =====================
189 // class MetricsRegistry
190 // =====================
191
192/// This class implements a mechanism that provides a registry of metrics
193/// that is transferable to implementations of the `bdlm::MetricsAdapter`
194/// protocol. This class is *usually* a singleton.
195///
196/// See @ref bdlm_metricsregistry
198
199 // DATA
201
202 public:
203 // TYPES
206
207 // TRAITS
209
210 // CLASS METHODS
211
212 /// Return a non-`const` reference to the metrics registry singleton.
214
215 // CREATORS
216
217 /// Create a `MetricsRegistry` object that stores the information
218 /// necessary to forward the registration and unregistration of metrics
219 /// to an adapter supplied with `setMetricsRegistry`. Optionally
220 /// specify a `basicAllocator` used to supply memory. If
221 /// `basicAllocator` is 0, the currently installed default allocator is
222 /// used.
223 explicit MetricsRegistry(bslma::Allocator *basicAllocator = 0);
224
225 /// Unregister all registered metrics and destroy this `MetricsRegistry`
226 /// object.
228
229 // MANIPULATORS
230
231 /// Disable metrics collection. If there is an associated metrics adapter,
232 /// all the collection callbacks are unregistered from that adapter. The
233 /// adapter remains associated with this registry. Collection callbacks
234 /// registered with this registry are not registered with the associated
235 /// adapter until metrics collections is enabled. Return 0 on success, and
236 /// a non-zero value otherwise. Metrics collection is enabled at
237 /// construction of this registry.
239
240 /// Enable metrics collection. If there is an associated metrics adapter,
241 /// all collection callbacks will be registered with the adapter. Return
242 /// 0 on success, and non-zero value otherwise. Metrics collection is
243 /// enabled at construction of this registry.
245
246 /// Register the metric described by the specified `descriptor` and
247 /// associate it with the specified `callback` to collect data from the
248 /// metric, and load the specified `result` with a handle can be used
249 /// later to unregister the metric. Return 0 on success, and a non-zero
250 /// value otherwise. After this operation completes,
251 /// `result->isRegistered()` will be `true`. The metric and associated
252 /// callback remain registered with this registry until either the
253 /// handle is unregistered or destroyed. When a `MetricsAdapter` is
254 /// associated with this registry using `setMetricsAdapter`, this
255 /// object registers all the registered metrics and callbacks with that
256 /// adapter, and similarly unregisters them if the `MetricAdapter` is
257 /// later disassociated with this registry (either on this objects
258 /// destruction, or due to a call to `removeMetricsAdapter` or
259 /// `setMetricsAdapter`). Furthermore, metrics collection can be disable
260 /// with `disableMetricsCollection` and enabled with
261 /// `enableMetricsCollection`. In this way, a `MetricsRegistry` serves as
262 /// an intermediary between users of `bdlm` that register metrics and the
263 /// subsystem for collecting and publishing metrics being adapted by a
264 /// concrete instance of `bdlm::MetricAdapter`.
267 const bdlm::MetricDescriptor& descriptor,
268 const Callback& callback);
269
270 /// If the specified `adapter` is the currently associated adapter, remove
271 /// all registered metrics from it and disassociate with this registry. Return 0 on success, and a non-zero value otherwise.
272 ///
273 /// \note Note that this
274 /// operation takes an `adapter` to disambiguate multiple, potentially
275 /// concurrent, calls to this method and `setMetricsAdapter`.
277
278 /// Configure this metrics registry to register all metrics collection
279 /// callbacks with the specified `adapter` when the registry has metrics
280 /// collection enabled. This operation first, if there is already an
281 /// associated metrics adapter, unregisters all the collection callbacks
282 /// from that adapter, then registers the collection callbacks with the new
283 /// `adapter` if metrics collection is enabled. Return 0 on success, and a
284 /// non-zero value otherwise.
286
287 // ACCESSORS
288
289 /// Return the number of registered metrics collection callbacks.
291
292 // Aspects
293
294 /// Return the allocator used by this object to supply memory.
296};
297
298 // =======================================
299 // class MetricsRegistryRegistrationHandle
300 // =======================================
301
302/// This class implements a registration handle that provides RAII semantics
303/// for metric registration.
304///
305/// See @ref bdlm_metricsregistry
307
308 // PRIVATE TYPES
310
311 // DATA
312 bsl::weak_ptr<MetricsRegistry_Impl> d_registry; // associated registry
313 MetricsRegistry::CallbackHandle d_handle; // handle for registration
314
315 // PRIVATE CREATORS
316
317 /// Create a `MetricsRegistryRegistrationHandle` object having the
318 /// associated specified `registry` and the specified `handle` in the
319 /// `registry`.
323
324 // FRIENDS
325 friend class MetricsRegistry;
326
327 private:
328 // NOT IMPLEMENTED
333 public:
334 // CREATORS
335
336 /// Create a `MetricsRegistryRegstrationHandle` object that is not
337 /// associated with a registered metrics collection callback
338 /// (`isRegistered` will return `false`).
340
341 /// Create a `MetricsRegistryRegistrationHandle` object that will manage
342 /// the metric collection callback registration associated with the
343 /// specified `original` handle. After creating this object, `original`
344 /// will no longer manage the registration, and
345 /// `original.isRegistered()` will be `false`. If `original` does not
346 /// manage a registration when this object is created, then neither this
347 /// object nor `original` will manage a registration.
351
352 /// Unregister the metric collection callback associated with this
353 /// handle (if `isRegistered` is `true`), and destroy this object.
355
356 // MANIPULATORS
357
358 /// Unregister the metric collection callback associated with this
359 /// handle (if `isRegistered` is `true`). Take an ownership on the
360 /// metric collection callback registration associated with the
361 /// specified `original` handle. Afterwards, `original` will no longer
362 /// manage the registration, and `original.isRegistered()` will be
363 /// `false`.
367
368 /// Exchange the associated metric collection callback registration with
369 /// the one controlled by the specified `other` handle.
371
372 /// Unregister the metric from the associated registry. Return 0 on
373 /// success, and a non-zero value if this handle is not currently
374 /// associated with a registered metrics collection callback.
376
377 // ACCESSORS
378
379 /// Return `true` if this handle has an associated registered metrics
380 /// collection callback.
381 bool isRegistered() const;
382};
383
384// ============================================================================
385// INLINE DEFINITIONS
386// ============================================================================
387
388 // ---------------------------------------
389 // class MetricsRegistryRegistrationHandle
390 // ---------------------------------------
391
392// CREATORS
393inline
395: d_registry()
396, d_handle(0)
397{
398}
399
400// MANIPULATORS
401inline
404{
405 bslalg::SwapUtil::swap(&d_registry, &other.d_registry);
406 bslalg::SwapUtil::swap(&d_handle, &other.d_handle);
407}
408
409// ACCESSORS
410inline
412{
413 return !d_registry.expired();
414}
415
416} // close package namespace
417
418
419#endif
420
421// ----------------------------------------------------------------------------
422// Copyright 2024 Bloomberg Finance L.P.
423//
424// Licensed under the Apache License, Version 2.0 (the "License");
425// you may not use this file except in compliance with the License.
426// You may obtain a copy of the License at
427//
428// http://www.apache.org/licenses/LICENSE-2.0
429//
430// Unless required by applicable law or agreed to in writing, software
431// distributed under the License is distributed on an "AS IS" BASIS,
432// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
433// See the License for the specific language governing permissions and
434// limitations under the License.
435// ----------------------------- END-OF-FILE ----------------------------------
436
437/** @} */
438/** @} */
439/** @} */
Definition bdlm_metricdescriptor.h:140
Definition bdlm_metricsadapter.h:291
int CallbackHandle
Definition bdlm_metricsadapter.h:295
Definition bdlm_metricsregistry.h:306
void swap(MetricsRegistryRegistrationHandle &other) BSLS_KEYWORD_NOEXCEPT
Definition bdlm_metricsregistry.h:402
MetricsRegistryRegistrationHandle & operator=(bslmf::MovableRef< MetricsRegistryRegistrationHandle > original) BSLS_KEYWORD_NOEXCEPT
bool isRegistered() const
Definition bdlm_metricsregistry.h:411
MetricsRegistryRegistrationHandle()
Definition bdlm_metricsregistry.h:394
MetricsRegistryRegistrationHandle(bslmf::MovableRef< MetricsRegistryRegistrationHandle > original) BSLS_KEYWORD_NOEXCEPT
Definition bdlm_metricsregistry.h:197
MetricsAdapter::Callback Callback
Definition bdlm_metricsregistry.h:204
MetricsAdapter::CallbackHandle CallbackHandle
Definition bdlm_metricsregistry.h:205
bslma::Allocator * allocator() const
Return the allocator used by this object to supply memory.
BSLMF_NESTED_TRAIT_DECLARATION(MetricsRegistry, bslma::UsesBslmaAllocator)
static MetricsRegistry & defaultInstance()
Return a non-const reference to the metrics registry singleton.
int setMetricsAdapter(MetricsAdapter *adapter)
MetricsRegistry(bslma::Allocator *basicAllocator=0)
int removeMetricsAdapter(MetricsAdapter *adapter)
int registerCollectionCallback(MetricsRegistryRegistrationHandle *result, const bdlm::MetricDescriptor &descriptor, const Callback &callback)
int numRegisteredCollectionCallbacks() const
Return the number of registered metrics collection callbacks.
Forward declaration.
Definition bslstl_function.h:946
Definition bslstl_sharedptr.h:1838
Definition bslstl_sharedptr.h:3773
bool expired() const BSLS_KEYWORD_NOEXCEPT
Definition bslstl_sharedptr.h:6025
static void swap(T *a, T *b)
Definition bslalg_swaputil.h:182
Definition bslma_allocator.h:545
Definition bslmf_movableref.h:752
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
#define BSLS_KEYWORD_DELETED
Definition bsls_keyword.h:651
#define BSLS_KEYWORD_NOEXCEPT
Definition bsls_keyword.h:674
Definition bdlm_instancecount.h:100
Definition bslma_usesbslmaallocator.h:344
Definition bslmf_movableref.h:795