• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

grpc / grpc-java / #19807

12 May 2025 07:14AM UTC coverage: 88.607% (+0.03%) from 88.581%
#19807

push

github

web-flow
xds: Fix pretty-print of Cluster with WrrLocality and LB policies (#12037)

34781 of 39253 relevant lines covered (88.61%)

0.89 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.32
/../xds/src/main/java/io/grpc/xds/MessagePrinter.java
1
/*
2
 * Copyright 2020 The gRPC Authors
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16

17
package io.grpc.xds;
18

19
import com.github.xds.type.v3.TypedStruct;
20
import com.google.protobuf.Descriptors.Descriptor;
21
import com.google.protobuf.InvalidProtocolBufferException;
22
import com.google.protobuf.Message;
23
import com.google.protobuf.MessageOrBuilder;
24
import com.google.protobuf.TypeRegistry;
25
import com.google.protobuf.util.JsonFormat;
26
import io.envoyproxy.envoy.config.cluster.v3.Cluster;
27
import io.envoyproxy.envoy.config.endpoint.v3.ClusterLoadAssignment;
28
import io.envoyproxy.envoy.config.listener.v3.Listener;
29
import io.envoyproxy.envoy.config.route.v3.RouteConfiguration;
30
import io.envoyproxy.envoy.extensions.clusters.aggregate.v3.ClusterConfig;
31
import io.envoyproxy.envoy.extensions.filters.http.fault.v3.HTTPFault;
32
import io.envoyproxy.envoy.extensions.filters.http.rbac.v3.RBAC;
33
import io.envoyproxy.envoy.extensions.filters.http.rbac.v3.RBACPerRoute;
34
import io.envoyproxy.envoy.extensions.filters.http.router.v3.Router;
35
import io.envoyproxy.envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager;
36
import io.envoyproxy.envoy.extensions.load_balancing_policies.round_robin.v3.RoundRobin;
37
import io.envoyproxy.envoy.extensions.load_balancing_policies.wrr_locality.v3.WrrLocality;
38
import io.envoyproxy.envoy.extensions.transport_sockets.tls.v3.DownstreamTlsContext;
39
import io.envoyproxy.envoy.extensions.transport_sockets.tls.v3.UpstreamTlsContext;
40
import io.grpc.xds.client.MessagePrettyPrinter;
41

42
/**
43
 * Converts protobuf message to human readable String format. Useful for protobuf messages
44
 * containing {@link com.google.protobuf.Any} fields.
45
 */
46
final class MessagePrinter implements MessagePrettyPrinter {
47
  public static final MessagePrinter INSTANCE = new MessagePrinter();
1✔
48

49
  private MessagePrinter() {}
50

51
  // The initialization-on-demand holder idiom.
52
  private static class LazyHolder {
53
    static final JsonFormat.Printer printer = newPrinter();
1✔
54

55
    private static JsonFormat.Printer newPrinter() {
56
      TypeRegistry.Builder registry =
57
          TypeRegistry.newBuilder()
1✔
58
              .add(Listener.getDescriptor())
1✔
59
              .add(HttpConnectionManager.getDescriptor())
1✔
60
              .add(HTTPFault.getDescriptor())
1✔
61
              .add(RBAC.getDescriptor())
1✔
62
              .add(RBACPerRoute.getDescriptor())
1✔
63
              .add(Router.getDescriptor())
1✔
64
              // UpstreamTlsContext and DownstreamTlsContext in v3 are not transitively imported
65
              // by top-level resource types.
66
              .add(UpstreamTlsContext.getDescriptor())
1✔
67
              .add(DownstreamTlsContext.getDescriptor())
1✔
68
              .add(RouteConfiguration.getDescriptor())
1✔
69
              .add(Cluster.getDescriptor())
1✔
70
              .add(ClusterConfig.getDescriptor())
1✔
71
              .add(ClusterLoadAssignment.getDescriptor())
1✔
72
              .add(WrrLocality.getDescriptor())
1✔
73
              .add(TypedStruct.getDescriptor())
1✔
74
              .add(RoundRobin.getDescriptor());
1✔
75
      try {
76
        @SuppressWarnings("unchecked")
77
        Class<? extends Message> routeLookupClusterSpecifierClass =
1✔
78
            (Class<? extends Message>)
79
                Class.forName("io.grpc.lookup.v1.RouteLookupClusterSpecifier");
1✔
80
        Descriptor descriptor =
1✔
81
            (Descriptor)
82
                routeLookupClusterSpecifierClass.getDeclaredMethod("getDescriptor").invoke(null);
1✔
83
        registry.add(descriptor);
1✔
84
      } catch (Exception e) {
×
85
        // Ignore. In most cases RouteLookup is not required.
86
      }
1✔
87
      return JsonFormat.printer().usingTypeRegistry(registry.build());
1✔
88
    }
89
  }
90

91
  @Override
92
  public String print(MessageOrBuilder message) {
93
    String res;
94
    try {
95
      res = LazyHolder.printer.print(message);
1✔
96
    } catch (InvalidProtocolBufferException e) {
×
97
      res = message + " (failed to pretty-print: " + e + ")";
×
98
    }
1✔
99
    return res;
1✔
100
  }
101
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc