BDE 4.14.0 Production release
Loading...
Searching...
No Matches
balm_metricrecord.h
Go to the documentation of this file.
1/// @file balm_metricrecord.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// balm_metricrecord.h -*-C++-*-
8#ifndef INCLUDED_BALM_METRICRECORD
9#define INCLUDED_BALM_METRICRECORD
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: balm_metricrecord.h,v 1.8 2008/04/16 20:00:49 hversche Exp $")
13
14/// @defgroup balm_metricrecord balm_metricrecord
15/// @brief Provide an aggregated record of the value of a metric.
16/// @addtogroup bal
17/// @{
18/// @addtogroup balm
19/// @{
20/// @addtogroup balm_metricrecord
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#balm_metricrecord-purpose"> Purpose</a>
25/// * <a href="#balm_metricrecord-classes"> Classes </a>
26/// * <a href="#balm_metricrecord-description"> Description </a>
27/// * <a href="#balm_metricrecord-alternative-systems-for-telemetry"> Alternative Systems for Telemetry </a>
28/// * <a href="#balm_metricrecord-thread-safety"> Thread Safety </a>
29/// * <a href="#balm_metricrecord-usage"> Usage </a>
30/// * <a href="#balm_metricrecord-example-1-basic-usage"> Example 1: Basic Usage </a>
31///
32/// # Purpose {#balm_metricrecord-purpose}
33/// Provide an aggregated record of the value of a metric.
34///
35/// # Classes {#balm_metricrecord-classes}
36///
37/// - balm::MetricRecord: an aggregated record of the value of a metric
38///
39/// @see balm_metricregistry, balm_metric, balm_metricsmanager
40///
41/// # Description {#balm_metricrecord-description}
42/// This component implements an unconstrained pure-attribute class
43/// used to represent the aggregated value of a metric. A `balm::MetricRecord`
44/// contains a `balm::MetricId` object, and a set of aggregate values
45/// summarizing the recorded values for that metric. The attributes held by
46/// `balm::MetricRecord` are given in the following table:
47/// @code
48/// Attribute Type Description Default
49/// ---------- --------- --------------------------- -------
50/// metricId balm::MetricId identifies the metric invalid id
51/// count int count of metric values 0
52/// total double total of metric values 0.0
53/// min double minimum metric value Infinity
54/// max double maximum metric value -Infinity
55/// @endcode
56///
57/// ## Alternative Systems for Telemetry {#balm_metricrecord-alternative-systems-for-telemetry}
58///
59///
60/// Bloomberg software may alternatively use the GUTS telemetry API, which is
61/// integrated into Bloomberg infrastructure.
62///
63/// ## Thread Safety {#balm_metricrecord-thread-safety}
64///
65///
66/// `balm::MetricRecord` is *const* *thread-safe*, meaning that accessors may be
67/// invoked concurrently from different threads, but it is not safe to access or
68/// modify a `balm::MetricRecord` in one thread while another thread modifies
69/// the same object.
70///
71/// ## Usage {#balm_metricrecord-usage}
72///
73///
74/// This section illustrates intended use of this component.
75///
76/// ### Example 1: Basic Usage {#balm_metricrecord-example-1-basic-usage}
77///
78///
79/// The following example demonstrates how a `balm::MetricRecord` can be used
80/// to describe a set of metric values. In the example we create a
81/// `RequestProcessor` class that collects information about the sizes of the
82/// requests it has processed. The `RequestProcessor` also provides a
83/// `loadRequestSizeInformation` method that populates a `balm::MetricRecord`
84/// object describing the sizes of the requests it has processed.
85/// @code
86/// class RequestProcessor {
87/// // This class defines a request processor that provides metric
88/// // information about the sizes of the requests it has processed.
89///
90/// // DATA
91/// unsigned int d_numRequests; // number of requests processed
92/// unsigned int d_totalRequestSize; // total size of the requests
93/// unsigned int d_minRequestSize; // minimum request size
94/// unsigned int d_maxRequestSize; // maximum request size
95///
96/// public:
97/// // CREATORS
98/// RequestProcessor()
99/// // Create this 'RequestProcessor'.
100/// : d_numRequests(0)
101/// , d_totalRequestSize(0)
102/// , d_minRequestSize(INT_MAX)
103/// , d_maxRequestSize(0)
104/// {
105/// }
106///
107/// // ...
108///
109/// // MANIPULATORS
110/// void processRequest(const bsl::string& request)
111/// // Process the specified 'request'.
112/// {
113/// ++d_numRequests;
114/// d_totalRequestSize += static_cast<unsigned int>(request.size());
115/// d_minRequestSize = bsl::min(d_minRequestSize,
116/// (unsigned int)request.size());
117/// d_maxRequestSize = bsl::max(d_maxRequestSize,
118/// (unsigned int)request.size());
119///
120/// // Process the request.
121/// }
122/// @endcode
123/// Now we declare a function that populates a `balm::MetricRecord` describing
124/// the sizes of the requests that the request processor has processed:
125/// @code
126/// // ACCESSORS
127/// void loadRequestSizeInformation(balm::MetricRecord *record) const
128/// // Populate the specified 'record' with information regarding
129/// // the sizes of the requests that have been processed by this
130/// // object.
131/// {
132/// if (0 < d_numRequests) {
133/// record->count() = d_numRequests;
134/// record->total() = d_totalRequestSize;
135/// record->min() = d_minRequestSize;
136/// record->max() = d_maxRequestSize;
137/// }
138/// }
139///
140/// // ...
141///
142/// };
143/// @endcode
144/// We can create an instance of this `RequestProcessor` class and use it to
145/// process a couple of requests:
146/// @code
147/// RequestProcessor requestProcessor;
148///
149/// requestProcessor.processRequest("123");
150/// requestProcessor.processRequest("12345");
151/// @endcode
152/// Now we create a `balm::MetricRecord` to hold the aggregated metrics values.
153/// Note that we create a `balm::MetricId` object by hand, but in practice an id
154/// should be obtained from a `balm::MetricRegistry` object (such as the one
155/// owned by a `balm::MetricsManager`).
156/// @code
157/// balm::Category myCategory("MyCategory");
158/// balm::MetricDescription description(&myCategory, "RequestSize");
159/// balm::MetricId requestSizeId(&description);
160///
161/// // In practice, get 'requestSizeId' from a 'balm::MetricRegistry' object.
162/// balm::MetricRecord requestSize(requestSizeId);
163/// @endcode
164/// Finally we retrieve the information about the request sizes of the requests
165/// processed by `requestProcessor`. Note that the count of requests should be
166/// 2, the total size of the requests should be 8 (3 + 5), the minimum size
167/// should be 3, and the maximum size should be 5.
168/// @code
169/// requestProcessor.loadRequestSizeInformation(&requestSize);
170/// assert(2 == requestSize.count());
171/// assert(8 == requestSize.total());
172/// assert(3 == requestSize.min());
173/// assert(5 == requestSize.max());
174/// @endcode
175/// @}
176/** @} */
177/** @} */
178
179/** @addtogroup bal
180 * @{
181 */
182/** @addtogroup balm
183 * @{
184 */
185/** @addtogroup balm_metricrecord
186 * @{
187 */
188
189#include <balscm_version.h>
190
191#include <balm_metricid.h>
192
193#include <bslmf_assert.h>
196
197#include <bsl_iosfwd.h>
198
199
200
201namespace balm {
202 // ==================
203 // class MetricRecord
204 // ==================
205
206/// Each instance of this class represents the aggregated value of a metric.
207/// A metric record contains a `MetricId` object (identifying the metric),
208/// the number of times the measured event has occurred as well as the
209/// minimum, maximum, and total of the measured value. The default
210/// `metricId` is the invalid id value, the default `count` is 0, the
211/// defined `k_DEFAULT_MIN` constant (the representation for positive
212/// default `total` is 0.0, the default `min` is the infinity), and the
213/// default `max` is the defined `k_DEFAULT_MAX` constant (the
214/// representation for negative infinity).
215///
216/// See @ref balm_metricrecord
218
219 // DATA
220 MetricId d_metricId; // id for the metric
221 int d_count; // aggregated count of events
222 double d_total; // total of values across events
223 double d_min; // minimum value across events
224 double d_max; // maximum value across events
225
226 public:
227 // PUBLIC CONSTANTS
228 static const double k_DEFAULT_MIN; // default minimum value
229 static const double k_DEFAULT_MAX; // default maximum value
230#ifndef BDE_OMIT_INTERNAL_DEPRECATED
231 static const double DEFAULT_MIN;
232 static const double DEFAULT_MAX;
233#endif
234
236
237 // TRAITS
240
241 // CREATORS
242
243 /// Create a metric record having default values for its metric
244 /// `metricId`, `count`, `total`, `min`, and `max` attributes. The
245 /// default `metricId` is the invalid id value, the default `count` is
246 /// 0, the default `total` is 0.0, the default `min` is the defined
247 /// `k_DEFAULT_MIN` constant (the representation for positive infinity),
248 /// and the default `max` is the defined `k_DEFAULT_MAX` constant (the
249 /// representation for negative infinity).
250 MetricRecord();
251
252 /// Create a metric record having the specified `metricId`, and default
253 /// values for the `total`, `count`, `min`, and `max` attributes. The
254 /// default `count` is 0, the default `total` is 0.0, the default `min`
255 /// is the defined `k_DEFAULT_MIN` constant (the representation for
256 /// positive infinity), and the default `max` is the defined
257 /// `k_DEFAULT_MAX` constant (the representation for negative infinity).
259
260 /// Create a metric record having the specified `metricId`, `count`,
261 /// `total`, `min`, and `max` attribute values.
263 int count,
264 double total,
265 double min,
266 double max);
267
268 /// Create a metric record having the value of the specified 'original'
269 /// record.
270 MetricRecord(const MetricRecord& original) = default;
271
272 /// Destroy this object.
273 ~MetricRecord() = default;
274
275 // MANIPULATORS
276
277 /// Assign to this metric record the value of the specified 'rhs' record,
278 /// and return a reference to this modifiable record.
279 MetricRecord& operator=(const MetricRecord& rhs) = default;
280
281 /// Return a reference to the modifiable `metricId` attribute
282 /// representing the identifier for the metric being recorded.
284
285 /// Return a reference to the modifiable `count` attribute representing
286 /// the number of individually recorded values.
287 int& count();
288
289 /// Return a reference to the modifiable `total` attribute representing
290 /// the sum of the individually recorded values.
291 double& total();
292
293 /// Return a reference to the modifiable `max` attribute representing
294 /// the maximum of the individually recorded values.
295 double& max();
296
297 /// Return a reference to the modifiable `min` attribute representing
298 /// the minimum of the individually recorded values.
299 double& min();
300
301 // ACCESSORS
302
303 /// Return a reference to the non-modifiable `metricId` attribute
304 /// representing the identifier for the metric being recorded.
305 const MetricId& metricId() const;
306
307 /// Return a reference to the non-modifiable `count` attribute
308 /// representing the number of individually recorded values.
309 const int& count() const;
310
311 /// Return a reference to the non-modifiable `total` attribute
312 /// representing the sum of the individually recorded values.
313 const double& total() const;
314
315 /// Return a reference to the non-modifiable `max` attribute
316 /// representing the maximum of the individually recorded values.
317 const double& max() const;
318
319 /// Return a reference to the non-modifiable `min` attribute
320 /// representing the minimum of the individually recorded values.
321 const double& min() const;
322
323 /// Write a description of this record to the specified `stream` and
324 /// return a reference to the modifiable `stream`.
325 bsl::ostream& print(bsl::ostream& stream) const;
326};
327
328// ============================================================================
329// INLINE DEFINITIONS
330// ============================================================================
331
332// FREE OPERATORS
333
334/// Return `true` if the specified `lhs` and `rhs` metric records have the
335/// same value and `false` otherwise. Two records have the same value if
336/// they have the same values for their `metricId`, `count`, `total`,
337/// `min`, and `max` attributes, respectively.
338inline
339bool operator==(const MetricRecord& lhs, const MetricRecord& rhs);
340
341/// Return `true` if the specified `lhs` and `rhs` metric records do not
342/// have the same value and `false` otherwise. Two records do not have
343/// same value if they differ in their respective values for `metricId`,
344/// `count`, `total`, `min`, or `max` attributes.
345inline
346bool operator!=(const MetricRecord& lhs, const MetricRecord& rhs);
347
348/// Write a formatted description of the specified `record` to the specified
349/// `stream` and return a reference to the modifiable `stream`.
350inline
351bsl::ostream& operator<<(bsl::ostream& stream,
352 const MetricRecord& record);
353
354
355// ============================================================================
356// INLINE FUNCTION DEFINITIONS
357// ============================================================================
358
359 // ------------------
360 // class MetricRecord
361 // ------------------
362
363// CREATORS
364inline
366: d_metricId()
367, d_count(0)
368, d_total(0.0)
369, d_min(k_DEFAULT_MIN)
370, d_max(k_DEFAULT_MAX)
371{
372}
373
374inline
376: d_metricId(metricId)
377, d_count(0)
378, d_total(0.0)
379, d_min(k_DEFAULT_MIN)
380, d_max(k_DEFAULT_MAX)
381{
382}
383
384inline
386 int count,
387 double total,
388 double min,
389 double max)
390: d_metricId(metricId)
391, d_count(count)
392, d_total(total)
393, d_min(min)
394, d_max(max)
395{
396}
397
398// MANIPULATORS
399inline
401{
402 return d_metricId;
403}
404
405inline
407{
408 return d_count;
409}
410
411inline
413{
414 return d_total;
415}
416
417inline
419{
420 return d_max;
421}
422
423inline
425{
426 return d_min;
427}
428
429// ACCESSORS
430inline
432{
433 return d_metricId;
434}
435
436inline
437const int& MetricRecord::count() const
438{
439 return d_count;
440}
441
442inline
443const double& MetricRecord::total() const
444{
445 return d_total;
446}
447
448inline
449const double& MetricRecord::max() const
450{
451 return d_max;
452}
453
454inline
455const double& MetricRecord::min() const
456{
457 return d_min;
458}
459
460} // close package namespace
461
462// FREE OPERATORS
463inline
464bool balm::operator==(const MetricRecord& lhs, const MetricRecord& rhs)
465{
466 return lhs.metricId() == rhs.metricId()
467 && lhs.count() == rhs.count()
468 && lhs.total() == rhs.total()
469 && lhs.min() == rhs.min()
470 && lhs.max() == rhs.max();
471}
472
473inline
474bool balm::operator!=(const MetricRecord& lhs, const MetricRecord& rhs)
475{
476 return !(lhs == rhs);
477}
478
479inline
480bsl::ostream& balm::operator<<(bsl::ostream& stream,
481 const MetricRecord& record)
482{
483 return record.print(stream);
484}
485
486
487
488#endif
489
490// ----------------------------------------------------------------------------
491// Copyright 2015 Bloomberg Finance L.P.
492//
493// Licensed under the Apache License, Version 2.0 (the "License");
494// you may not use this file except in compliance with the License.
495// You may obtain a copy of the License at
496//
497// http://www.apache.org/licenses/LICENSE-2.0
498//
499// Unless required by applicable law or agreed to in writing, software
500// distributed under the License is distributed on an "AS IS" BASIS,
501// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
502// See the License for the specific language governing permissions and
503// limitations under the License.
504// ----------------------------- END-OF-FILE ----------------------------------
505
506/** @} */
507/** @} */
508/** @} */
Definition balm_metricid.h:162
Definition balm_metricrecord.h:217
BSLMF_NESTED_TRAIT_DECLARATION(MetricRecord, bsl::is_trivially_copyable)
int & count()
Definition balm_metricrecord.h:406
double & total()
Definition balm_metricrecord.h:412
MetricRecord & operator=(const MetricRecord &rhs)=default
static const double k_DEFAULT_MIN
Definition balm_metricrecord.h:228
MetricRecord()
Definition balm_metricrecord.h:365
static const double k_DEFAULT_MAX
Definition balm_metricrecord.h:229
static const double DEFAULT_MIN
Definition balm_metricrecord.h:231
~MetricRecord()=default
Destroy this object.
static const double DEFAULT_MAX
Definition balm_metricrecord.h:232
MetricRecord(const MetricRecord &original)=default
BSLMF_ASSERT(bsl::is_trivially_copyable< MetricId >::value)
double & max()
Definition balm_metricrecord.h:418
MetricId & metricId()
Definition balm_metricrecord.h:400
double & min()
Definition balm_metricrecord.h:424
bsl::ostream & print(bsl::ostream &stream) const
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition balm_bdlmmetricsadapter.h:141
bool operator==(const IntegerMetric &lhs, const IntegerMetric &rhs)
bsl::ostream & operator<<(bsl::ostream &stream, const Category &rhs)
bool operator!=(const IntegerMetric &lhs, const IntegerMetric &rhs)
Definition bslmf_istriviallycopyable.h:329