2323#include < grpc/support/port_platform.h>
2424
2525#include < bitset>
26+ #include < cstddef>
2627#include < initializer_list>
28+ #include < memory>
29+ #include < optional>
30+ #include < utility>
2731#include < vector>
2832
2933#include " absl/functional/any_invocable.h"
3034#include " absl/status/status.h"
3135#include " absl/status/statusor.h"
36+ #include " absl/types/span.h"
3237
3338// TODO(vigneshbabu): Define the Endpoint::Write metrics collection system
3439// TODO(hork): remove all references to the factory methods.
@@ -240,28 +245,40 @@ class EventEngine : public std::enable_shared_from_this<EventEngine>,
240245 size_t key;
241246 int64_t value;
242247 };
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.
243251 using WriteEventCallback = absl::AnyInvocable<void (
244252 WriteEvent, absl::Time, std::vector<WriteMetric>) const >;
245253 // A bitmask of the events that the caller is interested in.
246254 // Each bit corresponds to an entry in WriteEvent.
247255 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+
248265 // A sink to receive write events.
249266 // The requested metrics are the keys of the metrics that the caller is
250267 // interested in. The on_event callback will be called on each event
251268 // requested.
252269 class WriteEventSink final {
253270 public:
254- WriteEventSink (absl::Span< const size_t > requested_metrics,
271+ WriteEventSink (std::shared_ptr<MetricsSet > requested_metrics,
255272 std::initializer_list<WriteEvent> requested_events,
256273 WriteEventCallback on_event)
257- : requested_metrics_(requested_metrics),
274+ : requested_metrics_(std::move( requested_metrics) ),
258275 on_event_ (std::move(on_event)) {
259276 for (auto event : requested_events) {
260277 requested_events_mask_.set (static_cast <int >(event));
261278 }
262279 }
263280
264- absl::Span< const size_t > requested_metrics () const {
281+ const std::shared_ptr<MetricsSet>& requested_metrics () const {
265282 return requested_metrics_;
266283 }
267284
@@ -273,10 +290,12 @@ class EventEngine : public std::enable_shared_from_this<EventEngine>,
273290 return requested_events_mask_;
274291 }
275292
293+ // / Takes the callback. Ownership is transferred. It is illegal to destroy
294+ // / the endpoint before this callback is invoked.
276295 WriteEventCallback TakeEventCallback () { return std::move (on_event_); }
277296
278297 private:
279- absl::Span< const size_t > requested_metrics_;
298+ std::shared_ptr<MetricsSet > requested_metrics_;
280299 WriteEventSet requested_events_mask_;
281300 // The callback to be called on each event.
282301 WriteEventCallback on_event_;
@@ -288,10 +307,28 @@ class EventEngine : public std::enable_shared_from_this<EventEngine>,
288307 class WriteArgs final {
289308 public:
290309 WriteArgs () = default ;
310+
311+ ~WriteArgs ();
312+
291313 WriteArgs (const WriteArgs&) = delete ;
292314 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+ }
295332
296333 // A sink to receive write events.
297334 std::optional<WriteEventSink> TakeMetricsSink () {
@@ -314,6 +351,10 @@ class EventEngine : public std::enable_shared_from_this<EventEngine>,
314351 return google_specific_;
315352 }
316353
354+ void * TakeDeprecatedAndDiscouragedGoogleSpecificPointer () {
355+ return std::exchange (google_specific_, nullptr );
356+ }
357+
317358 void SetDeprecatedAndDiscouragedGoogleSpecificPointer (void * pointer) {
318359 google_specific_ = pointer;
319360 }
@@ -333,6 +374,31 @@ class EventEngine : public std::enable_shared_from_this<EventEngine>,
333374 void * google_specific_ = nullptr ;
334375 int64_t max_frame_size_ = 1024 * 1024 ;
335376 };
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+
336402 // / Writes data out on the connection.
337403 // /
338404 // / If the write succeeds immediately, it returns true and the
@@ -359,17 +425,8 @@ class EventEngine : public std::enable_shared_from_this<EventEngine>,
359425 // / values are expected to remain valid for the life of the Endpoint.
360426 virtual const ResolvedAddress& GetPeerAddress () const = 0;
361427 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;
373430 };
374431
375432 // / Called when a new connection is established.
0 commit comments