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