[StatsPlugin] Fix use-after-free issue (#36664)

Fix https://github.com/grpc/grpc/issues/36663

Closes #36664

COPYBARA_INTEGRATE_REVIEW=https://github.com/grpc/grpc/pull/36664 from yashykt:StatsPluginFixUseAfterFree 107c341349
PiperOrigin-RevId: 635555326
pull/36671/head
Yash Tibrewal 9 months ago committed by Copybara-Service
parent a79a8111af
commit d3960484c0
  1. 12
      src/core/lib/surface/legacy_channel.cc
  2. 22
      test/cpp/ext/otel/otel_plugin_test.cc

@ -93,13 +93,13 @@ absl::StatusOr<OrphanablePtr<Channel>> LegacyChannel::Create(
*(*r)->stats_plugin_group =
GlobalStatsPluginRegistry::GetStatsPluginsForServer(args);
} else {
experimental::StatsPluginChannelScope scope(
target, args.GetOwnedString(GRPC_ARG_DEFAULT_AUTHORITY)
.value_or(CoreConfiguration::Get()
.resolver_registry()
.GetDefaultAuthority(target)));
std::string authority = args.GetOwnedString(GRPC_ARG_DEFAULT_AUTHORITY)
.value_or(CoreConfiguration::Get()
.resolver_registry()
.GetDefaultAuthority(target));
*(*r)->stats_plugin_group =
GlobalStatsPluginRegistry::GetStatsPluginsForChannel(scope);
GlobalStatsPluginRegistry::GetStatsPluginsForChannel(
experimental::StatsPluginChannelScope(target, authority));
}
return MakeOrphanable<LegacyChannel>(
grpc_channel_stack_type_is_client(builder.channel_stack_type()),

@ -471,6 +471,28 @@ TEST_F(OpenTelemetryPluginEnd2EndTest, NoMeterProviderRegistered) {
SendRPC();
}
// Test that the otel plugin sees the expected channel target and default
// authority.
TEST_F(OpenTelemetryPluginEnd2EndTest, VerifyChannelScopeTargetAndAuthority) {
Init(std::move(
Options()
.set_metric_names({grpc::OpenTelemetryPluginBuilder::
kClientAttemptStartedInstrumentName})
.set_channel_scope_filter(
[&](const OpenTelemetryPluginBuilder::ChannelScope& scope) {
return scope.target() == canonical_server_address_ &&
scope.default_authority() == server_address_;
})));
SendRPC();
const char* kMetricName = "grpc.client.attempt.started";
auto data = ReadCurrentMetricsData(
[&](const absl::flat_hash_map<
std::string,
std::vector<opentelemetry::sdk::metrics::PointDataAttributes>>&
data) { return !data.contains(kMetricName); });
ASSERT_EQ(data[kMetricName].size(), 1);
}
// Test that a channel scope filter returning true records metrics on the
// channel.
TEST_F(OpenTelemetryPluginEnd2EndTest, ChannelScopeFilterReturnsTrue) {

Loading…
Cancel
Save