19
19
import com .google .common .base .Stopwatch ;
20
20
import com .google .common .base .Supplier ;
21
21
import io .grpc .ClientInterceptor ;
22
+ import io .grpc .ManagedChannelBuilder ;
22
23
import io .grpc .ServerBuilder ;
23
24
import io .grpc .ServerStreamTracer ;
24
25
import io .opencensus .trace .Tracing ;
31
32
*/
32
33
public final class GrpcCensus {
33
34
34
- private GrpcCensus () {}
35
+ private final boolean statsEnabled ;
36
+ private final boolean tracingEnabled ;
37
+
38
+ private GrpcCensus (boolean statsEnabled , boolean tracingEnabled ) {
39
+ this .statsEnabled = statsEnabled ;
40
+ this .tracingEnabled = tracingEnabled ;
41
+ }
42
+
43
+ /**
44
+ * Creates a new builder for {@link GrpcCensus}.
45
+ */
46
+ public static GrpcCensusBuilder builder () {
47
+ return new GrpcCensusBuilder ();
48
+ }
35
49
36
50
private static final Supplier <Stopwatch > STOPWATCH_SUPPLIER = new Supplier <Stopwatch >() {
37
51
@ Override
@@ -40,6 +54,38 @@ public Stopwatch get() {
40
54
}
41
55
};
42
56
57
+ /**
58
+ * Configures a {@link ServerBuilder} to enable census stats and tracing.
59
+ *
60
+ * @param serverBuilder The server builder to configure.
61
+ * @return The configured server builder.
62
+ */
63
+ public <T extends ServerBuilder <T >> T configureServerBuilder (T serverBuilder ) {
64
+ if (statsEnabled ) {
65
+ serverBuilder .addStreamTracerFactory (newServerStatsStreamTracerFactory ());
66
+ }
67
+ if (tracingEnabled ) {
68
+ serverBuilder .addStreamTracerFactory (newServerTracingStreamTracerFactory ());
69
+ }
70
+ return serverBuilder ;
71
+ }
72
+
73
+ /**
74
+ * Configures a {@link ManagedChannelBuilder} to enable census stats and tracing.
75
+ *
76
+ * @param channelBuilder The channel builder to configure.
77
+ * @return The configured channel builder.
78
+ */
79
+ public <T extends ManagedChannelBuilder <T >> T configureChannelBuilder (T channelBuilder ) {
80
+ if (statsEnabled ) {
81
+ channelBuilder .intercept (newClientStatsInterceptor ());
82
+ }
83
+ if (tracingEnabled ) {
84
+ channelBuilder .intercept (newClientTracingInterceptor ());
85
+ }
86
+ return channelBuilder ;
87
+ }
88
+
43
89
/**
44
90
* Returns a {@link ClientInterceptor} with default stats implementation.
45
91
*/
@@ -93,21 +139,36 @@ private static ServerStreamTracer.Factory newServerTracingStreamTracerFactory()
93
139
}
94
140
95
141
/**
96
- * Configures the given {@link ServerBuilder} with serverStreamTracerFactory for stats.
97
- *
98
- * @param serverBuilder the server builder to configure
142
+ * Builder for {@link GrpcCensus}.
99
143
*/
100
- public static void configureServerBuilderWithStatsTracer ( ServerBuilder <?> serverBuilder ) {
101
- serverBuilder . addStreamTracerFactory ( newServerStatsStreamTracerFactory ()) ;
102
- }
144
+ public static final class GrpcCensusBuilder {
145
+ private boolean statsEnabled = true ;
146
+ private boolean tracingEnabled = true ;
103
147
104
- /**
105
- * Configures the given {@link ServerBuilder} with serverStreamTracerFactory for tracing.
106
- *
107
- * @param serverBuilder the server builder to configure
108
- */
109
- public static void configureServerBuilderWithTracingTracer (ServerBuilder <?> serverBuilder ) {
110
- serverBuilder .addStreamTracerFactory (newServerTracingStreamTracerFactory ());
111
- }
148
+ private GrpcCensusBuilder () {
149
+ }
150
+
151
+ /**
152
+ * Disables stats collection.
153
+ */
154
+ public GrpcCensusBuilder disableStats () {
155
+ this .statsEnabled = false ;
156
+ return this ;
157
+ }
112
158
159
+ /**
160
+ * Disables tracing.
161
+ */
162
+ public GrpcCensusBuilder disableTracing () {
163
+ this .tracingEnabled = false ;
164
+ return this ;
165
+ }
166
+
167
+ /**
168
+ * Builds a new {@link GrpcCensus}.
169
+ */
170
+ public GrpcCensus build () {
171
+ return new GrpcCensus (statsEnabled , tracingEnabled );
172
+ }
173
+ }
113
174
}
0 commit comments