BDE 4.39.x Production Release
Loading...
Searching...
No Matches
balm_bdlmmetricsadapter.h
Go to the documentation of this file.
1/// @file balm_bdlmmetricsadapter.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// balm_bdlmmetricsadapter.h -*-C++-*-
8
9#ifndef INCLUDED_BALM_BDLMMETRICSADAPTER
10#define INCLUDED_BALM_BDLMMETRICSADAPTER
11
12#include <bsls_ident.h>
13BSLS_IDENT("$Id: $")
14
15/// @defgroup balm_bdlmmetricsadapter balm_bdlmmetricsadapter
16/// @brief Provide a concrete instance of the `bdlm` metrics adapter.
17/// @addtogroup bal
18/// @{
19/// @addtogroup balm
20/// @{
21/// @addtogroup balm_bdlmmetricsadapter
22/// @{
23///
24/// <h1> Outline </h1>
25/// * <a href="#balm_bdlmmetricsadapter-purpose"> Purpose</a>
26/// * <a href="#balm_bdlmmetricsadapter-classes"> Classes </a>
27/// * <a href="#balm_bdlmmetricsadapter-description"> Description </a>
28/// * <a href="#balm_bdlmmetricsadapter-thread-safety"> Thread Safety </a>
29/// * <a href="#balm_bdlmmetricsadapter-usage"> Usage </a>
30/// * <a href="#balm_bdlmmetricsadapter-example-1-using-balm-bdlmmetricsadapter"> Example 1: Using balm::BdlmMetricsAdapter </a>
31///
32/// # Purpose {#balm_bdlmmetricsadapter-purpose}
33/// Provide a concrete instance of the `bdlm` metrics adapter.
34///
35/// # Classes {#balm_bdlmmetricsadapter-classes}
36///
37/// - balm::BdlmMetricsAdapter: concrete instance of the metrics registar
38///
39/// # Description {#balm_bdlmmetricsadapter-description}
40/// This component provides a concrete instance,
41/// `balm::BdlmMetricsAdapter`, of the `bdlm::MetricsAdapter` protocol, enabling
42/// registration of metric collection callbacks with a provided
43/// `balm::MetricsManager`. `balm::BdlmMetricsAdapter` also provides methods to
44/// aid in population of default `bdlm::MetricDescriptor` attribute values.
45///
46/// ## Thread Safety {#balm_bdlmmetricsadapter-thread-safety}
47///
48///
49/// This class is *thread-aware* (see {@ref bsldoc_glossary |Thread-Aware}).
50///
51/// ## Usage {#balm_bdlmmetricsadapter-usage}
52///
53///
54/// This section illustrates intended use of this component.
55///
56/// ### Example 1: Using balm::BdlmMetricsAdapter {#balm_bdlmmetricsadapter-example-1-using-balm-bdlmmetricsadapter}
57///
58///
59/// This example demonstrates the initialization and usage of the
60/// `balm::BdlmMetricsAdapter` object, allowing for registering metric callback
61/// functions with the `balm` monitoring system.
62///
63/// First, we provide a metric function to be used during callback registration
64/// with the `balm` monitoring system:
65/// @code
66/// void elidedMetric(BloombergLP::bdlm::Metric *value)
67/// {
68/// (void)value;
69/// // ...
70/// }
71/// @endcode
72/// Then, we construct a `balm::MetricsManager` object and use it to construct a
73/// `balm::BdlmMetricsAdapter` that will use "bdlm" as its default metric
74/// namespace, "svc" as its default object indentifier prefix, and will not
75/// attempt to set itself as the default metrics adapter:
76/// @code
77/// balm::MetricsManager manager;
78/// balm::BdlmMetricsAdapter adapter(&manager, "bdlm", "svc");
79/// @endcode
80/// Next, we construct a `bdlm::MetricsDescriptor` object to be used when
81/// registering the callback function, using constants from
82/// `bdlm::MetricDescriptor` for the namespace and identifier to indicate the
83/// implementation of the `bdlm::MetricsAdapter` protocol should supply values:
84/// @code
85/// bdlm::MetricDescriptor descriptor(
86/// bdlm::MetricDescriptor::k_USE_METRICS_ADAPTER_NAMESPACE_SELECTION,
87/// "example",
88/// 1,
89/// "balm.bdlmmetricsadapter",
90/// "bmr",
91/// bdlm::MetricDescriptor::k_USE_METRICS_ADAPTER_OBJECT_ID_SELECTION);
92/// @endcode
93/// Now, we register the collection callback:
94/// @code
95/// bdlm::MetricsAdapter::CallbackHandle handle =
96/// adapter.registerCollectionCallback(descriptor,
97/// elidedMetric);
98/// @endcode
99/// Finally, presumably during shutdown of the application, we remove the
100/// callback from the monitoring system, and verify the callback was
101/// successfully removed:
102/// @code
103/// int rc = adapter.removeCollectionCallback(handle);
104/// ASSERT(0 == rc);
105/// @endcode
106/// @}
107/** @} */
108/** @} */
109
110/** @addtogroup bal
111 * @{
112 */
113/** @addtogroup balm
114 * @{
115 */
116/** @addtogroup balm_bdlmmetricsadapter
117 * @{
118 */
119
120#include <bdlf_bind.h>
121
123#include <bdlm_metricsadapter.h>
124
127#include <balm_metricid.h>
128#include <balm_metricrecord.h>
129#include <balm_metricsmanager.h>
130
131#include <bslma_allocator.h>
133
135
136#include <bslmt_lockguard.h>
137#include <bslmt_mutex.h>
138
139#include <bsls_keyword.h>
140
141
142namespace balm {
143
144 // ========================
145 // class BdlmMetricsAdapter
146 // ========================
147
148/// This class implements a pure abstract interface for clients and
149/// suppliers of metrics adapters. The implementation registers callbacks
150/// with a provided `balm::MetricsManager` to enable monitoring of
151/// statistics collection objects.
152///
153/// See @ref balm_bdlmmetricsadapter
155
156 // PRIVATE CLASS METHODS
157
158 /// Load into the specified `records` a new record with the specified
159 /// `id` and statistics obtained from the specified `callback` and, if
160 /// the specified `resetFlag` is `true`, reset the statistics to their
161 /// default state.
162 static void metricCb(bsl::vector<MetricRecord> *records,
163 bool resetFlag,
164 MetricId id,
165 const Callback& callback);
166
167 // DATA
168 MetricsManager *d_metricsManager_p; // held, but not owned,
169 // metrics manager
170
171 const bsl::string d_metricNamespace; // default metric namespace
172 // attribute value
173
174 const bsl::string d_objectIdentifierPrefix; // default prefix for object
175 // identifier attribute
176 // values
177
178 private:
179 // NOT IMPLEMENTED
182 BdlmMetricsAdapter operator=(const BdlmMetricsAdapter&)
184
185 public:
186 // TRAITS
189
190 // CREATORS
191
192 /// Create a `BdlmMetricsAdapter` object that uses the specified
193 /// `metricsManager` to register and unregister collection callback
194 /// functors, the specified `metricNamespace` as the value returned by
195 /// `defaultNamespace()`, and the specified `objectIdentifierPrefix` as
196 /// the value returned by `defaultObjectIdentifierPrefix`. Optionally
197 /// specify a `basicAllocator` used to supply memory. If
198 /// `basicAllocator` is 0, the currently installed default allocator is
199 /// used.
200 BdlmMetricsAdapter(MetricsManager *metricsManager,
201 const bsl::string_view& metricNamespace,
202 const bsl::string_view& objectIdentifierPrefix,
203 bslma::Allocator *basicAllocator = 0);
204
205 /// Destroy this `BdlmMetricsAdapter` object.
207
208 // MANIPULATORS
209
210 /// Register the specified `callback` with the metrics manager specified
211 /// at construction, using the specified `metricDescriptor`. If
212 /// `metricDescriptor.metricsNamspace()` equals
213 /// `bdlm::MetricDescriptor::k_USE_METRICS_ADAPTER_NAMESPACE_SELECTION`,
214 /// use `defaultMetricNamespace()` for the namespace attribute during
215 /// registration. If `metricDescriptor.objectIdentifier()` equals
216 /// `bdlm::MetricDescriptor::k_USE_METRICS_ADAPTER_OBJECT_ID_SELECTION`,
217 /// use the concatination of `defaultObjectIdentifierPrefix()`, a
218 /// period, `metricDescriptor.objectTypeAbbreviation()`, a period, and
219 /// `metricDescriptor.instanceNumber()` for the object identifier
220 /// attribute during registration. The category name supplied to the
221 /// `MetricsManager` provided at construction is the concatenation of
222 /// the object type name attribute, a period, the metric name attribute,
223 /// a period, and the object identifier attribute. Return the callback
224 /// handle to be used with `removeCollectionCallback`.
226 const bdlm::MetricDescriptor& metricDescriptor,
227 const Callback& callback) BSLS_KEYWORD_OVERRIDE;
228
231 // Remove the callback associated with the specified 'handle' from the
232 // metrics manager specified at construction. Return 0 on success, or
233 // a non-zero value if 'handle' cannot be found.
234
235 // ACCESSORS
236
237 /// Return the namespace attribute value to be used as the default value
238 /// for `MetricDescriptor` instances.
239 const bsl::string& defaultMetricNamespace() const;
240
241 /// Return a string to be used as the default prefix for a
242 /// `MetricDescriptor` object identifier attribute value.
243 const bsl::string& defaultObjectIdentifierPrefix() const;
244
245 // Aspects
246
247 /// Return the allocator used by this object to supply memory.
248 bslma::Allocator *allocator() const;
249};
250
251// ============================================================================
252// INLINE DEFINITIONS
253// ============================================================================
254
255 // ------------------------
256 // class BdlmMetricsAdapter
257 // ------------------------
258
259// CREATORS
260inline
262 balm::MetricsManager *metricsManager,
263 const bsl::string_view& metricNamespace,
264 const bsl::string_view& objectIdentifierPrefix,
265 bslma::Allocator *basicAllocator)
266: d_metricsManager_p(metricsManager)
267, d_metricNamespace(metricNamespace, basicAllocator)
268, d_objectIdentifierPrefix(objectIdentifierPrefix, basicAllocator)
269{
270}
271
272// MANIPULATORS
273inline
275{
276 return d_metricsManager_p->removeCollectionCallback(handle);
277}
278
279// ACCESSORS
280inline
282{
283 return d_metricNamespace;
284}
285
286inline
288{
289 return d_objectIdentifierPrefix;
290}
291
292inline
294{
295 return d_metricNamespace.get_allocator().mechanism();
296}
297
298} // close package namespace
299
300
301#endif
302
303// ----------------------------------------------------------------------------
304// Copyright 2024 Bloomberg Finance L.P.
305//
306// Licensed under the Apache License, Version 2.0 (the "License");
307// you may not use this file except in compliance with the License.
308// You may obtain a copy of the License at
309//
310// http://www.apache.org/licenses/LICENSE-2.0
311//
312// Unless required by applicable law or agreed to in writing, software
313// distributed under the License is distributed on an "AS IS" BASIS,
314// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
315// See the License for the specific language governing permissions and
316// limitations under the License.
317// ----------------------------- END-OF-FILE ----------------------------------
318
319/** @} */
320/** @} */
321/** @} */
Definition balm_bdlmmetricsadapter.h:154
const bsl::string & defaultObjectIdentifierPrefix() const
Definition balm_bdlmmetricsadapter.h:287
const bsl::string & defaultMetricNamespace() const
Definition balm_bdlmmetricsadapter.h:281
bslma::Allocator * allocator() const
Return the allocator used by this object to supply memory.
Definition balm_bdlmmetricsadapter.h:293
~BdlmMetricsAdapter() BSLS_KEYWORD_OVERRIDE
Destroy this BdlmMetricsAdapter object.
BSLMF_NESTED_TRAIT_DECLARATION(BdlmMetricsAdapter, bslma::UsesBslmaAllocator)
int removeCollectionCallback(const CallbackHandle &handle) BSLS_KEYWORD_OVERRIDE
Definition balm_bdlmmetricsadapter.h:274
CallbackHandle registerCollectionCallback(const bdlm::MetricDescriptor &metricDescriptor, const Callback &callback) BSLS_KEYWORD_OVERRIDE
Definition balm_metricid.h:162
Definition balm_metricsmanager.h:490
int removeCollectionCallback(CallbackHandle handle)
Definition bdlm_metricsadapter.h:291
int CallbackHandle
Definition bdlm_metricsadapter.h:295
Definition bslstl_stringview.h:471
Definition bslstl_string.h:1252
allocator_type get_allocator() const BSLS_KEYWORD_NOEXCEPT
Return the allocator used by this string to supply memory.
Definition bslstl_string.h:7423
Forward declaration.
Definition bslstl_function.h:946
Definition bslstl_vector.h:1120
Definition bslma_allocator.h:545
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
#define BSLS_KEYWORD_DELETED
Definition bsls_keyword.h:651
#define BSLS_KEYWORD_OVERRIDE
Definition bsls_keyword.h:695
Definition balm_bdlmmetricsadapter.h:142
Definition bdlm_instancecount.h:100
Definition bdlat_valuetypefunctions.h:939
Definition baljsn_encoder_testtypes.h:76
Definition bslma_usesbslmaallocator.h:344