BDE 4.39.x Production Release
Loading...
Searching...
No Matches
balm_metricdescription.h
Go to the documentation of this file.
1/// @file balm_metricdescription.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// balm_metricdescription.h -*-C++-*-
8#ifndef INCLUDED_BALM_METRICDESCRIPTION
9#define INCLUDED_BALM_METRICDESCRIPTION
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup balm_metricdescription balm_metricdescription
15/// @brief Provide a description for a metric.
16/// @addtogroup bal
17/// @{
18/// @addtogroup balm
19/// @{
20/// @addtogroup balm_metricdescription
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#balm_metricdescription-purpose"> Purpose</a>
25/// * <a href="#balm_metricdescription-classes"> Classes </a>
26/// * <a href="#balm_metricdescription-description"> Description </a>
27/// * <a href="#balm_metricdescription-alternative-systems-for-telemetry"> Alternative Systems for Telemetry </a>
28/// * <a href="#balm_metricdescription-thread-safety"> Thread Safety </a>
29/// * <a href="#balm_metricdescription-usage"> Usage </a>
30/// * <a href="#balm_metricdescription-example-1-basic-usage"> Example 1: Basic Usage </a>
31///
32/// # Purpose {#balm_metricdescription-purpose}
33/// Provide a description for a metric.
34///
35/// # Classes {#balm_metricdescription-classes}
36///
37/// - balm::MetricDescription: describes a metric
38///
39/// @see balm_metricregistry, balm_metricid, balm_category
40///
41/// # Description {#balm_metricdescription-description}
42/// This component provides a class, `balm::MetricDescription`,
43/// used to describe a metric. A `balm::MetricDescription` object contains the
44/// address of the category to which the metric belongs and also the address of
45/// the null-terminated string holding the name of the metric. The
46/// `balm::MetricDescription` class suppresses copy construction and assignment,
47/// and does not provide equality operators: Applications should use a *single*
48/// `balm::MetricDescription` object per metric (such as one provided by the
49/// *`balm::MetricRegistry`* component).
50///
51/// IMPORTANT: The metric description's `name`, whose type is `const char *`,
52/// must remain constant and valid throughout the lifetime of the
53/// `balm::MetricDescription` object.
54///
55/// ## Alternative Systems for Telemetry {#balm_metricdescription-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_metricdescription-thread-safety}
62///
63///
64/// `balm::MetricDescription` is *const* *thread-safe*, meaning that accessors
65/// may be invoked concurrently from different threads, but it is not safe to
66/// access or modify a `balm::MetricDescription` in one thread while another
67/// thread modifies the same object. However, clients of the `balm` package
68/// accessing a non-modifiable `balm::MetricDescription` supplied by a
69/// `balm::MetricRegistry` (by way of a `balm::MetricId`) can safely access the
70/// properties of that metric description at any time.
71///
72/// ## Usage {#balm_metricdescription-usage}
73///
74///
75/// This section illustrates intended use of this component.
76///
77/// ### Example 1: Basic Usage {#balm_metricdescription-example-1-basic-usage}
78///
79///
80/// The following example demonstrates how to create and access a
81/// `balm::MetricDescription` object. We start by creating a category:
82/// @code
83/// balm::Category myCategory("MyCategory");
84/// @endcode
85/// Then we use that category to create three metric description objects with
86/// different names:
87/// @code
88/// balm::MetricDescription metricA(&myCategory, "A");
89/// balm::MetricDescription metricB(&myCategory, "B");
90/// balm::MetricDescription metricC(&myCategory, "C");
91/// @endcode
92/// We can use the `category` and `name` methods to access their values:
93/// @code
94/// assert(&myCategory == metricA.category());
95/// assert(&myCategory == metricB.category());
96/// assert(&myCategory == metricC.category());
97///
98/// assert(0 == bsl::strcmp("A", metricA.name()));
99/// assert(0 == bsl::strcmp("B", metricB.name()));
100/// assert(0 == bsl::strcmp("C", metricC.name()));
101/// @endcode
102/// Finally, we write all three metric descriptions to the console:
103/// @code
104/// bsl::cout << "metricA: " << metricA << bsl::endl
105/// << "metricB: " << metricB << bsl::endl
106/// << "metricC: " << metricC << bsl::endl;
107/// @endcode
108/// With the following console output:
109/// @code
110/// metricA: MyCategory.A
111/// metricB: MyCategory.B
112/// metricC: MyCategory.C
113/// @endcode
114/// @}
115/** @} */
116/** @} */
117
118/** @addtogroup bal
119 * @{
120 */
121/** @addtogroup balm
122 * @{
123 */
124/** @addtogroup balm_metricdescription
125 * @{
126 */
127
128#include <balscm_version.h>
129
130#include <balm_publicationtype.h>
131
132#include <bslmt_lockguard.h>
133#include <bslmt_mutex.h>
134
135#include <bsls_assert.h>
136#include <bsls_review.h>
137
138#include <bsl_iosfwd.h>
139#include <bsl_memory.h>
140
141
142
143
144namespace balm {
145
146class Category;
147class MetricFormat;
148
149 // =======================
150 // class MetricDescription
151 // =======================
152
153/// This class provides a mechanism for describing a metric. A
154/// `MetricDescription` holds the category to which the metric belongs, and
155/// a null-terminated string containing the name of the metric.
156///
157/// See @ref balm_metricdescription
159
160 // DATA
161 const Category *d_category_p; // category of metric (held, not owned)
162
163 const char *d_name_p; // name of metric (held, not owned)
164
166 d_preferredPublicationType;
167 // preferred publication type
168
170 d_format; // format for this metric
171
173 d_userData; // user data, indexed by keys
174
175 mutable bslmt::Mutex
176 d_mutex; // synchronize non-'const' elements
177 // (publication type, format, user data)
178
179 private:
180 // NOT IMPLEMENTED
182 MetricDescription& operator=(const MetricDescription&);
183
184 public:
185 // PUBLIC TYPES
186
187 /// A key used to refer to a data value associated with a metric.
188 ///
189 /// \note Note that a `UserDataKey` can be used by clients of `balm` to associate
190 /// additional information with a metric. See @ref balm_metricregistry
191 /// for information on obtaining a unique key.
192 typedef int UserDataKey;
193
194 // CREATORS
195
196 /// Create a metric description for the specified `category` and the
197 /// specified `name`. Optionally specify a `basicAllocator` used to
198 /// supply memory. If `basicAllocator` is 0, the currently installed
199 /// default allocator is used. The initial value for
200 /// `preferredPublicationType` is `e_UNSPECIFIED`, and the initial value for `format` is 0.
201 ///
202 /// \pre The behavior is undefined unless `name`
203 /// and `category` remain valid, and the contents of `name` remain
204 /// unmodified, for the lifetime of this object.
206 const char *name,
207 bslma::Allocator *basicAllocator = 0);
208
209 /// Destroy this object
211
212 // MANIPULATORS
213
214 /// Set the name of this metric description to the specified `name`.
215 ///
216 /// \pre The behavior is undefined unless the contents of `name` remains
217 /// valid and unmodified for the lifetime of this object.
218 void setName(const char *name);
219
220 /// Set the category of this metric description to the object at the specified `category` address.
221 ///
222 /// \pre The behavior is undefined unless
223 /// `category` remains valid for the lifetime of this object.
224 void setCategory(const Category *category);
225
226 /// Set the preferred publication type of this metric to the specified
227 /// `type`. The preferred publication type of this metric indicates the
228 /// preferred aggregate to publish for this metric, or
229 /// `PublicationType::UNSPECIFIED` if there is no preference.
230 ///
231 /// \note Note that there is no uniform definition for how publishers will
232 /// interpret this value; an `UNSPECIFIED` value generally indicates
233 /// that all the collected aggregates (total, count, minimum, and
234 /// maximum value) should be published.
236
237 /// Set the format for this metric description to the specified
238 /// `format`.
240
241 /// Associate the specified `value` with the specified data `key`.
242 ///
243 /// \pre The behavior is undefined unless `key >= 0`.
244 /// \note Note that this method
245 /// allows clients of `balm` to associate (opaque) application-specific
246 /// information with a metric.
247 void setUserData(UserDataKey key, const void *value);
248
249 // ACCESSORS
250
251 /// Return the address of the non-modifiable, null-terminated string
252 /// containing the name of the described metric.
253 const char *name() const;
254
255 /// Return the address of the non-modifiable category object indicating
256 /// the category of the metric described by this object.
257 const Category *category() const;
258
259 /// Return the preferred publication type of this metric. The
260 /// preferred publication type of this metric indicates the preferred
261 /// aggregate to publish for this metric, or
262 /// `PublicationType::UNSPECIFIED` if there is no preference.
263 ///
264 /// \note Note that there is no uniform definition for how publishers will
265 /// interpret this value; an `UNSPECIFIED` value generally indicates
266 /// that the all the collected aggregates (total, count, minimum, and
267 /// maximum value) should be published.
269
270 /// Return a shared pointer to the non-modifiable format for this metric description.
271 ///
272 /// \note Note that the returned shared pointer *may*
273 /// *be* *null* if a format has not been provided for the described
274 /// metric.
276
277 /// Return the non-modifiable value associated with the specified
278 /// user-data `key`. If the data for `key` has not been set, a value of
279 /// 0 is returned, which is indistinguishable from a valid `key` with a 0 value.
280 ///
281 /// \pre The behavior is undefined unless `key >= 0`.
282 ///
283 /// \note Note that this method allows clients of `balm` to access the (opaque)
284 /// application-specific information that they have previously
285 /// associated with a metric (via `setUserData`).
286 const void *userData(UserDataKey key) const;
287
288 /// Print the category and name of this metric description to the
289 /// specified output `stream` in some single-line human-readable form,
290 /// and return a reference to the modifiable `stream`.
291 bsl::ostream& print(bsl::ostream& stream) const;
292
293 /// Print the properties of this metric description to the specified
294 /// output `stream` in some single-line human-readable form, and return
295 /// a reference to the modifiable `stream`.
296 bsl::ostream& printDescription(bsl::ostream& stream) const;
297};
298
299// FREE OPERATORS
300
301/// Write a formatted single-line description of the specified `rhs` metric
302/// description to the specified `stream`, and return a reference to the
303/// modifiable `stream`.
304bsl::ostream& operator<<(bsl::ostream& stream,
305 const MetricDescription& rhs);
306
307// ============================================================================
308// INLINE DEFINITIONS
309// ============================================================================
310
311 // -----------------------
312 // class MetricDescription
313 // -----------------------
314
315// CREATORS
316inline
317MetricDescription::MetricDescription(const Category *category,
318 const char *name,
319 bslma::Allocator *basicAllocator)
320: d_category_p(category)
321, d_name_p(name)
322, d_preferredPublicationType(PublicationType::e_UNSPECIFIED)
323, d_format()
324, d_userData(basicAllocator)
325, d_mutex()
326{
327}
328
329// MANIPULATORS
330inline
331void MetricDescription::setName(const char *name)
332{
333 d_name_p = name;
334}
335
336inline
338{
339 d_category_p = category;
340}
341
342inline
345{
346 // This guard is not strictly required on any supported platform.
347 bslmt::LockGuard<bslmt::Mutex> guard(&d_mutex);
348 d_preferredPublicationType = type;
349}
350
351inline
354{
355 bslmt::LockGuard<bslmt::Mutex> guard(&d_mutex);
356 d_format = format;
357}
358
359inline
360void MetricDescription::setUserData(UserDataKey key, const void *value)
361{
362 BSLS_ASSERT(key >= 0);
363
364 bslmt::LockGuard<bslmt::Mutex> guard(&d_mutex);
365 if ((unsigned int)key >= d_userData.size()) {
366 d_userData.resize(key + 1, 0);
367 }
368 d_userData[key] = value;
369}
370
371// ACCESSORS
372inline
373const char *MetricDescription::name() const
374{
375 return d_name_p;
376}
377
378inline
380{
381 return d_category_p;
382}
383
384inline
387{
388 // This guard is not strictly required on any supported platform.
389 bslmt::LockGuard<bslmt::Mutex> guard(&d_mutex);
390 return d_preferredPublicationType;
391}
392
393inline
396
397{
398 bslmt::LockGuard<bslmt::Mutex> guard(&d_mutex);
399 return d_format;
400}
401
402inline
404{
405 BSLS_ASSERT(key >= 0);
406 bslmt::LockGuard<bslmt::Mutex> guard(&d_mutex);
407 return ((unsigned int)key < d_userData.size()) ? d_userData[key] : 0;
408}
409
410} // close package namespace
411
412// FREE OPERATORS
413inline
414bsl::ostream& balm::operator<<(bsl::ostream& stream,
415 const MetricDescription& rhs)
416{
417 return rhs.print(stream);
418}
419
420
421
422#endif
423
424// ----------------------------------------------------------------------------
425// Copyright 2015 Bloomberg Finance L.P.
426//
427// Licensed under the Apache License, Version 2.0 (the "License");
428// you may not use this file except in compliance with the License.
429// You may obtain a copy of the License at
430//
431// http://www.apache.org/licenses/LICENSE-2.0
432//
433// Unless required by applicable law or agreed to in writing, software
434// distributed under the License is distributed on an "AS IS" BASIS,
435// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
436// See the License for the specific language governing permissions and
437// limitations under the License.
438// ----------------------------- END-OF-FILE ----------------------------------
439
440/** @} */
441/** @} */
442/** @} */
Definition balm_category.h:152
Definition balm_metricdescription.h:158
void setName(const char *name)
Definition balm_metricdescription.h:331
bsl::ostream & printDescription(bsl::ostream &stream) const
void setPreferredPublicationType(PublicationType::Value type)
Definition balm_metricdescription.h:343
~MetricDescription()=default
Destroy this object.
void setCategory(const Category *category)
Definition balm_metricdescription.h:337
const void * userData(UserDataKey key) const
Definition balm_metricdescription.h:403
bsl::ostream & print(bsl::ostream &stream) const
void setUserData(UserDataKey key, const void *value)
Definition balm_metricdescription.h:360
void setFormat(const bsl::shared_ptr< const MetricFormat > &format)
Definition balm_metricdescription.h:352
int UserDataKey
Definition balm_metricdescription.h:192
const Category * category() const
Definition balm_metricdescription.h:379
bsl::shared_ptr< const MetricFormat > format() const
Definition balm_metricdescription.h:395
PublicationType::Value preferredPublicationType() const
Definition balm_metricdescription.h:386
const char * name() const
Definition balm_metricdescription.h:373
Definition bslstl_sharedptr.h:1838
size_type size() const BSLS_KEYWORD_NOEXCEPT
Return the number of elements in this vector.
Definition bslstl_vector.h:3019
Definition bslstl_vector.h:1120
void resize(size_type newSize)
Definition bslstl_vector.h:4189
Definition bslma_allocator.h:545
Definition bslmt_lockguard.h:234
Definition bslmt_mutex.h:317
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1976
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition balm_bdlmmetricsadapter.h:142
bsl::ostream & operator<<(bsl::ostream &stream, const Category &rhs)
Definition balm_publicationtype.h:79
Value
Definition balm_publicationtype.h:83