BDE 4.39.x Production Release
Loading...
Searching...
No Matches
balm_publisher.h
Go to the documentation of this file.
1/// @file balm_publisher.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// balm_publisher.h -*-C++-*-
8#ifndef INCLUDED_BALM_PUBLISHER
9#define INCLUDED_BALM_PUBLISHER
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: balm_publisher.h,v 1.6 2008/04/09 19:42:15 hversche Exp $")
13
14/// @defgroup balm_publisher balm_publisher
15/// @brief Provide a protocol to publish recorded metric values.
16/// @addtogroup bal
17/// @{
18/// @addtogroup balm
19/// @{
20/// @addtogroup balm_publisher
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#balm_publisher-purpose"> Purpose</a>
25/// * <a href="#balm_publisher-classes"> Classes </a>
26/// * <a href="#balm_publisher-description"> Description </a>
27/// * <a href="#balm_publisher-alternative-systems-for-telemetry"> Alternative Systems for Telemetry </a>
28/// * <a href="#balm_publisher-usage"> Usage </a>
29/// * <a href="#balm_publisher-example-1-implementing-the-balm-publisher-protocol"> Example 1: Implementing the balm::Publisher Protocol </a>
30/// * <a href="#balm_publisher-example-2-using-the-balm-publisher-protocol"> Example 2: Using the balm::Publisher Protocol </a>
31/// * <a href="#balm_publisher-example-3-publishing-collected-metrics-using-eventmanager"> Example 3: Publishing Collected Metrics Using EventManager </a>
32///
33/// # Purpose {#balm_publisher-purpose}
34/// Provide a protocol to publish recorded metric values.
35///
36/// # Classes {#balm_publisher-classes}
37///
38/// - balm::Publisher: a protocol providing a method to publish metric values
39///
40/// @see
41///
42/// # Description {#balm_publisher-description}
43/// This component defines a protocol class `balm::Publisher` used
44/// for publishing metric values. The protocol's primary method is `publish`,
45/// which takes a `balm::MetricSample`. The precise meaning of `publish` is
46/// left to derived classes to specify.
47///
48/// ## Alternative Systems for Telemetry {#balm_publisher-alternative-systems-for-telemetry}
49///
50///
51/// Bloomberg software may alternatively use the GUTS telemetry API, which is
52/// integrated into Bloomberg infrastructure.
53///
54/// ## Usage {#balm_publisher-usage}
55///
56///
57/// This section illustrates intended use of this component.
58///
59/// ### Example 1: Implementing the balm::Publisher Protocol {#balm_publisher-example-1-implementing-the-balm-publisher-protocol}
60///
61///
62/// The following example demonstrates a simple implementation of the
63/// `balm::Publisher` protocol. This implementation publishes the metric
64/// records to an output stream provided on construction.
65/// @code
66/// // simplestreampublisher.h
67/// class SimpleStreamPublisher : public balm::Publisher {
68/// // A simple implementation of the 'balm::Publisher' protocol that
69/// // writes metric records to a stream.
70///
71/// // DATA
72/// bsl::ostream& d_stream; // output stream (held, not owned)
73///
74/// private:
75/// // NOT IMPLEMENTED
76/// SimpleStreamPublisher(const SimpleStreamPublisher& );
77/// SimpleStreamPublisher& operator=(const SimpleStreamPublisher& );
78///
79/// public:
80/// // CREATORS
81/// SimpleStreamPublisher(bsl::ostream& stream);
82/// // Create this publisher that will publish metrics to the specified
83/// // 'stream'.
84///
85/// virtual ~SimpleStreamPublisher();
86/// // Destroy this publisher.
87///
88/// // MANIPULATORS
89/// virtual void publish(const balm::MetricSample& metricValues);
90/// // Publish the specified 'metricValues'. This implementation will
91/// // write the 'metricValues' to the output stream specified on
92/// // construction.
93/// };
94///
95/// // simplestreampublisher.cpp
96///
97/// // CREATORS
98/// SimpleStreamPublisher::SimpleStreamPublisher(bsl::ostream& stream)
99/// : d_stream(stream)
100/// {
101/// }
102///
103/// SimpleStreamPublisher::~SimpleStreamPublisher()
104/// {
105/// }
106///
107/// // MANIPULATORS
108/// void SimpleStreamPublisher::publish(const balm::MetricSample& metricValues)
109/// {
110/// if (0 >= metricValues.numRecords()) {
111/// return; // RETURN
112/// }
113/// d_stream << metricValues.timeStamp() << " "
114/// << metricValues.numRecords() << " Records" << bsl::endl;
115///
116/// balm::MetricSample::const_iterator sIt = metricValues.begin();
117/// for (; sIt != metricValues.end(); ++sIt) {
118/// d_stream << "\tElapsed Time: "
119/// << sIt->elapsedTime().totalSecondsAsDouble()
120/// << "s" << bsl::endl;
121/// balm::MetricSampleGroup::const_iterator gIt = sIt->begin();
122/// for (; gIt != sIt->end(); ++gIt) {
123/// d_stream << "\t" << gIt->metricId()
124/// << " [count = " << gIt->count()
125/// << ", total = " << gIt->total()
126/// << ", min = " << gIt->min()
127/// << ", max = " << gIt->max() << "]" << bsl::endl;
128/// }
129/// }
130/// }
131/// @endcode
132///
133/// ### Example 2: Using the balm::Publisher Protocol {#balm_publisher-example-2-using-the-balm-publisher-protocol}
134///
135///
136/// The following example defines a trivial `EventManager` class that uses the
137/// `balm::Publisher` protocol to publish metrics related to the incoming
138/// event. Note that this event manager does no actual processing and is
139/// intended only to illustrate how the publisher protocol might be used.
140/// @code
141/// class EventManager {
142/// // This class provides a dummy event handling mechanism that publishes
143/// // a metric for the size of the processed event messages.
144///
145/// // DATA
146/// balm::Collector d_eventMessageSize; // metric for the message size
147/// bdlt::DatetimeTz d_lastPublish; // time of the last publication
148///
149/// private:
150/// // NOT IMPLEMENTED
151/// EventManager(const EventManager& );
152/// EventManager& operator=(const EventManager& );
153///
154/// public:
155/// // CREATORS
156/// EventManager(const balm::MetricId& messageSizeId)
157/// // Create this event manager using the specified 'messageSizeId'
158/// // to identify the event message size metric.
159/// : d_eventMessageSize(messageSizeId)
160/// , d_lastPublish(bdlt::CurrentTime::nowAsDatetimeUTC(), 0)
161/// {}
162///
163/// // MANIPULATORS
164/// int handleEvent(int eventId, const bsl::string& eventMessage)
165/// // Process the event described by the specified 'eventId' and
166/// // 'eventMessage'. Return 0 on success, and a non-zero value if
167/// // there was an error processing the event.
168/// {
169/// // Update the metrics with the size of the 'eventMessage'.
170/// d_eventMessageSize.update(
171/// static_cast<double>(eventMessage.size()));
172///
173/// // ... process the event
174/// (void)eventId;
175///
176/// return 0;
177/// }
178/// @endcode
179/// We use a `balm::Publisher` to publish the metrics recorded by this event
180/// manager. Note that most of the functionality illustrated here is normally
181/// provided by the `balm::MetricsManager`.
182/// @code
183/// void publishMetrics(balm::Publisher *publisher)
184/// {
185/// bdlt::DatetimeTz now(bdlt::CurrentTime::nowAsDatetimeUTC(), 0);
186/// bdlt::DatetimeInterval dateInterval = now.utcDatetime() -
187/// d_lastPublish.utcDatetime();
188/// bsls::TimeInterval interval(dateInterval.totalSeconds(),
189/// dateInterval.milliseconds());
190///
191/// balm::MetricRecord record;
192/// d_eventMessageSize.loadAndReset(&record);
193///
194/// balm::MetricSample sample;
195/// sample.setTimeStamp(now);
196/// sample.appendGroup(&record, 1, interval);
197///
198/// // This is where we make use of the publisher argument to this
199/// // function.
200/// publisher->publish(sample);
201///
202/// d_lastPublish = now;
203/// }
204/// };
205/// @endcode
206///
207/// ### Example 3: Publishing Collected Metrics Using EventManager {#balm_publisher-example-3-publishing-collected-metrics-using-eventmanager}
208///
209///
210/// In this final example, we publish metrics collected for the `EventManager`
211/// object (defined above).
212///
213/// We start by creating a `balm::MetricId` object by hand, but in practice, an
214/// id should be obtained from a `balm::MetricRegistry` object (such as the one
215/// owned by a `balm::MetricsManager`).
216/// @code
217/// balm::Category myCategory("MyCategory");
218/// balm::MetricDescription description(&myCategory, "EventMessageSize");
219/// balm::MetricId eventMessageSizeId(&description);
220/// @endcode
221/// Now we create a `EventManager` object and supply it the metric id we have
222/// created.
223/// @code
224/// EventManager eventManager(eventMessageSizeId);
225/// @endcode
226/// We use the `EventManager` object to process two events and then publish the
227/// metrics for those events with a `SimpleStreamPublisher` object (also defined
228/// above).
229/// @code
230/// eventManager.handleEvent(0, "123");
231/// eventManager.handleEvent(0, "456789");
232///
233/// SimpleStreamPublisher myPublisher(bsl::cout);
234/// balm::Publisher *publisher = &myPublisher;
235/// eventManager.publishMetrics(publisher);
236/// @endcode
237/// Note that we have delivered two events, with the messages "123" and
238/// "456789", so the count should be 2, the total message size should be 9, the
239/// minimum should be 3, and the maximum should be 6. The output to the
240/// console should be:
241/// @code
242/// 05FEB2009_19:49:30.173+0000 1 Records
243/// Elapsed Time: 1e-09s
244/// MyCategory.EventMessageSize [count = 2, total = 9, min = 3, max = 6]
245/// @endcode
246/// @}
247/** @} */
248/** @} */
249
250/** @addtogroup bal
251 * @{
252 */
253/** @addtogroup balm
254 * @{
255 */
256/** @addtogroup balm_publisher
257 * @{
258 */
259
260#include <balscm_version.h>
261
262
263
264
265namespace balm {
266
267class MetricSample;
268
269 // ===============
270 // class Publisher
271 // ===============
272
273/// This protocol class provides a `publish` method to publish collected
274/// sample of recorded metric values. The precise meaning of publish is
275/// left to derived classes to specify.
276///
277/// See @ref balm_publisher
279
280 public:
281 // CREATORS
282
283 /// Destroy this object.
284 virtual ~Publisher();
285
286 // MANIPULATORS
287
288 /// Publish the specified `metricValue`. The exact definition of
289 /// publish depends on the implementing class, though the intention is
290 /// that the recorded metric values will be distributed in a human or
291 /// machine readable form.
292 virtual void publish(const MetricSample& metricValue) = 0;
293};
294
295} // close package namespace
296
297
298#endif
299
300// ----------------------------------------------------------------------------
301// Copyright 2015 Bloomberg Finance L.P.
302//
303// Licensed under the Apache License, Version 2.0 (the "License");
304// you may not use this file except in compliance with the License.
305// You may obtain a copy of the License at
306//
307// http://www.apache.org/licenses/LICENSE-2.0
308//
309// Unless required by applicable law or agreed to in writing, software
310// distributed under the License is distributed on an "AS IS" BASIS,
311// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
312// See the License for the specific language governing permissions and
313// limitations under the License.
314// ----------------------------- END-OF-FILE ----------------------------------
315
316/** @} */
317/** @} */
318/** @} */
Definition balm_metricsample.h:347
Definition balm_publisher.h:278
virtual void publish(const MetricSample &metricValue)=0
virtual ~Publisher()
Destroy this object.
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition balm_bdlmmetricsadapter.h:142