Skip to content

core, census: move census dependency out of grpc-core #6577

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 24 commits into from
Jan 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
1724783
Created grpc-census package and moved census stats and tracing module…
voidzcy Dec 27, 2019
dd11d99
Replaced overrideCensusStats with setCensusClientInterceptor and setC…
voidzcy Dec 27, 2019
d6edad4
Use reflection to access census modules in channel/server builder.
voidzcy Dec 28, 2019
192705c
Add overloading methods for creating client interceptor/server stream…
voidzcy Dec 28, 2019
6d59e66
Expose some internal types for testing.
voidzcy Dec 28, 2019
5f8bb19
Remove census modules in grpc-core and use grpc-census for testing.
voidzcy Dec 28, 2019
d9cfe78
Remove census dependencies from build files, delete census constants …
voidzcy Dec 28, 2019
7ff29e7
Clean up build files.
voidzcy Dec 28, 2019
9f368ab
Fix Javadoc configuration for census module.
voidzcy Jan 3, 2020
30bb278
Add @Internal annotation for census stats/tracing modules.
voidzcy Jan 3, 2020
d407590
Add missing final for class definition.
voidzcy Jan 3, 2020
49ad4d1
Revert whitespace change.
voidzcy Jan 3, 2020
2a6f789
Merge branch 'master' of github.com:grpc/grpc-java into refactor/move…
voidzcy Jan 6, 2020
b9a61cd
Eliminate hard-coded arguments for creating Census module via accessors.
voidzcy Jan 9, 2020
342474c
Remove direct dependency on Census module classes, make them package-…
voidzcy Jan 9, 2020
1d035d9
Remove special APIs on channel/server builder for overriding census i…
voidzcy Jan 9, 2020
c549c8d
Minor styling fixes.
voidzcy Jan 9, 2020
35ebeda
Revert making ServerCallInfoImpl public. Instead, create a simple in-…
voidzcy Jan 9, 2020
42084bf
Fix bazel dependencies for grpc-testing.
voidzcy Jan 9, 2020
03db456
Suppress error for no public classes.
voidzcy Jan 9, 2020
0dff1ac
Disable default census stats tracer factory on server side and use te…
voidzcy Jan 9, 2020
fc37246
Add generic type for ServerCallInfo impl.
voidzcy Jan 9, 2020
fdf94bf
Add grpc-census dependency for android-interop-testing.
voidzcy Jan 10, 2020
ba6d40d
Add logging for failures in loading runtime methods.
voidzcy Jan 13, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions android-interop-testing/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions census/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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 changes: 43 additions & 0 deletions census/build.gradle
Original file line number Diff line number Diff line change
@@ -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*'
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -35,6 +35,7 @@
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;
Expand Down Expand Up @@ -67,7 +68,7 @@
* 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);

Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package io.grpc.internal;
package io.grpc.census;

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

Expand Down
117 changes: 117 additions & 0 deletions census/src/main/java/io/grpc/census/InternalCensusStatsAccessor.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -56,7 +56,10 @@
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;
Expand Down Expand Up @@ -95,6 +98,7 @@
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;
Expand Down Expand Up @@ -1044,7 +1048,7 @@ public void serverBasicTracingNoHeaders() {
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));

Expand Down Expand Up @@ -1087,7 +1091,7 @@ public void serverTracingSampledToLocalSpanStore() {
serverStreamTracer.filterContext(Context.ROOT);

serverStreamTracer.serverCallStarted(
new ServerCallInfoImpl<>(sampledMethod, Attributes.EMPTY, null));
new CallInfo<>(sampledMethod, Attributes.EMPTY, null));

serverStreamTracer.streamClosed(Status.CANCELLED);

Expand Down Expand Up @@ -1250,8 +1254,39 @@ public Long apply(LastValueDataLong arg) {
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;
}
}
}
2 changes: 0 additions & 2 deletions core/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
Expand Down
Loading