Skip to content

Commit 30404f4

Browse files
committed
census: introduces builder pattern for stats and tracing
1 parent 04d0600 commit 30404f4

File tree

1 file changed

+76
-15
lines changed

1 file changed

+76
-15
lines changed

census/src/main/java/io/grpc/census/GrpcCensus.java

Lines changed: 76 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.google.common.base.Stopwatch;
2020
import com.google.common.base.Supplier;
2121
import io.grpc.ClientInterceptor;
22+
import io.grpc.ManagedChannelBuilder;
2223
import io.grpc.ServerBuilder;
2324
import io.grpc.ServerStreamTracer;
2425
import io.opencensus.trace.Tracing;
@@ -31,7 +32,20 @@
3132
*/
3233
public final class GrpcCensus {
3334

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+
}
3549

3650
private static final Supplier<Stopwatch> STOPWATCH_SUPPLIER = new Supplier<Stopwatch>() {
3751
@Override
@@ -40,6 +54,38 @@ public Stopwatch get() {
4054
}
4155
};
4256

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+
4389
/**
4490
* Returns a {@link ClientInterceptor} with default stats implementation.
4591
*/
@@ -93,21 +139,36 @@ private static ServerStreamTracer.Factory newServerTracingStreamTracerFactory()
93139
}
94140

95141
/**
96-
* Configures the given {@link ServerBuilder} with serverStreamTracerFactory for stats.
97-
*
98-
* @param serverBuilder the server builder to configure
142+
* Builder for {@link GrpcCensus}.
99143
*/
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;
103147

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+
}
112158

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+
}
113174
}

0 commit comments

Comments
 (0)