Skip to content

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
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 113 additions & 0 deletions census/src/main/java/io/grpc/census/GrpcCensus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* 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.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.
*
*/
public final class GrpcCensus {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Javadoc

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add private constructor to prevent construction.


private GrpcCensus() {}

private static final Supplier<Stopwatch> STOPWATCH_SUPPLIER = new Supplier<Stopwatch>() {
@Override
public Stopwatch get() {
return Stopwatch.createUnstarted();
}
};

/**
* Returns a {@link ClientInterceptor} with default stats implementation.
*/
public 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.
*/
public 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();
}

/**
* Configures the given {@link ServerBuilder} with serverStreamTracerFactory for stats.
*
* @param serverBuilder the server builder to configure
*/
public static void configureServerBuilderWithStatsTracer(ServerBuilder<?> serverBuilder) {
serverBuilder.addStreamTracerFactory(newServerStatsStreamTracerFactory());
}

/**
* Configures the given {@link ServerBuilder} with serverStreamTracerFactory for tracing.
*
* @param serverBuilder the server builder to configure
*/
public static void configureServerBuilderWithTracingTracer(ServerBuilder<?> serverBuilder) {
serverBuilder.addStreamTracerFactory(newServerTracingStreamTracerFactory());
}

}
2 changes: 1 addition & 1 deletion core/src/main/java/io/grpc/internal/ServerImplBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public static ServerBuilder<?> forPort(int port) {
ServerCallExecutorSupplier executorSupplier;

/**
* An interface to provide to provide transport specific information for the server. This method
* An interface to provide transport specific information for the server. This method
* is meant for Transport implementors and should not be used by normal users.
*/
public interface ClientTransportServersBuilder {
Expand Down