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

grpc / grpc-java / #19973

04 Sep 2025 03:30AM UTC coverage: 88.535% (-0.02%) from 88.552%
#19973

push

github

ejona86
xds: Pretty-print Resource in logs

Noticed at b/431017968#comment31

34680 of 39171 relevant lines covered (88.53%)

0.89 hits per line

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

90.63
/../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.envoyproxy.envoy.service.discovery.v3.Resource;
41
import io.grpc.xds.client.MessagePrettyPrinter;
42

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

50
  private MessagePrinter() {}
51

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

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

93
  @Override
94
  public String print(MessageOrBuilder message) {
95
    String res;
96
    try {
97
      res = LazyHolder.printer.print(message);
1✔
98
    } catch (InvalidProtocolBufferException e) {
×
99
      res = message + " (failed to pretty-print: " + e + ")";
×
100
    }
1✔
101
    return res;
1✔
102
  }
103
}
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

© 2025 Coveralls, Inc