From 0d33c43e7d47c2b9921ee297e7bd8f25213213f7 Mon Sep 17 00:00:00 2001 From: AgraVator Date: Mon, 5 May 2025 19:21:16 +0530 Subject: [PATCH 1/3] census: expose client interceptors --- .../main/java/io/grpc/census/GrpcCensus.java | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 census/src/main/java/io/grpc/census/GrpcCensus.java diff --git a/census/src/main/java/io/grpc/census/GrpcCensus.java b/census/src/main/java/io/grpc/census/GrpcCensus.java new file mode 100644 index 00000000000..c24a46f58e6 --- /dev/null +++ b/census/src/main/java/io/grpc/census/GrpcCensus.java @@ -0,0 +1,87 @@ +/* + * 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.opencensus.stats.StatsRecorder; +import io.opencensus.tags.Tagger; +import io.opencensus.tags.propagation.TagContextBinarySerializer; +import io.opencensus.trace.Tracing; + +public final class GrpcCensus { + + private static final Supplier STOPWATCH_SUPPLIER = new Supplier() { + @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 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() { + CensusTracingModule censusTracing = + new CensusTracingModule( + Tracing.getTracer(), + Tracing.getPropagationComponent().getBinaryFormat()); + return censusTracing.getClientInterceptor(); + } + +} From 8176f861ba4dba8f2ae6cc91fa24c26bae93d79a Mon Sep 17 00:00:00 2001 From: AgraVator Date: Tue, 6 May 2025 10:53:23 +0530 Subject: [PATCH 2/3] census: expose server tracer factories --- .../main/java/io/grpc/census/GrpcCensus.java | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/census/src/main/java/io/grpc/census/GrpcCensus.java b/census/src/main/java/io/grpc/census/GrpcCensus.java index c24a46f58e6..d3852bc7b9c 100644 --- a/census/src/main/java/io/grpc/census/GrpcCensus.java +++ b/census/src/main/java/io/grpc/census/GrpcCensus.java @@ -19,6 +19,8 @@ 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; @@ -84,4 +86,63 @@ public static ClientInterceptor newClientTracingInterceptor() { return censusTracing.getClientInterceptor(); } + /** + * Returns a {@link ServerStreamTracer.Factory} with default stats implementation. + */ + public static ServerStreamTracer.Factory getServerStreamTracerFactory( + 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 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) { + serverBuilder.addStreamTracerFactory(serverStreamTracerFactory); + } + } From 5ad6f865ab2c28c60f2bed8e5f7610db34ed5817 Mon Sep 17 00:00:00 2001 From: AgraVator Date: Mon, 12 May 2025 17:06:09 +0530 Subject: [PATCH 3/3] census: use no arg methods for ClientInterceptors and StreamFactories --- .../main/java/io/grpc/census/GrpcCensus.java | 101 ++++++------------ .../io/grpc/internal/ServerImplBuilder.java | 2 +- 2 files changed, 34 insertions(+), 69 deletions(-) diff --git a/census/src/main/java/io/grpc/census/GrpcCensus.java b/census/src/main/java/io/grpc/census/GrpcCensus.java index d3852bc7b9c..0557f096400 100644 --- a/census/src/main/java/io/grpc/census/GrpcCensus.java +++ b/census/src/main/java/io/grpc/census/GrpcCensus.java @@ -21,13 +21,18 @@ 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; +/** + * The entrypoint for OpenCensus instrumentation functionality in gRPC. + * + *

GrpcCensus uses {@link io.opencensus.api.OpenCensus} APIs for instrumentation. + * + */ public final class GrpcCensus { + private GrpcCensus() {} + private static final Supplier STOPWATCH_SUPPLIER = new Supplier() { @Override public Stopwatch get() { @@ -38,40 +43,15 @@ public Stopwatch get() { /** * Returns a {@link ClientInterceptor} with default stats implementation. */ - public static ClientInterceptor newClientStatsInterceptor( - boolean recordStartedRpcs, - boolean recordFinishedRpcs, - boolean recordRealTimeMetrics, - boolean recordRetryMetrics) { + public static ClientInterceptor newClientStatsInterceptor() { 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 stopwatchSupplier, - boolean propagateTags, - boolean recordStartedRpcs, - boolean recordFinishedRpcs, - boolean recordRealTimeMetrics, - boolean recordRetryMetrics) { - CensusStatsModule censusStats = - new CensusStatsModule( - tagger, tagCtxSerializer, statsRecorder, stopwatchSupplier, - propagateTags, recordStartedRpcs, recordFinishedRpcs, recordRealTimeMetrics, - recordRetryMetrics); + true, + true, + true, + false, + true); return censusStats.getClientInterceptor(); } @@ -89,44 +69,22 @@ public static ClientInterceptor newClientTracingInterceptor() { /** * Returns a {@link ServerStreamTracer.Factory} with default stats implementation. */ - public static ServerStreamTracer.Factory getServerStreamTracerFactory( - boolean recordStartedRpcs, - boolean recordFinishedRpcs, - boolean recordRealTimeMetrics) { + private static ServerStreamTracer.Factory newServerStatsStreamTracerFactory() { 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 stopwatchSupplier, - boolean propagateTags, - boolean recordStartedRpcs, - boolean recordFinishedRpcs, - boolean recordRealTimeMetrics) { - CensusStatsModule censusStats = - new CensusStatsModule( - tagger, tagCtxSerializer, statsRecorder, stopwatchSupplier, - propagateTags, recordStartedRpcs, recordFinishedRpcs, recordRealTimeMetrics, false); + true, + true, + true, + false, + true); return censusStats.getServerTracerFactory(); } /** * Returns a {@link ServerStreamTracer.Factory} with default tracing implementation. */ - public static ServerStreamTracer.Factory getServerStreamTracerFactory() { + private static ServerStreamTracer.Factory newServerTracingStreamTracerFactory() { CensusTracingModule censusTracing = new CensusTracingModule( Tracing.getTracer(), @@ -135,14 +93,21 @@ public static ServerStreamTracer.Factory getServerStreamTracerFactory() { } /** - * Configures the given {@link ServerBuilder} with the provided serverStreamTracerFactory. + * 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 - * @param serverStreamTracerFactory the server stream tracer factory to configure */ - public static void configureServerBuilder(ServerBuilder serverBuilder, - ServerStreamTracer.Factory serverStreamTracerFactory) { - serverBuilder.addStreamTracerFactory(serverStreamTracerFactory); + public static void configureServerBuilderWithTracingTracer(ServerBuilder serverBuilder) { + serverBuilder.addStreamTracerFactory(newServerTracingStreamTracerFactory()); } } diff --git a/core/src/main/java/io/grpc/internal/ServerImplBuilder.java b/core/src/main/java/io/grpc/internal/ServerImplBuilder.java index b679baf3a8b..f6566e067db 100644 --- a/core/src/main/java/io/grpc/internal/ServerImplBuilder.java +++ b/core/src/main/java/io/grpc/internal/ServerImplBuilder.java @@ -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 {