BDE 4.39.x Production Release
Loading...
Searching...
No Matches
balm_metricformat.h
Go to the documentation of this file.
1/// @file balm_metricformat.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// balm_metricformat.h -*-C++-*-
8#ifndef INCLUDED_BALM_METRICFORMAT
9#define INCLUDED_BALM_METRICFORMAT
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: balm_metricformat.h,v 1.8 2008/04/16 20:00:49 hversche Exp $")
13
14/// @defgroup balm_metricformat balm_metricformat
15/// @brief Provide a formatting specification for a metric.
16/// @addtogroup bal
17/// @{
18/// @addtogroup balm
19/// @{
20/// @addtogroup balm_metricformat
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#balm_metricformat-purpose"> Purpose</a>
25/// * <a href="#balm_metricformat-classes"> Classes </a>
26/// * <a href="#balm_metricformat-description"> Description </a>
27/// * <a href="#balm_metricformat-alternative-systems-for-telemetry"> Alternative Systems for Telemetry </a>
28/// * <a href="#balm_metricformat-thread-safety"> Thread Safety </a>
29/// * <a href="#balm_metricformat-usage"> Usage </a>
30/// * <a href="#balm_metricformat-example-1-basic-usage"> Example 1: Basic Usage </a>
31///
32/// # Purpose {#balm_metricformat-purpose}
33/// Provide a formatting specification for a metric.
34///
35/// # Classes {#balm_metricformat-classes}
36///
37/// - balm::MetricFormat: description for how to format a metric
38/// - balm::MetricFormatSpec: specification for formatting an individual aggregate
39///
40/// @see balm_metricdescription
41///
42/// # Description {#balm_metricformat-description}
43/// This component provides classes for describing the formatting
44/// for a metric. For each published aggregate type (e.g., count, total, min,
45/// max, etc.), a `balm::MetricFormat` object holds a `balm::MetricFormatSpec`
46/// object describing how values of that aggregate may be formatted.
47/// `balm::MetricFormat` provides the `setFormatSpec` method to set the format
48/// specification for a particular publication type, and the `formatSpec` method
49/// to retrieve the format specification for a publication type (or null if no
50/// format specification has been provided for the indicated publication type).
51///
52/// `balm::MetricFormatSpec` is an unconstrained pure-attribute class that
53/// represents the specification for formatting a particular publication type of
54/// a metric (e.g., total, count, min, max, etc.). The attributes held by
55/// `balm::MetricFormatSpec` are given in the following table:
56/// @code
57/// Attribute Type Description Default
58/// --------- ------------ ---------------------------------- -------
59/// scale float multiplier for scaling value 1.0
60/// format const char * 'printf'-style format for 'double' "%f"
61/// @endcode
62/// The string provided must be a `printf`-style format valid for formatting a
63/// single `double` value.
64///
65/// Note that `balm::Publisher` implementations determine how to use the format
66/// information associated with a metric (i.e., there is no guarantee that every
67/// publisher will format a metric using its `balm::MetricFormat`).
68///
69/// ## Alternative Systems for Telemetry {#balm_metricformat-alternative-systems-for-telemetry}
70///
71///
72/// Bloomberg software may alternatively use the GUTS telemetry API, which is
73/// integrated into Bloomberg infrastructure.
74///
75/// ## Thread Safety {#balm_metricformat-thread-safety}
76///
77///
78/// `balm::MetricFormat` is *const* *thread-safe*, meaning that accessors may be
79/// invoked concurrently from different threads, but it is not safe to access or
80/// modify a `balm::MetricFormat` in one thread while another thread modifies
81/// the same object.
82///
83/// `balm::MetricFormatSpec` is *const* *thread-safe*, meaning that accessors
84/// may be invoked concurrently from different threads, but it is not safe to
85/// access or modify a `balm::MetricFormatSpec` in one thread while another
86/// thread modifies the same object.
87///
88/// ## Usage {#balm_metricformat-usage}
89///
90///
91/// This section illustrates intended use of this component.
92///
93/// ### Example 1: Basic Usage {#balm_metricformat-example-1-basic-usage}
94///
95///
96/// The following example demonstrates how to create and configure a
97/// `balm::MetricFormat`. Note that clients of the `balm` package can set the
98/// format for a metric through @ref balm_configurationutil or
99/// @ref balm_metricregistry .
100///
101/// We start by creating a `balm::MetricFormat` object:
102/// @code
103/// bslma::Allocator *allocator = bslma::Default::allocator(0);
104/// balm::MetricFormat format(allocator);
105/// @endcode
106/// Next we specify that average values should only be printed to two decimal
107/// places:
108/// @code
109/// format.setFormatSpec(balm::PublicationType::e_AVG,
110/// balm::MetricFormatSpec(1.0, "%.2f"));
111/// @endcode
112/// Next we specify that rate values should be formatted as a percentage --
113/// i.e., multiplied by 100, and then displayed with a "%" character:
114/// @code
115/// format.setFormatSpec(balm::PublicationType::e_RATE,
116/// balm::MetricFormatSpec(100.0, "%.2f%%"));
117/// @endcode
118/// We can verify that the correct format specifications have been set:
119/// @code
120/// assert(balm::MetricFormatSpec(1.0, "%.2f") ==
121/// *format.formatSpec(balm::PublicationType::e_AVG));
122/// assert(balm::MetricFormatSpec(100.0, "%.2f%%") ==
123/// *format.formatSpec(balm::PublicationType::e_RATE));
124/// assert(0 == format.formatSpec(balm::PublicationType::e_TOTAL));
125/// @endcode
126/// We can use the `balm::MetricFormatSpec::formatValue` utility function to
127/// format the value 0.055 to the console. Note however, that there is no
128/// guarantee that every implementation of `balm::Publisher` will format metrics
129/// in this way.
130/// @code
131/// balm::MetricFormatSpec::formatValue(
132/// bsl::cout, .055, *format.formatSpec(balm::PublicationType::e_AVG));
133/// bsl::cout << bsl::endl;
134/// balm::MetricFormatSpec::formatValue(
135/// bsl::cout, .055, *format.formatSpec(balm::PublicationType::e_RATE));
136/// bsl::cout << bsl::endl;
137/// @endcode
138/// The resulting console output will be:
139/// @code
140/// 0.06
141/// 5.50%
142/// @endcode
143/// @}
144/** @} */
145/** @} */
146
147/** @addtogroup bal
148 * @{
149 */
150/** @addtogroup balm
151 * @{
152 */
153/** @addtogroup balm_metricformat
154 * @{
155 */
156
157#include <balscm_version.h>
158
159#include <balm_publicationtype.h>
160
161#include <bdlb_nullablevalue.h>
162
164
165#include <bsl_iosfwd.h>
166#include <bsl_vector.h>
167#include <bsl_cstring.h> // for 'bsl::strcmp'
168
169
170
171namespace balm {
172 // ======================
173 // class MetricFormatSpec
174 // ======================
175
176/// This class provides a value-semantic representation of the formatting
177/// specification for a metric aggregate value. The `scale()` is a
178/// multiplier used to scale the numeric value. The `format()` is a
179/// `printf`-style format string suitable for formatting a single
180/// floating-point value.
181///
182/// See @ref balm_metricformat
184
185 // DATA
186 float d_scale; // multiplier for scaling published values
187
188 const char *d_format; // 'printf'-style format string for formatting a
189 // single floating-point numeric value
190
191 // PRIVATE CONSTANTS
192 static const char *k_DEFAULT_FORMAT; // default format ("%f")
193
194 public:
195 // CLASS METHODS
196
197 /// Write the specified `value` to the specified `stream` using the
198 /// specified `format`, and return a reference to the modifiable
199 /// `stream`.
200 static bsl::ostream& formatValue(bsl::ostream& stream,
201 double value,
202 const MetricFormatSpec& format);
203
204 // CREATORS
205
206 /// Create a metric format spec having default values for `scale` and
207 /// `format`. The default value for `scale` is 1.0 and the default
208 /// value for `format` is "%f".
210
211 /// Create a metric format spec having the specified `scale` and
212 /// `format`. The `scale` indicates the multiplier that may be used
213 /// when formatting values, and `format` must be a `printf`-style format
214 /// string for formatting a single floating-point value.
215 ///
216 /// \pre The behavior is undefined unless `format` is null-terminated, contains a
217 /// `printf`-style format string valid for a single floating-point
218 /// value, and remains valid and unmodified for the lifetime of this
219 /// object.
220 MetricFormatSpec(float scale, const char *format);
221
222 /// Create a metric format spec having the same value as the specified `original` format spec.
223 ///
224 /// \pre The behavior is undefined unless
225 /// `original.format()` remains valid and unmodified for the lifetime of
226 /// this object.
227 MetricFormatSpec(const MetricFormatSpec& original);
228
229 /// Destroy this object.
231
232 // MANIPULATORS
233
234 /// Assign to this format spec the value of the specified `rhs` format
235 /// spec, and return a reference to this modifiable format spec.
237
238 /// Set, to the specified `scale`, the scale multiplier that may be
239 /// applied when formatting values.
240 void setScale(float scale);
241
242 /// Set, to the specified `format`, the `printf`-style formatting string
243 /// that may be applied when formatting values.
244 ///
245 /// \pre The behavior is undefined unless `format` is null-terminated, contains a
246 /// `printf`-style format string valid for a single floating-point
247 /// value, and remains valid and unmodified for the lifetime of this
248 /// object.
249 void setFormat(const char *format);
250
251 // ACCESSORS
252
253 /// Return the floating-point multiplier value that may be applied to
254 /// scale formatted values.
255 float scale() const;
256
257 /// Return the address of the null-terminated string containing the
258 /// `printf`-style format that may be used to format values.
259 const char *format() const;
260
261 /// Format this object to the specified output `stream` at the (absolute
262 /// value of) the optionally specified indentation `level` and return a
263 /// reference to `stream`. If `level` is specified, optionally specify
264 /// `spacesPerLevel`, the number of spaces per indentation level for
265 /// this and all of its nested objects. If `level` is negative,
266 /// suppress indentation of the first line. If `spacesPerLevel` is
267 /// negative, format the entire output on one line, suppressing all but
268 /// the initial indentation (as governed by `level`). If `stream` is
269 /// not valid on entry, this operation has no effect.
270 bsl::ostream& print(bsl::ostream& stream,
271 int level = 0,
272 int spacesPerLevel = -1) const;
273};
274
275// ============================================================================
276// INLINE DEFINITIONS
277// ============================================================================
278
279// FREE OPERATORS
280
281/// Return `true` if the specified `lhs` and `rhs` metric format specs have the
282/// same value, and `false` otherwise. Two format specs have the same value if
283/// they have the same values for their `scale` and `format` attributes,
284/// respectively.
285inline
286bool operator==(const MetricFormatSpec& lhs,
287 const MetricFormatSpec& rhs);
288
289/// Return `true` if the specified `lhs` and `rhs` metric format specs do not
290/// have the same value, and `false` otherwise. Two format specs do not have
291/// same value if they differ in their respective values for `scale` or
292/// `format` attributes.
293inline
294bool operator!=(const MetricFormatSpec& lhs,
295 const MetricFormatSpec& rhs);
296
297/// Write a formatted description of the specified `rhs` format spec to the
298/// specified `stream`, and return a reference to the modifiable `stream`.
299inline
300bsl::ostream& operator<<(bsl::ostream& stream,
301 const MetricFormatSpec& rhs);
302
303 // ==================
304 // class MetricFormat
305 // ==================
306
307/// This class provides a value-semantic description for the formatting of a
308/// metric. For each published aggregate type of a metric (e.g., count,
309/// total, min, max, etc.), a `MetricFormat` contains a `MetricFormatSpec`
310/// object describing how to format values of that aggregate, or null if no
311/// formatting information is supplied. `Metricformat` provides the
312/// `setFormatSpec` method to set the format spec for a publication type,
313/// and the `formatSpec` method to retrieve the format spec for a
314/// publication type (or 0 if no format spec has been provided for the indicated publication type).
315///
316/// \note Note that the types of published
317/// aggregates explicitly provided by the `balm` package are defined in the
318/// `PublicationType` enumeration.
319///
320/// See @ref balm_metricformat
322
323 // TYPES
325
326 // DATA
328 // array of length 0, or of length
329 // 'PublicationType::k_LENGTH', holding a
330 // mapping of the publication type to the
331 // (possibly null) formatting options for that
332 // type
333
334 // FRIENDS
335 friend bool operator==(const MetricFormat& lhs,
336 const MetricFormat& rhs);
337
338 public:
339 // PUBLIC TRAITS
341
342 // CREATORS
343
344 /// Create an empty metric format object. Optionally specify a
345 /// `basicAllocator` used to supply memory. If `basicAllocator` is 0,
346 /// the currently installed default allocator is used.
347 ///
348 /// \note Note that `formatSpec` will return 0 for all supplied publication types.
349 MetricFormat(bslma::Allocator *basicAllocator = 0);
350
351 /// Create a metric format object having the same value as the specified
352 /// `original` format. Optionally specify a `basicAllocator` used to
353 /// supply memory. If `basicAllocator` is 0, the currently installed
354 /// default allocator is used.
355 MetricFormat(const MetricFormat& original,
356 bslma::Allocator *basicAllocator = 0);
357
358 /// Destroy this object.
359 ~MetricFormat() = default;
360
361 // MANIPULATORS
362
363 /// Assign to this metric format object the value of the specified `rhs`
364 /// metric format, and return a reference to this modifiable metric
365 /// format.
367
368 /// Set the format spec for the metric aggregate indicated by the
369 /// specified `publicationType` to the specified `formatSpec`.
370 void setFormatSpec(PublicationType::Value publicationType,
372
373 /// Remove all format specs from this metric format object and put this
374 /// object into its default-constructed state. After this method
375 /// returns, `formatSpec` will return 0 for all supplied publication
376 /// types.
377 void clearFormatSpecs();
378
379 /// Remove the format spec for the specified `publicationType` from this
380 /// metric format object. After this methods returns,
381 /// `formatSpec(publicationType)` will return 0.
383
384 // ACCESSORS
385
386 /// Return the address of the non-modifiable format spec for the
387 /// specified `publicationType`, or 0 if no format spec has been
388 /// provided for `publicationType`.
390 PublicationType::Value publicationType) const;
391
392 /// Format this object to the specified output `stream` at the (absolute
393 /// value of) the optionally specified indentation `level` and return a
394 /// reference to `stream`. If `level` is specified, optionally specify
395 /// `spacesPerLevel`, the number of spaces per indentation level for
396 /// this and all of its nested objects. If `level` is negative,
397 /// suppress indentation of the first line. If `spacesPerLevel` is
398 /// negative, format the entire output on one line, suppressing all but
399 /// the initial indentation (as governed by `level`). If `stream` is
400 /// not valid on entry, this operation has no effect.
401 bsl::ostream& print(bsl::ostream& stream,
402 int level = 0,
403 int spacesPerLevel = 4) const;
404};
405
406// FREE OPERATORS
407
408/// Return `true` if the specified `lhs` and `rhs` metric formats have the
409/// same value, and `false` otherwise. Two metric formats have the same
410/// value if they have the same value for `formatSpec` for each of the
411/// enumerated publication types.
412inline
413bool operator==(const MetricFormat& lhs, const MetricFormat& rhs);
414
415/// Return `true` if the specified `lhs` and `rhs` metric formats do not
416/// have the same value, and `false` otherwise. Two metric formats do not
417/// have same value if they differ in their `formatSpec` for any of the
418/// enumerated publication types.
419inline
420bool operator!=(const MetricFormat& lhs, const MetricFormat& rhs);
421
422/// Write a formatted description of the specified `rhs` metric format to
423/// the specified `stream`, and return a reference to the modifiable
424/// `stream`.
425inline
426bsl::ostream& operator<<(bsl::ostream& stream, const MetricFormat& rhs);
427
428// ============================================================================
429// INLINE FUNCTION DEFINITIONS
430// ============================================================================
431
432 // ----------------------
433 // class MetricFormatSpec
434 // ----------------------
435
436// CREATORS
437inline
439: d_scale(1.0f)
440, d_format(k_DEFAULT_FORMAT)
441{
442}
443
444inline
445MetricFormatSpec::MetricFormatSpec(float scale, const char *format)
446: d_scale(scale)
447, d_format(format)
448{
449}
450
451inline
453 const MetricFormatSpec& original)
454: d_scale(original.d_scale)
455, d_format(original.d_format)
456{
457}
458
459// MANIPULATORS
460inline
462 const MetricFormatSpec& rhs)
463{
464 d_scale = rhs.d_scale;
465 d_format = rhs.d_format;
466 return *this;
467}
468
469inline
471{
472 d_scale = scale;
473}
474
475inline
476void MetricFormatSpec::setFormat(const char *format)
477{
478 d_format = format;
479}
480
481// ACCESSORS
482inline
484{
485 return d_scale;
486}
487
488inline
489const char *MetricFormatSpec::format() const
490{
491 return d_format;
492}
493} // close package namespace
494
495// FREE OPERATORS
496inline
497bool balm::operator==(const MetricFormatSpec& lhs,
498 const MetricFormatSpec& rhs)
499{
500 return lhs.scale() == rhs.scale()
501 && 0 == bsl::strcmp(lhs.format(), rhs.format());
502}
503
504inline
505bool balm::operator!=(const MetricFormatSpec& lhs,
506 const MetricFormatSpec& rhs)
507{
508 return !(lhs == rhs);
509}
510
511inline
512bsl::ostream& balm::operator<<(bsl::ostream& stream,
513 const MetricFormatSpec& rhs)
514{
515 return rhs.print(stream, 0, -1);
516}
517
518namespace balm {
519 // ------------------
520 // class MetricFormat
521 // ------------------
522
523// CREATORS
524inline
526: d_formatSpecs(basicAllocator)
527{
528}
529
530inline
532 bslma::Allocator *basicAllocator)
533: d_formatSpecs(original.d_formatSpecs, basicAllocator)
534{
535}
536
537// MANIPULATORS
538inline
541{
542 d_formatSpecs = rhs.d_formatSpecs;
543 return *this;
544}
545
546inline
548 const MetricFormatSpec& formatSpec)
549{
550 if (d_formatSpecs.empty()) {
551 d_formatSpecs.resize(PublicationType::k_LENGTH);
552 }
553 d_formatSpecs[(int)publicationType].makeValue(formatSpec);
554}
555
556inline
558{
559 d_formatSpecs.clear();
560}
561
562// ACCESSORS
563inline
565 PublicationType::Value publicationType) const
566{
567 if (d_formatSpecs.empty()) {
568 return 0; // RETURN
569 }
570 const AggregateFormatSpec& spec = d_formatSpecs[publicationType];
571 return spec.isNull() ? 0 : &spec.value();
572}
573
574} // close package namespace
575
576// FREE OPERATORS
577inline
578bool balm::operator==(const MetricFormat& lhs,
579 const MetricFormat& rhs)
580{
581 return lhs.d_formatSpecs == rhs.d_formatSpecs;
582}
583
584inline
585bool balm::operator!=(const MetricFormat& lhs,
586 const MetricFormat& rhs)
587{
588 return !(lhs == rhs);
589}
590
591inline
592bsl::ostream& balm::operator<<(bsl::ostream& stream, const MetricFormat& rhs)
593{
594 return rhs.print(stream, 0, -1);
595}
596
597
598
599#endif
600
601// ----------------------------------------------------------------------------
602// Copyright 2015 Bloomberg Finance L.P.
603//
604// Licensed under the Apache License, Version 2.0 (the "License");
605// you may not use this file except in compliance with the License.
606// You may obtain a copy of the License at
607//
608// http://www.apache.org/licenses/LICENSE-2.0
609//
610// Unless required by applicable law or agreed to in writing, software
611// distributed under the License is distributed on an "AS IS" BASIS,
612// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
613// See the License for the specific language governing permissions and
614// limitations under the License.
615// ----------------------------- END-OF-FILE ----------------------------------
616
617/** @} */
618/** @} */
619/** @} */
Definition balm_metricformat.h:183
float scale() const
Definition balm_metricformat.h:483
MetricFormatSpec & operator=(const MetricFormatSpec &rhs)
Definition balm_metricformat.h:461
~MetricFormatSpec()
Destroy this object.
const char * format() const
Definition balm_metricformat.h:489
MetricFormatSpec()
Definition balm_metricformat.h:438
void setFormat(const char *format)
Definition balm_metricformat.h:476
bsl::ostream & print(bsl::ostream &stream, int level=0, int spacesPerLevel=-1) const
void setScale(float scale)
Definition balm_metricformat.h:470
static bsl::ostream & formatValue(bsl::ostream &stream, double value, const MetricFormatSpec &format)
Definition balm_metricformat.h:321
~MetricFormat()=default
Destroy this object.
void clearFormatSpec(PublicationType::Value publicationType)
void setFormatSpec(PublicationType::Value publicationType, const MetricFormatSpec &formatSpec)
Definition balm_metricformat.h:547
void clearFormatSpecs()
Definition balm_metricformat.h:557
const MetricFormatSpec * formatSpec(PublicationType::Value publicationType) const
Definition balm_metricformat.h:564
MetricFormat & operator=(const MetricFormat &rhs)
Definition balm_metricformat.h:540
bsl::ostream & print(bsl::ostream &stream, int level=0, int spacesPerLevel=4) const
friend bool operator==(const MetricFormat &lhs, const MetricFormat &rhs)
BSLMF_NESTED_TRAIT_DECLARATION(MetricFormat, bslma::UsesBslmaAllocator)
MetricFormat(bslma::Allocator *basicAllocator=0)
Definition balm_metricformat.h:525
Definition bdlb_nullablevalue.h:262
bool isNull() const BSLS_KEYWORD_NOEXCEPT
Return true if this object is null, and false otherwise.
Definition bdlb_nullablevalue.h:1829
TYPE & value()
Definition bdlb_nullablevalue.h:1792
Definition bslstl_vector.h:1120
Definition bslma_allocator.h:545
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition balm_bdlmmetricsadapter.h:142
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)
ALLOCATOR const STRING_VIEW_LIKE_TYPE & rhs
Definition bslstl_string.h:3918
ALLOCATOR & lhs
Definition bslstl_string.h:3917
@ k_LENGTH
Definition balm_publicationtype.h:128
Value
Definition balm_publicationtype.h:83
Definition bslma_usesbslmaallocator.h:344