Skip to content

Commit

Permalink
core: use new OpenCensus stats/tagging API.
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 committed Nov 1, 2017
1 parent 80a8c8f commit 1c52536
Show file tree
Hide file tree
Showing 19 changed files with 690 additions and 401 deletions.
5 changes: 3 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,9 @@ subprojects {
google_auth_credentials: 'com.google.auth:google-auth-library-credentials:0.4.0',
okhttp: 'com.squareup.okhttp:okhttp:2.5.0',
okio: 'com.squareup.okio:okio:1.6.0',
opencensus_api: 'io.opencensus:opencensus-api:0.7.0',
opencensus_impl: 'io.opencensus:opencensus-impl:0.7.0',
opencensus_api: 'io.opencensus:opencensus-api:0.8.0-SNAPSHOT',
opencensus_contrib_grpc_metrics: 'io.opencensus:opencensus-contrib-grpc-metrics:0.8.0-SNAPSHOT',
opencensus_impl: 'io.opencensus:opencensus-impl:0.8.0-SNAPSHOT',
instrumentation_api: 'com.google.instrumentation:instrumentation-api:0.4.3',
protobuf: "com.google.protobuf:protobuf-java:${protobufVersion}",
protobuf_lite: "com.google.protobuf:protobuf-lite:3.0.1",
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 All @@ -34,6 +32,11 @@
import io.grpc.NameResolver;
import io.grpc.NameResolverProvider;
import io.grpc.PickFirstBalancerFactory;
import io.opencensus.stats.Stats;
import io.opencensus.stats.StatsRecorder;
import io.opencensus.tags.Tagger;
import io.opencensus.tags.Tags;
import io.opencensus.tags.propagation.TagContextBinarySerializer;
import io.opencensus.trace.Tracing;
import java.net.SocketAddress;
import java.net.URI;
Expand Down Expand Up @@ -152,7 +155,13 @@ protected final int maxInboundMessageSize() {
private boolean tracingEnabled = true;

@Nullable
private StatsContextFactory statsFactory;
private Tagger tagger;

@Nullable
private TagContextBinarySerializer tagCtxSerializer;

@Nullable
private StatsRecorder statsRecorder;

protected AbstractManagedChannelImplBuilder(String target) {
this.target = Preconditions.checkNotNull(target, "target");
Expand Down Expand Up @@ -287,8 +296,11 @@ 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 statsImplementation(
Tagger tagger, TagContextBinarySerializer tagCtxSerializer, StatsRecorder statsRecorder) {
this.tagger = tagger;
this.tagCtxSerializer = tagCtxSerializer;
this.statsRecorder = statsRecorder;
return thisT();
}

Expand Down Expand Up @@ -346,15 +358,28 @@ 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());
}
Tagger tagger = this.tagger != null ? this.tagger : Tags.getTagger();
TagContextBinarySerializer tagCtxSerializer =
this.tagCtxSerializer != null
? this.tagCtxSerializer
: Tags.getTagPropagationComponent().getBinarySerializer();
StatsRecorder statsRecorder =
this.statsRecorder != null ? this.statsRecorder : Stats.getStatsRecorder();
// // TODO: How do we check whether stats is enabled, now that the StatsRecorder is always
// // non-null? Uncommenting this line causes test failures.
// if (Stats.getState() == StatsCollectionState.ENABLED) {
CensusStatsModule censusStats =
new CensusStatsModule(
tagger,
tagCtxSerializer,
statsRecorder,
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());
// }
}
if (tracingEnabled) {
CensusTracingModule censusTracing =
Expand Down
51 changes: 39 additions & 12 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 All @@ -36,6 +34,11 @@
import io.grpc.ServerServiceDefinition;
import io.grpc.ServerStreamTracer;
import io.grpc.ServerTransportFilter;
import io.opencensus.stats.Stats;
import io.opencensus.stats.StatsRecorder;
import io.opencensus.tags.Tagger;
import io.opencensus.tags.Tags;
import io.opencensus.tags.propagation.TagContextBinarySerializer;
import io.opencensus.trace.Tracing;
import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -97,7 +100,13 @@ public List<ServerServiceDefinition> getServices() {
CompressorRegistry compressorRegistry = DEFAULT_COMPRESSOR_REGISTRY;

@Nullable
private StatsContextFactory statsFactory;
private Tagger tagger;

@Nullable
private TagContextBinarySerializer tagCtxSerializer;

@Nullable
private StatsRecorder statsRecorder;

private boolean statsEnabled = true;
private boolean recordStats = true;
Expand Down Expand Up @@ -184,8 +193,13 @@ public final T compressorRegistry(CompressorRegistry registry) {
* Override the default stats implementation.
*/
@VisibleForTesting
protected T statsContextFactory(StatsContextFactory statsFactory) {
this.statsFactory = statsFactory;
protected T statsImplementation(
final Tagger tagger,
TagContextBinarySerializer tagCtxSerializer,
StatsRecorder statsRecorder) {
this.tagger = tagger;
this.tagCtxSerializer = tagCtxSerializer;
this.statsRecorder = statsRecorder;
return thisT();
}

Expand Down Expand Up @@ -228,13 +242,26 @@ 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());
}
Tagger tagger = this.tagger != null ? this.tagger : Tags.getTagger();
TagContextBinarySerializer tagCtxSerializer =
this.tagCtxSerializer != null
? this.tagCtxSerializer
: Tags.getTagPropagationComponent().getBinarySerializer();
StatsRecorder statsRecorder =
this.statsRecorder != null ? this.statsRecorder : Stats.getStatsRecorder();
// // TODO: How do we check whether stats is enabled, now that the StatsRecorder is always
// // non-null? Uncommenting this line causes test failures.
// if (Stats.getState() == StatsCollectionState.ENABLED) {
CensusStatsModule censusStats =
new CensusStatsModule(
tagger,
tagCtxSerializer,
statsRecorder,
GrpcUtil.STOPWATCH_SUPPLIER,
true,
recordStats);
tracerFactories.add(censusStats.getServerTracerFactory());
// }
}
if (tracingEnabled) {
CensusTracingModule censusTracing =
Expand Down
Loading

0 comments on commit 1c52536

Please sign in to comment.