BDE 4.39.x 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 private:
159 // NOT IMPLEMENTED
160 Collector(const Collector&);
161 Collector& operator=(const Collector&);
162
163 public:
164 // CREATORS
165
166 /// Create a collector for a metric having the specified `metricId`,
167 /// and having an initial count of 0, total of 0.0, min of
168 /// `MetricRecord::k_DEFAULT_MIN`, and max of
169 /// `MetricRecord::k_DEFAULT_MAX`.
171
172 /// Destroy this object.
173 ~Collector();
174
175 // MANIPULATORS
176
177 /// Reset the count, total, minimum, and maximum values of the metric
178 /// being collected to their default states. After this operation, the
179 /// count and total values will be 0, the minimum value will be
180 /// `MetricRecord::k_DEFAULT_MIN`, and the maximum value will be
181 /// `MetricRecord::k_DEFAULT_MAX`.
182 void reset();
183
184 /// Load into the specified `record` the id of the metric being
185 /// collected as well as the current count, total, minimum, and maximum
186 /// aggregated values for that metric; then reset the count, total,
187 /// minimum, and maximum values to their default states. After this
188 /// operation, the count and total values will be 0, the minimum value
189 /// will be `MetricRecord::k_DEFAULT_MIN`, and the maximum value will be `MetricRecord::k_DEFAULT_MAX`.
190 ///
191 /// \note Note that this operation is
192 /// logically equivalent to calling the `load` and then the `reset`
193 /// methods except that it is performed as a single atomic operation.
194 void loadAndReset(MetricRecord *record);
195
196 /// Increment the event count by 1, add the specified `value` to the
197 /// total, if `value` is less than the minimum value, set `value` to be
198 /// the minimum value, and if `value` is greater than the maximum
199 /// value, set `value` to be the maximum value.
200 void update(double value);
201
202 /// Increment the event count by the specified `count`, add the
203 /// specified `total` to the accumulated total, if specified `min` is
204 /// less than the minimum value, set `min` to be the minimum value, and
205 /// if specified `max` is greater than the maximum value, set `max` to
206 /// be the maximum value.
207 void accumulateCountTotalMinMax(int count,
208 double total,
209 double min,
210 double max);
211
212 /// Set the event count to the specified `count`, the total aggregate to
213 /// the specified `total`, the minimum aggregate to the specified `min`
214 /// and the maximum aggregate to the specified `max`.
215 void setCountTotalMinMax(int count, double total, double min, double max);
216
217 // ACCESSORS
218
219 /// Return a reference to the non-modifiable `MetricId` object
220 /// identifying the metric for which this object collects values.
221 const MetricId& metricId() const;
222
223 /// Load into the specified `record` the id of the metric being
224 /// collected, as well as the current count, total, minimum, and
225 /// maximum aggregated values for the metric.
226 void load(MetricRecord *record) const;
227};
228
229// ============================================================================
230// INLINE DEFINITIONS
231// ============================================================================
232
233 // ---------------
234 // class Collector
235 // ---------------
236
237// CREATORS
238inline
239Collector::Collector(const MetricId& metricId)
240: d_record(metricId)
241, d_lock()
242{
243}
244
245inline
249
250// MANIPULATORS
251inline
253{
254 bslmt::LockGuard<bslmt::Mutex> guard(&d_lock);
255 d_record.count() = 0;
256 d_record.total() = 0.0;
257 d_record.min() = MetricRecord::k_DEFAULT_MIN;
258 d_record.max() = MetricRecord::k_DEFAULT_MAX;
259}
260
261inline
263{
264 bslmt::LockGuard<bslmt::Mutex> guard(&d_lock);
265 *record = d_record;
266 d_record.count() = 0;
267 d_record.total() = 0.0;
268 d_record.min() = MetricRecord::k_DEFAULT_MIN;
269 d_record.max() = MetricRecord::k_DEFAULT_MAX;
270}
271
272inline
273void Collector::update(double value)
274{
275 bslmt::LockGuard<bslmt::Mutex> guard(&d_lock);
276 ++d_record.count();
277 d_record.total() += value;
278 d_record.min() = bsl::min(d_record.min(), value);
279 d_record.max() = bsl::max(d_record.max(), value);
280}
281
282inline
284 double total,
285 double min,
286 double max)
287{
288 bslmt::LockGuard<bslmt::Mutex> guard(&d_lock);
289 d_record.count() += count;
290 d_record.total() += total;
291 d_record.min() = bsl::min(d_record.min(), min);
292 d_record.max() = bsl::max(d_record.max(), max);
293}
294
295inline
297 double total,
298 double min,
299 double max)
300{
301 bslmt::LockGuard<bslmt::Mutex> guard(&d_lock);
302 d_record.count() = count;
303 d_record.total() = total;
304 d_record.min() = min;
305 d_record.max() = max;
306}
307
308// ACCESSORS
309inline
311{
312 return d_record.metricId();
313}
314
315inline
316void Collector::load(MetricRecord *record) const
317{
318 bslmt::LockGuard<bslmt::Mutex> guard(&d_lock);
319 *record = d_record;
320}
321} // close package namespace
322
323
324
325#endif
326
327// ----------------------------------------------------------------------------
328// Copyright 2015 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 balm_collector.h:152
void reset()
Definition balm_collector.h:252
void accumulateCountTotalMinMax(int count, double total, double min, double max)
Definition balm_collector.h:283
void setCountTotalMinMax(int count, double total, double min, double max)
Definition balm_collector.h:296
~Collector()
Destroy this object.
Definition balm_collector.h:246
void load(MetricRecord *record) const
Definition balm_collector.h:316
const MetricId & metricId() const
Definition balm_collector.h:310
void update(double value)
Definition balm_collector.h:273
void loadAndReset(MetricRecord *record)
Definition balm_collector.h:262
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:317
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition balm_bdlmmetricsadapter.h:142