BDE 4.14.0 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 // NOT IMPLEMENTED
181 MetricDescription& operator=(const MetricDescription&);
182
183 public:
184 // PUBLIC TYPES
185
186 /// A key used to refer to a data value associated with a metric. Note
187 /// that a `UserDataKey` can be used by clients of `balm` to associate
188 /// additional information with a metric. See @ref balm_metricregistry
189 /// for information on obtaining a unique key.
190 typedef int UserDataKey;
191
192 // CREATORS
193
194 /// Create a metric description for the specified `category` and the
195 /// specified `name`. Optionally specify a `basicAllocator` used to
196 /// supply memory. If `basicAllocator` is 0, the currently installed
197 /// default allocator is used. The initial value for
198 /// `preferredPublicationType` is `e_UNSPECIFIED`, and the initial
199 /// value for `format` is 0. The behavior is undefined unless `name`
200 /// and `category` remain valid, and the contents of `name` remain
201 /// unmodified, for the lifetime of this object.
203 const char *name,
204 bslma::Allocator *basicAllocator = 0);
205
206 /// Destroy this object
208
209 // MANIPULATORS
210
211 /// Set the name of this metric description to the specified `name`.
212 /// The behavior is undefined unless the contents of `name` remains
213 /// valid and unmodified for the lifetime of this object.
214 void setName(const char *name);
215
216 /// Set the category of this metric description to the object at the
217 /// specified `category` address. The behavior is undefined unless
218 /// `category` remains valid for the lifetime of this object.
219 void setCategory(const Category *category);
220
221 /// Set the preferred publication type of this metric to the specified
222 /// `type`. The preferred publication type of this metric indicates the
223 /// preferred aggregate to publish for this metric, or
224 /// `PublicationType::UNSPECIFIED` if there is no preference. Note
225 /// that there is no uniform definition for how publishers will
226 /// interpret this value; an `UNSPECIFIED` value generally indicates
227 /// that all the collected aggregates (total, count, minimum, and
228 /// maximum value) should be published.
230
231 /// Set the format for this metric description to the specified
232 /// `format`.
234
235 /// Associate the specified `value` with the specified data `key`. The
236 /// behavior is undefined unless `key >= 0`. Note that this method
237 /// allows clients of `balm` to associate (opaque) application-specific
238 /// information with a metric.
239 void setUserData(UserDataKey key, const void *value);
240
241 // ACCESSORS
242
243 /// Return the address of the non-modifiable, null-terminated string
244 /// containing the name of the described metric.
245 const char *name() const;
246
247 /// Return the address of the non-modifiable category object indicating
248 /// the category of the metric described by this object.
249 const Category *category() const;
250
251 /// Return the preferred publication type of this metric. The
252 /// preferred publication type of this metric indicates the preferred
253 /// aggregate to publish for this metric, or
254 /// `PublicationType::UNSPECIFIED` if there is no preference. Note
255 /// that there is no uniform definition for how publishers will
256 /// interpret this value; an `UNSPECIFIED` value generally indicates
257 /// that the all the collected aggregates (total, count, minimum, and
258 /// maximum value) should be published.
260
261 /// Return a shared pointer to the non-modifiable format for this
262 /// metric description. Note that the returned shared pointer *may*
263 /// *be* *null* if a format has not been provided for the described
264 /// metric.
266
267 /// Return the non-modifiable value associated with the specified
268 /// user-data `key`. If the data for `key` has not been set, a value of
269 /// 0 is returned, which is indistinguishable from a valid `key` with a
270 /// 0 value. The behavior is undefined unless `key >= 0`. Note that
271 /// this method allows clients of `balm` to access the (opaque)
272 /// application-specific information that they have previously
273 /// associated with a metric (via `setUserData`).
274 const void *userData(UserDataKey key) const;
275
276 /// Print the category and name of this metric description to the
277 /// specified output `stream` in some single-line human-readable form,
278 /// and return a reference to the modifiable `stream`.
279 bsl::ostream& print(bsl::ostream& stream) const;
280
281 /// Print the properties of this metric description to the specified
282 /// output `stream` in some single-line human-readable form, and return
283 /// a reference to the modifiable `stream`.
284 bsl::ostream& printDescription(bsl::ostream& stream) const;
285};
286
287// FREE OPERATORS
288
289/// Write a formatted single-line description of the specified `rhs` metric
290/// description to the specified `stream`, and return a reference to the
291/// modifiable `stream`.
292bsl::ostream& operator<<(bsl::ostream& stream,
293 const MetricDescription& rhs);
294
295// ============================================================================
296// INLINE DEFINITIONS
297// ============================================================================
298
299 // -----------------------
300 // class MetricDescription
301 // -----------------------
302
303// CREATORS
304inline
305MetricDescription::MetricDescription(const Category *category,
306 const char *name,
307 bslma::Allocator *basicAllocator)
308: d_category_p(category)
309, d_name_p(name)
310, d_preferredPublicationType(PublicationType::e_UNSPECIFIED)
311, d_format()
312, d_userData(basicAllocator)
313, d_mutex()
314{
315}
316
317// MANIPULATORS
318inline
319void MetricDescription::setName(const char *name)
320{
321 d_name_p = name;
322}
323
324inline
326{
327 d_category_p = category;
328}
329
330inline
333{
334 // This guard is not strictly required on any supported platform.
335 bslmt::LockGuard<bslmt::Mutex> guard(&d_mutex);
336 d_preferredPublicationType = type;
337}
338
339inline
342{
343 bslmt::LockGuard<bslmt::Mutex> guard(&d_mutex);
344 d_format = format;
345}
346
347inline
348void MetricDescription::setUserData(UserDataKey key, const void *value)
349{
350 BSLS_ASSERT(key >= 0);
351
352 bslmt::LockGuard<bslmt::Mutex> guard(&d_mutex);
353 if ((unsigned int)key >= d_userData.size()) {
354 d_userData.resize(key + 1, 0);
355 }
356 d_userData[key] = value;
357}
358
359// ACCESSORS
360inline
361const char *MetricDescription::name() const
362{
363 return d_name_p;
364}
365
366inline
368{
369 return d_category_p;
370}
371
372inline
375{
376 // This guard is not strictly required on any supported platform.
377 bslmt::LockGuard<bslmt::Mutex> guard(&d_mutex);
378 return d_preferredPublicationType;
379}
380
381inline
384
385{
386 bslmt::LockGuard<bslmt::Mutex> guard(&d_mutex);
387 return d_format;
388}
389
390inline
392{
393 BSLS_ASSERT(key >= 0);
394 bslmt::LockGuard<bslmt::Mutex> guard(&d_mutex);
395 return ((unsigned int)key < d_userData.size()) ? d_userData[key] : 0;
396}
397
398} // close package namespace
399
400// FREE OPERATORS
401inline
402bsl::ostream& balm::operator<<(bsl::ostream& stream,
403 const MetricDescription& rhs)
404{
405 return rhs.print(stream);
406}
407
408
409
410#endif
411
412// ----------------------------------------------------------------------------
413// Copyright 2015 Bloomberg Finance L.P.
414//
415// Licensed under the Apache License, Version 2.0 (the "License");
416// you may not use this file except in compliance with the License.
417// You may obtain a copy of the License at
418//
419// http://www.apache.org/licenses/LICENSE-2.0
420//
421// Unless required by applicable law or agreed to in writing, software
422// distributed under the License is distributed on an "AS IS" BASIS,
423// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
424// See the License for the specific language governing permissions and
425// limitations under the License.
426// ----------------------------- END-OF-FILE ----------------------------------
427
428/** @} */
429/** @} */
430/** @} */
Definition balm_category.h:151
Definition balm_metricdescription.h:158
void setName(const char *name)
Definition balm_metricdescription.h:319
bsl::ostream & printDescription(bsl::ostream &stream) const
void setPreferredPublicationType(PublicationType::Value type)
Definition balm_metricdescription.h:331
~MetricDescription()=default
Destroy this object.
void setCategory(const Category *category)
Definition balm_metricdescription.h:325
const void * userData(UserDataKey key) const
Definition balm_metricdescription.h:391
bsl::ostream & print(bsl::ostream &stream) const
void setUserData(UserDataKey key, const void *value)
Definition balm_metricdescription.h:348
void setFormat(const bsl::shared_ptr< const MetricFormat > &format)
Definition balm_metricdescription.h:340
int UserDataKey
Definition balm_metricdescription.h:190
const Category * category() const
Definition balm_metricdescription.h:367
bsl::shared_ptr< const MetricFormat > format() const
Definition balm_metricdescription.h:383
PublicationType::Value preferredPublicationType() const
Definition balm_metricdescription.h:374
const char * name() const
Definition balm_metricdescription.h:361
Definition bslstl_sharedptr.h:1830
size_type size() const BSLS_KEYWORD_NOEXCEPT
Return the number of elements in this vector.
Definition bslstl_vector.h:2664
Definition bslstl_vector.h:1025
void resize(size_type newSize)
Definition bslstl_vector.h:3616
Definition bslma_allocator.h:457
Definition bslmt_lockguard.h:234
Definition bslmt_mutex.h:315
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1804
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition balm_bdlmmetricsadapter.h:141
bsl::ostream & operator<<(bsl::ostream &stream, const Category &rhs)
Definition balm_publicationtype.h:77
Value
Definition balm_publicationtype.h:81