BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bdlm_metricsadapter.h
Go to the documentation of this file.
1/// @file bdlm_metricsadapter.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdlm_metricsadapter.h -*-C++-*-
8#ifndef INCLUDED_BDLM_METRICSADAPTER
9#define INCLUDED_BDLM_METRICSADAPTER
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bdlm_metricsadapter bdlm_metricsadapter
15/// @brief Provide an abstract interface for metrics registration mechanisms.
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdlm
19/// @{
20/// @addtogroup bdlm_metricsadapter
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdlm_metricsadapter-purpose"> Purpose</a>
25/// * <a href="#bdlm_metricsadapter-classes"> Classes </a>
26/// * <a href="#bdlm_metricsadapter-description"> Description </a>
27/// * <a href="#bdlm_metricsadapter-thread-safety"> Thread Safety </a>
28/// * <a href="#bdlm_metricsadapter-usage"> Usage </a>
29/// * <a href="#bdlm_metricsadapter-example-1-implementing-the-bdlm-metricsadapter-protocol"> Example 1: Implementing the bdlm::MetricsAdapter Protocol </a>
30///
31/// # Purpose {#bdlm_metricsadapter-purpose}
32/// Provide an abstract interface for metrics registration mechanisms.
33///
34/// @deprecated This component is not ready for public use.
35///
36/// # Classes {#bdlm_metricsadapter-classes}
37///
38/// - bdlm::MetricsAdapter: protocol class for registration and de-registration
39///
40/// # Description {#bdlm_metricsadapter-description}
41/// This component provides the base-level protocol (pure abstract
42/// interface) class, `bdlm::MetricsAdapter`, that serves as a ubiquitous
43/// vocabulary type for various metrics registration mechanisms. Clients of
44/// this abstract base class will typically accept a supplied metrics adapter
45/// (often at construction) and use its `registerCollectionCallback` and
46/// `removeCollectionCallback` methods to register a collection object with a
47/// monitoring mechanism and remove a collection object from monitoring,
48/// respectively.
49///
50/// The `registerCollectionCallback` method supplies a metric descriptor and a
51/// collection callback to a monitoring system. Specific monitoring systems may
52/// adapt the provided metric descriptor attributes to their needs.
53///
54/// ## Thread Safety {#bdlm_metricsadapter-thread-safety}
55///
56///
57/// This class is *minimally* *thread-safe* (see {@ref bsldoc_glossary |Minimally
58/// Thread-Safe}).
59///
60/// ## Usage {#bdlm_metricsadapter-usage}
61///
62///
63/// This section illustrates intended use of this component.
64///
65/// ### Example 1: Implementing the bdlm::MetricsAdapter Protocol {#bdlm_metricsadapter-example-1-implementing-the-bdlm-metricsadapter-protocol}
66///
67///
68/// This example demonstrates an elided concrete implementation of the
69/// `bdlm::MetricsAdapter` protocol that allows for registering metric callback
70/// functions with a monitoring system.
71///
72/// First, we define the interface of a limited `my_MetricsMonitor` class that
73/// allows only one metric collection function to be registered:
74/// @code
75/// /// This class implements a metric monitoring system.
76/// class my_MetricsMonitor {
77///
78/// // ...
79///
80/// // DATA
81/// bdlm::Metric d_value; // metric supplied to
82/// // 'd_callback'
83///
84/// bsl::string d_name; // register metric name
85///
86/// bdlm::MetricsAdapter::Callback d_callback; // registered callback
87///
88/// public:
89/// // ...
90///
91/// // MANIPULATORS
92///
93/// /// Register the specified `callback` with this monitoring system,
94/// /// using the specified `name` for display purposes. Return a
95/// /// callback handle to be used with `removeCallback`.
96/// bdlm::MetricsAdapter::CallbackHandle registerCallback(
97/// const bsl::string& name,
98/// const bdlm::MetricsAdapter::Callback callback);
99///
100/// /// Remove the callback associated with the specified `handle`.
101/// /// Return 0 on success, or a non-zero value if `handle` cannot be
102/// /// found.
103/// int removeCallback(const bdlm::MetricsAdapter::CallbackHandle& handle);
104///
105/// /// Invoke the registered callback.
106/// void update();
107///
108/// // ACCESSORS
109///
110/// /// Return the name of the registered metric.
111/// const bsl::string& name() const;
112///
113/// /// Return the value computed by the invocations of the registered
114/// /// callback.
115/// double value() const;
116/// };
117/// @endcode
118/// Then, we implement the functions:
119/// @code
120/// // MANIPULATORS
121/// bdlm::MetricsAdapter::CallbackHandle my_MetricsMonitor::registerCallback(
122/// const bsl::string& name,
123/// const bdlm::MetricsAdapter::Callback callback)
124/// {
125/// d_value = bdlm::Metric::Gauge(0.0);
126/// d_name = name;
127/// d_callback = callback;
128///
129/// return 1;
130/// }
131///
132/// int my_MetricsMonitor::removeCallback(
133/// const bdlm::MetricsAdapter::CallbackHandle& handle)
134/// {
135/// (void)handle;
136/// return 0;
137/// }
138///
139/// void my_MetricsMonitor::update()
140/// {
141/// d_callback(&d_value);
142/// }
143///
144/// // ACCESSORS
145/// const bsl::string& my_MetricsMonitor::name() const
146/// {
147/// return d_name;
148/// }
149///
150/// double my_MetricsMonitor::value() const
151/// {
152/// return d_value.theGauge();
153/// }
154/// @endcode
155/// Next, we define the implementation class of the `bdlm::MetricsAdapter`
156/// protocol:
157/// @code
158/// /// This class implements an interface for clients and suppliers of
159/// /// metrics adapters.
160/// class my_MetricsAdapter : public bdlm::MetricsAdapter {
161///
162/// // DATA
163/// my_MetricsMonitor *d_monitor_p; // pointer to monitor to use for
164/// // metrics (held not owned)
165///
166/// public:
167/// // CREATORS
168///
169/// /// Create a `my_MetricsAdapter` using the specified `monitor` for
170/// /// registered callbacks.
171/// my_MetricsAdapter(my_MetricsMonitor *monitor);
172///
173/// /// Destroy this object.
174/// ~my_MetricsAdapter();
175///
176/// // MANIPULATORS
177///
178/// /// Register the specified `callback` with a monitoring system,
179/// /// using the specified `metricDescriptor` for the registration.
180/// /// Return the callback handle to be used with
181/// /// `removeCollectionCallback`. Note the information used for
182/// /// registration is implementation dependant, and may involve values
183/// /// computed from the supplied arguments.
184/// CallbackHandle registerCollectionCallback(
185/// const bdlm::MetricDescriptor& metricDescriptor,
186/// const Callback& callback);
187///
188/// /// Remove the callback associated with the specified `handle`.
189/// /// Return 0 on success, or a non-zero value if `handle` cannot be
190/// /// found.
191/// int removeCollectionCallback(const CallbackHandle& handle);
192/// };
193/// @endcode
194/// Then, we implement the methods of `myMetricsAdapter`:
195/// @code
196/// // CREATORS
197/// my_MetricsAdapter::my_MetricsAdapter(my_MetricsMonitor *monitor)
198/// : d_monitor_p(monitor)
199/// {
200/// }
201///
202/// my_MetricsAdapter::~my_MetricsAdapter()
203/// {
204/// }
205///
206/// // MANIPULATORS
207/// bdlm::MetricsAdapter::CallbackHandle
208/// my_MetricsAdapter::registerCollectionCallback(
209/// const bdlm::MetricDescriptor& metricDescriptor,
210/// const Callback& callback)
211/// {
212/// bsl::string name = metricDescriptor.metricNamespace() + '.'
213/// + metricDescriptor.metricName() + '.'
214/// + metricDescriptor.objectTypeName() + '.'
215/// + metricDescriptor.objectTypeAbbreviation() + '.'
216/// + metricDescriptor.objectIdentifier();
217///
218/// return d_monitor_p->registerCallback(name, callback);
219/// }
220///
221/// int my_MetricsAdapter::removeCollectionCallback(
222/// const bdlm::MetricsAdapter::CallbackHandle& handle)
223/// {
224/// return d_monitor_p->removeCallback(handle);
225/// }
226/// @endcode
227/// Next, we provide the metric method, `my_metric`, which will compute its
228/// invocation count:
229/// @code
230/// void my_metric(BloombergLP::bdlm::Metric *value)
231/// {
232/// *value = value->theGauge() + 1.0;
233/// }
234/// @endcode
235/// Then, we instantiate a `my_MetricsMonitor` and a `myMetricsAdapter`:
236/// @code
237/// my_MetricsMonitor monitor;
238/// my_MetricsAdapter adapter(&monitor);
239/// @endcode
240/// Next, we construct a `bdlm::MetricDescriptor`, register the `my_metric`
241/// method with the `monitor`, and verify the `monitor` has the expected name
242/// for the metric:
243/// @code
244/// bdlm::MetricDescriptor descriptor("a", "b", 1, "c", "d", "e");
245///
246/// adapter.registerCollectionCallback(descriptor, my_metric);
247///
248/// assert(monitor.name() == "a.b.c.d.e");
249/// @endcode
250/// Now, we invoke the `update` method a few times:
251/// @code
252/// monitor.update();
253/// monitor.update();
254/// monitor.update();
255/// @endcode
256/// Finally, we verify the metric has the expected value:
257/// @code
258/// assert(monitor.value() == 3.0);
259/// @endcode
260/// @}
261/** @} */
262/** @} */
263
264/** @addtogroup bdl
265 * @{
266 */
267/** @addtogroup bdlm
268 * @{
269 */
270/** @addtogroup bdlm_metricsadapter
271 * @{
272 */
273
274#include <bdlscm_version.h>
275
276#include <bdlm_metric.h>
277
278#include <bsl_functional.h>
279
280
281namespace bdlm {
282
283class MetricDescriptor;
284
285 // ====================
286 // class MetricsAdapter
287 // ====================
288
289/// This protocol class provides a pure abstract interface and contract for
290/// clients and suppliers of metrics adapters.
291///
292/// See @ref bdlm_metricsadapter
294
295 public:
296 // TYPES
297 typedef int CallbackHandle; // identifies a
298 // callback functor
299
300 typedef bsl::function<void (Metric *)> Callback; // callback functor
301
302 public:
303 // CREATORS
304
305 /// Destroy this `MetricsAdapter`.
306 virtual ~MetricsAdapter() = 0;
307
308 // MANIPULATORS
309
310 /// Register the specified `callback` with a monitoring system, using
311 /// the specified `metricDescriptor` for the registration. Return the
312 /// callback handle to be used with `removeCollectionCallback`. Note
313 /// the information used for registration is implementation dependant,
314 /// and may involve values computed from the supplied arguments.
316 const MetricDescriptor& metricDescriptor,
317 const Callback& callback) = 0;
318
319 /// Remove the callback associated with the specified `handle`. Return
320 /// 0 on success, or a non-zero value if `handle` cannot be found.
321 virtual int removeCollectionCallback(const CallbackHandle& handle) = 0;
322};
323
324} // close package namespace
325
326
327#endif
328
329// ----------------------------------------------------------------------------
330// Copyright 2024 Bloomberg Finance L.P.
331//
332// Licensed under the Apache License, Version 2.0 (the "License");
333// you may not use this file except in compliance with the License.
334// You may obtain a copy of the License at
335//
336// http://www.apache.org/licenses/LICENSE-2.0
337//
338// Unless required by applicable law or agreed to in writing, software
339// distributed under the License is distributed on an "AS IS" BASIS,
340// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
341// See the License for the specific language governing permissions and
342// limitations under the License.
343// ----------------------------- END-OF-FILE ----------------------------------
344
345/** @} */
346/** @} */
347/** @} */
Definition bdlm_metricdescriptor.h:142
Definition bdlm_metric.h:77
Definition bdlm_metricsadapter.h:293
int CallbackHandle
Definition bdlm_metricsadapter.h:297
virtual int removeCollectionCallback(const CallbackHandle &handle)=0
bsl::function< void(Metric *)> Callback
Definition bdlm_metricsadapter.h:300
virtual CallbackHandle registerCollectionCallback(const MetricDescriptor &metricDescriptor, const Callback &callback)=0
virtual ~MetricsAdapter()=0
Destroy this MetricsAdapter.
Forward declaration.
Definition bslstl_function.h:934
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bdlm_instancecount.h:101