Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

xds/internal/xdsclient: Process string metadata in CDS for com.google.csm.telemetry_labels #7085

Merged
merged 3 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions xds/internal/xdsclient/xdsresource/type_cds.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,7 @@ type ClusterUpdate struct {

// Raw is the resource from the xds response.
Raw *anypb.Any
// All the string valued metadata of filter_metadata type
// "com.google.csm.telemetry_labels".
StringMD map[string]string
dfawley marked this conversation as resolved.
Show resolved Hide resolved
}
24 changes: 24 additions & 0 deletions xds/internal/xdsclient/xdsresource/unmarshal_cds.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
"google.golang.org/grpc/xds/internal/xdsclient/xdsresource/version"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/anypb"
"google.golang.org/protobuf/types/known/structpb"
)

// ValidateClusterAndConstructClusterUpdateForTesting exports the
Expand Down Expand Up @@ -81,6 +82,28 @@ const (
)

func validateClusterAndConstructClusterUpdate(cluster *v3clusterpb.Cluster) (ClusterUpdate, error) {
stringMD := make(map[string]string)
if fmd := cluster.GetMetadata().GetFilterMetadata(); fmd != nil {
if val, ok := fmd["com.google.csm.telemetry_labels"]; ok {
/*
"service_name" = "",
"service_namespace" = ""
*/
dfawley marked this conversation as resolved.
Show resolved Hide resolved
if fields := val.GetFields(); fields != nil {
if val, ok := fields["service_name"]; ok {
if _, isStringVal := val.GetKind().(*structpb.Value_StringValue); isStringVal {
dfawley marked this conversation as resolved.
Show resolved Hide resolved
stringMD["service_name"] = val.GetStringValue()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels weird. We check the type of a thing, then call GetStringValue on some other thing. Does GetStringValue do something different if the Kind() is not StringValue?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this is the best solution that I could come up with. GetStringValue() returns a string, not a *string, so there is no way to distinguish between an empty string (a valid label value, which will be recorded) and an unset string (should eventually record method labels as "unknown" if unset). Thus, I don't set if not a string value (not set = "unknown" in OpenTelemetry component) and set it even if it's empty if it's present.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe you can do:

if s, ok := val.AsInterface().(string); ok {
	stringMD["service_name"] = s
}

But I'm not sure that that's much better.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no function on the val called AsInterface() :/. Also, I think that typecast is wrong because it's a wrapper interface which happens to contain the value string. I.e. there's another layer (not 100% sure on this, but pretty sure).

}
}
if val, ok := fields["service_namespace"]; ok {
if _, isStringVal := val.GetKind().(*structpb.Value_StringValue); isStringVal {
stringMD["service_namespace"] = val.GetStringValue()
}
}
}
}
}

var lbPolicy json.RawMessage
var err error
switch cluster.GetLbPolicy() {
Expand Down Expand Up @@ -160,6 +183,7 @@ func validateClusterAndConstructClusterUpdate(cluster *v3clusterpb.Cluster) (Clu
MaxRequests: circuitBreakersFromCluster(cluster),
LBPolicy: lbPolicy,
OutlierDetection: od,
StringMD: stringMD,
}

// Note that this is different from the gRFC (gRFC A47 says to include the
Expand Down
95 changes: 95 additions & 0 deletions xds/internal/xdsclient/xdsresource/unmarshal_cds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (
v3matcherpb "github.com/envoyproxy/go-control-plane/envoy/type/matcher/v3"
"google.golang.org/protobuf/types/known/anypb"
"google.golang.org/protobuf/types/known/durationpb"
"google.golang.org/protobuf/types/known/structpb"
"google.golang.org/protobuf/types/known/wrapperspb"
)

Expand Down Expand Up @@ -1216,6 +1217,71 @@ func (s) TestUnmarshalCluster(t *testing.T) {
},
},
})

