Provide a protocol to publish recorded metric values.
More...
Namespaces |
namespace | balm |
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:
- In the following examples we create a simple implementation of a
balm::Publisher
, and then use it to publish metrics recorded by a trivial event manager.
-
- 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.
class SimpleStreamPublisher : public balm::Publisher {
bsl::ostream& d_stream;
SimpleStreamPublisher(const SimpleStreamPublisher& );
SimpleStreamPublisher& operator=(const SimpleStreamPublisher& );
public:
SimpleStreamPublisher(bsl::ostream& stream);
virtual ~SimpleStreamPublisher();
virtual void publish(const balm::MetricSample& metricValues);
};
SimpleStreamPublisher::SimpleStreamPublisher(bsl::ostream& stream)
: d_stream(stream)
{
}
SimpleStreamPublisher::~SimpleStreamPublisher()
{
}
void SimpleStreamPublisher::publish(const balm::MetricSample& metricValues)
{
if (0 >= metricValues.numRecords()) {
return;
}
d_stream << metricValues.timeStamp() << " "
<< metricValues.numRecords() << " Records" << bsl::endl;
balm::MetricSample::const_iterator sIt = metricValues.begin();
for (; sIt != metricValues.end(); ++sIt) {
d_stream << "\tElapsed Time: "
<< sIt->elapsedTime().totalSecondsAsDouble()
<< "s" << bsl::endl;
balm::MetricSampleGroup::const_iterator gIt = sIt->begin();
for (; gIt != sIt->end(); ++gIt) {
d_stream << "\t" << gIt->metricId()
<< " [count = " << gIt->count()
<< ", total = " << gIt->total()
<< ", min = " << gIt->min()
<< ", max = " << gIt->max() << "]" << bsl::endl;
}
}
}
-
- 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 {
balm::Collector d_eventMessageSize;
bdlt::DatetimeTz d_lastPublish;
EventManager(const EventManager& );
EventManager& operator=(const EventManager& );
public:
EventManager(const balm::MetricId& messageSizeId)
: d_eventMessageSize(messageSizeId)
, d_lastPublish(bdlt::CurrentTime::nowAsDatetimeUTC(), 0)
{}
int handleEvent(int eventId, const bsl::string& eventMessage)
{
d_eventMessageSize.update(
static_cast<double>(eventMessage.size()));
(void)eventId;
return 0;
}
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
.
-
- 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
). 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]