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