Skip to content

Commit 308dbc4

Browse files
authored
xds/internal/xdsclient: Process string metadata in CDS for com.google.csm.telemetry_labels (#7085)
1 parent 554f107 commit 308dbc4

File tree

3 files changed

+119
-0
lines changed

3 files changed

+119
-0
lines changed

xds/internal/xdsclient/xdsresource/type_cds.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,8 @@ type ClusterUpdate struct {
8585

8686
// Raw is the resource from the xds response.
8787
Raw *anypb.Any
88+
// TelemetryLabels are the string valued metadata of filter_metadata type
89+
// "com.google.csm.telemetry_labels" with keys "service_name" or
90+
// "service_namespace".
91+
TelemetryLabels map[string]string
8892
}

xds/internal/xdsclient/xdsresource/unmarshal_cds.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import (
3939
"google.golang.org/grpc/xds/internal/xdsclient/xdsresource/version"
4040
"google.golang.org/protobuf/proto"
4141
"google.golang.org/protobuf/types/known/anypb"
42+
"google.golang.org/protobuf/types/known/structpb"
4243
)
4344

4445
// ValidateClusterAndConstructClusterUpdateForTesting exports the
@@ -81,6 +82,24 @@ const (
8182
)
8283

8384
func validateClusterAndConstructClusterUpdate(cluster *v3clusterpb.Cluster) (ClusterUpdate, error) {
85+
telemetryLabels := make(map[string]string)
86+
if fmd := cluster.GetMetadata().GetFilterMetadata(); fmd != nil {
87+
if val, ok := fmd["com.google.csm.telemetry_labels"]; ok {
88+
if fields := val.GetFields(); fields != nil {
89+
if val, ok := fields["service_name"]; ok {
90+
if _, ok := val.GetKind().(*structpb.Value_StringValue); ok {
91+
telemetryLabels["service_name"] = val.GetStringValue()
92+
}
93+
}
94+
if val, ok := fields["service_namespace"]; ok {
95+
if _, ok := val.GetKind().(*structpb.Value_StringValue); ok {
96+
telemetryLabels["service_namespace"] = val.GetStringValue()
97+
}
98+
}
99+
}
100+
}
101+
}
102+
84103
var lbPolicy json.RawMessage
85104
var err error
86105
switch cluster.GetLbPolicy() {
@@ -160,6 +179,7 @@ func validateClusterAndConstructClusterUpdate(cluster *v3clusterpb.Cluster) (Clu
160179
MaxRequests: circuitBreakersFromCluster(cluster),
161180
LBPolicy: lbPolicy,
162181
OutlierDetection: od,
182+
TelemetryLabels: telemetryLabels,
163183
}
164184

165185
// Note that this is different from the gRFC (gRFC A47 says to include the

xds/internal/xdsclient/xdsresource/unmarshal_cds_test.go

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import (
4141
v3matcherpb "github.com/envoyproxy/go-control-plane/envoy/type/matcher/v3"
4242
"google.golang.org/protobuf/types/known/anypb"
4343
"google.golang.org/protobuf/types/known/durationpb"
44+
"google.golang.org/protobuf/types/known/structpb"
4445
"google.golang.org/protobuf/types/known/wrapperspb"
4546
)
4647

@@ -1216,6 +1217,71 @@ func (s) TestUnmarshalCluster(t *testing.T) {
12161217
},
12171218
},
12181219
})
1220+
1221+
v3ClusterAnyWithTelemetryLabels = testutils.MarshalAny(t, &v3clusterpb.Cluster{
1222+
Name: v3ClusterName,
1223+
ClusterDiscoveryType: &v3clusterpb.Cluster_Type{Type: v3clusterpb.Cluster_EDS},
1224+
EdsClusterConfig: &v3clusterpb.Cluster_EdsClusterConfig{
1225+
EdsConfig: &v3corepb.ConfigSource{
1226+
ConfigSourceSpecifier: &v3corepb.ConfigSource_Ads{
1227+
Ads: &v3corepb.AggregatedConfigSource{},
1228+
},
1229+
},
1230+
ServiceName: v3Service,
1231+
},
1232+
LbPolicy: v3clusterpb.Cluster_ROUND_ROBIN,
1233+
LrsServer: &v3corepb.ConfigSource{
1234+
ConfigSourceSpecifier: &v3corepb.ConfigSource_Self{
1235+
Self: &v3corepb.SelfConfigSource{},
1236+
},
1237+
},
1238+
Metadata: &v3corepb.Metadata{
1239+
FilterMetadata: map[string]*structpb.Struct{
1240+
"com.google.csm.telemetry_labels": {
1241+
Fields: map[string]*structpb.Value{
1242+
"service_name": structpb.NewStringValue("grpc-service"),
1243+
"service_namespace": structpb.NewStringValue("grpc-service-namespace"),
1244+
},
1245+
},
1246+
},
1247+
},
1248+
})
1249+
v3ClusterAnyWithTelemetryLabelsIgnoreSome = testutils.MarshalAny(t, &v3clusterpb.Cluster{
1250+
Name: v3ClusterName,
1251+
ClusterDiscoveryType: &v3clusterpb.Cluster_Type{Type: v3clusterpb.Cluster_EDS},
1252+
EdsClusterConfig: &v3clusterpb.Cluster_EdsClusterConfig{
1253+
EdsConfig: &v3corepb.ConfigSource{
1254+
ConfigSourceSpecifier: &v3corepb.ConfigSource_Ads{
1255+
Ads: &v3corepb.AggregatedConfigSource{},
1256+
},
1257+
},
1258+
ServiceName: v3Service,
1259+
},
1260+
LbPolicy: v3clusterpb.Cluster_ROUND_ROBIN,
1261+
LrsServer: &v3corepb.ConfigSource{
1262+
ConfigSourceSpecifier: &v3corepb.ConfigSource_Self{
1263+
Self: &v3corepb.SelfConfigSource{},
1264+
},
1265+
},
1266+
Metadata: &v3corepb.Metadata{
1267+
FilterMetadata: map[string]*structpb.Struct{
1268+
"com.google.csm.telemetry_labels": {
1269+
Fields: map[string]*structpb.Value{
1270+
"string-value-should-ignore": structpb.NewStringValue("string-val"),
1271+
"float-value-ignore": structpb.NewNumberValue(3),
1272+
"bool-value-ignore": structpb.NewBoolValue(false),
1273+
"service_name": structpb.NewStringValue("grpc-service"), // shouldn't ignore
1274+
"service_namespace": structpb.NewNullValue(), // should ignore - wrong type
1275+
},
1276+
},
1277+
"ignore-this-metadata": { // should ignore this filter_metadata
1278+
Fields: map[string]*structpb.Value{
1279+
"service_namespace": structpb.NewStringValue("string-val-should-ignore"),
1280+
},
1281+
},
1282+
},
1283+
},
1284+
})
12191285
)
12201286

