Skip to content

Commit

Permalink
core: use new OpenCensus stats/tagging API. (grpc#3647)
Browse files Browse the repository at this point in the history
This commit updates gRPC core to use io.opencensus:opencensus-api and
io.opencensus:opencensus-contrib-grpc-metrics instead of
com.google.instrumentation:instrumentation-api for stats and tagging. The gRPC
Monitoring Service continues to use instrumentation-api.

The main changes affecting gRPC:

- The StatsContextFactory is replaced by three objects, StatsRecorder, Tagger,
  and TagContextBinarySerializer.

- The StatsRecorder, Tagger, and TagContextBinarySerializer are never null,
  but the objects are no-ops when the OpenCensus implementation is not
  available.

This commit includes changes written by @songy23 and @sebright.
  • Loading branch information
sebright authored and zhangkun83 committed Nov 9, 2017
1 parent 3936557 commit 362c370
Show file tree
Hide file tree
Showing 18 changed files with 596 additions and 445 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ subprojects {
okhttp: 'com.squareup.okhttp:okhttp:2.5.0',
okio: 'com.squareup.okio:okio:1.6.0',
opencensus_api: 'io.opencensus:opencensus-api:0.8.0',
opencensus_contrib_grpc_metrics: 'io.opencensus:opencensus-contrib-grpc-metrics:0.8.0',
opencensus_impl: 'io.opencensus:opencensus-impl:0.8.0',
instrumentation_api: 'com.google.instrumentation:instrumentation-api:0.4.3',
protobuf: "com.google.protobuf:protobuf-java:${protobufVersion}",
Expand Down
6 changes: 6 additions & 0 deletions core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ dependencies {
// we'll always be more up-to-date
exclude group: 'io.grpc', module: 'grpc-context'
}
compile (libraries.opencensus_contrib_grpc_metrics) {
// prefer 3.0.0 from libraries instead of 3.0.1
exclude group: 'com.google.code.findbugs', module: 'jsr305'
// we'll always be more up-to-date
exclude group: 'io.grpc', module: 'grpc-context'
}

testCompile project(':grpc-testing')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import com.google.common.util.concurrent.MoreExecutors;
import com.google.instrumentation.stats.Stats;
import com.google.instrumentation.stats.StatsContextFactory;
import io.grpc.Attributes;
import io.grpc.ClientInterceptor;
import io.grpc.CompressorRegistry;
Expand Down Expand Up @@ -152,7 +150,7 @@ protected final int maxInboundMessageSize() {
private boolean tracingEnabled = true;

@Nullable
private StatsContextFactory statsFactory;
private CensusStatsModule censusStatsOverride;

protected AbstractManagedChannelImplBuilder(String target) {
this.target = Preconditions.checkNotNull(target, "target");
Expand Down Expand Up @@ -287,8 +285,8 @@ public final T idleTimeout(long value, TimeUnit unit) {
* Override the default stats implementation.
*/
@VisibleForTesting
protected final T statsContextFactory(StatsContextFactory statsFactory) {
this.statsFactory = statsFactory;
protected final T overrideCensusStatsModule(CensusStatsModule censusStats) {
this.censusStatsOverride = censusStats;
return thisT();
}

Expand Down Expand Up @@ -346,15 +344,13 @@ final List<ClientInterceptor> getEffectiveInterceptors() {
List<ClientInterceptor> effectiveInterceptors =
new ArrayList<ClientInterceptor>(this.interceptors);
if (statsEnabled) {
StatsContextFactory statsCtxFactory =
this.statsFactory != null ? this.statsFactory : Stats.getStatsContextFactory();
if (statsCtxFactory != null) {
CensusStatsModule censusStats =
new CensusStatsModule(statsCtxFactory, GrpcUtil.STOPWATCH_SUPPLIER, true, recordStats);
// First interceptor runs last (see ClientInterceptors.intercept()), so that no
// other interceptor can override the tracer factory we set in CallOptions.
effectiveInterceptors.add(0, censusStats.getClientInterceptor());
CensusStatsModule censusStats = this.censusStatsOverride;
if (censusStats == null) {
censusStats = new CensusStatsModule(GrpcUtil.STOPWATCH_SUPPLIER, true);
}
// First interceptor runs last (see ClientInterceptors.intercept()), so that no
// other interceptor can override the tracer factory we set in CallOptions.
effectiveInterceptors.add(0, censusStats.getClientInterceptor(recordStats));
}
if (tracingEnabled) {
CensusTracingModule censusTracing =
Expand Down
18 changes: 7 additions & 11 deletions core/src/main/java/io/grpc/internal/AbstractServerImplBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@

import com.google.common.annotations.VisibleForTesting;
import com.google.common.util.concurrent.MoreExecutors;
import com.google.instrumentation.stats.Stats;
import com.google.instrumentation.stats.StatsContextFactory;
import io.grpc.BindableService;
import io.grpc.CompressorRegistry;
import io.grpc.Context;
Expand Down Expand Up @@ -97,7 +95,7 @@ public List<ServerServiceDefinition> getServices() {
CompressorRegistry compressorRegistry = DEFAULT_COMPRESSOR_REGISTRY;

@Nullable
private StatsContextFactory statsFactory;
private CensusStatsModule censusStatsOverride;

private boolean statsEnabled = true;
private boolean recordStats = true;
Expand Down Expand Up @@ -184,8 +182,8 @@ public final T compressorRegistry(CompressorRegistry registry) {
* Override the default stats implementation.
*/
@VisibleForTesting
protected T statsContextFactory(StatsContextFactory statsFactory) {
this.statsFactory = statsFactory;
protected T overrideCensusStatsModule(CensusStatsModule censusStats) {
this.censusStatsOverride = censusStats;
return thisT();
}

Expand Down Expand Up @@ -228,13 +226,11 @@ final List<ServerStreamTracer.Factory> getTracerFactories() {
ArrayList<ServerStreamTracer.Factory> tracerFactories =
new ArrayList<ServerStreamTracer.Factory>();
if (statsEnabled) {
StatsContextFactory statsFactory =
this.statsFactory != null ? this.statsFactory : Stats.getStatsContextFactory();
if (statsFactory != null) {
CensusStatsModule censusStats =
new CensusStatsModule(statsFactory, GrpcUtil.STOPWATCH_SUPPLIER, true, recordStats);
tracerFactories.add(censusStats.getServerTracerFactory());
CensusStatsModule censusStats = this.censusStatsOverride;
if (censusStats == null) {
censusStats = new CensusStatsModule(GrpcUtil.STOPWATCH_SUPPLIER, true);
}
tracerFactories.add(censusStats.getServerTracerFactory(recordStats));
}
if (tracingEnabled) {
CensusTracingModule censusTracing =
Expand Down
Loading

0 comments on commit 362c370

Please sign in to comment.