BDE 4.14.0 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).
168
169 private:
170 // CLASS DATA
171 static MetricsManager *s_singleton_p; // metrics manager default
172 // instance
173
174 static bslma::Allocator *s_allocator_p; // allocator used to initialize
175 // the singleton
176 public:
177 // CLASS METHODS
178
179 /// If the optionally specified `manager` is not 0, return `manager`;
180 /// otherwise return the address of the default metrics manager
181 /// instance, or 0 if the default metrics manager instance has not yet
182 /// been created or has already been destroyed. Note that this
183 /// operation is logically equivalent to
184 /// `manager ? manager : instance()`.
186
187 /// Create the default `MetricsManager` instance and return the address
188 /// of the modifiable created instance. Optionally specify a
189 /// `basicAllocator` used to supply memory. If `basicAllocator` is 0,
190 /// the currently installed global allocator is used. The behavior is
191 /// undefined unless `0 == MetricsManager::instance()` prior to calling
192 /// this method, or if this method is called from one thread while
193 /// another thread is attempting to access the default metrics manager
194 /// instance (i.e., this method is *not* thread-safe). Note that the
195 /// returned default metrics manager instance is not configured with a
196 /// publisher; clients must create a `Publisher` and add it to the
197 /// default metrics manager in order to publish metrics.
198 static MetricsManager *create(bslma::Allocator *basicAllocator = 0);
199
200 /// Create the default `MetricsManager` instance and configure it with
201 /// a `StreamPublisher` that will publish recorded metrics to the
202 /// specified `stream`, then return the address of the modifiable
203 /// created metrics manager instance. Optionally specify
204 /// `basicAllocator` to use to obtain memory. If `basicAllocator` is 0,
205 /// the currently installed global allocator is used. The behavior is
206 /// undefined unless `0 == MetricsManager::instance()` prior to calling
207 /// this method, or if this method is called from one thread while
208 /// another thread is attempting to access the default metrics manager
209 /// instance (i.e., this method is *not* thread-safe).
210 static MetricsManager *create(bsl::ostream& stream,
211 bslma::Allocator *basicAllocator = 0);
212
213 /// Return the default instance of the `MetricsManager` or 0 if the
214 /// default instance has not yet been created or has already been
215 /// destroyed.
216 static MetricsManager *instance();
217
218 /// Destroy the default instance of `MetricsManager`. After this
219 /// method returns, `instance()` will return 0. The behavior is
220 /// undefined if `instance()` is 0 or if this method is called from one
221 /// thread while another thread is accessing the default metrics
222 /// manager instance (i.e., this method is *not* thread-safe).
223 static void destroy();
224};
225
226 // ======================================
227 // class DefaultMetricsManagerScopedGuard
228 // ======================================
229
230/// This class implements a scoped guard that, on construction, creates the
231/// default instance of the metrics manager, and, on destruction, destroys
232/// that instance. Note that the behavior is undefined if the default
233/// instance of the metrics manager is created before creating this guard,
234/// or if the default instance is externally destroyed before destroying
235/// this guard.
236///
237/// See @ref balm_defaultmetricsmanager
239
240 // NOT IMPLEMENTED
244
245 public:
246 // CREATORS
247
248 /// Create a scoped guard which invokes
249 /// `DefaultMetricsManager::create()` to create a default metrics
250 /// manager instance that is configured with a stream publisher that
251 /// will publish collected metrics to the specified `stream`.
252 /// Optionally specify a `basicAllocator` used to supply memory. If
253 /// `basicAllocator` is 0, the currently installed global allocator is
254 /// used. The behavior is undefined unless
255 /// `DefaultMetricsManager::instance()` is 0 prior to creating the
256 /// guard.
257 DefaultMetricsManagerScopedGuard(bsl::ostream& stream,
258 bslma::Allocator *basicAllocator = 0);
259
260 /// Create a scoped guard which invokes the
261 /// `DefaultMetricsManager::create` method. Optionally specify a
262 /// `basicAllocator` used to obtain memory. If `basicAllocator` is 0,
263 /// the currently installed global allocator is used. The behavior is
264 /// undefined unless `0 == DefaultMetricsManager::instance()` prior to
265 /// creating the guard. Note that the default metrics manager instance
266 /// is not configured with a publisher; clients must create a
267 /// `Publisher` object and add it to the default metrics manager in
268 /// order to publish metrics.
270
271 /// Destroy this scoped guard which invokes
272 /// `DefaultMetricsManager::destroy()`. The behavior is undefined if
273 /// the default instance of the metrics manager is externally destroyed
274 /// prior to this destructor being invoked.
276
277 // ACCESSORS
278
279 /// Return the address of the `MetricsManager` object managed by this
280 /// scoped guard. The behavior is undefined if the default instance of
281 /// the metrics manager is externally destroyed, or if the returned
282 /// address is retained after this scoped guard is destroyed.
283 MetricsManager *instance() const;
284};
285
286// ============================================================================
287// INLINE DEFINITIONS
288// ============================================================================
289
290 // ---------------------------
291 // class DefaultMetricsManager
292 // ---------------------------
293
294// CLASS METHODS
295inline
297{
298 return s_singleton_p;
299}
300
301inline
303 MetricsManager *manager)
304{
305 return manager ? manager : s_singleton_p;
306}
307
308 // --------------------------------------
309 // class DefaultMetricsManagerScopedGuard
310 // --------------------------------------
311
312// CREATORS
313inline
314DefaultMetricsManagerScopedGuard::DefaultMetricsManagerScopedGuard(
315 bsl::ostream& stream,
316 bslma::Allocator *basicAllocator)
317{
318 DefaultMetricsManager::create(stream, basicAllocator);
319}
320
321inline
322DefaultMetricsManagerScopedGuard::DefaultMetricsManagerScopedGuard(
323 bslma::Allocator *basicAllocator)
324{
325 DefaultMetricsManager::create(basicAllocator);
326}
327
328inline
333
334// ACCESSORS
335inline
340} // close package namespace
341
342
343
344#endif
345
346// ----------------------------------------------------------------------------
347// Copyright 2015 Bloomberg Finance L.P.
348//
349// Licensed under the Apache License, Version 2.0 (the "License");
350// you may not use this file except in compliance with the License.
351// You may obtain a copy of the License at
352//
353// http://www.apache.org/licenses/LICENSE-2.0
354//
355// Unless required by applicable law or agreed to in writing, software
356// distributed under the License is distributed on an "AS IS" BASIS,
357// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
358// See the License for the specific language governing permissions and
359// limitations under the License.
360// ----------------------------- END-OF-FILE ----------------------------------
361
362/** @} */
363/** @} */
364/** @} */
Definition balm_defaultmetricsmanager.h:238
~DefaultMetricsManagerScopedGuard()
Definition balm_defaultmetricsmanager.h:329
MetricsManager * instance() const
Definition balm_defaultmetricsmanager.h:336
Definition balm_metricsmanager.h:490
Definition bslma_allocator.h:457
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition balm_bdlmmetricsadapter.h:141
Definition balm_defaultmetricsmanager.h:167
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:302
static MetricsManager * instance()
Definition balm_defaultmetricsmanager.h:296