BDE 4.14.0 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. The behavior
215 /// is undefined unless `format` is null-terminated, contains a
216 /// `printf`-style format string valid for a single floating-point
217 /// value, and remains valid and unmodified for the lifetime of this
218 /// object.
219 MetricFormatSpec(float scale, const char *format);
220
221 /// Create a metric format spec having the same value as the specified
222 /// `original` format spec. The behavior is undefined unless
223 /// `original.format()` remains valid and unmodified for the lifetime of
224 /// this object.
225 MetricFormatSpec(const MetricFormatSpec& original);
226
227 /// Destroy this object.
229
230 // MANIPULATORS
231
232 /// Assign to this format spec the value of the specified `rhs` format
233 /// spec, and return a reference to this modifiable format spec.
235
236 /// Set, to the specified `scale`, the scale multiplier that may be
237 /// applied when formatting values.
238 void setScale(float scale);
239
240 /// Set, to the specified `format`, the `printf`-style formatting string
241 /// that may be applied when formatting values. The behavior is
242 /// undefined unless `format` is null-terminated, contains a
243 /// `printf`-style format string valid for a single floating-point
244 /// value, and remains valid and unmodified for the lifetime of this
245 /// object.
246 void setFormat(const char *format);
247
248 // ACCESSORS
249
250 /// Return the floating-point multiplier value that may be applied to
251 /// scale formatted values.
252 float scale() const;
253
254 /// Return the address of the null-terminated string containing the
255 /// `printf`-style format that may be used to format values.
256 const char *format() const;
257
258 /// Format this object to the specified output `stream` at the (absolute
259 /// value of) the optionally specified indentation `level` and return a
260 /// reference to `stream`. If `level` is specified, optionally specify
261 /// `spacesPerLevel`, the number of spaces per indentation level for
262 /// this and all of its nested objects. If `level` is negative,
263 /// suppress indentation of the first line. If `spacesPerLevel` is
264 /// negative, format the entire output on one line, suppressing all but
265 /// the initial indentation (as governed by `level`). If `stream` is
266 /// not valid on entry, this operation has no effect.
267 bsl::ostream& print(bsl::ostream& stream,
268 int level = 0,
269 int spacesPerLevel = -1) const;
270};
271
272// ============================================================================
273// INLINE DEFINITIONS
274// ============================================================================
275
276// FREE OPERATORS
277
278/// Return `true` if the specified `lhs` and `rhs` metric format specs have the
279/// same value, and `false` otherwise. Two format specs have the same value if
280/// they have the same values for their `scale` and `format` attributes,
281/// respectively.
282inline
283bool operator==(const MetricFormatSpec& lhs,
284 const MetricFormatSpec& rhs);
285
286/// Return `true` if the specified `lhs` and `rhs` metric format specs do not
287/// have the same value, and `false` otherwise. Two format specs do not have
288/// same value if they differ in their respective values for `scale` or
289/// `format` attributes.
290inline
291bool operator!=(const MetricFormatSpec& lhs,
292 const MetricFormatSpec& rhs);
293
294/// Write a formatted description of the specified `rhs` format spec to the
295/// specified `stream`, and return a reference to the modifiable `stream`.
296inline
297bsl::ostream& operator<<(bsl::ostream& stream,
298 const MetricFormatSpec& rhs);
299
300 // ==================
301 // class MetricFormat
302 // ==================
303
304/// This class provides a value-semantic description for the formatting of a
305/// metric. For each published aggregate type of a metric (e.g., count,
306/// total, min, max, etc.), a `MetricFormat` contains a `MetricFormatSpec`
307/// object describing how to format values of that aggregate, or null if no
308/// formatting information is supplied. `Metricformat` provides the
309/// `setFormatSpec` method to set the format spec for a publication type,
310/// and the `formatSpec` method to retrieve the format spec for a
311/// publication type (or 0 if no format spec has been provided for the
312/// indicated publication type). Note that the types of published
313/// aggregates explicitly provided by the `balm` package are defined in the
314/// `PublicationType` enumeration.
315///
316/// See @ref balm_metricformat
318
319 // TYPES
321
322 // DATA
324 // array of length 0, or of length
325 // 'PublicationType::k_LENGTH', holding a
326 // mapping of the publication type to the
327 // (possibly null) formatting options for that
328 // type
329
330 // FRIENDS
331 friend bool operator==(const MetricFormat& lhs,
332 const MetricFormat& rhs);
333
334 public:
335 // PUBLIC TRAITS
337
338 // CREATORS
339
340 /// Create an empty metric format object. Optionally specify a
341 /// `basicAllocator` used to supply memory. If `basicAllocator` is 0,
342 /// the currently installed default allocator is used. Note that
343 /// `formatSpec` will return 0 for all supplied publication types.
344 MetricFormat(bslma::Allocator *basicAllocator = 0);
345
346 /// Create a metric format object having the same value as the specified
347 /// `original` format. Optionally specify a `basicAllocator` used to
348 /// supply memory. If `basicAllocator` is 0, the currently installed
349 /// default allocator is used.
350 MetricFormat(const MetricFormat& original,
351 bslma::Allocator *basicAllocator = 0);
352
353 /// Destroy this object.
354 ~MetricFormat() = default;
355
356 // MANIPULATORS
357
358 /// Assign to this metric format object the value of the specified `rhs`
359 /// metric format, and return a reference to this modifiable metric
360 /// format.
362
363 /// Set the format spec for the metric aggregate indicated by the
364 /// specified `publicationType` to the specified `formatSpec`.
365 void setFormatSpec(PublicationType::Value publicationType,
367
368 /// Remove all format specs from this metric format object and put this
369 /// object into its default-constructed state. After this method
370 /// returns, `formatSpec` will return 0 for all supplied publication
371 /// types.
372 void clearFormatSpecs();
373
374 /// Remove the format spec for the specified `publicationType` from this
375 /// metric format object. After this methods returns,
376 /// `formatSpec(publicationType)` will return 0.
378
379 // ACCESSORS
380
381 /// Return the address of the non-modifiable format spec for the
382 /// specified `publicationType`, or 0 if no format spec has been
383 /// provided for `publicationType`.
385 PublicationType::Value publicationType) const;
386
387 /// Format this object to the specified output `stream` at the (absolute
388 /// value of) the optionally specified indentation `level` and return a
389 /// reference to `stream`. If `level` is specified, optionally specify
390 /// `spacesPerLevel`, the number of spaces per indentation level for
391 /// this and all of its nested objects. If `level` is negative,
392 /// suppress indentation of the first line. If `spacesPerLevel` is
393 /// negative, format the entire output on one line, suppressing all but
394 /// the initial indentation (as governed by `level`). If `stream` is
395 /// not valid on entry, this operation has no effect.
396 bsl::ostream& print(bsl::ostream& stream,
397 int level = 0,
398 int spacesPerLevel = 4) const;
399};
400
401// FREE OPERATORS
402
403/// Return `true` if the specified `lhs` and `rhs` metric formats have the
404/// same value, and `false` otherwise. Two metric formats have the same
405/// value if they have the same value for `formatSpec` for each of the
406/// enumerated publication types.
407inline
408bool operator==(const MetricFormat& lhs, const MetricFormat& rhs);
409
410/// Return `true` if the specified `lhs` and `rhs` metric formats do not
411/// have the same value, and `false` otherwise. Two metric formats do not
412/// have same value if they differ in their `formatSpec` for any of the
413/// enumerated publication types.
414inline
415bool operator!=(const MetricFormat& lhs, const MetricFormat& rhs);
416
417/// Write a formatted description of the specified `rhs` metric format to
418/// the specified `stream`, and return a reference to the modifiable
419/// `stream`.
420inline
421bsl::ostream& operator<<(bsl::ostream& stream, const MetricFormat& rhs);
422
423// ============================================================================
424// INLINE FUNCTION DEFINITIONS
425// ============================================================================
426
427 // ----------------------
428 // class MetricFormatSpec
429 // ----------------------
430
431// CREATORS
432inline
434: d_scale(1.0f)
435, d_format(k_DEFAULT_FORMAT)
436{
437}
438
439inline
440MetricFormatSpec::MetricFormatSpec(float scale, const char *format)
441: d_scale(scale)
442, d_format(format)
443{
444}
445
446inline
448 const MetricFormatSpec& original)
449: d_scale(original.d_scale)
450, d_format(original.d_format)
451{
452}
453
454// MANIPULATORS
455inline
457 const MetricFormatSpec& rhs)
458{
459 d_scale = rhs.d_scale;
460 d_format = rhs.d_format;
461 return *this;
462}
463
464inline
466{
467 d_scale = scale;
468}
469
470inline
471void MetricFormatSpec::setFormat(const char *format)
472{
473 d_format = format;
474}
475
476// ACCESSORS
477inline
479{
480 return d_scale;
481}
482
483inline
484const char *MetricFormatSpec::format() const
485{
486 return d_format;
487}
488} // close package namespace
489
490// FREE OPERATORS
491inline
492bool balm::operator==(const MetricFormatSpec& lhs,
493 const MetricFormatSpec& rhs)
494{
495 return lhs.scale() == rhs.scale()
496 && 0 == bsl::strcmp(lhs.format(), rhs.format());
497}
498
499inline
500bool balm::operator!=(const MetricFormatSpec& lhs,
501 const MetricFormatSpec& rhs)
502{
503 return !(lhs == rhs);
504}
505
506inline
507bsl::ostream& balm::operator<<(bsl::ostream& stream,
508 const MetricFormatSpec& rhs)
509{
510 return rhs.print(stream, 0, -1);
511}
512
513namespace balm {
514 // ------------------
515 // class MetricFormat
516 // ------------------
517
518// CREATORS
519inline
521: d_formatSpecs(basicAllocator)
522{
523}
524
525inline
527 bslma::Allocator *basicAllocator)
528: d_formatSpecs(original.d_formatSpecs, basicAllocator)
529{
530}
531
532// MANIPULATORS
533inline
536{
537 d_formatSpecs = rhs.d_formatSpecs;
538 return *this;
539}
540
541inline
543 const MetricFormatSpec& formatSpec)
544{
545 if (d_formatSpecs.empty()) {
546 d_formatSpecs.resize(PublicationType::k_LENGTH);
547 }
548 d_formatSpecs[(int)publicationType].makeValue(formatSpec);
549}
550
551inline
553{
554 d_formatSpecs.clear();
555}
556
557// ACCESSORS
558inline
560 PublicationType::Value publicationType) const
561{
562 if (d_formatSpecs.empty()) {
563 return 0; // RETURN
564 }
565 const AggregateFormatSpec& spec = d_formatSpecs[publicationType];
566 return spec.isNull() ? 0 : &spec.value();
567}
568
569} // close package namespace
570
571// FREE OPERATORS
572inline
573bool balm::operator==(const MetricFormat& lhs,
574 const MetricFormat& rhs)
575{
576 return lhs.d_formatSpecs == rhs.d_formatSpecs;
577}
578
579inline
580bool balm::operator!=(const MetricFormat& lhs,
581 const MetricFormat& rhs)
582{
583 return !(lhs == rhs);
584}
585
586inline
587bsl::ostream& balm::operator<<(bsl::ostream& stream, const MetricFormat& rhs)
588{
589 return rhs.print(stream, 0, -1);
590}
591
592
593
594#endif
595
596// ----------------------------------------------------------------------------
597// Copyright 2015 Bloomberg Finance L.P.
598//
599// Licensed under the Apache License, Version 2.0 (the "License");
600// you may not use this file except in compliance with the License.
601// You may obtain a copy of the License at
602//
603// http://www.apache.org/licenses/LICENSE-2.0
604//
605// Unless required by applicable law or agreed to in writing, software
606// distributed under the License is distributed on an "AS IS" BASIS,
607// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
608// See the License for the specific language governing permissions and
609// limitations under the License.
610// ----------------------------- END-OF-FILE ----------------------------------
611
612/** @} */
613/** @} */
614/** @} */
Definition balm_metricformat.h:183
float scale() const
Definition balm_metricformat.h:478
MetricFormatSpec & operator=(const MetricFormatSpec &rhs)
Definition balm_metricformat.h:456
~MetricFormatSpec()
Destroy this object.
const char * format() const
Definition balm_metricformat.h:484
MetricFormatSpec()
Definition balm_metricformat.h:433
void setFormat(const char *format)
Definition balm_metricformat.h:471
bsl::ostream & print(bsl::ostream &stream, int level=0, int spacesPerLevel=-1) const
void setScale(float scale)
Definition balm_metricformat.h:465
static bsl::ostream & formatValue(bsl::ostream &stream, double value, const MetricFormatSpec &format)
Definition balm_metricformat.h:317
~MetricFormat()=default
Destroy this object.
void clearFormatSpec(PublicationType::Value publicationType)
void setFormatSpec(PublicationType::Value publicationType, const MetricFormatSpec &formatSpec)
Definition balm_metricformat.h:542
void clearFormatSpecs()
Definition balm_metricformat.h:552
const MetricFormatSpec * formatSpec(PublicationType::Value publicationType) const
Definition balm_metricformat.h:559
MetricFormat & operator=(const MetricFormat &rhs)
Definition balm_metricformat.h:535
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:520
Definition bdlb_nullablevalue.h:257
bool isNull() const BSLS_KEYWORD_NOEXCEPT
Return true if this object is null, and false otherwise.
Definition bdlb_nullablevalue.h:1779
TYPE & value()
Definition bdlb_nullablevalue.h:1742
Definition bslstl_vector.h:1025
Definition bslma_allocator.h:457
#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)
@ k_LENGTH
Definition balm_publicationtype.h:126
Value
Definition balm_publicationtype.h:81
Definition bslma_usesbslmaallocator.h:343