BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bdlm.h
Go to the documentation of this file.
1/// @file bdlm.h
2///
3///
4/// @defgroup bdlm Package bdlm
5/// @brief Basic Development Library Metrics (bdlm)
6/// @addtogroup bdl
7/// @{
8/// @addtogroup bdlm
9/// [bdlm]: group__bdlm.html
10/// @{
11///
12/// # Purpose {#bdlm-purpose}
13/// Provide metrics registrars.
14///
15/// # Mnemonic {#bdlm-mnemonic}
16/// Basic Development Library Metrics (bdlm)
17///
18/// # Description {#bdlm-description}
19/// The 'bdlm' package provides a means for low-level library
20/// software to collect and publish metrics through a metric publishing framework,
21/// without a library depending on the metrics publishing framework. The 'bdlm'
22/// package provides a protocol (i.e., a pure abstract interface),
23/// 'bdlm::MetricsAdapter', that can be implemented for (higher level) metrics
24/// facilities. In addition, it also provides a registry of metrics,
25/// 'bdlm::MetricRegistry', that allows low-level users of 'bdlm' to register
26/// metrics at any time, serving as an intermediary with concrete
27/// 'bdlm::MetricsAdapter' implementations (which may be configured before or
28/// after the creation of any particular metric).
29///
30/// As a low-level metrics facility, this package does not directly manage
31/// schedulers to collect metrics values or publishers to publish collected
32/// values. Instead it is designed to allow applications to plug in different
33/// high-level feature-rich metrics collection and publication frameworks (without
34/// requiring a library dependency on those frameworks).
35///
36/// ## Hierarchical Synopsis
37///
38/// The 'bdlm' package currently has 5 components having 4 levels of physical
39/// dependency. The list below shows the hierarchical ordering of the components.
40/// The order of components within each level is not architecturally significant,
41/// just alphabetical.
42/// @code
43/// 4. bdlm_metricsregistry !DEPRECATED!
44///
45/// 3. bdlm_metricsadapter !DEPRECATED!
46///
47/// 2. bdlm_metricdescriptor !DEPRECATED!
48///
49/// 1. bdlm_instancecount !DEPRECATED!
50/// bdlm_metric !DEPRECATED!
51/// @endcode
52///
53/// ## Component Synopsis
54///
55/// @ref bdlm_instancecount : !DEPRECATED!
56/// Provide a type specific instance count.
57///
58/// @ref bdlm_metric : !DEPRECATED!
59/// Provide a class to store metric values of different types.
60///
61/// @ref bdlm_metricdescriptor : !DEPRECATED!
62/// Provide an attribute class to describe a metric.
63///
64/// @ref bdlm_metricsadapter : !DEPRECATED!
65/// Provide an abstract interface for metrics registration mechanisms.
66///
67/// @ref bdlm_metricsregistry : !DEPRECATED!
68/// Provide a transferable registry of metric registrations.
69///
70/// ## Instrumenting a Class
71///
72/// Here, we describe how to instrument a low level class ('YourClass') to report
73/// metrics through 'bdlm'. Applications will configure 'bdlm' with a
74/// 'bdlm::MetricAdapter' implementation for their preferred metrics framework, so
75/// that metrics reported via 'bdlm' will be published through that framework
76/// (without requiring a direct library dependency).
77///
78/// A software metric is a measurement (or collection of measurements) about a
79/// running system. The only metric classification 'bdlm' currently supports is a
80/// 'Guage', which is a metric holding a single value for the most recent
81/// measurement (other possible metric classifications include summaries,
82/// counters, and distributions). Information is provided to 'bdlm' about a
83/// metric by registering a function having the 'bdlm::MetricsRegistry::Callback'
84/// signature. Typically, the function is declared in an unnamed namespace.
85///
86/// Here we define a metric reporting function 'youMetric' for reporting a metric
87/// related to 'YourClass':
88/// @code
89/// void yourMetric(BloombergLP::bdlm::Metric *value,
90/// const BloombergLP::package::YourClass *object)
91/// {
92/// *value = BloombergLP::bdlm::Metric::Gauge(object->interestingValue());
93/// }
94/// @endcode
95/// A class exposes metrics by registering functors with a
96/// 'bdlm::MetricsRegistry'. Typically, this registry is provided in the
97/// constructors of the class, or the default singleton registry,
98/// 'bdlm::MetricsRegistry::defaultInstance()', is used:
99/// @code
100/// YourClass(bdlm::MetricsRegistry *metricsRegistry)
101/// {
102/// bdlm::MetricsRegistry *registry = metricsRegistry
103/// ? metricsRegistry
104/// : &bdlm::MetricsRegistry::defaultInstance();
105/// @endcode
106/// A metrics registration requires information to identify the metric and a
107/// functor, with signature 'bdlm::MetricsRegistry::Callback', to produce the
108/// metric when the publication system requests the value. The identity
109/// information is provided in a 'bdlm::MetricDescriptor', which is meant to
110/// contain a superset of data needed by used publication systems (e.g., BALM and
111/// GUTS).
112///
113/// The pieces of information used to identify a metric are (i.e., the arguments
114/// to create a 'MetricDescriptor):
115/// * Metric Namespace
116/// * Metric Name
117/// * A number uniquely identifying this object's instance of the class
118/// * A name identifying the class
119/// * An abbreviation for the class name
120/// * A unique text identifying this object's instance of the class
121///
122/// See @ref bdlm_metricdescriptor for more detail.
123///
124/// The instance number is generally best provided by the 'bdlm::InstanceCount'
125/// class. Here we use the constant
126/// 'bdlm::MetricDescriptor::k_USE_METRICS_ADAPTER_NAMESPACE_SELECTION' for the
127/// metric namespace, and
128/// 'bdlm::MetricDescriptor::k_USE_METRICS_ADAPTER_OBJECT_ID_SELECTION' for the
129/// unique text identifying the object instance, to allow the concrete
130/// 'MetricAdapter' to select appropriate values for the particular metrics
131/// framework.
132/// @code
133/// bdlm::InstanceCount::Value instanceNumber =
134/// bdlm::InstanceCount::nextInstanceNumber<YourClass>();
135///
136/// bdlm::MetricDescriptor mdInteresting(
137/// bdlm::MetricDescriptor::k_USE_METRICS_ADAPTER_NAMESPACE_SELECTION,
138/// "requestCount", // the metric name
139/// instanceNumber,
140/// "package.yourclass", // the class identifier
141/// "yc", // the class abreviation
142/// bdlm::MetricDescriptor::k_USE_METRICS_ADAPTER_OBJECT_ID_SELECTION);
143/// @endcode
144/// Assuming a class member 'd_interestingHandle' to hold the handle for the
145/// registered metric, the metric is registered with the 'bdlm::MetricsRegistry':
146/// @code
147/// registry->registerCollectionCallback(
148/// &d_interestingHandle,
149/// mdInteresting,
150/// bdlf::BindUtil::bind(&yourMetric,
151/// bdlf::PlaceHolders::_1,
152/// this));
153/// }
154/// @endcode
155/// Note that the destructor of the 'bdlm::MetricsRegistryRegistrationHandle'
156/// unregisters the metric.
157///
158/// ## Configuring a MetricsAdapter
159///
160/// 'bdlm' is designed to allow application owners to plugin a higher level
161/// metrics reporting framework by supplying a concrete 'bdlm::MetricsAdapter'
162/// instance to the 'bdlm::MetricsRegistry'. Imagine we have a hypothetical
163/// metrics publication framework GUTS, and a concrete 'bdlm::MetricsAdapter' for
164/// GUTS, 'guta::BdlmMetricsAdapter'.
165/// @code
166/// int main(int argc, const char *argv[]) {
167/// // Initialize GUTS metrics publication
168///
169/// gtout::PublisherConfig config;
170/// config.intervalSec() = 1.0;
171/// gtout::PublisherGuard publisher(config);
172///
173/// // Create concrete 'bdlm::MetricAdapter' implementation of
174/// // 'guta::BdlmMetricsAdapter'.
175///
176/// guta::BdlmMetricsAdapter adapter(
177/// gutz::DefaultMetricsManager::instance(),
178/// "myNamespace", // a namespace for the metrics
179/// "myServiceName"); // an identifier for the application
180///
181/// // Assign the adapter to the registry singleton.
182///
183/// bdlm::MetricsRegistry::singleton().setMetricsAdapter(&adapter);
184///
185/// // ...
186///
187/// // Remove the adapter from the registry singleton.
188///
189/// bdlm::MetricRegistry::removeMetricsAdapter(&adapter);
190/// }
191/// @endcode
192///
193/// @}
194/** @} */