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

grpc / grpc-java / #20227

27 Mar 2026 03:13PM UTC coverage: 88.702% (-0.005%) from 88.707%
#20227

push

github

web-flow
Add custom label for per-RPC metrics

Implements gRFC A108.

https://github.com/grpc/proposal/blob/master/A108-otel-custom-per-call-label.md

35518 of 40042 relevant lines covered (88.7%)

0.89 hits per line

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

85.71
/../api/src/main/java/io/grpc/Grpc.java
1
/*
2
 * Copyright 2016 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;
18

19
import java.lang.annotation.Documented;
20
import java.lang.annotation.Retention;
21
import java.lang.annotation.RetentionPolicy;
22
import java.net.SocketAddress;
23
import java.net.URI;
24
import java.net.URISyntaxException;
25
import javax.net.ssl.SSLSession;
26

27
/**
28
 * Stuff that are part of the public API but are not bound to particular classes, e.g., static
29
 * methods, constants, attribute and context keys.
30
 */
31
public final class Grpc {
32
  private Grpc() {
33
  }
34

35
  /**
36
   * Attribute key for the remote address of a transport.
37
   */
38
  @ExperimentalApi("https://github.com/grpc/grpc-java/issues/1710")
39
  @TransportAttr
40
  public static final Attributes.Key<SocketAddress> TRANSPORT_ATTR_REMOTE_ADDR =
1✔
41
      Attributes.Key.create("io.grpc.Grpc.TRANSPORT_ATTR_REMOTE_ADDR");
1✔
42

43
  /**
44
   * Attribute key for the local address of a transport.
45
   */
46
  @ExperimentalApi("https://github.com/grpc/grpc-java/issues/1710")
47
  @TransportAttr
48
  public static final Attributes.Key<SocketAddress> TRANSPORT_ATTR_LOCAL_ADDR =
1✔
49
      Attributes.Key.create("io.grpc.Grpc.TRANSPORT_ATTR_LOCAL_ADDR");
1✔
50

51
  /**
52
   * Attribute key for SSL session of a transport.
53
   */
54
  @ExperimentalApi("https://github.com/grpc/grpc-java/issues/1710")
55
  @TransportAttr
56
  public static final Attributes.Key<SSLSession> TRANSPORT_ATTR_SSL_SESSION =
1✔
57
      Attributes.Key.create("io.grpc.Grpc.TRANSPORT_ATTR_SSL_SESSION");
1✔
58

59
  /**
60
   * The value for the custom label of per-RPC metrics. Defaults to empty string when unset. Must
61
   * not be set to {@code null}.
62
   */
63
  public static final CallOptions.Key<String> CALL_OPTION_CUSTOM_LABEL =
1✔
64
      CallOptions.Key.createWithDefault("io.grpc.Grpc.CALL_OPTION_CUSTOM_LABEL", "");
1✔
65

66
  /**
67
   * Annotation for transport attributes. It follows the annotation semantics defined
68
   * by {@link Attributes}.
69
   */
70
  @ExperimentalApi("https://github.com/grpc/grpc-java/issues/4972")
71
  @Retention(RetentionPolicy.SOURCE)
72
  @Documented
73
  public @interface TransportAttr {}
74

75
  /**
76
   * Creates a channel builder with a target string and credentials. The target can be either a
77
   * valid {@link NameResolver}-compliant URI, or an authority string.
78
   *
79
   * <p>A {@code NameResolver}-compliant URI is an absolute hierarchical URI as defined by {@link
80
   * java.net.URI}. Example URIs:
81
   * <ul>
82
   *   <li>{@code "dns:///foo.googleapis.com:8080"}</li>
83
   *   <li>{@code "dns:///foo.googleapis.com"}</li>
84
   *   <li>{@code "dns:///%5B2001:db8:85a3:8d3:1319:8a2e:370:7348%5D:443"}</li>
85
   *   <li>{@code "dns://8.8.8.8/foo.googleapis.com:8080"}</li>
86
   *   <li>{@code "dns://8.8.8.8/foo.googleapis.com"}</li>
87
   *   <li>{@code "zookeeper://zk.example.com:9900/example_service"}</li>
88
   * </ul>
89
   *
90
   * <p>An authority string will be converted to a {@code NameResolver}-compliant URI, which has
91
   * the scheme from the name resolver with the highest priority (e.g. {@code "dns"}),
92
   * no authority, and the original authority string as its path after properly escaped.
93
   * We recommend libraries to specify the schema explicitly if it is known, since libraries cannot
94
   * know which NameResolver will be default during runtime.
95
   * Example authority strings:
96
   * <ul>
97
   *   <li>{@code "localhost"}</li>
98
   *   <li>{@code "127.0.0.1"}</li>
99
   *   <li>{@code "localhost:8080"}</li>
100
   *   <li>{@code "foo.googleapis.com:8080"}</li>
101
   *   <li>{@code "127.0.0.1:8080"}</li>
102
   *   <li>{@code "[2001:db8:85a3:8d3:1319:8a2e:370:7348]"}</li>
103
   *   <li>{@code "[2001:db8:85a3:8d3:1319:8a2e:370:7348]:443"}</li>
104
   * </ul>
105
   */
106
  public static ManagedChannelBuilder<?> newChannelBuilder(
107
      String target, ChannelCredentials creds) {
108
    return ManagedChannelRegistry.getDefaultRegistry().newChannelBuilder(target, creds);
1✔
109
  }
110

111
  /**
112
   * Creates a channel builder from a host, port, and credentials. The host and port are combined to
113
   * form an authority string and then passed to {@link #newChannelBuilder(String,
114
   * ChannelCredentials)}. IPv6 addresses are properly surrounded by square brackets ("[]").
115
   */
116
  public static ManagedChannelBuilder<?> newChannelBuilderForAddress(
117
      String host, int port, ChannelCredentials creds) {
118
    return newChannelBuilder(authorityFromHostAndPort(host, port), creds);
1✔
119
  }
120

121
  /**
122
   * Combine a host and port into an authority string.
123
   */
124
  // A copy of GrpcUtil.authorityFromHostAndPort
125
  private static String authorityFromHostAndPort(String host, int port) {
126
    try {
127
      return new URI(null, null, host, port, null, null, null).getAuthority();
1✔
128
    } catch (URISyntaxException ex) {
×
129
      throw new IllegalArgumentException("Invalid host or port: " + host + " " + port, ex);
×
130
    }
131
  }
132

133
  /**
134
   * Static factory for creating a new ServerBuilder.
135
   *
136
   * @param port the port to listen on
137
   * @param creds the server identity
138
   */
139
  public static ServerBuilder<?> newServerBuilderForPort(int port, ServerCredentials creds) {
140
    return ServerRegistry.getDefaultRegistry().newServerBuilderForPort(port, creds);
1✔
141
  }
142
}
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