core, census, testing, interop-testing, android-interop-testing: move census dependency out of grpc-core (#6577)

Decouples grpc-core with census, while still preserve the default integration of census in grpc-core. Users wishing to enable census needs to add grpc-census to their runtime classpath.

- Created a grpc-census module:
    - Moved CensusStatsModule.java and CensusTracingModule.java into grpc-census from grpc-core. CensusModuleTests.java is also moved. They now belong to io.grpc.census package.
Moved DeprecatedCensusConstants.java into io.grpc.census.internal (is this necessary?) in grpc-census.
    - Created CensusStatsAccessor.java and CensusTracingAccessor.java, which are used to create census ClientInterceptor and ServerStreamTracer.Factory.
    - Everything in grpc-census are package private, except the accessor classes. They only publicly expose ClientInterceptor and ServerStreamTracer.Factory, no Census specific types are exposed.

- Use runtime reflection to load and apply census stats/tracing to channel/server builders, if grpc-census is found in runtime classpath.

- Removed special APIs on AbstractManagedChannelImplBuilder and AbstractServerImplBuilder for overriding census module. They are only used for testing. Now we changed tests to apply Census ClientInterceptor and ServerStreamTracer.Factory just as normal interceptor/stream tracer factory. Test writer is responsible for taking care of the ordering concerns of interceptors and stream tracer factories.
This commit is contained in:
Chengyuan Zhang 2020-01-13 14:35:29 -08:00 committed by GitHub
parent 1cefe851e1
commit b7ccc0d142
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
29 changed files with 447 additions and 155 deletions

View File

@ -73,6 +73,7 @@ dependencies {
// You need to build grpc-java to obtain the grpc libraries below.
implementation 'io.grpc:grpc-auth:1.27.0-SNAPSHOT' // CURRENT_GRPC_VERSION
implementation 'io.grpc:grpc-census:1.27.0-SNAPSHOT' // CURRENT_GRPC_VERSION
implementation 'io.grpc:grpc-okhttp:1.27.0-SNAPSHOT' // CURRENT_GRPC_VERSION
implementation 'io.grpc:grpc-protobuf-lite:1.27.0-SNAPSHOT' // CURRENT_GRPC_VERSION
implementation 'io.grpc:grpc-stub:1.27.0-SNAPSHOT' // CURRENT_GRPC_VERSION

15
census/BUILD.bazel Normal file
View File

@ -0,0 +1,15 @@
java_library(
name = "census",
srcs = glob([
"src/main/java/**/*.java",
]),
visibility = ["//visibility:public"],
deps = [
"//api",
"//context",
"@com_google_code_findbugs_jsr305//jar",
"@com_google_guava_guava//jar",
"@io_opencensus_opencensus_api//jar",
"@io_opencensus_opencensus_contrib_grpc_metrics//jar",
],
)

43
census/build.gradle Normal file
View File

@ -0,0 +1,43 @@
plugins {
id "java"
id "maven-publish"
}
description = 'gRPC: Census'
evaluationDependsOn(project(':grpc-api').path)
dependencies {
compile project(':grpc-api')
compile (libraries.opencensus_api) {
// prefer 3.0.2 from libraries instead of 3.0.1
exclude group: 'com.google.code.findbugs', module: 'jsr305'
// prefer 20.0 from libraries instead of 19.0
exclude group: 'com.google.guava', module: 'guava'
// we'll always be more up-to-date
exclude group: 'io.grpc', module: 'grpc-context'
}
compile (libraries.opencensus_contrib_grpc_metrics) {
// prefer 3.0.2 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'
// prefer 20.0 from libraries instead of 19.0
exclude group: 'com.google.guava', module: 'guava'
}
testCompile project(':grpc-api').sourceSets.test.output,
project(':grpc-context').sourceSets.test.output,
project(':grpc-core').sourceSets.test.output,
project(':grpc-testing'),
libraries.guava_testlib,
libraries.opencensus_impl
}
javadoc {
failOnError false // no public or protected classes found to document
exclude 'io/grpc/census/internal/**'
exclude 'io/grpc/census/Internal*'
}

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
package io.grpc.internal;
package io.grpc.census;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
@ -35,6 +35,7 @@ import io.grpc.MethodDescriptor;
import io.grpc.ServerStreamTracer;
import io.grpc.Status;
import io.grpc.StreamTracer;
import io.grpc.census.internal.DeprecatedCensusConstants;
import io.opencensus.contrib.grpc.metrics.RpcMeasureConstants;
import io.opencensus.stats.Measure.MeasureDouble;
import io.opencensus.stats.Measure.MeasureLong;
@ -67,7 +68,7 @@ import javax.annotation.Nullable;
* starts earlier than the ServerCall. Therefore, only one tracer is created per stream/call and
* it's the tracer that reports the summary to Census.
*/
public final class CensusStatsModule {
final class CensusStatsModule {
private static final Logger logger = Logger.getLogger(CensusStatsModule.class.getName());
private static final double NANOS_PER_MILLI = TimeUnit.MILLISECONDS.toNanos(1);
@ -98,7 +99,7 @@ public final class CensusStatsModule {
/**
* Creates a {@link CensusStatsModule} with the given OpenCensus implementation.
*/
public CensusStatsModule(
CensusStatsModule(
final Tagger tagger,
final TagContextBinarySerializer tagCtxSerializer,
StatsRecorder statsRecorder, Supplier<Stopwatch> stopwatchSupplier,

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
package io.grpc.internal;
package io.grpc.census;
import static com.google.common.base.Preconditions.checkNotNull;

View File

@ -0,0 +1,117 @@
/*
* Copyright 2019 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.Internal;
import io.grpc.ServerStreamTracer;
import io.opencensus.stats.StatsRecorder;
import io.opencensus.tags.Tagger;
import io.opencensus.tags.propagation.TagContextBinarySerializer;
/**
* Accessor for getting {@link ClientInterceptor} or {@link ServerStreamTracer.Factory} with
* default Census stats implementation.
*/
@Internal
public final class InternalCensusStatsAccessor {
private static final Supplier<Stopwatch> STOPWATCH_SUPPLIER = new Supplier<Stopwatch>() {
@Override
public Stopwatch get() {
return Stopwatch.createUnstarted();
}
};
// Prevent instantiation.
private InternalCensusStatsAccessor() {
}
/**
* Returns a {@link ClientInterceptor} with default stats implementation.
*/
public static ClientInterceptor getClientInterceptor(
boolean recordStartedRpcs,
boolean recordFinishedRpcs,
boolean recordRealTimeMetrics) {
CensusStatsModule censusStats =
new CensusStatsModule(
STOPWATCH_SUPPLIER,
true, /* propagateTags */
recordStartedRpcs,
recordFinishedRpcs,
recordRealTimeMetrics);
return censusStats.getClientInterceptor();
}
/**
* Returns a {@link ClientInterceptor} with custom stats implementation.
*/
public static ClientInterceptor getClientInterceptor(
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);
return censusStats.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);
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);
return censusStats.getServerTracerFactory();
}
}

View File

@ -0,0 +1,56 @@
/*
* Copyright 2019 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 io.grpc.ClientInterceptor;
import io.grpc.Internal;
import io.grpc.ServerStreamTracer;
import io.opencensus.trace.Tracing;
/**
* Accessor for getting {@link ClientInterceptor} or {@link ServerStreamTracer.Factory} with
* default Census tracing implementation.
*/
@Internal
public final class InternalCensusTracingAccessor {
// Prevent instantiation.
private InternalCensusTracingAccessor() {
}
/**
* Returns a {@link ClientInterceptor} with default tracing implementation.
*/
public static ClientInterceptor getClientInterceptor() {
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() {
CensusTracingModule censusTracing =
new CensusTracingModule(
Tracing.getTracer(),
Tracing.getPropagationComponent().getBinaryFormat());
return censusTracing.getServerTracerFactory();
}
}

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
package io.grpc.internal;
package io.grpc.census.internal;
import com.google.common.annotations.VisibleForTesting;
import io.opencensus.contrib.grpc.metrics.RpcMeasureConstants;

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
package io.grpc.internal;
package io.grpc.census;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;
@ -56,7 +56,10 @@ import io.grpc.ServerCall;
import io.grpc.ServerCallHandler;
import io.grpc.ServerServiceDefinition;
import io.grpc.ServerStreamTracer;
import io.grpc.ServerStreamTracer.ServerCallInfo;
import io.grpc.Status;
import io.grpc.census.internal.DeprecatedCensusConstants;
import io.grpc.internal.FakeClock;
import io.grpc.internal.testing.StatsTestUtils;
import io.grpc.internal.testing.StatsTestUtils.FakeStatsRecorder;
import io.grpc.internal.testing.StatsTestUtils.FakeTagContextBinarySerializer;
@ -95,6 +98,7 @@ import java.util.List;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;
import javax.annotation.Nullable;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
@ -1044,7 +1048,7 @@ public class CensusModulesTest {
assertSame(spyServerSpan, ContextUtils.getValue(filteredContext));
serverStreamTracer.serverCallStarted(
new ServerCallInfoImpl<>(method, Attributes.EMPTY, null));
new CallInfo<>(method, Attributes.EMPTY, null));
verify(spyServerSpan, never()).end(any(EndSpanOptions.class));
@ -1087,7 +1091,7 @@ public class CensusModulesTest {
serverStreamTracer.filterContext(Context.ROOT);
serverStreamTracer.serverCallStarted(
new ServerCallInfoImpl<>(sampledMethod, Attributes.EMPTY, null));
new CallInfo<>(sampledMethod, Attributes.EMPTY, null));
serverStreamTracer.streamClosed(Status.CANCELLED);
@ -1250,8 +1254,39 @@ public class CensusModulesTest {
new Function<AggregationData, Long>() {
@Override
public Long apply(AggregationData arg) {
return ((io.opencensus.stats.AggregationData.MeanData) arg).getCount();
return ((AggregationData.MeanData) arg).getCount();
}
});
}
private static class CallInfo<ReqT, RespT> extends ServerCallInfo<ReqT, RespT> {
private final MethodDescriptor<ReqT, RespT> methodDescriptor;
private final Attributes attributes;
private final String authority;
CallInfo(
MethodDescriptor<ReqT, RespT> methodDescriptor,
Attributes attributes,
@Nullable String authority) {
this.methodDescriptor = methodDescriptor;
this.attributes = attributes;
this.authority = authority;
}
@Override
public MethodDescriptor<ReqT, RespT> getMethodDescriptor() {
return methodDescriptor;
}
@Override
public Attributes getAttributes() {
return attributes;
}
@Nullable
@Override
public String getAuthority() {
return authority;
}
}
}

View File

@ -38,8 +38,6 @@ java_library(
"@com_google_errorprone_error_prone_annotations//jar",
"@com_google_guava_guava//jar",
"@com_google_j2objc_j2objc_annotations//jar",
"@io_opencensus_opencensus_api//jar",
"@io_opencensus_opencensus_contrib_grpc_metrics//jar",
"@io_perfmark_perfmark_api//jar",
"@org_codehaus_mojo_animal_sniffer_annotations//jar",
],

View File

@ -20,25 +20,14 @@ dependencies {
compile (libraries.perfmark) {
exclude group: 'com.google.errorprone', module: 'error_prone_annotations'
}
compile (libraries.opencensus_api) {
// prefer our own versions instead of opencensus-api's dependency
exclude group: 'com.google.code.findbugs', module: 'jsr305'
exclude group: 'com.google.guava', module: 'guava'
exclude group: 'io.grpc', module: 'grpc-context'
}
compile (libraries.opencensus_contrib_grpc_metrics) {
// prefer our own versions instead of opencensus-contrib's dependency
exclude group: 'com.google.code.findbugs', module: 'jsr305'
exclude group: 'io.grpc', module: 'grpc-context'
exclude group: 'com.google.guava', module: 'guava'
}
testCompile project(':grpc-context').sourceSets.test.output,
project(':grpc-api').sourceSets.test.output,
project(':grpc-testing'),
project(':grpc-grpclb'),
libraries.guava_testlib,
libraries.opencensus_impl
libraries.guava_testlib
testRuntimeOnly project(':grpc-census')
jmh project(':grpc-testing')

View File

@ -33,7 +33,8 @@ import io.grpc.ManagedChannelBuilder;
import io.grpc.NameResolver;
import io.grpc.NameResolverRegistry;
import io.grpc.ProxyDetector;
import io.opencensus.trace.Tracing;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.SocketAddress;
import java.net.URI;
import java.net.URISyntaxException;
@ -45,6 +46,8 @@ import java.util.List;
import java.util.Map;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Nullable;
/**
@ -56,6 +59,9 @@ public abstract class AbstractManagedChannelImplBuilder
<T extends AbstractManagedChannelImplBuilder<T>> extends ManagedChannelBuilder<T> {
private static final String DIRECT_ADDRESS_SCHEME = "directaddress";
private static final Logger log =
Logger.getLogger(AbstractManagedChannelImplBuilder.class.getName());
public static ManagedChannelBuilder<?> forAddress(String name, int port) {
throw new UnsupportedOperationException("Subclass failed to hide static factory");
}
@ -184,9 +190,6 @@ public abstract class AbstractManagedChannelImplBuilder
private boolean recordRealTimeMetrics = false;
private boolean tracingEnabled = true;
@Nullable
private CensusStatsModule censusStatsOverride;
protected AbstractManagedChannelImplBuilder(String target) {
this.target = Preconditions.checkNotNull(target, "target");
this.directServerAddress = null;
@ -376,15 +379,6 @@ public abstract class AbstractManagedChannelImplBuilder
return thisT();
}
/**
* Override the default stats implementation.
*/
@VisibleForTesting
protected final T overrideCensusStatsModule(CensusStatsModule censusStats) {
this.censusStatsOverride = censusStats;
return thisT();
}
@Override
public T proxyDetector(@Nullable ProxyDetector proxyDetector) {
this.proxyDetector = proxyDetector;
@ -565,22 +559,49 @@ public abstract class AbstractManagedChannelImplBuilder
temporarilyDisableRetry = false;
if (statsEnabled) {
temporarilyDisableRetry = true;
CensusStatsModule censusStats = this.censusStatsOverride;
if (censusStats == null) {
censusStats = new CensusStatsModule(
GrpcUtil.STOPWATCH_SUPPLIER, true, recordStartedRpcs, recordFinishedRpcs,
recordRealTimeMetrics);
ClientInterceptor statsInterceptor = null;
try {
Class<?> censusStatsAccessor =
Class.forName("io.grpc.census.InternalCensusStatsAccessor");
Method getClientInterceptorMethod =
censusStatsAccessor.getDeclaredMethod(
"getClientInterceptor",
boolean.class,
boolean.class,
boolean.class);
statsInterceptor =
(ClientInterceptor) getClientInterceptorMethod
.invoke(
null,
recordStartedRpcs,
recordFinishedRpcs,
recordRealTimeMetrics);
} catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException
| InvocationTargetException e) {
log.log(Level.FINE, "Unable to apply census stats", e);
}
if (statsInterceptor != null) {
// First interceptor runs last (see ClientInterceptors.intercept()), so that no
// other interceptor can override the tracer factory we set in CallOptions.
effectiveInterceptors.add(0, statsInterceptor);
}
// 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) {
temporarilyDisableRetry = true;
CensusTracingModule censusTracing =
new CensusTracingModule(Tracing.getTracer(),
Tracing.getPropagationComponent().getBinaryFormat());
effectiveInterceptors.add(0, censusTracing.getClientInterceptor());
ClientInterceptor tracingInterceptor = null;
try {
Class<?> censusTracingAccessor =
Class.forName("io.grpc.census.InternalCensusTracingAccessor");
Method getClientInterceptroMethod =
censusTracingAccessor.getDeclaredMethod("getClientInterceptor");
tracingInterceptor = (ClientInterceptor) getClientInterceptroMethod.invoke(null);
} catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException
| InvocationTargetException e) {
log.log(Level.FINE, "Unable to apply census tracing", e);
}
if (tracingInterceptor != null) {
effectiveInterceptors.add(0, tracingInterceptor);
}
}
return effectiveInterceptors;
}

View File

@ -37,12 +37,15 @@ import io.grpc.ServerMethodDefinition;
import io.grpc.ServerServiceDefinition;
import io.grpc.ServerStreamTracer;
import io.grpc.ServerTransportFilter;
import io.opencensus.trace.Tracing;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Nullable;
/**
@ -53,6 +56,8 @@ import javax.annotation.Nullable;
public abstract class AbstractServerImplBuilder<T extends AbstractServerImplBuilder<T>>
extends ServerBuilder<T> {
private static final Logger log = Logger.getLogger(AbstractServerImplBuilder.class.getName());
public static ServerBuilder<?> forPort(int port) {
throw new UnsupportedOperationException("Subclass failed to hide static factory");
}
@ -80,7 +85,6 @@ public abstract class AbstractServerImplBuilder<T extends AbstractServerImplBuil
CompressorRegistry compressorRegistry = DEFAULT_COMPRESSOR_REGISTRY;
long handshakeTimeoutMillis = DEFAULT_HANDSHAKE_TIMEOUT_MILLIS;
Deadline.Ticker ticker = Deadline.getSystemTicker();
@Nullable private CensusStatsModule censusStatsOverride;
private boolean statsEnabled = true;
private boolean recordStartedRpcs = true;
private boolean recordFinishedRpcs = true;
@ -165,15 +169,6 @@ public abstract class AbstractServerImplBuilder<T extends AbstractServerImplBuil
return thisT();
}
/**
* Override the default stats implementation.
*/
@VisibleForTesting
protected final T overrideCensusStatsModule(@Nullable CensusStatsModule censusStats) {
this.censusStatsOverride = censusStats;
return thisT();
}
@VisibleForTesting
public final T setTransportTracerFactory(TransportTracer.Factory transportTracerFactory) {
this.transportTracerFactory = transportTracerFactory;
@ -241,19 +236,47 @@ public abstract class AbstractServerImplBuilder<T extends AbstractServerImplBuil
final List<? extends ServerStreamTracer.Factory> getTracerFactories() {
ArrayList<ServerStreamTracer.Factory> tracerFactories = new ArrayList<>();
if (statsEnabled) {
CensusStatsModule censusStats = censusStatsOverride;
if (censusStats == null) {
censusStats = new CensusStatsModule(
GrpcUtil.STOPWATCH_SUPPLIER, true, recordStartedRpcs, recordFinishedRpcs,
recordRealTimeMetrics);
ServerStreamTracer.Factory censusStatsTracerFactory = null;
try {
Class<?> censusStatsAccessor =
Class.forName("io.grpc.census.InternalCensusStatsAccessor");
Method getServerStreamTracerFactoryMethod =
censusStatsAccessor.getDeclaredMethod(
"getServerStreamTracerFactory",
boolean.class,
boolean.class,
boolean.class);
censusStatsTracerFactory =
(ServerStreamTracer.Factory) getServerStreamTracerFactoryMethod
.invoke(
null,
recordStartedRpcs,
recordFinishedRpcs,
recordRealTimeMetrics);
} catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException
| InvocationTargetException e) {
log.log(Level.FINE, "Unable to apply census stats", e);
}
if (censusStatsTracerFactory != null) {
tracerFactories.add(censusStatsTracerFactory);
}
tracerFactories.add(censusStats.getServerTracerFactory());
}
if (tracingEnabled) {
CensusTracingModule censusTracing =
new CensusTracingModule(Tracing.getTracer(),
Tracing.getPropagationComponent().getBinaryFormat());
tracerFactories.add(censusTracing.getServerTracerFactory());
ServerStreamTracer.Factory tracingStreamTracerFactory = null;
try {
Class<?> censusTracingAccessor =
Class.forName("io.grpc.census.InternalCensusTracingAccessor");
Method getServerStreamTracerFactoryMethod =
censusTracingAccessor.getDeclaredMethod("getServerStreamTracerFactory");
tracingStreamTracerFactory =
(ServerStreamTracer.Factory) getServerStreamTracerFactoryMethod.invoke(null);
} catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException
| InvocationTargetException e) {
log.log(Level.FINE, "Unable to apply census tracing", e);
}
if (tracingStreamTracerFactory != null) {
tracerFactories.add(tracingStreamTracerFactory);
}
}
tracerFactories.addAll(streamTracerFactories);
tracerFactories.trimToSize();

View File

@ -36,9 +36,6 @@ import io.grpc.CompressorRegistry;
import io.grpc.DecompressorRegistry;
import io.grpc.MethodDescriptor;
import io.grpc.NameResolver;
import io.grpc.internal.testing.StatsTestUtils.FakeStatsRecorder;
import io.grpc.internal.testing.StatsTestUtils.FakeTagContextBinarySerializer;
import io.grpc.internal.testing.StatsTestUtils.FakeTagger;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.net.URI;
@ -284,10 +281,10 @@ public class AbstractManagedChannelImplBuilderTest {
builder.intercept(DUMMY_USER_INTERCEPTOR);
List<ClientInterceptor> effectiveInterceptors = builder.getEffectiveInterceptors();
assertEquals(3, effectiveInterceptors.size());
assertThat(effectiveInterceptors.get(0))
.isInstanceOf(CensusTracingModule.TracingClientInterceptor.class);
assertThat(effectiveInterceptors.get(1))
.isInstanceOf(CensusStatsModule.StatsClientInterceptor.class);
assertThat(effectiveInterceptors.get(0).getClass().getName())
.isEqualTo("io.grpc.census.CensusTracingModule$TracingClientInterceptor");
assertThat(effectiveInterceptors.get(1).getClass().getName())
.isEqualTo("io.grpc.census.CensusStatsModule$StatsClientInterceptor");
assertThat(effectiveInterceptors.get(2)).isSameInstanceAs(DUMMY_USER_INTERCEPTOR);
}
@ -297,8 +294,8 @@ public class AbstractManagedChannelImplBuilderTest {
builder.setStatsEnabled(false);
List<ClientInterceptor> effectiveInterceptors = builder.getEffectiveInterceptors();
assertEquals(2, effectiveInterceptors.size());
assertThat(effectiveInterceptors.get(0))
.isInstanceOf(CensusTracingModule.TracingClientInterceptor.class);
assertThat(effectiveInterceptors.get(0).getClass().getName())
.isEqualTo("io.grpc.census.CensusTracingModule$TracingClientInterceptor");
assertThat(effectiveInterceptors.get(1)).isSameInstanceAs(DUMMY_USER_INTERCEPTOR);
}
@ -308,8 +305,8 @@ public class AbstractManagedChannelImplBuilderTest {
builder.setTracingEnabled(false);
List<ClientInterceptor> effectiveInterceptors = builder.getEffectiveInterceptors();
assertEquals(2, effectiveInterceptors.size());
assertThat(effectiveInterceptors.get(0))
.isInstanceOf(CensusStatsModule.StatsClientInterceptor.class);
assertThat(effectiveInterceptors.get(0).getClass().getName())
.isEqualTo("io.grpc.census.CensusStatsModule$StatsClientInterceptor");
assertThat(effectiveInterceptors.get(1)).isSameInstanceAs(DUMMY_USER_INTERCEPTOR);
}
@ -500,24 +497,10 @@ public class AbstractManagedChannelImplBuilderTest {
static class Builder extends AbstractManagedChannelImplBuilder<Builder> {
Builder(String target) {
super(target);
overrideCensusStatsModule(
new CensusStatsModule(
new FakeTagger(),
new FakeTagContextBinarySerializer(),
new FakeStatsRecorder(),
GrpcUtil.STOPWATCH_SUPPLIER,
true, true, true, true));
}
Builder(SocketAddress directServerAddress, String authority) {
super(directServerAddress, authority);
overrideCensusStatsModule(
new CensusStatsModule(
new FakeTagger(),
new FakeTagContextBinarySerializer(),
new FakeStatsRecorder(),
GrpcUtil.STOPWATCH_SUPPLIER,
true, true, true, true));
}
@Override

View File

@ -21,9 +21,6 @@ import static org.junit.Assert.assertEquals;
import io.grpc.Metadata;
import io.grpc.ServerStreamTracer;
import io.grpc.internal.testing.StatsTestUtils.FakeStatsRecorder;
import io.grpc.internal.testing.StatsTestUtils.FakeTagContextBinarySerializer;
import io.grpc.internal.testing.StatsTestUtils.FakeTagger;
import java.io.File;
import java.util.List;
import org.junit.Test;
@ -51,8 +48,10 @@ public class AbstractServerImplBuilderTest {
List<? extends ServerStreamTracer.Factory> factories = builder.getTracerFactories();
assertEquals(3, factories.size());
assertThat(factories.get(0)).isInstanceOf(CensusStatsModule.ServerTracerFactory.class);
assertThat(factories.get(1)).isInstanceOf(CensusTracingModule.ServerTracerFactory.class);
assertThat(factories.get(0).getClass().getName())
.isEqualTo("io.grpc.census.CensusStatsModule$ServerTracerFactory");
assertThat(factories.get(1).getClass().getName())
.isEqualTo("io.grpc.census.CensusTracingModule$ServerTracerFactory");
assertThat(factories.get(2)).isSameInstanceAs(DUMMY_USER_TRACER);
}
@ -64,7 +63,8 @@ public class AbstractServerImplBuilderTest {
List<? extends ServerStreamTracer.Factory> factories = builder.getTracerFactories();
assertEquals(2, factories.size());
assertThat(factories.get(0)).isInstanceOf(CensusTracingModule.ServerTracerFactory.class);
assertThat(factories.get(0).getClass().getName())
.isEqualTo("io.grpc.census.CensusTracingModule$ServerTracerFactory");
assertThat(factories.get(1)).isSameInstanceAs(DUMMY_USER_TRACER);
}
@ -76,7 +76,8 @@ public class AbstractServerImplBuilderTest {
List<? extends ServerStreamTracer.Factory> factories = builder.getTracerFactories();
assertEquals(2, factories.size());
assertThat(factories.get(0)).isInstanceOf(CensusStatsModule.ServerTracerFactory.class);
assertThat(factories.get(0).getClass().getName())
.isEqualTo("io.grpc.census.CensusStatsModule$ServerTracerFactory");
assertThat(factories.get(1)).isSameInstanceAs(DUMMY_USER_TRACER);
}
@ -90,15 +91,6 @@ public class AbstractServerImplBuilderTest {
}
static class Builder extends AbstractServerImplBuilder<Builder> {
Builder() {
overrideCensusStatsModule(
new CensusStatsModule(
new FakeTagger(),
new FakeTagContextBinarySerializer(),
new FakeStatsRecorder(),
GrpcUtil.STOPWATCH_SUPPLIER,
true, true, true, true));
}
@Override
protected List<io.grpc.internal.InternalServer> buildTransportServers(

View File

@ -18,6 +18,7 @@ evaluationDependsOn(project(':grpc-context').path)
dependencies {
compile project(':grpc-alts'),
project(':grpc-auth'),
project(':grpc-census'),
project(':grpc-core'),
project(':grpc-netty'),
project(':grpc-okhttp'),

View File

@ -61,9 +61,9 @@ import io.grpc.ServerStreamTracer;
import io.grpc.Status;
import io.grpc.StatusRuntimeException;
import io.grpc.auth.MoreCallCredentials;
import io.grpc.census.InternalCensusStatsAccessor;
import io.grpc.census.internal.DeprecatedCensusConstants;
import io.grpc.internal.AbstractServerImplBuilder;
import io.grpc.internal.CensusStatsModule;
import io.grpc.internal.DeprecatedCensusConstants;
import io.grpc.internal.GrpcUtil;
import io.grpc.internal.testing.StatsTestUtils;
import io.grpc.internal.testing.StatsTestUtils.FakeStatsRecorder;
@ -240,15 +240,15 @@ public abstract class AbstractInteropTest {
.addStreamTracerFactory(serverStreamTracerFactory);
if (builder instanceof AbstractServerImplBuilder) {
customCensusModulePresent = true;
ServerStreamTracer.Factory censusTracerFactory =
InternalCensusStatsAccessor
.getServerStreamTracerFactory(
tagger, tagContextBinarySerializer, serverStatsRecorder,
GrpcUtil.STOPWATCH_SUPPLIER,
true, true, true, false /* real-time metrics */);
AbstractServerImplBuilder<?> sb = (AbstractServerImplBuilder<?>) builder;
io.grpc.internal.TestingAccessor.setStatsImplementation(
sb,
new CensusStatsModule(
tagger,
tagContextBinarySerializer,
serverStatsRecorder,
GrpcUtil.STOPWATCH_SUPPLIER,
true, true, true, false /* real-time metrics */));
io.grpc.internal.TestingAccessor.setStatsEnabled(sb, false);
sb.addStreamTracerFactory(censusTracerFactory);
}
if (metricsExpected()) {
assertThat(builder).isInstanceOf(AbstractServerImplBuilder.class);
@ -353,10 +353,13 @@ public abstract class AbstractInteropTest {
return null;
}
protected final CensusStatsModule createClientCensusStatsModule() {
return new CensusStatsModule(
tagger, tagContextBinarySerializer, clientStatsRecorder, GrpcUtil.STOPWATCH_SUPPLIER,
true, true, true, false /* real-time metrics */);
protected final ClientInterceptor createCensusStatsClientInterceptor() {
return
InternalCensusStatsAccessor
.getClientInterceptor(
tagger, tagContextBinarySerializer, clientStatsRecorder,
GrpcUtil.STOPWATCH_SUPPLIER,
true, true, true, false /* real-time metrics */);
}
/**

View File

@ -450,9 +450,9 @@ public class TestServiceClient {
}
builder = okBuilder;
}
io.grpc.internal.TestingAccessor.setStatsImplementation(
builder, createClientCensusStatsModule());
return builder.build();
// Disable the default census stats interceptor, use testing interceptor instead.
io.grpc.internal.TestingAccessor.setStatsEnabled(builder, false);
return builder.intercept(createCensusStatsClientInterceptor()).build();
}
@Override

View File

@ -46,8 +46,8 @@ public class AutoWindowSizingOnTest extends AbstractInteropTest {
NettyChannelBuilder builder = NettyChannelBuilder.forAddress(getListenAddress())
.negotiationType(NegotiationType.PLAINTEXT)
.maxInboundMessageSize(AbstractInteropTest.MAX_MESSAGE_SIZE);
io.grpc.internal.TestingAccessor.setStatsImplementation(
builder, createClientCensusStatsModule());
return builder.build();
// Disable the default census stats interceptor, use testing interceptor instead.
io.grpc.internal.TestingAccessor.setStatsEnabled(builder, false);
return builder.intercept(createCensusStatsClientInterceptor()).build();
}
}

View File

@ -57,9 +57,9 @@ public class Http2NettyLocalChannelTest extends AbstractInteropTest {
.eventLoopGroup(eventLoopGroup)
.flowControlWindow(65 * 1024)
.maxInboundMessageSize(AbstractInteropTest.MAX_MESSAGE_SIZE);
io.grpc.internal.TestingAccessor.setStatsImplementation(
builder, createClientCensusStatsModule());
return builder.build();
// Disable the default census stats interceptor, use testing interceptor instead.
io.grpc.internal.TestingAccessor.setStatsEnabled(builder, false);
return builder.intercept(createCensusStatsClientInterceptor()).build();
}
@Override

View File

@ -71,9 +71,9 @@ public class Http2NettyTest extends AbstractInteropTest {
.trustManager(TestUtils.loadX509Cert("ca.pem"))
.ciphers(TestUtils.preferredTestCiphers(), SupportedCipherSuiteFilter.INSTANCE)
.build());
io.grpc.internal.TestingAccessor.setStatsImplementation(
builder, createClientCensusStatsModule());
return builder.build();
// Disable the default census stats interceptor, use testing interceptor instead.
io.grpc.internal.TestingAccessor.setStatsEnabled(builder, false);
return builder.intercept(createCensusStatsClientInterceptor()).build();
} catch (Exception ex) {
throw new RuntimeException(ex);
}

View File

@ -100,15 +100,15 @@ public class Http2OkHttpTest extends AbstractInteropTest {
.build())
.overrideAuthority(GrpcUtil.authorityFromHostAndPort(
TestUtils.TEST_SERVER_HOST, port));
io.grpc.internal.TestingAccessor.setStatsImplementation(
builder, createClientCensusStatsModule());
try {
builder.sslSocketFactory(TestUtils.newSslSocketFactoryForCa(Platform.get().getProvider(),
TestUtils.loadCert("ca.pem")));
} catch (Exception e) {
throw new RuntimeException(e);
}
return builder;
// Disable the default census stats interceptor, use testing interceptor instead.
io.grpc.internal.TestingAccessor.setStatsEnabled(builder, false);
return builder.intercept(createCensusStatsClientInterceptor());
}
@Test

View File

@ -38,9 +38,9 @@ public class InProcessTest extends AbstractInteropTest {
@Override
protected ManagedChannel createChannel() {
InProcessChannelBuilder builder = InProcessChannelBuilder.forName(SERVER_NAME);
io.grpc.internal.TestingAccessor.setStatsImplementation(
builder, createClientCensusStatsModule());
return builder.build();
// Disable the default census stats interceptor, use testing interceptor instead.
io.grpc.internal.TestingAccessor.setStatsEnabled(builder, false);
return builder.intercept(createCensusStatsClientInterceptor()).build();
}
@Override

View File

@ -165,9 +165,9 @@ public class TransportCompressionTest extends AbstractInteropTest {
}
})
.usePlaintext();
io.grpc.internal.TestingAccessor.setStatsImplementation(
builder, createClientCensusStatsModule());
return builder.build();
// Disable the default census stats interceptor, use testing interceptor instead.
io.grpc.internal.TestingAccessor.setStatsEnabled(builder, false);
return builder.intercept(createCensusStatsClientInterceptor()).build();
}
/**

View File

@ -70,6 +70,7 @@ IO_GRPC_GRPC_JAVA_OVERRIDE_TARGETS = {
"io.grpc:grpc-alts": "@io_grpc_grpc_java//alts",
"io.grpc:grpc-api": "@io_grpc_grpc_java//api",
"io.grpc:grpc-auth": "@io_grpc_grpc_java//auth",
"io.grpc:grpc-census": "@io_grpc_grpc_java//census",
"io.grpc:grpc-context": "@io_grpc_grpc_java//context",
"io.grpc:grpc-core": "@io_grpc_grpc_java//core:core_maven",
"io.grpc:grpc-grpclb": "@io_grpc_grpc_java//grpclb",

View File

@ -29,6 +29,7 @@ include ":grpc-api"
include ":grpc-core"
include ":grpc-context"
include ":grpc-stub"
include ":grpc-census"
include ":grpc-auth"
include ":grpc-okhttp"
include ":grpc-protobuf"
@ -51,6 +52,7 @@ project(':grpc-api').projectDir = "$rootDir/api" as File
project(':grpc-core').projectDir = "$rootDir/core" as File
project(':grpc-context').projectDir = "$rootDir/context" as File
project(':grpc-stub').projectDir = "$rootDir/stub" as File
project(':grpc-census').projectDir = "$rootDir/census" as File
project(':grpc-auth').projectDir = "$rootDir/auth" as File
project(':grpc-okhttp').projectDir = "$rootDir/okhttp" as File
project(':grpc-protobuf').projectDir = "$rootDir/protobuf" as File

View File

@ -31,5 +31,6 @@ java_library(
deps = [
"//api",
"//core:internal",
"@io_opencensus_opencensus_api//jar",
],
)

View File

@ -14,6 +14,15 @@ dependencies {
project(':grpc-stub'),
libraries.junit
compile (libraries.opencensus_api) {
// prefer 3.0.2 from libraries instead of 3.0.1
exclude group: 'com.google.code.findbugs', module: 'jsr305'
// prefer 20.0 from libraries instead of 19.0
exclude group: 'com.google.guava', module: 'guava'
// we'll always be more up-to-date
exclude group: 'io.grpc', module: 'grpc-context'
}
testCompile (libraries.mockito) {
// prefer our own versions instead of mockito's dependency
exclude group: 'org.hamcrest', module: 'hamcrest-core'

View File

@ -21,19 +21,20 @@ package io.grpc.internal;
*/
public final class TestingAccessor {
/**
* Sets a custom stats implementation for tests.
* Disable or enable client side census stats features.
*/
public static void setStatsImplementation(
AbstractManagedChannelImplBuilder<?> builder, CensusStatsModule censusStats) {
builder.overrideCensusStatsModule(censusStats);
public static void setStatsEnabled(
AbstractManagedChannelImplBuilder<?> builder, boolean statsEnabled) {
builder.setStatsEnabled(statsEnabled);
}
/**
* Sets a custom stats implementation for tests.
* Disable or enable server side census stats features.
*/
public static void setStatsImplementation(
AbstractServerImplBuilder<?> builder, CensusStatsModule censusStats) {
builder.overrideCensusStatsModule(censusStats);
public static void setStatsEnabled(
AbstractServerImplBuilder<?> builder,
boolean statsEnabled) {
builder.setStatsEnabled(statsEnabled);
}
private TestingAccessor() {