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