12211287
tests := []struct {
@@ -1300,6 +1366,35 @@ func (s) TestUnmarshalCluster(t *testing.T) {
13001366
Raw: v3ClusterAnyWithEDSConfigSourceSelf,
13011367
},
13021368
},
1369+
{
1370+
name: "v3 cluster with telemetry case",
1371+
resource: v3ClusterAnyWithTelemetryLabels,
1372+
wantName: v3ClusterName,
1373+
wantUpdate: ClusterUpdate{
1374+
ClusterName: v3ClusterName,
1375+
EDSServiceName: v3Service,
1376+
LRSServerConfig: ClusterLRSServerSelf,
1377+
Raw: v3ClusterAnyWithTelemetryLabels,
1378+
TelemetryLabels: map[string]string{
1379+
"service_name": "grpc-service",
1380+
"service_namespace": "grpc-service-namespace",
1381+
},
1382+
},
1383+
},
1384+
{
1385+
name: "v3 metadata ignore other types not string and not com.google.csm.telemetry_labels",
1386+
resource: v3ClusterAnyWithTelemetryLabelsIgnoreSome,
1387+
wantName: v3ClusterName,
1388+
wantUpdate: ClusterUpdate{
1389+
ClusterName: v3ClusterName,
1390+
EDSServiceName: v3Service,
1391+
LRSServerConfig: ClusterLRSServerSelf,
1392+
Raw: v3ClusterAnyWithTelemetryLabelsIgnoreSome,
1393+
TelemetryLabels: map[string]string{
1394+
"service_name": "grpc-service",
1395+
},
1396+
},
1397+
},
13031398
{
13041399
name: "xdstp cluster resource with unset EDS service name",
13051400
resource: testutils.MarshalAny(t, &v3clusterpb.Cluster{

0 commit comments

Comments
 (0)