BDE 4.39.x Production Release
Loading...
Searching...
No Matches
balm_defaultmetricsmanager.h
Go to the documentation of this file.
1/// @file balm_defaultmetricsmanager.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// balm_defaultmetricsmanager.h -*-C++-*-
8#ifndef INCLUDED_BALM_DEFAULTMETRICSMANAGER
9#define INCLUDED_BALM_DEFAULTMETRICSMANAGER
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup balm_defaultmetricsmanager balm_defaultmetricsmanager
15/// @brief Provide for a default instance of the metrics manager.
16/// @addtogroup bal
17/// @{
18/// @addtogroup balm
19/// @{
20/// @addtogroup balm_defaultmetricsmanager
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#balm_defaultmetricsmanager-purpose"> Purpose</a>
25/// * <a href="#balm_defaultmetricsmanager-classes"> Classes </a>
26/// * <a href="#balm_defaultmetricsmanager-description"> Description </a>
27/// * <a href="#balm_defaultmetricsmanager-alternative-systems-for-telemetry"> Alternative Systems for Telemetry </a>
28/// * <a href="#balm_defaultmetricsmanager-thread-safety"> Thread Safety </a>
29/// * <a href="#balm_defaultmetricsmanager-usage"> Usage </a>
30/// * <a href="#balm_defaultmetricsmanager-example-1-create-and-access-the-default-balm-metricsmanager-instance"> Example 1: Create and Access the Default balm::MetricsManager Instance </a>
31///
32/// # Purpose {#balm_defaultmetricsmanager-purpose}
33/// Provide for a default instance of the metrics manager.
34///
35/// # Classes {#balm_defaultmetricsmanager-classes}
36///
37/// - balm::DefaultMetricsManager: namespace for the default instance
38/// - balm::DefaultMetricsManagerScopedGuard: guard for the default instance
39///
40/// @see balm_metricsmanager, balm_metric
41///
42/// # Description {#balm_defaultmetricsmanager-description}
43/// This component provides a namespace for a default instance of
44/// the `balm::MetricsManager`. This `balm::DefaultMetricsManager` provides
45/// static operations to create, access, and destroy the default instance of the
46/// `balm::MetricsManager`. The `balm::DefaultMetricsManagedScopedGuard`
47/// provides a proctor that creates a default metrics manager on construction
48/// and destroys it on destruction.
49///
50/// `balm::DefaultMetricsManagerScopedGuard` is also here.
51///
52/// ## Alternative Systems for Telemetry {#balm_defaultmetricsmanager-alternative-systems-for-telemetry}
53///
54///
55/// Bloomberg software may alternatively use the GUTS telemetry API, which is
56/// integrated into Bloomberg infrastructure.
57///
58/// ## Thread Safety {#balm_defaultmetricsmanager-thread-safety}
59///
60///
61/// The default `balm::MetricsManager` instance, once initialized, can be safely
62/// accessed from multiple threads. However, the `create` and `destroy`
63/// operations supplied by the `balm::DefaultMetricsManager` are *not*
64/// *thread-safe*. Care must be taken, particularly when releasing the
65/// instance. The expected usage is that the instance will be created during
66/// the initialization of an application (while the task has a single thread)
67/// and that it will be destroyed just prior to termination (when there is
68/// similarly just a single thread).
69///
70/// ## Usage {#balm_defaultmetricsmanager-usage}
71///
72///
73/// The following examples demonstrate how to create, configure, and destroy
74/// the default `balm::MetricsManager` instance.
75///
76/// ### Example 1: Create and Access the Default balm::MetricsManager Instance {#balm_defaultmetricsmanager-example-1-create-and-access-the-default-balm-metricsmanager-instance}
77///
78///
79/// This example demonstrates how to create the default `balm::MetricManager`
80/// instance and publish a single metric to the console. See the documentation
81/// of @ref balm_metric and @ref balm_metricsmanager for information on how to record
82/// metrics.
83///
84/// First we create a `balm::DefaultMetricsManagerScopedGuard`, which manages
85/// the lifetime of the default metrics manager instance. At construction, we
86/// provide the `balm::DefaultMetricsManagerScopedGuard` an output stream
87/// (`stdout`) to which it will publish metrics. Note that the default metrics
88/// manager is intended to be created and destroyed by the *owner* of `main`.
89/// The instance should be created during the initialization of an application
90/// (while the task has a single thread) and destroyed just prior to termination
91/// (when there is similarly a single thread).
92/// @code
93/// int main(int argc, char *argv[])
94/// {
95///
96/// // ...
97///
98/// balm::DefaultMetricsManagerScopedGuard managerGuard(bsl::cout);
99/// @endcode
100/// Once the default instance has been created, it can be accessed using the
101/// static `instance` method.
102/// @code
103/// balm::MetricsManager *manager = balm::DefaultMetricsManager::instance();
104/// assert(0 != manager);
105/// @endcode
106/// The default metrics manager, by default, is configured with a
107/// `balm::StreamPublisher` object that will publish all recorded metrics to the
108/// consoled. We use the default `manager` instance to update the collector
109/// for a single metric, and then publish all metrics.
110/// @code
111/// balm::Collector *myMetric =
112/// manager->collectorRepository().getDefaultCollector(
113/// "MyCategory", "MyMetric");
114/// myMetric->update(10);
115/// manager->publishAll();
116///
117/// // ... rest of program elided ...
118/// }
119/// @endcode
120/// The output of this example would look similar to:
121/// @code
122/// 05FEB2009_19:20:12.697+0000 1 Records
123/// Elapsed Time: 0.009311s
124/// MyCategory.MyMetric [ count = 1, total = 10, min = 10, max = 10 ]
125/// @endcode
126/// Note that the default metrics manager will be destroyed when `managerGuard`
127/// exits this scope and is destroyed. Clients that choose to explicitly call
128/// `balm::DefaultMetricsManager::create()` must also explicitly call
129/// `balm::DefaultMetricsManager::destroy()`.
130/// @}
131/** @} */
132/** @} */
133
134/** @addtogroup bal
135 * @{
136 */
137/** @addtogroup balm
138 * @{
139 */
140/** @addtogroup balm_defaultmetricsmanager
141 * @{
142 */
143
144#include <balscm_version.h>
145
146#include <bslma_allocator.h>
147
148#include <bsl_iosfwd.h>
149
150
151
152
153namespace balm {
154
155class MetricsManager;
156
157 // ============================
158 // struct DefaultMetricsManager
159 // ============================
160
161/// This struct provides a namespace for static functions that create,
162/// access, and destroy the default instance of the `MetricsManager`. The
163/// expected usage is that the default instance will be created during the
164/// initialization of an application (while the task has a single thread)
165/// and that it will be destroyed just prior to termination (when there is
166/// similarly a single thread).
167///
168/// See @ref balm_defaultmetricsmanager
170
171 private:
172 // CLASS DATA
173 static MetricsManager *s_singleton_p; // metrics manager default
174 // instance
175
176 static bslma::Allocator *s_allocator_p; // allocator used to initialize
177 // the singleton
178 public:
179 // CLASS METHODS
180
181 /// If the optionally specified `manager` is not 0, return `manager`;
182 /// otherwise return the address of the default metrics manager
183 /// instance, or 0 if the default metrics manager instance has not yet been created or has already been destroyed.
184 ///
185 /// \note Note that this
186 /// operation is logically equivalent to
187 /// `manager ? manager : instance()`.
189
190 /// Create the default `MetricsManager` instance and return the address
191 /// of the modifiable created instance. Optionally specify a
192 /// `basicAllocator` used to supply memory. If `basicAllocator` is 0,
193 /// the currently installed global allocator is used.
194 ///
195 /// \pre The behavior is undefined unless `0 == MetricsManager::instance()` prior to calling
196 /// this method, or if this method is called from one thread while
197 /// another thread is attempting to access the default metrics manager instance (i.e., this method is *not* thread-safe).
198 ///
199 /// \note Note that the
200 /// returned default metrics manager instance is not configured with a
201 /// publisher; clients must create a `Publisher` and add it to the
202 /// default metrics manager in order to publish metrics.
203 static MetricsManager *create(bslma::Allocator *basicAllocator = 0);
204
205 /// Create the default `MetricsManager` instance and configure it with
206 /// a `StreamPublisher` that will publish recorded metrics to the
207 /// specified `stream`, then return the address of the modifiable
208 /// created metrics manager instance. Optionally specify
209 /// `basicAllocator` to use to obtain memory. If `basicAllocator` is 0,
210 /// the currently installed global allocator is used.
211 ///
212 /// \pre The behavior is undefined unless `0 == MetricsManager::instance()` prior to calling
213 /// this method, or if this method is called from one thread while
214 /// another thread is attempting to access the default metrics manager
215 /// instance (i.e., this method is *not* thread-safe).
216 static MetricsManager *create(bsl::ostream& stream,
217 bslma::Allocator *basicAllocator = 0);
218
219 /// Return the default instance of the `MetricsManager` or 0 if the
220 /// default instance has not yet been created or has already been
221 /// destroyed.
222 static MetricsManager *instance();
223
224 /// Destroy the default instance of `MetricsManager`. After this
225 /// method returns, `instance()` will return 0.
226 ///
227 /// \pre The behavior is undefined if `instance()` is 0 or if this method is called from one
228 /// thread while another thread is accessing the default metrics
229 /// manager instance (i.e., this method is *not* thread-safe).
230 static void destroy();
231};
232
233 // ======================================
234 // class DefaultMetricsManagerScopedGuard
235 // ======================================
236
237/// This class implements a scoped guard that, on construction, creates the
238/// default instance of the metrics manager, and, on destruction, destroys that instance.
239///
240/// \note Note that the behavior is undefined if the default
241/// instance of the metrics manager is created before creating this guard,
242/// or if the default instance is externally destroyed before destroying
243/// this guard.
244///
245/// See @ref balm_defaultmetricsmanager
247
248 private:
249 // NOT IMPLEMENTED
253
254 public:
255 // CREATORS
256
257 /// Create a scoped guard which invokes
258 /// `DefaultMetricsManager::create()` to create a default metrics
259 /// manager instance that is configured with a stream publisher that
260 /// will publish collected metrics to the specified `stream`.
261 /// Optionally specify a `basicAllocator` used to supply memory. If
262 /// `basicAllocator` is 0, the currently installed global allocator is used.
263 ///
264 /// \pre The behavior is undefined unless
265 /// `DefaultMetricsManager::instance()` is 0 prior to creating the
266 /// guard.
267 DefaultMetricsManagerScopedGuard(bsl::ostream& stream,
268 bslma::Allocator *basicAllocator = 0);
269
270 /// Create a scoped guard which invokes the
271 /// `DefaultMetricsManager::create` method. Optionally specify a
272 /// `basicAllocator` used to obtain memory. If `basicAllocator` is 0,
273 /// the currently installed global allocator is used.
274 ///
275 /// \pre The behavior is undefined unless `0 == DefaultMetricsManager::instance()` prior to creating the guard.
276 ///
277 /// \note Note that the default metrics manager instance
278 /// is not configured with a publisher; clients must create a
279 /// `Publisher` object and add it to the default metrics manager in
280 /// order to publish metrics.
282
283 /// Destroy this scoped guard which invokes `DefaultMetricsManager::destroy()`.
284 ///
285 /// \pre The behavior is undefined if
286 /// the default instance of the metrics manager is externally destroyed
287 /// prior to this destructor being invoked.
289
290 // ACCESSORS
291
292 /// Return the address of the `MetricsManager` object managed by this scoped guard.
293 ///
294 /// \pre The behavior is undefined if the default instance of
295 /// the metrics manager is externally destroyed, or if the returned
296 /// address is retained after this scoped guard is destroyed.
297 MetricsManager *instance() const;
298};
299
300// ============================================================================
301// INLINE DEFINITIONS
302// ============================================================================
303
304 // ---------------------------
305 // class DefaultMetricsManager
306 // ---------------------------
307
308// CLASS METHODS
309inline
311{
312 return s_singleton_p;
313}
314
315inline
317 MetricsManager *manager)
318{
319 return manager ? manager : s_singleton_p;
320}
321
322 // --------------------------------------
323 // class DefaultMetricsManagerScopedGuard
324 // --------------------------------------
325
326// CREATORS
327inline
328DefaultMetricsManagerScopedGuard::DefaultMetricsManagerScopedGuard(
329 bsl::ostream& stream,
330 bslma::Allocator *basicAllocator)
331{
332 DefaultMetricsManager::create(stream, basicAllocator);
333}
334
335inline
336DefaultMetricsManagerScopedGuard::DefaultMetricsManagerScopedGuard(
337 bslma::Allocator *basicAllocator)
338{
339 DefaultMetricsManager::create(basicAllocator);
340}
341
342inline
347
348// ACCESSORS
349inline
354} // close package namespace
355
356
357
358#endif
359
360// ----------------------------------------------------------------------------
361// Copyright 2015 Bloomberg Finance L.P.
362//
363// Licensed under the Apache License, Version 2.0 (the "License");
364// you may not use this file except in compliance with the License.
365// You may obtain a copy of the License at
366//
367// http://www.apache.org/licenses/LICENSE-2.0
368//
369// Unless required by applicable law or agreed to in writing, software
370// distributed under the License is distributed on an "AS IS" BASIS,
371// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
372// See the License for the specific language governing permissions and
373// limitations under the License.
374// ----------------------------- END-OF-FILE ----------------------------------
375
376/** @} */
377/** @} */
378/** @} */
Definition balm_defaultmetricsmanager.h:246
~DefaultMetricsManagerScopedGuard()
Definition balm_defaultmetricsmanager.h:343
MetricsManager * instance() const
Definition balm_defaultmetricsmanager.h:350
Definition balm_metricsmanager.h:490
Definition bslma_allocator.h:545
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition balm_bdlmmetricsadapter.h:142
Definition balm_defaultmetricsmanager.h:169
static MetricsManager * create(bsl::ostream &stream, bslma::Allocator *basicAllocator=0)
static MetricsManager * create(bslma::Allocator *basicAllocator=0)
static MetricsManager * manager(MetricsManager *manager=0)
Definition balm_defaultmetricsmanager.h:316
static MetricsManager * instance()
Definition balm_defaultmetricsmanager.h:310