Skip to content

Commit

Permalink
[StatsPlugin] Fix use-after-free issue (#36664) (#36670)
Browse files Browse the repository at this point in the history
Backport #36664 to 1.64.x
  • Loading branch information
yashykt authored May 21, 2024
1 parent b8a04ac commit 979afe0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/core/lib/surface/legacy_channel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down
22 changes: 22 additions & 0 deletions test/cpp/ext/otel/otel_plugin_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 979afe0

Please sign in to comment.