[CSM] Add target selector for csm channels (#34311)

<!--

If you know who should review your pull request, please assign it to
that
person, otherwise the pull request would get assigned randomly.

If your pull request is for a specific language, please add the
appropriate
lang label.

-->
pull/34335/head
Yash Tibrewal 1 year ago committed by GitHub
parent dcd0f6b8bd
commit ee6ed6ab4f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      src/cpp/ext/csm/BUILD
  2. 23
      src/cpp/ext/csm/csm_observability.cc
  3. 4
      src/cpp/ext/csm/csm_observability.h
  4. 23
      test/cpp/ext/csm/csm_observability_test.cc

@ -52,9 +52,11 @@ grpc_cc_library(
language = "c++",
visibility = ["//:__subpackages__"],
deps = [
"//:gpr",
"//:gpr_platform",
"//:grpc_base",
"//:protobuf_struct_upb",
"//:uri_parser",
"//src/core:env",
"//src/core:error",
"//src/core:json",

@ -20,8 +20,12 @@
#include "src/cpp/ext/csm/csm_observability.h"
#include <string>
#include <utility>
#include <grpc/support/log.h>
#include "src/core/lib/uri/uri_parser.h"
#include "src/cpp/ext/otel/otel_plugin.h"
namespace grpc {
@ -71,8 +75,27 @@ CsmObservabilityBuilder& CsmObservabilityBuilder::SetTargetAttributeFilter(
absl::StatusOr<CsmObservability> CsmObservabilityBuilder::BuildAndRegister() {
builder_.BuildAndRegisterGlobal();
builder_.SetTargetSelector(CsmChannelTargetSelector);
return CsmObservability();
}
bool CsmChannelTargetSelector(absl::string_view target) {
auto uri = grpc_core::URI::Parse(target);
if (!uri.ok()) {
gpr_log(GPR_ERROR, "Failed to parse URI: %s", std::string(target).c_str());
return false;
}
// CSM channels should have an "xds" scheme
if (uri->scheme() != "xds") {
return false;
}
// If set, the authority should be TD
if (!uri->authority().empty() &&
uri->authority() != "traffic-director-global.xds.googleapis.com") {
return false;
}
return true;
}
} // namespace internal
} // namespace grpc

@ -76,6 +76,10 @@ class CsmObservabilityBuilder {
OpenTelemetryPluginBuilder builder_;
};
// EXPOSED FOR TESTING PURPOSES ONLY
// Returns true if the channel is a CSM channel.
bool CsmChannelTargetSelector(absl::string_view target);
} // namespace internal
} // namespace grpc

@ -37,6 +37,29 @@ TEST(GsmDependencyTest, GoogleCloudOpenTelemetryDependency) {
EXPECT_NE(google::cloud::otel::MakeResourceDetector(), nullptr);
}
TEST(CsmChannelTargetSelectorTest, NonXdsTargets) {
EXPECT_FALSE(internal::CsmChannelTargetSelector("foo.bar.google.com"));
EXPECT_FALSE(internal::CsmChannelTargetSelector("dns:///foo.bar.google.com"));
EXPECT_FALSE(
internal::CsmChannelTargetSelector("dns:///foo.bar.google.com:1234"));
EXPECT_FALSE(internal::CsmChannelTargetSelector(
"dns://authority/foo.bar.google.com:1234"));
}
TEST(CsmChannelTargetSelectorTest, XdsTargets) {
EXPECT_TRUE(internal::CsmChannelTargetSelector("xds:///foo"));
EXPECT_TRUE(internal::CsmChannelTargetSelector("xds:///foo.bar"));
}
TEST(CsmChannelTargetSelectorTest, XdsTargetsWithNonTDAuthority) {
EXPECT_FALSE(internal::CsmChannelTargetSelector("xds://authority/foo"));
}
TEST(CsmChannelTargetSelectorTest, XdsTargetsWithTDAuthority) {
EXPECT_TRUE(internal::CsmChannelTargetSelector(
"xds://traffic-director-global.xds.googleapis.com/foo"));
}
} // namespace
} // namespace testing
} // namespace grpc

Loading…
Cancel
Save