[context] Move backend metric provider to arena based context (#36781)

Closes #36781

COPYBARA_INTEGRATE_REVIEW=https://github.com/grpc/grpc/pull/36781 from ctiller:ctx7 06f44a31d1
PiperOrigin-RevId: 639185505
pull/36789/head^2
Craig Tiller 6 months ago committed by Copybara-Service
parent 0c37233892
commit d26a878e83
  1. 1
      src/core/BUILD
  2. 6
      src/core/ext/filters/backend_metrics/backend_metric_filter.cc
  3. 7
      src/core/ext/filters/backend_metrics/backend_metric_provider.h
  4. 4
      src/core/lib/channel/context.h
  5. 3
      src/cpp/server/server_context.cc

@ -5032,6 +5032,7 @@ grpc_cc_library(
"ext/filters/backend_metrics/backend_metric_provider.h", "ext/filters/backend_metrics/backend_metric_provider.h",
], ],
language = "c++", language = "c++",
deps = ["arena"],
) )
grpc_cc_library( grpc_cc_library(

@ -130,16 +130,14 @@ BackendMetricFilter::Create(const ChannelArgs&, ChannelFilter::Args) {
void BackendMetricFilter::Call::OnServerTrailingMetadata(ServerMetadata& md) { void BackendMetricFilter::Call::OnServerTrailingMetadata(ServerMetadata& md) {
if (md.get(GrpcCallWasCancelled()).value_or(false)) return; if (md.get(GrpcCallWasCancelled()).value_or(false)) return;
auto* ctx = &GetContext< auto* ctx = MaybeGetContext<BackendMetricProvider>();
grpc_call_context_element>()[GRPC_CONTEXT_BACKEND_METRIC_PROVIDER];
if (ctx == nullptr) { if (ctx == nullptr) {
if (GRPC_TRACE_FLAG_ENABLED(grpc_backend_metric_filter_trace)) { if (GRPC_TRACE_FLAG_ENABLED(grpc_backend_metric_filter_trace)) {
gpr_log(GPR_INFO, "[%p] No BackendMetricProvider.", this); gpr_log(GPR_INFO, "[%p] No BackendMetricProvider.", this);
} }
return; return;
} }
absl::optional<std::string> serialized = MaybeSerializeBackendMetrics( absl::optional<std::string> serialized = MaybeSerializeBackendMetrics(ctx);
reinterpret_cast<BackendMetricProvider*>(ctx->value));
if (serialized.has_value() && !serialized->empty()) { if (serialized.has_value() && !serialized->empty()) {
if (GRPC_TRACE_FLAG_ENABLED(grpc_backend_metric_filter_trace)) { if (GRPC_TRACE_FLAG_ENABLED(grpc_backend_metric_filter_trace)) {
gpr_log(GPR_INFO, "[%p] Backend metrics serialized. size: %" PRIuPTR, gpr_log(GPR_INFO, "[%p] Backend metrics serialized. size: %" PRIuPTR,

@ -15,6 +15,8 @@
#ifndef GRPC_SRC_CORE_EXT_FILTERS_BACKEND_METRICS_BACKEND_METRIC_PROVIDER_H #ifndef GRPC_SRC_CORE_EXT_FILTERS_BACKEND_METRICS_BACKEND_METRIC_PROVIDER_H
#define GRPC_SRC_CORE_EXT_FILTERS_BACKEND_METRICS_BACKEND_METRIC_PROVIDER_H #define GRPC_SRC_CORE_EXT_FILTERS_BACKEND_METRICS_BACKEND_METRIC_PROVIDER_H
#include "src/core/lib/resource_quota/arena.h"
namespace grpc_core { namespace grpc_core {
struct BackendMetricData; struct BackendMetricData;
@ -24,6 +26,11 @@ class BackendMetricProvider {
virtual BackendMetricData GetBackendMetricData() = 0; virtual BackendMetricData GetBackendMetricData() = 0;
}; };
template <>
struct ArenaContextType<BackendMetricProvider> {
static void Destroy(BackendMetricProvider*) {}
};
} // namespace grpc_core } // namespace grpc_core
#endif // GRPC_SRC_CORE_EXT_FILTERS_BACKEND_METRICS_BACKEND_METRIC_PROVIDER_H #endif // GRPC_SRC_CORE_EXT_FILTERS_BACKEND_METRICS_BACKEND_METRIC_PROVIDER_H

@ -32,10 +32,6 @@ typedef enum {
/// Holds a pointer to ServiceConfigCallData associated with this call. /// Holds a pointer to ServiceConfigCallData associated with this call.
GRPC_CONTEXT_SERVICE_CONFIG_CALL_DATA, GRPC_CONTEXT_SERVICE_CONFIG_CALL_DATA,
/// Holds a pointer to BackendMetricProvider associated with this call on
/// the server.
GRPC_CONTEXT_BACKEND_METRIC_PROVIDER,
GRPC_CONTEXT_COUNT GRPC_CONTEXT_COUNT
} grpc_context_index; } grpc_context_index;

@ -410,8 +410,7 @@ void ServerContextBase::CreateCallMetricRecorder(
auto* backend_metric_state = auto* backend_metric_state =
arena->New<BackendMetricState>(server_metric_recorder); arena->New<BackendMetricState>(server_metric_recorder);
call_metric_recorder_ = backend_metric_state; call_metric_recorder_ = backend_metric_state;
grpc_call_context_set(call_.call, GRPC_CONTEXT_BACKEND_METRIC_PROVIDER, arena->SetContext<grpc_core::BackendMetricProvider>(backend_metric_state);
backend_metric_state, nullptr);
} }
grpc::string_ref ServerContextBase::ExperimentalGetAuthority() const { grpc::string_ref ServerContextBase::ExperimentalGetAuthority() const {

Loading…
Cancel
Save