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

Detailed Description

Outline

Purpose

Provide a namespace for metrics configuration utilities.

Classes

See also
balm_metricsmanager, balm_defaultmetricsmanager

Description

This component provides a set of utility functions for configuring metrics. The balm::ConfigurationUtil struct provides short-cuts for common configuration operations that are performed on other components in the balm package.

Alternative Systems for Telemetry

Bloomberg software may alternatively use the GUTS telemetry API, which is integrated into Bloomberg infrastructure.

Thread Safety

balm::ConfigurationUtil is fully thread-safe, meaning that all the methods can be safely invoked simultaneously from multiple threads.

Usage

This section illustrates intended use of this component.

Example 1: Configuring the Output of a Metric

This example uses balm::ConfigurationUtil to configure the output for a metric.

We start by initializing a default metrics manager by creating a balm::DefaultMetricsManagerScopedGuard, which manages the lifetime of the default metrics manager object. At construction, we provide the scoped guard an output stream (stdout) to which the default metrics manager will publish metrics. Note that the default metrics manager is intended to be created and destroyed by the owner of main. A metrics manager should be created during the initialization of an application (while the task has a single thread) and destroyed just prior to termination (when there is similarly a single thread).

int main(int argc, char *argv[])
{
// ...
balm::DefaultMetricsManagerScopedGuard managerGuard(bsl::cout);
Definition balm_defaultmetricsmanager.h:238

Next we create a metric, "avgElapsedTimeMs", that will output the average time, in milliseconds, spent in a section of code. We set the preferred publication type for the metric to be average:

"myCategory",
"avgElapsedTimeMs",
static int setPreferredPublicationType(const char *category, const char *metricName, PublicationType::Value publicationType, MetricsManager *manager=0)
@ e_AVG
Definition balm_publicationtype.h:93

Next, because we will record the elapsed time in seconds, we configure a format to scale the elapsed time by 1000.0:

"myCategory",
"avgElapsedTimeMs",
balm::MetricFormatSpec(1000.0, "%.2f ms");
Definition balm_metricformat.h:183
static int setFormatSpec(const char *category, const char *metricName, PublicationType::Value publicationType, const MetricFormatSpec &formatSpec, MetricsManager *manager=0)

We now collect an example value of .005:

BALM_METRIC_UPDATE("myCategory", "avgElapsedTimeMs", .005);

Finally, we publish the metric. Note that in practice, clients of the balm package can use the balm::PublicationScheduler to schedule the periodic publication of metrics:

void publishAll(bool resetFlag=true)
static MetricsManager * instance()
Definition balm_defaultmetricsmanager.h:296

The output for the publication will look like:

06AUG2009_20:27:51.982+0000 1 Records
Elapsed Time: 0.000816s
myCategory.avgElapsedTimeMs[ avg (total/count) = 5.00 ms ]

Using a Metric's User Data

In the following example we configure, using balm::ConfigurationUtil, application-specific publication thresholds for a series of metrics. We will create an application-specific publisher that will use the configured thresholds to determine whether a metric should be written to the console. For simplicity, the metric thresholds in this example will be a single unsigned integer value that will be compared with the metric's total.

We start by defining an application-specific publisher implementation. This implementation is supplied a user data key on construction, which it uses to look up the threshold for a particular metric. If a metric's total value is greater than its threshold, it will log the metric to the console.

// thresholdpublisher.h
class ThresholdPublisher : public balm::Publisher {
// A simple implementation of the 'balm::Publisher' protocol that
// writes metric records to the console when their value is greater
// than an application-specific threshold.
// DATA
balm::MetricDescription::UserDataKey d_thresholdKey; // key for a
// metric's
// threshold
// NOT IMPLEMENTED
ThresholdPublisher(const ThresholdPublisher&);
ThresholdPublisher& operator=(const ThresholdPublisher&);
public:
// CREATORS
ThresholdPublisher(balm::MetricDescription::UserDataKey thresholdKey);
// Create a publisher that will publish metrics to the console if
// their total value is greater than their associated threshold,
// accessed via the specified 'thresholdKey'.
~ThresholdPublisher() BSLS_KEYWORD_OVERRIDE;
// Destroy this publisher.
// MANIPULATORS
virtual void publish(const balm::MetricSample& metricValues)
// Publish the specified 'metricValues' to the console if they are
// greater than their associated threshold.
};
// thresholdpublisher.cpp
// CREATORS
ThresholdPublisher::ThresholdPublisher(
balm::MetricDescription::UserDataKey thresholdKey)
: d_thresholdKey(thresholdKey)
{
}
ThresholdPublisher::~ThresholdPublisher()
{
}
// MANIPULATORS
void ThresholdPublisher::publish(const balm::MetricSample& metricValues)
{
if (0 >= metricValues.numRecords()) {
return; // RETURN
}
for (; sIt != metricValues.end(); ++sIt) {
for (; gIt != sIt->end(); ++gIt) {
int UserDataKey
Definition balm_metricdescription.h:190
Definition balm_metricrecord.h:217
Definition balm_metricsample.h:342
const_iterator end() const
Definition balm_metricsample.h:691
const_iterator begin() const
Definition balm_metricsample.h:685
int numRecords() const
Definition balm_metricsample.h:703
bsl::vector< MetricSampleGroup >::const_iterator const_iterator
Definition balm_metricsample.h:364
Definition balm_publisher.h:276
#define BSLS_KEYWORD_OVERRIDE
Definition bsls_keyword.h:653
Definition balm_bdlmmetricsadapter.h:141

We now use the user data key to lookup the address of the threshold value. If this address is 0, no threshold is specified for the metric.

const balm::MetricDescription& description =
*gIt->metricId().description();
unsigned int *thresholdPtr =
(unsigned int *)description.userData(d_thresholdKey);
if (thresholdPtr && gIt->total() > *thresholdPtr) {
bsl::cout << "WARNING: " << gIt->metricId()
<< " = " << gIt->total()
<< bsl::endl;
}
}
}
}
Definition balm_metricdescription.h:158
const void * userData(UserDataKey key) const
Definition balm_metricdescription.h:391
const MetricDescription *& description()
Definition balm_metricid.h:300
double & total()
Definition balm_metricrecord.h:412
MetricId & metricId()
Definition balm_metricrecord.h:400

Now we examine how to configure a metrics manager with a ThresholdPublisher, and set the thresholds for a couple of metrics. We start by defining a couple of threshold constants for our metrics:

static const unsigned int ELAPSED_TIME_THRESHOLD = 10;
static const unsigned int NUM_REQUESTS_THRESHOLD = 100;

Now, we configure a default metrics manager and publish a couple of example metrics. We start by initializing a default metrics manager by creating a balm::DefaultMetricsManagerScopedGuard, which manages the lifetime of the default metrics manager:

int main(int argc, char *argv[])
{
// ...
Definition bslma_allocator.h:457
static Allocator * allocator(Allocator *basicAllocator=0)
Definition bslma_default.h:897

Now we create a user data key for our threshold information:

static MetricDescription::UserDataKey createUserDataKey(MetricsManager *manager=0)

Next we create an object of our application-specific publisher type, ThresholdPublisher, and configure the default metrics manager to publish metrics using this publisher:

new (*allocator) ThresholdPublisher(thresholdKey),
allocator);
int addGeneralPublisher(const bsl::shared_ptr< Publisher > &publisher)
Definition bslstl_sharedptr.h:1830

Next we configure two metric thresholds:

"elapsedTime",
thresholdKey,
&ELAPSED_TIME_THRESHOLD);
"numRequests",
thresholdKey,
&NUM_REQUESTS_THRESHOLD);
static void setUserData(const char *category, const char *metricName, MetricDescription::UserDataKey key, const void *value, MetricsManager *manager=0)

Now we update the value of a couple of metrics. Note that the recorded number of requests is greater than the metric's configured threshold:

BALM_METRICS_UPDATE("myCategory", "elapsedTime", 2);
BALM_METRICS_UPDATE("myCategory", "numRequests", 150);
#define BALM_METRICS_UPDATE(CATEGORY, METRIC1, VALUE1)
Definition balm_metrics.h:575

Finally, we publish the collected metrics. Note that in practice, clients of the balm package can use the balm::PublicationScheduler to schedule the periodic publication of metrics:

The console output of the call to publishAll will look like:

WARNING: myCategory.numRequests = 150