-
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
base: master
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
/* | ||
* 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.stats.StatsRecorder; | ||
import io.opencensus.tags.Tagger; | ||
import io.opencensus.tags.propagation.TagContextBinarySerializer; | ||
import io.opencensus.trace.Tracing; | ||
|
||
public final class GrpcCensus { | ||
ejona86 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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( | ||
boolean recordStartedRpcs, | ||
boolean recordFinishedRpcs, | ||
boolean recordRealTimeMetrics, | ||
boolean recordRetryMetrics) { | ||
CensusStatsModule censusStats = | ||
new CensusStatsModule( | ||
STOPWATCH_SUPPLIER, | ||
true, /* propagateTags */ | ||
recordStartedRpcs, | ||
recordFinishedRpcs, | ||
recordRealTimeMetrics, | ||
recordRetryMetrics); | ||
return censusStats.getClientInterceptor(); | ||
} | ||
|
||
/** | ||
* Returns a {@link ClientInterceptor} with custom stats implementation. | ||
*/ | ||
public static ClientInterceptor newClientStatsInterceptor( | ||
Tagger tagger, | ||
TagContextBinarySerializer tagCtxSerializer, | ||
StatsRecorder statsRecorder, | ||
Supplier<Stopwatch> stopwatchSupplier, | ||
boolean propagateTags, | ||
boolean recordStartedRpcs, | ||
boolean recordFinishedRpcs, | ||
boolean recordRealTimeMetrics, | ||
boolean recordRetryMetrics) { | ||
CensusStatsModule censusStats = | ||
new CensusStatsModule( | ||
tagger, tagCtxSerializer, statsRecorder, stopwatchSupplier, | ||
propagateTags, recordStartedRpcs, recordFinishedRpcs, recordRealTimeMetrics, | ||
recordRetryMetrics); | ||
return censusStats.getClientInterceptor(); | ||
} | ||
|
||
/** | ||
* Returns a {@link ClientInterceptor} with default tracing implementation. | ||
*/ | ||
public static ClientInterceptor newClientTracingInterceptor() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seeing all the statics is making me see why to more closely mirror GrpcOpenTelemetry. This could be fine as-is, because we don't care about it too much, but there do seem to be advantages in the GrpcOpenTelemetry API in clarity and letting us add options when needed. If we had only There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 |
||
CensusTracingModule censusTracing = | ||
new CensusTracingModule( | ||
Tracing.getTracer(), | ||
Tracing.getPropagationComponent().getBinaryFormat()); | ||
return censusTracing.getClientInterceptor(); | ||
} | ||
|
||
/** | ||
* Returns a {@link ServerStreamTracer.Factory} with default stats implementation. | ||
*/ | ||
public static ServerStreamTracer.Factory getServerStreamTracerFactory( | ||
ejona86 marked this conversation as resolved.
Show resolved
Hide resolved
ejona86 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
boolean recordStartedRpcs, | ||
boolean recordFinishedRpcs, | ||
boolean recordRealTimeMetrics) { | ||
CensusStatsModule censusStats = | ||
new CensusStatsModule( | ||
STOPWATCH_SUPPLIER, | ||
true, /* propagateTags */ | ||
recordStartedRpcs, | ||
recordFinishedRpcs, | ||
recordRealTimeMetrics, | ||
false); | ||
return censusStats.getServerTracerFactory(); | ||
} | ||
|
||
/** | ||
* Returns a {@link ServerStreamTracer.Factory} with custom stats implementation. | ||
*/ | ||
public static ServerStreamTracer.Factory getServerStreamTracerFactory( | ||
Tagger tagger, | ||
TagContextBinarySerializer tagCtxSerializer, | ||
StatsRecorder statsRecorder, | ||
Supplier<Stopwatch> stopwatchSupplier, | ||
boolean propagateTags, | ||
boolean recordStartedRpcs, | ||
boolean recordFinishedRpcs, | ||
boolean recordRealTimeMetrics) { | ||
CensusStatsModule censusStats = | ||
new CensusStatsModule( | ||
tagger, tagCtxSerializer, statsRecorder, stopwatchSupplier, | ||
propagateTags, recordStartedRpcs, recordFinishedRpcs, recordRealTimeMetrics, false); | ||
return censusStats.getServerTracerFactory(); | ||
} | ||
|
||
/** | ||
* Returns a {@link ServerStreamTracer.Factory} with default tracing implementation. | ||
*/ | ||
public static ServerStreamTracer.Factory getServerStreamTracerFactory() { | ||
CensusTracingModule censusTracing = | ||
new CensusTracingModule( | ||
Tracing.getTracer(), | ||
Tracing.getPropagationComponent().getBinaryFormat()); | ||
return censusTracing.getServerTracerFactory(); | ||
} | ||
|
||
/** | ||
* Configures the given {@link ServerBuilder} with the provided serverStreamTracerFactory. | ||
* | ||
* @param serverBuilder the server builder to configure | ||
* @param serverStreamTracerFactory the server stream tracer factory to configure | ||
*/ | ||
public static void configureServerBuilder(ServerBuilder<?> serverBuilder, | ||
ServerStreamTracer.Factory serverStreamTracerFactory) { | ||
ejona86 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
serverBuilder.addStreamTracerFactory(serverStreamTracerFactory); | ||
} | ||
|
||
} |
Uh oh!
There was an error while loading. Please reload this page.