BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bdlm_metricsregistry

Detailed Description

Outline

Purpose

Provide a transferable registry of metric registrations.

Deprecated:
This component is not ready for public use.

Classes

Description

This component contains a mechanism, bdlm::MetricsRegistry, that provides a registry of metrics that is transferable to implementations of the bdlm::MetricsAdapter protocol. bdlm, as a low-level metrics facility, does not directly manage schedulers to collect metrics values or publishers to publish those value. Instead bdlm is designed to allow applications to plug in different high-level feature-rich metrics collection and publication frameworks (without requiring a library dependency on those frameworks). A bdlm::MetricsRegistry effectively serves as a proxy for a higher-level metrics collection system implementing the MetricsAdapter protocol – it keeps track of registered metrics allowing a higher-level metrics subsystem to be installed (by calling 'setMetricsAdapter) at any time, either before or after a metric is registered.

A singleton instance of MetricsRegistry is available from the defaultInstance class method. This component also provides a registration handle class, bdlm::MetricsRegistryRegistrationHandle, that provides RAII semantics for metric registration.

Thread Safety

The class bdlm::MetricsRegistry is thread-aware (see {bsldoc_glossary |Thread-Aware}), and bdlm::MetricsRegistryRegistrationHandle is minimally thread-safe (see {bsldoc_glossary |Minimally Thread-Safe}).

Usage

This section illustrates intended use of this component.

Example 1: Using bdlm::MetricsRegistry

This example demonstrates the initialization and usage of the bdlm::MetricsRegistry object, allowing for registering metric callback functions with the bdlm monitoring system.

First, we declare a class that provides some metric for the bdlm monitoring system:

class LowLevelFacility {
// PRIVATE DATA
public:
// CREATORS
explicit LowLevelFacility(bdlm::MetricsRegistry& metricsRegistry =
// ACCESSORS
int someMetric() const
{
return 0; // just a stub
}
};
Definition bdlm_metricsregistry.h:287
Definition bdlm_metricsregistry.h:199
static MetricsRegistry & defaultInstance()
Return a non-const reference to the metrics registry singleton.

Next, we provide a metric function to be used during callback registration:

void metricCallback(bdlm::Metric *value, const LowLevelFacility *object)
{
*value = bdlm::Metric::Gauge(object->someMetric());
}
Definition bdlm_metric.h:77
double Gauge
Definition bdlm_metric.h:81

Here is the constructor definition that registers the collection callback:

/// Construct a `bdlm::MetricsDescriptor` object to be used when
/// registering the callback function:
LowLevelFacility::LowLevelFacility(bdlm::MetricsRegistry& metricsRegistry)
{
bdlm::MetricDescriptor descriptor("bdlm",
"example",
1,
"bdlmmetricsregistry",
"bmr",
"identifier");
// Register the collection callback:
metricsRegistry.registerCollectionCallback(
&d_metricHandle,
descriptor,
bdlf::BindUtil::bind(&metricCallback,
this));
assert(d_metricHandle.isRegistered());
}
static Bind< bslmf::Nil, t_FUNC, Bind_BoundTuple0 > bind(t_FUNC func)
Definition bdlf_bind.h:1830
Definition bdlm_metricdescriptor.h:142
bool isRegistered() const
Definition bdlm_metricsregistry.h:391
void registerCollectionCallback(MetricsRegistryRegistrationHandle *result, const bdlm::MetricDescriptor &descriptor, const Callback &callback)
const PlaceHolder< 1 > _1

Notice that the compiler-supplied destructor is sufficient because the d_metricHandle will deregister the metric on destruction.

Now, we construct a bdlm::MetricsRegistry object with a test allocator:

bdlm::MetricsRegistry registry(&ta);
assert(registry.numRegisteredCollectionCallbacks() == 0);
Definition bslma_testallocator.h:384

Then, we create the object and pass the constructed bdlm::MetricsRegistry object there:

{
LowLevelFacility facility(registry);
assert(registry.numRegisteredCollectionCallbacks() == 1);

If we don't provide a bdlm::MetricsRegistry object explicitly, the default global instance will be used.

Finally, the callback is removed the monitoring system by the destructor of facility object:

} // 'facility.d_metricHandle.unregister()' is called here
assert(registry.numRegisteredCollectionCallbacks() == 0);