23
23
#include < grpc/support/port_platform.h>
24
24
25
25
#include < bitset>
26
+ #include < cstddef>
26
27
#include < initializer_list>
28
+ #include < memory>
29
+ #include < optional>
30
+ #include < utility>
27
31
#include < vector>
28
32
29
33
#include " absl/functional/any_invocable.h"
30
34
#include " absl/status/status.h"
31
35
#include " absl/status/statusor.h"
36
+ #include " absl/types/span.h"
32
37
33
38
// TODO(vigneshbabu): Define the Endpoint::Write metrics collection system
34
39
// TODO(hork): remove all references to the factory methods.
@@ -240,28 +245,40 @@ class EventEngine : public std::enable_shared_from_this<EventEngine>,
240
245
size_t key;
241
246
int64_t value;
242
247
};
248
+ // It is the responsibility of the caller of WriteEventCallback to make sure
249
+ // that the corresponding endpoint is still valid. HINT: Do NOT offload
250
+ // callbacks onto the EventEngine or other threads.
243
251
using WriteEventCallback = absl::AnyInvocable<void (
244
252
WriteEvent, absl::Time, std::vector<WriteMetric>) const >;
245
253
// A bitmask of the events that the caller is interested in.
246
254
// Each bit corresponds to an entry in WriteEvent.
247
255
using WriteEventSet = std::bitset<static_cast <int >(WriteEvent::kCount )>;
256
+
257
+ // A set of metrics that the caller is interested in.
258
+ class MetricsSet {
259
+ public:
260
+ virtual ~MetricsSet () = default ;
261
+
262
+ virtual bool IsSet (size_t key) const = 0;
263
+ };
264
+
248
265
// A sink to receive write events.
249
266
// The requested metrics are the keys of the metrics that the caller is
250
267
// interested in. The on_event callback will be called on each event
251
268
// requested.
252
269
class WriteEventSink final {
253
270
public:
254
- WriteEventSink (absl::Span< const size_t > requested_metrics,
271
+ WriteEventSink (std::shared_ptr<MetricsSet > requested_metrics,
255
272
std::initializer_list<WriteEvent> requested_events,
256
273
WriteEventCallback on_event)
257
- : requested_metrics_(requested_metrics),
274
+ : requested_metrics_(std::move( requested_metrics) ),
258
275
on_event_ (std::move(on_event)) {
259
276
for (auto event : requested_events) {
260
277
requested_events_mask_.set (static_cast <int >(event));
261
278
}
262
279
}
263
280
264
- absl::Span< const size_t > requested_metrics () const {
281
+ const std::shared_ptr<MetricsSet>& requested_metrics () const {
265
282
return requested_metrics_;
266
283
}
267
284
@@ -273,10 +290,12 @@ class EventEngine : public std::enable_shared_from_this<EventEngine>,
273
290
return requested_events_mask_;
274
291
}
275
292
293
+ // / Takes the callback. Ownership is transferred. It is illegal to destroy
294
+ // / the endpoint before this callback is invoked.
276
295
WriteEventCallback TakeEventCallback () { return std::move (on_event_); }
277
296
278
297
private:
279
- absl::Span< const size_t > requested_metrics_;
298
+ std::shared_ptr<MetricsSet > requested_metrics_;
280
299
WriteEventSet requested_events_mask_;
281
300
// The callback to be called on each event.
282
301
WriteEventCallback on_event_;
@@ -288,10 +307,28 @@ class EventEngine : public std::enable_shared_from_this<EventEngine>,
288
307
class WriteArgs final {
289
308
public:
290
309
WriteArgs () = default ;
310
+
311
+ ~WriteArgs ();
312
+
291
313
WriteArgs (const WriteArgs&) = delete ;
292
314
WriteArgs& operator =(const WriteArgs&) = delete ;
293
- WriteArgs (WriteArgs&&) = default ;
294
- WriteArgs& operator =(WriteArgs&&) = default ;
315
+
316
+ WriteArgs (WriteArgs&& other) noexcept
317
+ : metrics_sink_(std::move(other.metrics_sink_)),
318
+ google_specific_ (other.google_specific_),
319
+ max_frame_size_(other.max_frame_size_) {
320
+ other.google_specific_ = nullptr ;
321
+ }
322
+
323
+ WriteArgs& operator =(WriteArgs&& other) noexcept {
324
+ if (this != &other) {
325
+ metrics_sink_ = std::move (other.metrics_sink_ );
326
+ google_specific_ = other.google_specific_ ;
327
+ other.google_specific_ = nullptr ; // Nullify source
328
+ max_frame_size_ = other.max_frame_size_ ;
329
+ }
330
+ return *this ;
331
+ }
295
332
296
333
// A sink to receive write events.
297
334
std::optional<WriteEventSink> TakeMetricsSink () {
@@ -314,6 +351,10 @@ class EventEngine : public std::enable_shared_from_this<EventEngine>,
314
351
return google_specific_;
315
352
}
316
353
354
+ void * TakeDeprecatedAndDiscouragedGoogleSpecificPointer () {
355
+ return std::exchange (google_specific_, nullptr );
356
+ }
357
+
317
358
void SetDeprecatedAndDiscouragedGoogleSpecificPointer (void * pointer) {
318
359
google_specific_ = pointer;
319
360
}
@@ -333,6 +374,31 @@ class EventEngine : public std::enable_shared_from_this<EventEngine>,
333
374
void * google_specific_ = nullptr ;
334
375
int64_t max_frame_size_ = 1024 * 1024 ;
335
376
};
377
+
378
+ class TelemetryInfo {
379
+ public:
380
+ virtual ~TelemetryInfo () = default ;
381
+
382
+ // / Returns the list of write metrics that the endpoint supports.
383
+ // / The keys are used to identify the metrics in the GetMetricName and
384
+ // / GetMetricKey APIs. The current value of the metric can be queried by
385
+ // / adding a WriteEventSink to the WriteArgs of a Write call.
386
+ virtual std::vector<size_t > AllWriteMetrics () const = 0;
387
+ // / Returns the name of the write metric with the given key.
388
+ // / If the key is not found, returns std::nullopt.
389
+ virtual std::optional<absl::string_view> GetMetricName (
390
+ size_t key) const = 0;
391
+ // / Returns the key of the write metric with the given name.
392
+ // / If the name is not found, returns std::nullopt.
393
+ virtual std::optional<size_t > GetMetricKey (
394
+ absl::string_view name) const = 0;
395
+ // / Returns a MetricsSet with all the keys from \a keys set.
396
+ virtual std::shared_ptr<MetricsSet> GetMetricsSet (
397
+ absl::Span<const size_t > keys) const = 0;
398
+ // / Returns a MetricsSet with all supported keys set.
399
+ virtual std::shared_ptr<MetricsSet> GetFullMetricsSet () const = 0;
400
+ };
401
+
336
402
// / Writes data out on the connection.
337
403
// /
338
404
// / If the write succeeds immediately, it returns true and the
@@ -359,17 +425,8 @@ class EventEngine : public std::enable_shared_from_this<EventEngine>,
359
425
// / values are expected to remain valid for the life of the Endpoint.
360
426
virtual const ResolvedAddress& GetPeerAddress () const = 0;
361
427
virtual const ResolvedAddress& GetLocalAddress () const = 0;
362
- // / Returns the list of write metrics that the endpoint supports.
363
- // / The keys are used to identify the metrics in the GetMetricName and
364
- // / GetMetricKey APIs. The current value of the metric can be queried by
365
- // / adding a WriteEventSink to the WriteArgs of a Write call.
366
- virtual std::vector<size_t > AllWriteMetrics () = 0;
367
- // / Returns the name of the write metric with the given key.
368
- // / If the key is not found, returns std::nullopt.
369
- virtual std::optional<absl::string_view> GetMetricName (size_t key) = 0;
370
- // / Returns the key of the write metric with the given name.
371
- // / If the name is not found, returns std::nullopt.
372
- virtual std::optional<size_t > GetMetricKey (absl::string_view name) = 0;
428
+
429
+ virtual std::shared_ptr<TelemetryInfo> GetTelemetryInfo () const = 0;
373
430
};
374
431
375
432
// / Called when a new connection is established.
0 commit comments