• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 javax.annotation.Nullable;
20 import javax.annotation.concurrent.ThreadSafe;
21 
22 /**
23  * Listens to events on a stream to collect metrics.
24  */
25 @ExperimentalApi("https://github.com/grpc/grpc-java/issues/2861")
26 @ThreadSafe
27 public abstract class ServerStreamTracer extends StreamTracer {
28   /**
29    * Called before the interceptors and the call handlers and make changes to the Context object
30    * if needed.
31    */
filterContext(Context context)32   public Context filterContext(Context context) {
33     return context;
34   }
35 
36   /**
37    * Called when {@link ServerCall} is created.  This is for the tracer to access information about
38    * the {@code ServerCall}.  Called after {@link #filterContext} and before the application call
39    * handler.
40    */
41   @SuppressWarnings("deprecation")
serverCallStarted(ServerCallInfo<?, ?> callInfo)42   public void serverCallStarted(ServerCallInfo<?, ?> callInfo) {
43     serverCallStarted(ReadOnlyServerCall.create(callInfo));
44   }
45 
46   /**
47    * Called when {@link ServerCall} is created.  This is for the tracer to access information about
48    * the {@code ServerCall}.  Called after {@link #filterContext} and before the application call
49    * handler.
50    *
51    * @deprecated Implement {@link #serverCallStarted(ServerCallInfo)} instead. This method will be
52    *     removed in a future release of gRPC.
53    */
54   @Deprecated
serverCallStarted(ServerCall<?, ?> call)55   public void serverCallStarted(ServerCall<?, ?> call) {
56   }
57 
58   public abstract static class Factory {
59     /**
60      * Creates a {@link ServerStreamTracer} for a new server stream.
61      *
62      * <p>Called right before the stream is created
63      *
64      * @param fullMethodName the fully qualified method name
65      * @param headers the received request headers.  It can be safely mutated within this method.
66      *        It should not be saved because it is not safe for read or write after the method
67      *        returns.
68      */
newServerStreamTracer( String fullMethodName, Metadata headers)69     public abstract ServerStreamTracer newServerStreamTracer(
70         String fullMethodName, Metadata headers);
71   }
72 
73   /**
74    * A data class with info about the started {@link ServerCall}.
75    */
76   public abstract static class ServerCallInfo<ReqT, RespT> {
getMethodDescriptor()77     public abstract MethodDescriptor<ReqT, RespT> getMethodDescriptor();
78 
getAttributes()79     public abstract Attributes getAttributes();
80 
81     @Nullable
getAuthority()82     public abstract String getAuthority();
83   }
84 
85   /**
86    * This class exists solely to help transition to the {@link ServerCallInfo} based API.
87    *
88    * @deprecated Will be deleted when {@link #serverCallStarted(ServerCall)} is removed.
89    */
90   @Deprecated
91   private static final class ReadOnlyServerCall<ReqT, RespT>
92       extends ForwardingServerCall<ReqT, RespT> {
93     private final ServerCallInfo<ReqT, RespT> callInfo;
94 
create( ServerCallInfo<ReqT, RespT> callInfo)95     private static <ReqT, RespT> ReadOnlyServerCall<ReqT, RespT> create(
96         ServerCallInfo<ReqT, RespT> callInfo) {
97       return new ReadOnlyServerCall<ReqT, RespT>(callInfo);
98     }
99 
ReadOnlyServerCall(ServerCallInfo<ReqT, RespT> callInfo)100     private ReadOnlyServerCall(ServerCallInfo<ReqT, RespT> callInfo) {
101       this.callInfo = callInfo;
102     }
103 
104     @Override
getMethodDescriptor()105     public MethodDescriptor<ReqT, RespT> getMethodDescriptor() {
106       return callInfo.getMethodDescriptor();
107     }
108 
109     @Override
getAttributes()110     public Attributes getAttributes() {
111       return callInfo.getAttributes();
112     }
113 
114     @Override
isReady()115     public boolean isReady() {
116       // a dummy value
117       return false;
118     }
119 
120     @Override
isCancelled()121     public boolean isCancelled() {
122       // a dummy value
123       return false;
124     }
125 
126     @Override
getAuthority()127     public String getAuthority() {
128       return callInfo.getAuthority();
129     }
130 
131     @Override
delegate()132     protected ServerCall<ReqT, RespT> delegate() {
133       throw new UnsupportedOperationException();
134     }
135   }
136 }
137