v3ClusterAnyWithTelemetryLabels = testutils.MarshalAny(t, &v3clusterpb.Cluster{
Name: v3ClusterName,
ClusterDiscoveryType: &v3clusterpb.Cluster_Type{Type: v3clusterpb.Cluster_EDS},
EdsClusterConfig: &v3clusterpb.Cluster_EdsClusterConfig{
EdsConfig: &v3corepb.ConfigSource{
ConfigSourceSpecifier: &v3corepb.ConfigSource_Ads{
Ads: &v3corepb.AggregatedConfigSource{},
},
},
ServiceName: v3Service,
},
LbPolicy: v3clusterpb.Cluster_ROUND_ROBIN,
LrsServer: &v3corepb.ConfigSource{
ConfigSourceSpecifier: &v3corepb.ConfigSource_Self{
Self: &v3corepb.SelfConfigSource{},
},
},
Metadata: &v3corepb.Metadata{
FilterMetadata: map[string]*structpb.Struct{
"com.google.csm.telemetry_labels": {
Fields: map[string]*structpb.Value{
"service_name": structpb.NewStringValue("grpc-service"),
"service_namespace": structpb.NewStringValue("grpc-service-namespace"),
},
},
},
},
})
v3ClusterAnyWithTelemetryLabelsIgnoreSome = testutils.MarshalAny(t, &v3clusterpb.Cluster{
Name: v3ClusterName,
ClusterDiscoveryType: &v3clusterpb.Cluster_Type{Type: v3clusterpb.Cluster_EDS},
EdsClusterConfig: &v3clusterpb.Cluster_EdsClusterConfig{
EdsConfig: &v3corepb.ConfigSource{
ConfigSourceSpecifier: &v3corepb.ConfigSource_Ads{
Ads: &v3corepb.AggregatedConfigSource{},
},
},
ServiceName: v3Service,
},
LbPolicy: v3clusterpb.Cluster_ROUND_ROBIN,
LrsServer: &v3corepb.ConfigSource{
ConfigSourceSpecifier: &v3corepb.ConfigSource_Self{
Self: &v3corepb.SelfConfigSource{},
},
},
Metadata: &v3corepb.Metadata{
FilterMetadata: map[string]*structpb.Struct{
"com.google.csm.telemetry_labels": {
Fields: map[string]*structpb.Value{
"string-value-should-ignore": structpb.NewStringValue("string-val"),
"float-value-ignore": structpb.NewNumberValue(3),
"bool-value-ignore": structpb.NewBoolValue(false),
"service_name": structpb.NewStringValue("grpc-service"), // shouldn't ignore
"service_namespace": structpb.NewNullValue(), // should ignore - wrong type
},
},
"ignore-this-metadata": { // should ignore this filter_metadata
Fields: map[string]*structpb.Value{
"service_namespace": structpb.NewStringValue("string-val-should-ignore"),
},
},
},
},
})
)

tests := []struct {
Expand Down Expand Up @@ -1300,6 +1366,35 @@ func (s) TestUnmarshalCluster(t *testing.T) {
Raw: v3ClusterAnyWithEDSConfigSourceSelf,
},
},
{
name: "v3 cluster with telemetry case",
resource: v3ClusterAnyWithTelemetryLabels,
wantName: v3ClusterName,
wantUpdate: ClusterUpdate{
ClusterName: v3ClusterName,
EDSServiceName: v3Service,
LRSServerConfig: ClusterLRSServerSelf,
Raw: v3ClusterAnyWithTelemetryLabels,
StringMD: map[string]string{
"service_name": "grpc-service",
"service_namespace": "grpc-service-namespace",
},
},
},
{
name: "v3 metadata ignore other types not string and not com.google.csm.telemetry_labels",
resource: v3ClusterAnyWithTelemetryLabelsIgnoreSome,
wantName: v3ClusterName,
wantUpdate: ClusterUpdate{
ClusterName: v3ClusterName,
EDSServiceName: v3Service,
LRSServerConfig: ClusterLRSServerSelf,
Raw: v3ClusterAnyWithTelemetryLabelsIgnoreSome,
StringMD: map[string]string{
"service_name": "grpc-service",
},
},
},
{
name: "xdstp cluster resource with unset EDS service name",
resource: testutils.MarshalAny(t, &v3clusterpb.Cluster{
Expand Down
Loading