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

grpc / grpc-java / #19180

29 Apr 2024 11:30PM UTC coverage: 88.296% (-0.06%) from 88.351%
#19180

push

github

web-flow
Plumb optional labels from LB to ClientStreamTracer

As part of gRFC A78:

> To support the locality label in the per-call metrics, we will provide
> a mechanism for LB picker to add optional labels to the call attempt
> tracer.

31435 of 35602 relevant lines covered (88.3%)

0.88 hits per line

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

84.62
/../api/src/main/java/io/grpc/ClientStreamTracer.java
1
/*
2
 * Copyright 2017 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 static com.google.common.base.Preconditions.checkNotNull;
20

21
import com.google.common.base.MoreObjects;
22
import javax.annotation.concurrent.ThreadSafe;
23

24
/**
25
 * {@link StreamTracer} for the client-side.
26
 */
27
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/2861")
28
@ThreadSafe
29
public abstract class ClientStreamTracer extends StreamTracer {
1✔
30
  /**
31
   * Indicates how long the call was delayed, in nanoseconds, due to waiting for name resolution
32
   * result. If the call option is not set, the call did not experience name resolution delay.
33
   */
34
  public static final CallOptions.Key<Long> NAME_RESOLUTION_DELAYED =
1✔
35
      CallOptions.Key.create("io.grpc.ClientStreamTracer.NAME_RESOLUTION_DELAYED");
1✔
36

37
  /**
38
   * The stream is being created on a ready transport.
39
   *
40
   * @param headers the mutable initial metadata. Modifications to it will be sent to the socket but
41
   *     not be seen by client interceptors and the application.
42
   *
43
   * @since 1.40.0
44
   */
45
  public void streamCreated(@Grpc.TransportAttr Attributes transportAttrs, Metadata headers) {
46
  }
1✔
47

48
  /**
49
   * Name resolution is completed and the connection starts getting established. This method is only
50
   * invoked on the streams that encounter such delay.
51
   *
52
   * </p>gRPC buffers the client call if the remote address and configurations, e.g. timeouts and
53
   * retry policy, are not ready. Asynchronously gRPC internally does the name resolution to get
54
   * this information. The streams that are processed immediately on ready transports by the time
55
   * the RPC comes do not go through the pending process, thus this callback will not be invoked.
56
   */
57
  public void createPendingStream() {
58
  }
1✔
59

60
  /**
61
   * Headers has been sent to the socket.
62
   */
63
  public void outboundHeaders() {
64
  }
1✔
65

66
  /**
67
   * Headers has been received from the server.
68
   */
69
  public void inboundHeaders() {
70
  }
1✔
71

72
  /**
73
   * Trailing metadata has been received from the server.
74
   *
75
   * @param trailers the mutable trailing metadata.  Modifications to it will be seen by
76
   *                 interceptors and the application.
77
   * @since 1.17.0
78
   */
79
  public void inboundTrailers(Metadata trailers) {
80
  }
1✔
81

82
  /**
83
   * Information providing context to the call became available.
84
   */
85
  @Internal
86
  public void addOptionalLabel(String key, String value) {
87
  }
1✔
88

89
  /**
90
   * Factory class for {@link ClientStreamTracer}.
91
   */
92
  public abstract static class Factory {
1✔
93
    /**
94
     * Creates a {@link ClientStreamTracer} for a new client stream.  This is called inside the
95
     * transport when it's creating the stream.
96
     *
97
     * @param info information about the stream
98
     * @param headers the mutable headers of the stream. It can be safely mutated within this
99
     *        method.  Changes made to it will be sent by the stream.  It should not be saved
100
     *        because it is not safe for read or write after the method returns.
101
     *
102
     * @since 1.20.0
103
     */
104
    public ClientStreamTracer newClientStreamTracer(StreamInfo info, Metadata headers) {
105
      throw new UnsupportedOperationException("Not implemented");
×
106
    }
107
  }
108

109
  /**
110
   * Information about a stream.
111
   *
112
   * <p>Note this class doesn't override {@code equals()} and {@code hashCode}, as is the case for
113
   * {@link CallOptions}.
114
   *
115
   * @since 1.20.0
116
   */
117
  @ExperimentalApi("https://github.com/grpc/grpc-java/issues/2861")
118
  public static final class StreamInfo {
119
    private final CallOptions callOptions;
120
    private final int previousAttempts;
121
    private final boolean isTransparentRetry;
122

123
    StreamInfo(
124
        CallOptions callOptions, int previousAttempts, boolean isTransparentRetry) {
1✔
125
      this.callOptions = checkNotNull(callOptions, "callOptions");
1✔
126
      this.previousAttempts = previousAttempts;
1✔
127
      this.isTransparentRetry = isTransparentRetry;
1✔
128
    }
1✔
129

130
    /**
131
     * Returns the effective CallOptions of the call.
132
     */
133
    public CallOptions getCallOptions() {
134
      return callOptions;
1✔
135
    }
136

137
    /**
138
     * Returns the number of preceding attempts for the RPC.
139
     *
140
     * @since 1.40.0
141
     */
142
    public int getPreviousAttempts() {
143
      return previousAttempts;
1✔
144
    }
145

146
    /**
147
     * Whether the stream is a transparent retry.
148
     *
149
     * @since 1.40.0
150
     */
151
    public boolean isTransparentRetry() {
152
      return isTransparentRetry;
1✔
153
    }
154

155
    /**
156
     * Converts this StreamInfo into a new Builder.
157
     *
158
     * @since 1.21.0
159
     */
160
    public Builder toBuilder() {
161
      return new Builder()
1✔
162
          .setCallOptions(callOptions)
1✔
163
          .setPreviousAttempts(previousAttempts)
1✔
164
          .setIsTransparentRetry(isTransparentRetry);
1✔
165
    }
166

167
    /**
168
     * Creates an empty Builder.
169
     *
170
     * @since 1.21.0
171
     */
172
    public static Builder newBuilder() {
173
      return new Builder();
1✔
174
    }
175

176
    @Override
177
    public String toString() {
178
      return MoreObjects.toStringHelper(this)
×
179
          .add("callOptions", callOptions)
×
180
          .add("previousAttempts", previousAttempts)
×
181
          .add("isTransparentRetry", isTransparentRetry)
×
182
          .toString();
×
183
    }
184

185
    /**
186
     * Builds {@link StreamInfo} objects.
187
     *
188
     * @since 1.21.0
189
     */
190
    public static final class Builder {
191
      private CallOptions callOptions = CallOptions.DEFAULT;
1✔
192
      private int previousAttempts;
193
      private boolean isTransparentRetry;
194

195
      Builder() {
1✔
196
      }
1✔
197

198
      /**
199
       * Sets the effective CallOptions of the call.  This field is optional.
200
       */
201
      public Builder setCallOptions(CallOptions callOptions) {
202
        this.callOptions = checkNotNull(callOptions, "callOptions cannot be null");
1✔
203
        return this;
1✔
204
      }
205

206
      /**
207
       * Set the number of preceding attempts of the RPC.
208
       *
209
       * @since 1.40.0
210
       */
211
      public Builder setPreviousAttempts(int previousAttempts) {
212
        this.previousAttempts = previousAttempts;
1✔
213
        return this;
1✔
214
      }
215

216
      /**
217
       * Sets whether the stream is a transparent retry.
218
       *
219
       * @since 1.40.0
220
       */
221
      public Builder setIsTransparentRetry(boolean isTransparentRetry) {
222
        this.isTransparentRetry = isTransparentRetry;
1✔
223
        return this;
1✔
224
      }
225

226
      /**
227
       * Builds a new StreamInfo.
228
       */
229
      public StreamInfo build() {
230
        return new StreamInfo(callOptions, previousAttempts, isTransparentRetry);
1✔
231
      }
232
    }
233
  }
234
}
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