BDE 4.14.0 Production release
Loading...
Searching...
No Matches
balm_collector.h
Go to the documentation of this file.
1/// @file balm_collector.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// balm_collector.h -*-C++-*-
8#ifndef INCLUDED_BALM_COLLECTOR
9#define INCLUDED_BALM_COLLECTOR
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: balm_collector.h,v 1.7 2008/04/16 20:00:49 hversche Exp $")
13
14/// @defgroup balm_collector balm_collector
15/// @brief Provide a container for collecting and aggregating metric values.
16/// @addtogroup bal
17/// @{
18/// @addtogroup balm
19/// @{
20/// @addtogroup balm_collector
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#balm_collector-purpose"> Purpose</a>
25/// * <a href="#balm_collector-classes"> Classes </a>
26/// * <a href="#balm_collector-description"> Description </a>
27/// * <a href="#balm_collector-alternative-systems-for-telemetry"> Alternative Systems for Telemetry </a>
28/// * <a href="#balm_collector-thread-safety"> Thread Safety </a>
29/// * <a href="#balm_collector-usage"> Usage </a>
30/// * <a href="#balm_collector-example-1-basic-usage"> Example 1: Basic Usage </a>
31///
32/// # Purpose {#balm_collector-purpose}
33/// Provide a container for collecting and aggregating metric values.
34///
35/// # Classes {#balm_collector-classes}
36///
37/// - balm::Collector: a container for collecting and aggregating metric values
38///
39/// @see balm_collectorrepository, balm_metric
40///
41/// # Description {#balm_collector-description}
42/// This component provides a class for collecting and aggregating
43/// the values of a metric. The collector records the number of times an event
44/// occurs as well as an associated measurement value. This component does
45/// *not* define what constitutes an event or what the value measures. The
46/// collector manages, in a thread-safe manner, the count of event occurrences
47/// and the aggregated minimum, maximum, and total of the measured metric
48/// value. This collector class provides operations to update the aggregated
49/// value, a `load` operator to populate a `balm::MetricRecord` with the current
50/// state of the collector, a `reset` method to reset the current state of the
51/// collector, and finally a combined `loadAndReset` method that performs both
52/// a load and a reset as an atomic operation. Note that in practice, most
53/// clients should not need to access a `balm::Collector` directly, but instead
54/// use it through another type (see @ref balm_metric ).
55///
56/// ## Alternative Systems for Telemetry {#balm_collector-alternative-systems-for-telemetry}
57///
58///
59/// Bloomberg software may alternatively use the GUTS telemetry API, which is
60/// integrated into Bloomberg infrastructure.
61///
62/// ## Thread Safety {#balm_collector-thread-safety}
63///
64///
65/// `balm::Collector` is fully *thread-safe*, meaning that all non-creator
66/// operations on a given instance can be safely invoked simultaneously from
67/// multiple threads.
68///
69/// ## Usage {#balm_collector-usage}
70///
71///
72/// This section illustrates intended use of this component.
73///
74/// ### Example 1: Basic Usage {#balm_collector-example-1-basic-usage}
75///
76///
77/// The following example creates a `balm::Collector`, modifies its values, then
78/// collects a `balm::MetricRecord`.
79///
80/// We start by creating a `balm::MetricId` object by hand, but in practice, an
81/// id should be obtained from a `balm::MetricRegistry` object (such as the one
82/// owned by a `balm::MetricsManager`):
83/// @code
84/// balm::Category myCategory("MyCategory");
85/// balm::MetricDescription description(&myCategory, "MyMetric");
86/// balm::MetricId myMetric(&description);
87/// @endcode
88/// Now we create a `balm::Collector` object for `myMetric` and use the `update`
89/// method to update its collected value:
90/// @code
91/// balm::Collector collector(myMetric);
92///
93/// collector.update(1.0);
94/// collector.update(3.0);
95/// @endcode
96/// The collector accumulated the values 1 and 3. The result should have a
97/// count of 2, a total of 4 (3 + 1), a max of 3 (max(3, 1)), and a min of 1
98/// (min(3, 1)).
99/// @code
100/// balm::MetricRecord record;
101/// collector.loadAndReset(&record);
102///
103/// assert(myMetric == record.metricId());
104/// assert(2 == record.count());
105/// assert(4 == record.total());
106/// assert(1.0 == record.min());
107/// assert(3.0 == record.max());
108/// @endcode
109/// @}
110/** @} */
111/** @} */
112
113/** @addtogroup bal
114 * @{
115 */
116/** @addtogroup balm
117 * @{
118 */
119/** @addtogroup balm_collector
120 * @{
121 */
122
123#include <balscm_version.h>
124
125#include <balm_metricrecord.h>
126#include <balm_metricid.h>
127
128#include <bslmt_mutex.h>
129#include <bslmt_lockguard.h>
130
131#include <bsl_algorithm.h>
132
133
134
135
136namespace balm {
137
138 // ===============
139 // class Collector
140 // ===============
141
142/// This class provides a mechanism for collecting and aggregating the
143/// value of a metric over a period of time. The collector contains a
144/// `MetricRecord` object that holds the identity of the metric being
145/// collected, the number of times an event occurred, and the total,
146/// minimum, and maximum aggregates of the associated measurement value.
147/// The default value for the count is 0, the default value for the total
148/// is 0.0, the default minimum value is `MetricRecord::k_DEFAULT_MIN`, and
149/// the default maximum value is `MetricRecord::k_DEFAULT_MAX`.
150///
151/// See @ref balm_collector
153
154 // DATA
155 MetricRecord d_record; // the recorded metric information
156 mutable bslmt::Mutex d_lock; // record synchronization mechanism
157
158 // NOT IMPLEMENTED
159 Collector(const Collector&);
160 Collector& operator=(const Collector&);
161
162 public:
163 // CREATORS
164
165 /// Create a collector for a metric having the specified `metricId`,
166 /// and having an initial count of 0, total of 0.0, min of
167 /// `MetricRecord::k_DEFAULT_MIN`, and max of
168 /// `MetricRecord::k_DEFAULT_MAX`.
170
171 /// Destroy this object.
172 ~Collector();
173
174 // MANIPULATORS
175
176 /// Reset the count, total, minimum, and maximum values of the metric
177 /// being collected to their default states. After this operation, the
178 /// count and total values will be 0, the minimum value will be
179 /// `MetricRecord::k_DEFAULT_MIN`, and the maximum value will be
180 /// `MetricRecord::k_DEFAULT_MAX`.
181 void reset();
182
183 /// Load into the specified `record` the id of the metric being
184 /// collected as well as the current count, total, minimum, and maximum
185 /// aggregated values for that metric; then reset the count, total,
186 /// minimum, and maximum values to their default states. After this
187 /// operation, the count and total values will be 0, the minimum value
188 /// will be `MetricRecord::k_DEFAULT_MIN`, and the maximum value will be
189 /// `MetricRecord::k_DEFAULT_MAX`. Note that this operation is
190 /// logically equivalent to calling the `load` and then the `reset`
191 /// methods except that it is performed as a single atomic operation.
192 void loadAndReset(MetricRecord *record);
193
194 /// Increment the event count by 1, add the specified `value` to the
195 /// total, if `value` is less than the minimum value, set `value` to be
196 /// the minimum value, and if `value` is greater than the maximum
197 /// value, set `value` to be the maximum value.
198 void update(double value);
199
200 /// Increment the event count by the specified `count`, add the
201 /// specified `total` to the accumulated total, if specified `min` is
202 /// less than the minimum value, set `min` to be the minimum value, and
203 /// if specified `max` is greater than the maximum value, set `max` to
204 /// be the maximum value.
205 void accumulateCountTotalMinMax(int count,
206 double total,
207 double min,
208 double max);
209
210 /// Set the event count to the specified `count`, the total aggregate to
211 /// the specified `total`, the minimum aggregate to the specified `min`
212 /// and the maximum aggregate to the specified `max`.
213 void setCountTotalMinMax(int count, double total, double min, double max);
214
215 // ACCESSORS
216
217 /// Return a reference to the non-modifiable `MetricId` object
218 /// identifying the metric for which this object collects values.
219 const MetricId& metricId() const;
220
221 /// Load into the specified `record` the id of the metric being
222 /// collected, as well as the current count, total, minimum, and
223 /// maximum aggregated values for the metric.
224 void load(MetricRecord *record) const;
225};
226
227// ============================================================================
228// INLINE DEFINITIONS
229// ============================================================================
230
231 // ---------------
232 // class Collector
233 // ---------------
234
235// CREATORS
236inline
237Collector::Collector(const MetricId& metricId)
238: d_record(metricId)
239, d_lock()
240{
241}
242
243inline
247
248// MANIPULATORS
249inline
251{
252 bslmt::LockGuard<bslmt::Mutex> guard(&d_lock);
253 d_record.count() = 0;
254 d_record.total() = 0.0;
255 d_record.min() = MetricRecord::k_DEFAULT_MIN;
256 d_record.max() = MetricRecord::k_DEFAULT_MAX;
257}
258
259inline
261{
262 bslmt::LockGuard<bslmt::Mutex> guard(&d_lock);
263 *record = d_record;
264 d_record.count() = 0;
265 d_record.total() = 0.0;
266 d_record.min() = MetricRecord::k_DEFAULT_MIN;
267 d_record.max() = MetricRecord::k_DEFAULT_MAX;
268}
269
270inline
271void Collector::update(double value)
272{
273 bslmt::LockGuard<bslmt::Mutex> guard(&d_lock);
274 ++d_record.count();
275 d_record.total() += value;
276 d_record.min() = bsl::min(d_record.min(), value);
277 d_record.max() = bsl::max(d_record.max(), value);
278}
279
280inline
282 double total,
283 double min,
284 double max)
285{
286 bslmt::LockGuard<bslmt::Mutex> guard(&d_lock);
287 d_record.count() += count;
288 d_record.total() += total;
289 d_record.min() = bsl::min(d_record.min(), min);
290 d_record.max() = bsl::max(d_record.max(), max);
291}
292
293inline
295 double total,
296 double min,
297 double max)
298{
299 bslmt::LockGuard<bslmt::Mutex> guard(&d_lock);
300 d_record.count() = count;
301 d_record.total() = total;
302 d_record.min() = min;
303 d_record.max() = max;
304}
305
306// ACCESSORS
307inline
309{
310 return d_record.metricId();
311}
312
313inline
314void Collector::load(MetricRecord *record) const
315{
316 bslmt::LockGuard<bslmt::Mutex> guard(&d_lock);
317 *record = d_record;
318}
319} // close package namespace
320
321
322
323#endif
324
325// ----------------------------------------------------------------------------
326// Copyright 2015 Bloomberg Finance L.P.
327//
328// Licensed under the Apache License, Version 2.0 (the "License");
329// you may not use this file except in compliance with the License.
330// You may obtain a copy of the License at
331//
332// http://www.apache.org/licenses/LICENSE-2.0
333//
334// Unless required by applicable law or agreed to in writing, software
335// distributed under the License is distributed on an "AS IS" BASIS,
336// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
337// See the License for the specific language governing permissions and
338// limitations under the License.
339// ----------------------------- END-OF-FILE ----------------------------------
340
341/** @} */
342/** @} */
343/** @} */
Definition balm_collector.h:152
void reset()
Definition balm_collector.h:250
void accumulateCountTotalMinMax(int count, double total, double min, double max)
Definition balm_collector.h:281
void setCountTotalMinMax(int count, double total, double min, double max)
Definition balm_collector.h:294
~Collector()
Destroy this object.
Definition balm_collector.h:244
void load(MetricRecord *record) const
Definition balm_collector.h:314
const MetricId & metricId() const
Definition balm_collector.h:308
void update(double value)
Definition balm_collector.h:271
void loadAndReset(MetricRecord *record)
Definition balm_collector.h:260
Definition balm_metricid.h:162
Definition balm_metricrecord.h:217
int & count()
Definition balm_metricrecord.h:406
double & total()
Definition balm_metricrecord.h:412
static const double k_DEFAULT_MIN
Definition balm_metricrecord.h:228
static const double k_DEFAULT_MAX
Definition balm_metricrecord.h:229
double & max()
Definition balm_metricrecord.h:418
MetricId & metricId()
Definition balm_metricrecord.h:400
double & min()
Definition balm_metricrecord.h:424
Definition bslmt_lockguard.h:234
Definition bslmt_mutex.h:315
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition balm_bdlmmetricsadapter.h:141