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