-
Notifications
You must be signed in to change notification settings - Fork 3.9k
census: expose client interceptors #12050
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
AgraVator
wants to merge
5
commits into
grpc:master
Choose a base branch
from
AgraVator:expose-census-apis
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
afde3d7
census: expose client interceptors
AgraVator 6530b70
census: expose server tracer factories
AgraVator 94e92f4
census: use no arg methods for ClientInterceptors and StreamFactories
AgraVator 1ea671c
census: introduces builder pattern for stats and tracing
AgraVator 5a6d988
census: introduces builder pattern for stats and tracing
AgraVator File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
/* | ||
* Copyright 2025 The gRPC Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.grpc.census; | ||
|
||
import com.google.common.base.Stopwatch; | ||
import com.google.common.base.Supplier; | ||
import io.grpc.ClientInterceptor; | ||
import io.grpc.ExperimentalApi; | ||
import io.grpc.ManagedChannelBuilder; | ||
import io.grpc.ServerBuilder; | ||
import io.grpc.ServerStreamTracer; | ||
import io.opencensus.trace.Tracing; | ||
|
||
/** | ||
* The entrypoint for OpenCensus instrumentation functionality in gRPC. | ||
* | ||
* <p>GrpcCensus uses {@link io.opencensus.api.OpenCensus} APIs for instrumentation. | ||
* | ||
*/ | ||
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/12178") | ||
public final class GrpcCensus { | ||
ejona86 marked this conversation as resolved.
Show resolved
Hide resolved
AgraVator marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
private final boolean statsEnabled; | ||
private final boolean tracingEnabled; | ||
|
||
private GrpcCensus(Builder builder) { | ||
this.statsEnabled = builder.statsEnabled; | ||
this.tracingEnabled = builder.tracingEnabled; | ||
} | ||
|
||
/** | ||
* Creates a new builder for {@link GrpcCensus}. | ||
*/ | ||
public static Builder newBuilder() { | ||
return new Builder(); | ||
} | ||
|
||
private static final Supplier<Stopwatch> STOPWATCH_SUPPLIER = new Supplier<Stopwatch>() { | ||
@Override | ||
public Stopwatch get() { | ||
return Stopwatch.createUnstarted(); | ||
} | ||
}; | ||
|
||
/** | ||
* Configures a {@link ServerBuilder} to enable census stats and tracing. | ||
* | ||
* @param serverBuilder The server builder to configure. | ||
* @return The configured server builder. | ||
*/ | ||
public <T extends ServerBuilder<T>> T configureServerBuilder(T serverBuilder) { | ||
if (statsEnabled) { | ||
serverBuilder.addStreamTracerFactory(newServerStatsStreamTracerFactory()); | ||
} | ||
if (tracingEnabled) { | ||
serverBuilder.addStreamTracerFactory(newServerTracingStreamTracerFactory()); | ||
} | ||
return serverBuilder; | ||
} | ||
|
||
/** | ||
* Configures a {@link ManagedChannelBuilder} to enable census stats and tracing. | ||
* | ||
* @param channelBuilder The channel builder to configure. | ||
* @return The configured channel builder. | ||
*/ | ||
public <T extends ManagedChannelBuilder<T>> T configureChannelBuilder(T channelBuilder) { | ||
if (statsEnabled) { | ||
channelBuilder.intercept(newClientStatsInterceptor()); | ||
} | ||
if (tracingEnabled) { | ||
channelBuilder.intercept(newClientTracingInterceptor()); | ||
} | ||
return channelBuilder; | ||
} | ||
|
||
/** | ||
* Returns a {@link ClientInterceptor} with default stats implementation. | ||
*/ | ||
private static ClientInterceptor newClientStatsInterceptor() { | ||
CensusStatsModule censusStats = | ||
new CensusStatsModule( | ||
STOPWATCH_SUPPLIER, | ||
true, | ||
true, | ||
true, | ||
false, | ||
true); | ||
return censusStats.getClientInterceptor(); | ||
} | ||
|
||
/** | ||
* Returns a {@link ClientInterceptor} with default tracing implementation. | ||
*/ | ||
private static ClientInterceptor newClientTracingInterceptor() { | ||
CensusTracingModule censusTracing = | ||
new CensusTracingModule( | ||
Tracing.getTracer(), | ||
Tracing.getPropagationComponent().getBinaryFormat()); | ||
return censusTracing.getClientInterceptor(); | ||
} | ||
|
||
/** | ||
* Returns a {@link ServerStreamTracer.Factory} with default stats implementation. | ||
*/ | ||
private static ServerStreamTracer.Factory newServerStatsStreamTracerFactory() { | ||
CensusStatsModule censusStats = | ||
new CensusStatsModule( | ||
STOPWATCH_SUPPLIER, | ||
true, | ||
true, | ||
true, | ||
false, | ||
true); | ||
return censusStats.getServerTracerFactory(); | ||
} | ||
|
||
/** | ||
* Returns a {@link ServerStreamTracer.Factory} with default tracing implementation. | ||
*/ | ||
private static ServerStreamTracer.Factory newServerTracingStreamTracerFactory() { | ||
CensusTracingModule censusTracing = | ||
new CensusTracingModule( | ||
Tracing.getTracer(), | ||
Tracing.getPropagationComponent().getBinaryFormat()); | ||
return censusTracing.getServerTracerFactory(); | ||
} | ||
|
||
/** | ||
* Builder for {@link GrpcCensus}. | ||
*/ | ||
public static final class Builder { | ||
private boolean statsEnabled = true; | ||
private boolean tracingEnabled = true; | ||
|
||
private Builder() { | ||
} | ||
|
||
/** | ||
* Disables stats collection. | ||
*/ | ||
public Builder disableStats() { | ||
this.statsEnabled = false; | ||
return this; | ||
} | ||
|
||
/** | ||
* Disables tracing. | ||
*/ | ||
public Builder disableTracing() { | ||
this.tracingEnabled = false; | ||
return this; | ||
} | ||
|
||
/** | ||
* Builds a new {@link GrpcCensus}. | ||
*/ | ||
public GrpcCensus build() { | ||
return new GrpcCensus(this); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.