Skip to content

Commit b7ccc0d

Browse files
authored
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.
1 parent 1cefe85 commit b7ccc0d

29 files changed

+447
-155
lines changed

Diff for: android-interop-testing/app/build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ dependencies {
7373

7474
// You need to build grpc-java to obtain the grpc libraries below.
7575
implementation 'io.grpc:grpc-auth:1.27.0-SNAPSHOT' // CURRENT_GRPC_VERSION
76+
implementation 'io.grpc:grpc-census:1.27.0-SNAPSHOT' // CURRENT_GRPC_VERSION
7677
implementation 'io.grpc:grpc-okhttp:1.27.0-SNAPSHOT' // CURRENT_GRPC_VERSION
7778
implementation 'io.grpc:grpc-protobuf-lite:1.27.0-SNAPSHOT' // CURRENT_GRPC_VERSION
7879
implementation 'io.grpc:grpc-stub:1.27.0-SNAPSHOT' // CURRENT_GRPC_VERSION

Diff for: census/BUILD.bazel

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
java_library(
2+
name = "census",
3+
srcs = glob([
4+
"src/main/java/**/*.java",
5+
]),
6+
visibility = ["//visibility:public"],
7+
deps = [
8+
"//api",
9+
"//context",
10+
"@com_google_code_findbugs_jsr305//jar",
11+
"@com_google_guava_guava//jar",
12+
"@io_opencensus_opencensus_api//jar",
13+
"@io_opencensus_opencensus_contrib_grpc_metrics//jar",
14+
],
15+
)

Diff for: census/build.gradle

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
plugins {
2+
id "java"
3+
id "maven-publish"
4+
}
5+
6+
description = 'gRPC: Census'
7+
8+
evaluationDependsOn(project(':grpc-api').path)
9+
10+
dependencies {
11+
compile project(':grpc-api')
12+
13+
compile (libraries.opencensus_api) {
14+
// prefer 3.0.2 from libraries instead of 3.0.1
15+
exclude group: 'com.google.code.findbugs', module: 'jsr305'
16+
// prefer 20.0 from libraries instead of 19.0
17+
exclude group: 'com.google.guava', module: 'guava'
18+
// we'll always be more up-to-date
19+
exclude group: 'io.grpc', module: 'grpc-context'
20+
}
21+
22+
compile (libraries.opencensus_contrib_grpc_metrics) {
23+
// prefer 3.0.2 from libraries instead of 3.0.1
24+
exclude group: 'com.google.code.findbugs', module: 'jsr305'
25+
// we'll always be more up-to-date
26+
exclude group: 'io.grpc', module: 'grpc-context'
27+
// prefer 20.0 from libraries instead of 19.0
28+
exclude group: 'com.google.guava', module: 'guava'
29+
}
30+
31+
testCompile project(':grpc-api').sourceSets.test.output,
32+
project(':grpc-context').sourceSets.test.output,
33+
project(':grpc-core').sourceSets.test.output,
34+
project(':grpc-testing'),
35+
libraries.guava_testlib,
36+
libraries.opencensus_impl
37+
}
38+
39+
javadoc {
40+
failOnError false // no public or protected classes found to document
41+
exclude 'io/grpc/census/internal/**'
42+
exclude 'io/grpc/census/Internal*'
43+
}

Diff for: core/src/main/java/io/grpc/internal/CensusStatsModule.java renamed to census/src/main/java/io/grpc/census/CensusStatsModule.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package io.grpc.internal;
17+
package io.grpc.census;
1818

1919
import static com.google.common.base.Preconditions.checkNotNull;
2020
import static com.google.common.base.Preconditions.checkState;
@@ -35,6 +35,7 @@
3535
import io.grpc.ServerStreamTracer;
3636
import io.grpc.Status;
3737
import io.grpc.StreamTracer;
38+
import io.grpc.census.internal.DeprecatedCensusConstants;
3839
import io.opencensus.contrib.grpc.metrics.RpcMeasureConstants;
3940
import io.opencensus.stats.Measure.MeasureDouble;
4041
import io.opencensus.stats.Measure.MeasureLong;
@@ -67,7 +68,7 @@
6768
* starts earlier than the ServerCall. Therefore, only one tracer is created per stream/call and
6869
* it's the tracer that reports the summary to Census.
6970
*/
70-
public final class CensusStatsModule {
71+
final class CensusStatsModule {
7172
private static final Logger logger = Logger.getLogger(CensusStatsModule.class.getName());
7273
private static final double NANOS_PER_MILLI = TimeUnit.MILLISECONDS.toNanos(1);
7374

@@ -98,7 +99,7 @@ public final class CensusStatsModule {
9899
/**
99100
* Creates a {@link CensusStatsModule} with the given OpenCensus implementation.
100101
*/
101-
public CensusStatsModule(
102+
CensusStatsModule(
102103
final Tagger tagger,
103104
final TagContextBinarySerializer tagCtxSerializer,
104105
StatsRecorder statsRecorder, Supplier<Stopwatch> stopwatchSupplier,

Diff for: core/src/main/java/io/grpc/internal/CensusTracingModule.java renamed to census/src/main/java/io/grpc/census/CensusTracingModule.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package io.grpc.internal;
17+
package io.grpc.census;
1818

1919
import static com.google.common.base.Preconditions.checkNotNull;
2020

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*
2+
* Copyright 2019 The gRPC Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.grpc.census;
18+
19+
import com.google.common.base.Stopwatch;
20+
import com.google.common.base.Supplier;
21+
import io.grpc.ClientInterceptor;
22+
import io.grpc.Internal;
23+
import io.grpc.ServerStreamTracer;
24+
import io.opencensus.stats.StatsRecorder;
25+
import io.opencensus.tags.Tagger;
26+
import io.opencensus.tags.propagation.TagContextBinarySerializer;
27+
28+
/**
29+
* Accessor for getting {@link ClientInterceptor} or {@link ServerStreamTracer.Factory} with
30+
* default Census stats implementation.
31+
*/
32+
@Internal
33+
public final class InternalCensusStatsAccessor {
34+
35+
private static final Supplier<Stopwatch> STOPWATCH_SUPPLIER = new Supplier<Stopwatch>() {
36+
@Override
37+
public Stopwatch get() {
38+
return Stopwatch.createUnstarted();
39+
}
40+
};
41+
42+
// Prevent instantiation.
43+
private InternalCensusStatsAccessor() {
44+
}
45+
46+
/**
47+
* Returns a {@link ClientInterceptor} with default stats implementation.
48+
*/
49+
public static ClientInterceptor getClientInterceptor(
50+
boolean recordStartedRpcs,
51+
boolean recordFinishedRpcs,
52+
boolean recordRealTimeMetrics) {
53+
CensusStatsModule censusStats =
54+
new CensusStatsModule(
55+
STOPWATCH_SUPPLIER,
56+
true, /* propagateTags */
57+
recordStartedRpcs,
58+
recordFinishedRpcs,
59+
recordRealTimeMetrics);
60+
return censusStats.getClientInterceptor();
61+
}
62+
63+
/**
64+
* Returns a {@link ClientInterceptor} with custom stats implementation.
65+
*/
66+
public static ClientInterceptor getClientInterceptor(
67+
Tagger tagger,
68+
TagContextBinarySerializer tagCtxSerializer,
69+
StatsRecorder statsRecorder,
70+
Supplier<Stopwatch> stopwatchSupplier,
71+
boolean propagateTags,
72+
boolean recordStartedRpcs,
73+
boolean recordFinishedRpcs,
74+
boolean recordRealTimeMetrics) {
75+
CensusStatsModule censusStats =
76+
new CensusStatsModule(
77+
tagger, tagCtxSerializer, statsRecorder, stopwatchSupplier,
78+
propagateTags, recordStartedRpcs, recordFinishedRpcs, recordRealTimeMetrics);
79+
return censusStats.getClientInterceptor();
80+
}
81+
82+
/**
83+
* Returns a {@link ServerStreamTracer.Factory} with default stats implementation.
84+
*/
85+
public static ServerStreamTracer.Factory getServerStreamTracerFactory(
86+
boolean recordStartedRpcs,
87+
boolean recordFinishedRpcs,
88+
boolean recordRealTimeMetrics) {
89+
CensusStatsModule censusStats =
90+
new CensusStatsModule(
91+
STOPWATCH_SUPPLIER,
92+
true, /* propagateTags */
93+
recordStartedRpcs,
94+
recordFinishedRpcs,
95+
recordRealTimeMetrics);
96+
return censusStats.getServerTracerFactory();
97+
}
98+
99+
/**
100+
* Returns a {@link ServerStreamTracer.Factory} with custom stats implementation.
101+
*/
102+
public static ServerStreamTracer.Factory getServerStreamTracerFactory(
103+
Tagger tagger,
104+
TagContextBinarySerializer tagCtxSerializer,
105+
StatsRecorder statsRecorder,
106+
Supplier<Stopwatch> stopwatchSupplier,
107+
boolean propagateTags,
108+
boolean recordStartedRpcs,
109+
boolean recordFinishedRpcs,
110+
boolean recordRealTimeMetrics) {
111+
CensusStatsModule censusStats =
112+
new CensusStatsModule(
113+
tagger, tagCtxSerializer, statsRecorder, stopwatchSupplier,
114+
propagateTags, recordStartedRpcs, recordFinishedRpcs, recordRealTimeMetrics);
115+
return censusStats.getServerTracerFactory();
116+
}
117+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2019 The gRPC Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.grpc.census;
18+
19+
import io.grpc.ClientInterceptor;
20+
import io.grpc.Internal;
21+
import io.grpc.ServerStreamTracer;
22+
import io.opencensus.trace.Tracing;
23+
24+
/**
25+
* Accessor for getting {@link ClientInterceptor} or {@link ServerStreamTracer.Factory} with
26+
* default Census tracing implementation.
27+
*/
28+
@Internal
29+
public final class InternalCensusTracingAccessor {
30+
31+
// Prevent instantiation.
32+
private InternalCensusTracingAccessor() {
33+
}
34+
35+
/**
36+
* Returns a {@link ClientInterceptor} with default tracing implementation.
37+
*/
38+
public static ClientInterceptor getClientInterceptor() {
39+
CensusTracingModule censusTracing =
40+
new CensusTracingModule(
41+
Tracing.getTracer(),
42+
Tracing.getPropagationComponent().getBinaryFormat());
43+
return censusTracing.getClientInterceptor();
44+
}
45+
46+
/**
47+
* Returns a {@link ServerStreamTracer.Factory} with default stats implementation.
48+
*/
49+
public static ServerStreamTracer.Factory getServerStreamTracerFactory() {
50+
CensusTracingModule censusTracing =
51+
new CensusTracingModule(
52+
Tracing.getTracer(),
53+
Tracing.getPropagationComponent().getBinaryFormat());
54+
return censusTracing.getServerTracerFactory();
55+
}
56+
}

Diff for: core/src/main/java/io/grpc/internal/DeprecatedCensusConstants.java renamed to census/src/main/java/io/grpc/census/internal/DeprecatedCensusConstants.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package io.grpc.internal;
17+
package io.grpc.census.internal;
1818

1919
import com.google.common.annotations.VisibleForTesting;
2020
import io.opencensus.contrib.grpc.metrics.RpcMeasureConstants;

Diff for: core/src/test/java/io/grpc/internal/CensusModulesTest.java renamed to census/src/test/java/io/grpc/census/CensusModulesTest.java

+39-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package io.grpc.internal;
17+
package io.grpc.census;
1818

1919
import static com.google.common.truth.Truth.assertThat;
2020
import static com.google.common.truth.Truth.assertWithMessage;
@@ -56,7 +56,10 @@
5656
import io.grpc.ServerCallHandler;
5757
import io.grpc.ServerServiceDefinition;
5858
import io.grpc.ServerStreamTracer;
59+
import io.grpc.ServerStreamTracer.ServerCallInfo;
5960
import io.grpc.Status;
61+
import io.grpc.census.internal.DeprecatedCensusConstants;
62+
import io.grpc.internal.FakeClock;
6063
import io.grpc.internal.testing.StatsTestUtils;
6164
import io.grpc.internal.testing.StatsTestUtils.FakeStatsRecorder;
6265
import io.grpc.internal.testing.StatsTestUtils.FakeTagContextBinarySerializer;
@@ -95,6 +98,7 @@
9598
import java.util.Random;
9699
import java.util.Set;
97100
import java.util.concurrent.atomic.AtomicReference;
101+
import javax.annotation.Nullable;
98102
import org.junit.After;
99103
import org.junit.Before;
100104
import org.junit.Rule;
@@ -1044,7 +1048,7 @@ public void serverBasicTracingNoHeaders() {
10441048
assertSame(spyServerSpan, ContextUtils.getValue(filteredContext));
10451049

10461050
serverStreamTracer.serverCallStarted(
1047-
new ServerCallInfoImpl<>(method, Attributes.EMPTY, null));
1051+
new CallInfo<>(method, Attributes.EMPTY, null));
10481052

10491053
verify(spyServerSpan, never()).end(any(EndSpanOptions.class));
10501054

@@ -1087,7 +1091,7 @@ public void serverTracingSampledToLocalSpanStore() {
10871091
serverStreamTracer.filterContext(Context.ROOT);
10881092

10891093
serverStreamTracer.serverCallStarted(
1090-
new ServerCallInfoImpl<>(sampledMethod, Attributes.EMPTY, null));
1094+
new CallInfo<>(sampledMethod, Attributes.EMPTY, null));
10911095

10921096
serverStreamTracer.streamClosed(Status.CANCELLED);
10931097

@@ -1250,8 +1254,39 @@ public Long apply(LastValueDataLong arg) {
12501254
new Function<AggregationData, Long>() {
12511255
@Override
12521256
public Long apply(AggregationData arg) {
1253-
return ((io.opencensus.stats.AggregationData.MeanData) arg).getCount();
1257+
return ((AggregationData.MeanData) arg).getCount();
12541258
}
12551259
});
12561260
}
1261+
1262+
private static class CallInfo<ReqT, RespT> extends ServerCallInfo<ReqT, RespT> {
1263+
private final MethodDescriptor<ReqT, RespT> methodDescriptor;
1264+
private final Attributes attributes;
1265+
private final String authority;
1266+
1267+
CallInfo(
1268+
MethodDescriptor<ReqT, RespT> methodDescriptor,
1269+
Attributes attributes,
1270+
@Nullable String authority) {
1271+
this.methodDescriptor = methodDescriptor;
1272+
this.attributes = attributes;
1273+
this.authority = authority;
1274+
}
1275+
1276+
@Override
1277+
public MethodDescriptor<ReqT, RespT> getMethodDescriptor() {
1278+
return methodDescriptor;
1279+
}
1280+
1281+
@Override
1282+
public Attributes getAttributes() {
1283+
return attributes;
1284+
}
1285+
1286+
@Nullable
1287+
@Override
1288+
public String getAuthority() {
1289+
return authority;
1290+
}
1291+
}
12571292
}

Diff for: core/BUILD.bazel

-2
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ java_library(
3838
"@com_google_errorprone_error_prone_annotations//jar",
3939
"@com_google_guava_guava//jar",
4040
"@com_google_j2objc_j2objc_annotations//jar",
41-
"@io_opencensus_opencensus_api//jar",
42-
"@io_opencensus_opencensus_contrib_grpc_metrics//jar",
4341
"@io_perfmark_perfmark_api//jar",
4442
"@org_codehaus_mojo_animal_sniffer_annotations//jar",
4543
],

0 commit comments

Comments
 (0)