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

Detailed Description

Outline

Purpose

Provide a protocol to publish recorded metric values.

Classes

See also

Description

This component defines a protocol class balm::Publisher used for publishing metric values. The protocol's primary method is publish, which takes a balm::MetricSample. The precise meaning of publish is left to derived classes to specify.

Alternative Systems for Telemetry

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

Usage

This section illustrates intended use of this component.

Example 1: Implementing the balm::Publisher Protocol

The following example demonstrates a simple implementation of the balm::Publisher protocol. This implementation publishes the metric records to an output stream provided on construction.

// simplestreampublisher.h
class SimpleStreamPublisher : public balm::Publisher {
// A simple implementation of the 'balm::Publisher' protocol that
// writes metric records to a stream.
// DATA
bsl::ostream& d_stream; // output stream (held, not owned)
// NOT IMPLEMENTED
SimpleStreamPublisher(const SimpleStreamPublisher& );
SimpleStreamPublisher& operator=(const SimpleStreamPublisher& );
public:
// CREATORS
SimpleStreamPublisher(bsl::ostream& stream);
// Create this publisher that will publish metrics to the specified
// 'stream'.
virtual ~SimpleStreamPublisher();
// Destroy this publisher.
// MANIPULATORS
virtual void publish(const balm::MetricSample& metricValues);
// Publish the specified 'metricValues'. This implementation will
// write the 'metricValues' to the output stream specified on
// construction.
};
// simplestreampublisher.cpp
// CREATORS
SimpleStreamPublisher::SimpleStreamPublisher(bsl::ostream& stream)
: d_stream(stream)
{
}
SimpleStreamPublisher::~SimpleStreamPublisher()
{
}
// MANIPULATORS
void SimpleStreamPublisher::publish(const balm::MetricSample& metricValues)
{
if (0 >= metricValues.numRecords()) {
return; // RETURN
}
d_stream << metricValues.timeStamp() << " "
<< metricValues.numRecords() << " Records" << bsl::endl;
for (; sIt != metricValues.end(); ++sIt) {
d_stream << "\tElapsed Time: "
<< sIt->elapsedTime().totalSecondsAsDouble()
<< "s" << bsl::endl;
for (; gIt != sIt->end(); ++gIt) {
d_stream << "\t" << gIt->metricId()
<< " [count = " << gIt->count()
<< ", total = " << gIt->total()
<< ", min = " << gIt->min()
<< ", max = " << gIt->max() << "]" << bsl::endl;
}
}
}
Definition balm_metricrecord.h:217
int & count()
Definition balm_metricrecord.h:406
double & total()
Definition balm_metricrecord.h:412
double & max()
Definition balm_metricrecord.h:418
MetricId & metricId()
Definition balm_metricrecord.h:400
double & min()
Definition balm_metricrecord.h:424
Definition balm_metricsample.h:342
const_iterator end() const
Definition balm_metricsample.h:691
const_iterator begin() const
Definition balm_metricsample.h:685
const bdlt::DatetimeTz & timeStamp() const
Return a reference to the non-modifiable timestamp for this sample.
Definition balm_metricsample.h:679
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

Example 2: Using the balm::Publisher Protocol

The following example defines a trivial EventManager class that uses the balm::Publisher protocol to publish metrics related to the incoming event. Note that this event manager does no actual processing and is intended only to illustrate how the publisher protocol might be used.

class EventManager {
// This class provides a dummy event handling mechanism that publishes
// a metric for the size of the processed event messages.
// DATA
balm::Collector d_eventMessageSize; // metric for the message size
bdlt::DatetimeTz d_lastPublish; // time of the last publication
// NOT IMPLEMENTED
EventManager(const EventManager& );
EventManager& operator=(const EventManager& );
public:
// CREATORS
EventManager(const balm::MetricId& messageSizeId)
// Create this event manager using the specified 'messageSizeId'
// to identify the event message size metric.
: d_eventMessageSize(messageSizeId)
, d_lastPublish(bdlt::CurrentTime::nowAsDatetimeUTC(), 0)
{}
// MANIPULATORS
int handleEvent(int eventId, const bsl::string& eventMessage)
// Process the event described by the specified 'eventId' and
// 'eventMessage'. Return 0 on success, and a non-zero value if
// there was an error processing the event.
{
// Update the metrics with the size of the 'eventMessage'.
d_eventMessageSize.update(
static_cast<double>(eventMessage.size()));
// ... process the event
(void)eventId;
return 0;
}
Definition balm_collector.h:152
void update(double value)
Definition balm_collector.h:271
Definition balm_metricid.h:162
Definition bdlt_datetimetz.h:308
Definition bslstl_string.h:1281
size_type size() const BSLS_KEYWORD_NOEXCEPT
Definition bslstl_string.h:6592
Definition bbldc_basicisma30360.h:112

We use a balm::Publisher to publish the metrics recorded by this event manager. Note that most of the functionality illustrated here is normally provided by the balm::MetricsManager.

void publishMetrics(balm::Publisher *publisher)
{
bdlt::DatetimeTz now(bdlt::CurrentTime::nowAsDatetimeUTC(), 0);
bdlt::DatetimeInterval dateInterval = now.utcDatetime() -
d_lastPublish.utcDatetime();
bsls::TimeInterval interval(dateInterval.totalSeconds(),
dateInterval.milliseconds());
d_eventMessageSize.loadAndReset(&record);
sample.setTimeStamp(now);
sample.appendGroup(&record, 1, interval);
// This is where we make use of the publisher argument to this
// function.
publisher->publish(sample);
d_lastPublish = now;
}
};
void loadAndReset(MetricRecord *record)
Definition balm_collector.h:260
void setTimeStamp(const bdlt::DatetimeTz &timeStamp)
Definition balm_metricsample.h:639
void appendGroup(const MetricSampleGroup &group)
Definition balm_metricsample.h:645
virtual void publish(const MetricSample &metricValue)=0
Definition bdlt_datetimeinterval.h:201
int milliseconds() const
Definition bdlt_datetimeinterval.h:1137
bsls::Types::Int64 totalSeconds() const
Definition bdlt_datetimeinterval.h:1170
Datetime utcDatetime() const
Definition bdlt_datetimetz.h:678
Definition bsls_timeinterval.h:301

Example 3: Publishing Collected Metrics Using EventManager

In this final example, we publish metrics collected for the EventManager object (defined above).

We start by creating a balm::MetricId object by hand, but in practice, an id should be obtained from a balm::MetricRegistry object (such as the one owned by a balm::MetricsManager).

balm::Category myCategory("MyCategory");
balm::MetricDescription description(&myCategory, "EventMessageSize");
balm::MetricId eventMessageSizeId(&description);
Definition balm_category.h:151
Definition balm_metricdescription.h:158

Now we create a EventManager object and supply it the metric id we have created.

EventManager eventManager(eventMessageSizeId);

We use the EventManager object to process two events and then publish the metrics for those events with a SimpleStreamPublisher object (also defined above).

eventManager.handleEvent(0, "123");
eventManager.handleEvent(0, "456789");
SimpleStreamPublisher myPublisher(bsl::cout);
balm::Publisher *publisher = &myPublisher;
eventManager.publishMetrics(publisher);

Note that we have delivered two events, with the messages "123" and "456789", so the count should be 2, the total message size should be 9, the minimum should be 3, and the maximum should be 6. The output to the console should be:

05FEB2009_19:49:30.173+0000 1 Records
Elapsed Time: 1e-09s
MyCategory.EventMessageSize [count = 2, total = 9, min = 3, max = 6]