core: set sampled for local span per MethodDescriptor. (#3627)

This moves away from the global String-based Span name registry which
is not as flexible as we desire.

Also renamed the option name to be more accurate.  This is not
API-breaking because the origianl addition to MethodDescriptor and
code-gen didn't make it into the 1.7.0 release.
This commit is contained in:
Kun Zhang 2017-11-01 16:46:05 -07:00 committed by GitHub
parent 253a01461b
commit d87ef74082
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
28 changed files with 238 additions and 231 deletions

View File

@ -47,7 +47,7 @@ public final class BenchmarkServiceGrpc {
.setType(io.grpc.MethodDescriptor.MethodType.UNARY)
.setFullMethodName(generateFullMethodName(
"grpc.testing.BenchmarkService", "UnaryCall"))
.setRegisterForTracing(true)
.setSampledToLocalTracing(true)
.setRequestMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
io.grpc.benchmarks.proto.Messages.SimpleRequest.getDefaultInstance()))
.setResponseMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
@ -79,7 +79,7 @@ public final class BenchmarkServiceGrpc {
.setType(io.grpc.MethodDescriptor.MethodType.BIDI_STREAMING)
.setFullMethodName(generateFullMethodName(
"grpc.testing.BenchmarkService", "StreamingCall"))
.setRegisterForTracing(true)
.setSampledToLocalTracing(true)
.setRequestMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
io.grpc.benchmarks.proto.Messages.SimpleRequest.getDefaultInstance()))
.setResponseMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
@ -111,7 +111,7 @@ public final class BenchmarkServiceGrpc {
.setType(io.grpc.MethodDescriptor.MethodType.CLIENT_STREAMING)
.setFullMethodName(generateFullMethodName(
"grpc.testing.BenchmarkService", "StreamingFromClient"))
.setRegisterForTracing(true)
.setSampledToLocalTracing(true)
.setRequestMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
io.grpc.benchmarks.proto.Messages.SimpleRequest.getDefaultInstance()))
.setResponseMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
@ -143,7 +143,7 @@ public final class BenchmarkServiceGrpc {
.setType(io.grpc.MethodDescriptor.MethodType.SERVER_STREAMING)
.setFullMethodName(generateFullMethodName(
"grpc.testing.BenchmarkService", "StreamingFromServer"))
.setRegisterForTracing(true)
.setSampledToLocalTracing(true)
.setRequestMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
io.grpc.benchmarks.proto.Messages.SimpleRequest.getDefaultInstance()))
.setResponseMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
@ -175,7 +175,7 @@ public final class BenchmarkServiceGrpc {
.setType(io.grpc.MethodDescriptor.MethodType.BIDI_STREAMING)
.setFullMethodName(generateFullMethodName(
"grpc.testing.BenchmarkService", "StreamingBothWays"))
.setRegisterForTracing(true)
.setSampledToLocalTracing(true)
.setRequestMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
io.grpc.benchmarks.proto.Messages.SimpleRequest.getDefaultInstance()))
.setResponseMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(

View File

@ -47,7 +47,7 @@ public final class ReportQpsScenarioServiceGrpc {
.setType(io.grpc.MethodDescriptor.MethodType.UNARY)
.setFullMethodName(generateFullMethodName(
"grpc.testing.ReportQpsScenarioService", "ReportScenario"))
.setRegisterForTracing(true)
.setSampledToLocalTracing(true)
.setRequestMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
io.grpc.benchmarks.proto.Control.ScenarioResult.getDefaultInstance()))
.setResponseMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(

View File

@ -47,7 +47,7 @@ public final class WorkerServiceGrpc {
.setType(io.grpc.MethodDescriptor.MethodType.BIDI_STREAMING)
.setFullMethodName(generateFullMethodName(
"grpc.testing.WorkerService", "RunServer"))
.setRegisterForTracing(true)
.setSampledToLocalTracing(true)
.setRequestMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
io.grpc.benchmarks.proto.Control.ServerArgs.getDefaultInstance()))
.setResponseMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
@ -79,7 +79,7 @@ public final class WorkerServiceGrpc {
.setType(io.grpc.MethodDescriptor.MethodType.BIDI_STREAMING)
.setFullMethodName(generateFullMethodName(
"grpc.testing.WorkerService", "RunClient"))
.setRegisterForTracing(true)
.setSampledToLocalTracing(true)
.setRequestMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
io.grpc.benchmarks.proto.Control.ClientArgs.getDefaultInstance()))
.setResponseMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
@ -111,7 +111,7 @@ public final class WorkerServiceGrpc {
.setType(io.grpc.MethodDescriptor.MethodType.UNARY)
.setFullMethodName(generateFullMethodName(
"grpc.testing.WorkerService", "CoreCount"))
.setRegisterForTracing(true)
.setSampledToLocalTracing(true)
.setRequestMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
io.grpc.benchmarks.proto.Control.CoreRequest.getDefaultInstance()))
.setResponseMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
@ -143,7 +143,7 @@ public final class WorkerServiceGrpc {
.setType(io.grpc.MethodDescriptor.MethodType.UNARY)
.setFullMethodName(generateFullMethodName(
"grpc.testing.WorkerService", "QuitWorker"))
.setRegisterForTracing(true)
.setSampledToLocalTracing(true)
.setRequestMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
io.grpc.benchmarks.proto.Control.Void.getDefaultInstance()))
.setResponseMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(

View File

@ -191,8 +191,8 @@ 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',
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}",
protobuf_lite: "com.google.protobuf:protobuf-lite:3.0.1",

View File

@ -363,7 +363,7 @@ static void PrintMethodFields(
" .setType($MethodType$.$method_type$)\n"
" .setFullMethodName(generateFullMethodName(\n"
" \"$Package$$service_name$\", \"$method_name$\"))\n"
" .setRegisterForTracing(true)\n"
" .setSampledToLocalTracing(true)\n"
" .setRequestMarshaller($NanoUtils$.<$input_type$>marshaller(\n"
" new NanoFactory<$input_type$>(ARG_IN_$method_field_name$)))\n"
" .setResponseMarshaller($NanoUtils$.<$output_type$>marshaller(\n"
@ -402,7 +402,7 @@ static void PrintMethodFields(
" .setType($MethodType$.$method_type$)\n"
" .setFullMethodName(generateFullMethodName(\n"
" \"$Package$$service_name$\", \"$method_name$\"))\n"
" .setRegisterForTracing(true)\n"
" .setSampledToLocalTracing(true)\n"
" .setRequestMarshaller($ProtoUtils$.marshaller(\n"
" $input_type$.getDefaultInstance()))\n"
" .setResponseMarshaller($ProtoUtils$.marshaller(\n"

View File

@ -50,7 +50,7 @@ public final class TestServiceGrpc {
.setType(io.grpc.MethodDescriptor.MethodType.UNARY)
.setFullMethodName(generateFullMethodName(
"grpc.testing.TestService", "UnaryCall"))
.setRegisterForTracing(true)
.setSampledToLocalTracing(true)
.setRequestMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
io.grpc.testing.integration.Test.SimpleRequest.getDefaultInstance()))
.setResponseMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
@ -82,7 +82,7 @@ public final class TestServiceGrpc {
.setType(io.grpc.MethodDescriptor.MethodType.SERVER_STREAMING)
.setFullMethodName(generateFullMethodName(
"grpc.testing.TestService", "StreamingOutputCall"))
.setRegisterForTracing(true)
.setSampledToLocalTracing(true)
.setRequestMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
io.grpc.testing.integration.Test.StreamingOutputCallRequest.getDefaultInstance()))
.setResponseMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
@ -114,7 +114,7 @@ public final class TestServiceGrpc {
.setType(io.grpc.MethodDescriptor.MethodType.CLIENT_STREAMING)
.setFullMethodName(generateFullMethodName(
"grpc.testing.TestService", "StreamingInputCall"))
.setRegisterForTracing(true)
.setSampledToLocalTracing(true)
.setRequestMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
io.grpc.testing.integration.Test.StreamingInputCallRequest.getDefaultInstance()))
.setResponseMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
@ -146,7 +146,7 @@ public final class TestServiceGrpc {
.setType(io.grpc.MethodDescriptor.MethodType.BIDI_STREAMING)
.setFullMethodName(generateFullMethodName(
"grpc.testing.TestService", "FullBidiCall"))
.setRegisterForTracing(true)
.setSampledToLocalTracing(true)
.setRequestMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
io.grpc.testing.integration.Test.StreamingOutputCallRequest.getDefaultInstance()))
.setResponseMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
@ -178,7 +178,7 @@ public final class TestServiceGrpc {
.setType(io.grpc.MethodDescriptor.MethodType.BIDI_STREAMING)
.setFullMethodName(generateFullMethodName(
"grpc.testing.TestService", "HalfBidiCall"))
.setRegisterForTracing(true)
.setSampledToLocalTracing(true)
.setRequestMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
io.grpc.testing.integration.Test.StreamingOutputCallRequest.getDefaultInstance()))
.setResponseMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(

View File

@ -50,7 +50,7 @@ public final class TestServiceGrpc {
.setType(io.grpc.MethodDescriptor.MethodType.UNARY)
.setFullMethodName(generateFullMethodName(
"grpc.testing.TestService", "UnaryCall"))
.setRegisterForTracing(true)
.setSampledToLocalTracing(true)
.setRequestMarshaller(io.grpc.protobuf.lite.ProtoLiteUtils.marshaller(
io.grpc.testing.integration.Test.SimpleRequest.getDefaultInstance()))
.setResponseMarshaller(io.grpc.protobuf.lite.ProtoLiteUtils.marshaller(
@ -81,7 +81,7 @@ public final class TestServiceGrpc {
.setType(io.grpc.MethodDescriptor.MethodType.SERVER_STREAMING)
.setFullMethodName(generateFullMethodName(
"grpc.testing.TestService", "StreamingOutputCall"))
.setRegisterForTracing(true)
.setSampledToLocalTracing(true)
.setRequestMarshaller(io.grpc.protobuf.lite.ProtoLiteUtils.marshaller(
io.grpc.testing.integration.Test.StreamingOutputCallRequest.getDefaultInstance()))
.setResponseMarshaller(io.grpc.protobuf.lite.ProtoLiteUtils.marshaller(
@ -112,7 +112,7 @@ public final class TestServiceGrpc {
.setType(io.grpc.MethodDescriptor.MethodType.CLIENT_STREAMING)
.setFullMethodName(generateFullMethodName(
"grpc.testing.TestService", "StreamingInputCall"))
.setRegisterForTracing(true)
.setSampledToLocalTracing(true)
.setRequestMarshaller(io.grpc.protobuf.lite.ProtoLiteUtils.marshaller(
io.grpc.testing.integration.Test.StreamingInputCallRequest.getDefaultInstance()))
.setResponseMarshaller(io.grpc.protobuf.lite.ProtoLiteUtils.marshaller(
@ -143,7 +143,7 @@ public final class TestServiceGrpc {
.setType(io.grpc.MethodDescriptor.MethodType.BIDI_STREAMING)
.setFullMethodName(generateFullMethodName(
"grpc.testing.TestService", "FullBidiCall"))
.setRegisterForTracing(true)
.setSampledToLocalTracing(true)
.setRequestMarshaller(io.grpc.protobuf.lite.ProtoLiteUtils.marshaller(
io.grpc.testing.integration.Test.StreamingOutputCallRequest.getDefaultInstance()))
.setResponseMarshaller(io.grpc.protobuf.lite.ProtoLiteUtils.marshaller(
@ -174,7 +174,7 @@ public final class TestServiceGrpc {
.setType(io.grpc.MethodDescriptor.MethodType.BIDI_STREAMING)
.setFullMethodName(generateFullMethodName(
"grpc.testing.TestService", "HalfBidiCall"))
.setRegisterForTracing(true)
.setSampledToLocalTracing(true)
.setRequestMarshaller(io.grpc.protobuf.lite.ProtoLiteUtils.marshaller(
io.grpc.testing.integration.Test.StreamingOutputCallRequest.getDefaultInstance()))
.setResponseMarshaller(io.grpc.protobuf.lite.ProtoLiteUtils.marshaller(

View File

@ -54,7 +54,7 @@ public final class TestServiceGrpc {
.setType(io.grpc.MethodDescriptor.MethodType.UNARY)
.setFullMethodName(generateFullMethodName(
"grpc.testing.TestService", "UnaryCall"))
.setRegisterForTracing(true)
.setSampledToLocalTracing(true)
.setRequestMarshaller(io.grpc.protobuf.nano.NanoUtils.<io.grpc.testing.integration.nano.Test.SimpleRequest>marshaller(
new NanoFactory<io.grpc.testing.integration.nano.Test.SimpleRequest>(ARG_IN_METHOD_UNARY_CALL)))
.setResponseMarshaller(io.grpc.protobuf.nano.NanoUtils.<io.grpc.testing.integration.nano.Test.SimpleResponse>marshaller(
@ -87,7 +87,7 @@ public final class TestServiceGrpc {
.setType(io.grpc.MethodDescriptor.MethodType.SERVER_STREAMING)
.setFullMethodName(generateFullMethodName(
"grpc.testing.TestService", "StreamingOutputCall"))
.setRegisterForTracing(true)
.setSampledToLocalTracing(true)
.setRequestMarshaller(io.grpc.protobuf.nano.NanoUtils.<io.grpc.testing.integration.nano.Test.StreamingOutputCallRequest>marshaller(
new NanoFactory<io.grpc.testing.integration.nano.Test.StreamingOutputCallRequest>(ARG_IN_METHOD_STREAMING_OUTPUT_CALL)))
.setResponseMarshaller(io.grpc.protobuf.nano.NanoUtils.<io.grpc.testing.integration.nano.Test.StreamingOutputCallResponse>marshaller(
@ -120,7 +120,7 @@ public final class TestServiceGrpc {
.setType(io.grpc.MethodDescriptor.MethodType.CLIENT_STREAMING)
.setFullMethodName(generateFullMethodName(
"grpc.testing.TestService", "StreamingInputCall"))
.setRegisterForTracing(true)
.setSampledToLocalTracing(true)
.setRequestMarshaller(io.grpc.protobuf.nano.NanoUtils.<io.grpc.testing.integration.nano.Test.StreamingInputCallRequest>marshaller(
new NanoFactory<io.grpc.testing.integration.nano.Test.StreamingInputCallRequest>(ARG_IN_METHOD_STREAMING_INPUT_CALL)))
.setResponseMarshaller(io.grpc.protobuf.nano.NanoUtils.<io.grpc.testing.integration.nano.Test.StreamingInputCallResponse>marshaller(
@ -153,7 +153,7 @@ public final class TestServiceGrpc {
.setType(io.grpc.MethodDescriptor.MethodType.BIDI_STREAMING)
.setFullMethodName(generateFullMethodName(
"grpc.testing.TestService", "FullBidiCall"))
.setRegisterForTracing(true)
.setSampledToLocalTracing(true)
.setRequestMarshaller(io.grpc.protobuf.nano.NanoUtils.<io.grpc.testing.integration.nano.Test.StreamingOutputCallRequest>marshaller(
new NanoFactory<io.grpc.testing.integration.nano.Test.StreamingOutputCallRequest>(ARG_IN_METHOD_FULL_BIDI_CALL)))
.setResponseMarshaller(io.grpc.protobuf.nano.NanoUtils.<io.grpc.testing.integration.nano.Test.StreamingOutputCallResponse>marshaller(
@ -186,7 +186,7 @@ public final class TestServiceGrpc {
.setType(io.grpc.MethodDescriptor.MethodType.BIDI_STREAMING)
.setFullMethodName(generateFullMethodName(
"grpc.testing.TestService", "HalfBidiCall"))
.setRegisterForTracing(true)
.setSampledToLocalTracing(true)
.setRequestMarshaller(io.grpc.protobuf.nano.NanoUtils.<io.grpc.testing.integration.nano.Test.StreamingOutputCallRequest>marshaller(
new NanoFactory<io.grpc.testing.integration.nano.Test.StreamingOutputCallRequest>(ARG_IN_METHOD_HALF_BIDI_CALL)))
.setResponseMarshaller(io.grpc.protobuf.nano.NanoUtils.<io.grpc.testing.integration.nano.Test.StreamingOutputCallResponse>marshaller(

View File

@ -37,11 +37,4 @@ public final class InternalMethodDescriptor {
public void setRawMethodName(MethodDescriptor<?, ?> md, Object o) {
md.setRawMethodName(transport.ordinal(), o);
}
public interface RegisterForTracingCallback extends
MethodDescriptor.Registrations.RegisterForTracingCallback {}
public static void setRegisterCallback(RegisterForTracingCallback registerCallback) {
MethodDescriptor.Registrations.setRegisterForTracingCallback(registerCallback);
}
}

View File

@ -17,15 +17,9 @@
package io.grpc;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import com.google.common.base.Preconditions;
import java.io.InputStream;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.concurrent.atomic.AtomicReferenceArray;
import javax.annotation.CheckReturnValue;
import javax.annotation.Nullable;
@ -51,10 +45,7 @@ public final class MethodDescriptor<ReqT, RespT> {
private final @Nullable Object schemaDescriptor;
private final boolean idempotent;
private final boolean safe;
// This field is not exposed, since the result would be misleading. Setting this value to true,
// ensures that the method is traced, but false means that it might still be traced, due to
// another descriptor tracing the same name.
private final boolean registerForTracing;
private final boolean sampledToLocalTracing;
// Must be set to InternalKnownTransport.values().length
// Not referenced to break the dependency.
@ -231,7 +222,7 @@ public final class MethodDescriptor<ReqT, RespT> {
Object schemaDescriptor,
boolean idempotent,
boolean safe,
boolean registerForTracing) {
boolean sampledToLocalTracing) {
this.type = Preconditions.checkNotNull(type, "type");
this.fullMethodName = Preconditions.checkNotNull(fullMethodName, "fullMethodName");
@ -240,12 +231,9 @@ public final class MethodDescriptor<ReqT, RespT> {
this.schemaDescriptor = schemaDescriptor;
this.idempotent = idempotent;
this.safe = safe;
this.registerForTracing = registerForTracing;
this.sampledToLocalTracing = sampledToLocalTracing;
Preconditions.checkArgument(!safe || type == MethodType.UNARY,
"Only unary methods can be specified safe");
if (registerForTracing) {
Registrations.registerForTracing(this);
}
}
/**
@ -366,6 +354,13 @@ public final class MethodDescriptor<ReqT, RespT> {
return safe;
}
/**
* Returns whether RPCs for this method may be sampled into the local tracing store.
*/
public boolean isSampledToLocalTracing() {
return sampledToLocalTracing;
}
/**
* Generate the fully qualified method name.
*
@ -443,7 +438,7 @@ public final class MethodDescriptor<ReqT, RespT> {
.setFullMethodName(fullMethodName)
.setIdempotent(idempotent)
.setSafe(safe)
.setRegisterForTracing(registerForTracing);
.setSampledToLocalTracing(sampledToLocalTracing);
}
/**
@ -460,7 +455,7 @@ public final class MethodDescriptor<ReqT, RespT> {
private boolean idempotent;
private boolean safe;
private Object schemaDescriptor;
private boolean registerForTracing;
private boolean sampledToLocalTracing;
private Builder() {}
@ -548,13 +543,13 @@ public final class MethodDescriptor<ReqT, RespT> {
}
/**
* Sets whether the new MethodDescriptor should be registered into the tracing system, so that
* RPCs on this method may be collected and stored.
* Sets whether RPCs for this method may be sampled into the local tracing store. If true,
* sampled traces of this method may be kept in memory by tracing libraries.
*
* @since 1.7.0
* @since 1.8.0
*/
public Builder<ReqT, RespT> setRegisterForTracing(boolean value) {
this.registerForTracing = value;
public Builder<ReqT, RespT> setSampledToLocalTracing(boolean value) {
this.sampledToLocalTracing = value;
return this;
}
@ -573,72 +568,7 @@ public final class MethodDescriptor<ReqT, RespT> {
schemaDescriptor,
idempotent,
safe,
registerForTracing);
}
}
static final class Registrations {
private static final ReferenceQueue<MethodDescriptor<?, ?>> droppedMethodDescriptors =
new ReferenceQueue<MethodDescriptor<?, ?>>();
private static final Collection<WeakReference<MethodDescriptor<?, ?>>> pendingRegistrations =
new LinkedList<WeakReference<MethodDescriptor<?, ?>>>();
private static volatile RegisterForTracingCallback registerCallback;
interface RegisterForTracingCallback {
void onRegister(MethodDescriptor<?, ?> md);
}
/**
* Sets a callback for method descriptor builds. Called for descriptors with
* {@link MethodDescriptor#registerForTracing} set. This should only be called by
* {@link io.grpc.internal.CensusTracingModule} since it will only work for one invocation.
*
* @param registerCallback the callback to handle descriptor registration.
*/
static synchronized void setRegisterForTracingCallback(
RegisterForTracingCallback registerCallback) {
checkState(Registrations.registerCallback == null, "callback already present");
Registrations.registerCallback = checkNotNull(registerCallback, "registerCallback");
for (WeakReference<MethodDescriptor<?, ?>> mdRef : pendingRegistrations) {
MethodDescriptor<?, ?> md = mdRef.get();
if (md != null) {
mdRef.clear();
registerCallback.onRegister(md);
}
}
drainDroppedMethodDescriptors();
}
private static synchronized void drainDroppedMethodDescriptors() {
boolean found = false;
while (droppedMethodDescriptors.poll() != null) {
found = true;
}
if (found) {
Iterator<WeakReference<MethodDescriptor<?, ?>>> it = pendingRegistrations.iterator();
while (it.hasNext()) {
if (it.next().get() == null) {
it.remove();
}
}
}
}
private static void registerForTracing(MethodDescriptor<?, ?> md) {
RegisterForTracingCallback reg;
if ((reg = registerCallback) == null) {
synchronized (MethodDescriptor.class) {
if ((reg = registerCallback) == null) {
pendingRegistrations.add(
new WeakReference<MethodDescriptor<?, ?>>(md, droppedMethodDescriptors));
drainDroppedMethodDescriptors();
return;
}
}
}
reg.onRegister(md);
sampledToLocalTracing);
}
}
}

View File

@ -33,8 +33,9 @@ public abstract class ServerStreamTracer extends StreamTracer {
}
/**
* Called when {@link ServerCall} is created. This is for the tracer to access information
* about the {@code ServerCall}.
* Called when {@link ServerCall} is created. This is for the tracer to access information about
* the {@code ServerCall}. Called after {@link #filterContext} and before the application call
* handler.
*/
public void serverCallStarted(ServerCall<?, ?> call) {
}

View File

@ -28,10 +28,9 @@ import io.grpc.ClientStreamTracer;
import io.grpc.Context;
import io.grpc.ForwardingClientCall.SimpleForwardingClientCall;
import io.grpc.ForwardingClientCallListener.SimpleForwardingClientCallListener;
import io.grpc.InternalMethodDescriptor;
import io.grpc.InternalMethodDescriptor.RegisterForTracingCallback;
import io.grpc.Metadata;
import io.grpc.MethodDescriptor;
import io.grpc.ServerCall;
import io.grpc.ServerStreamTracer;
import io.grpc.StreamTracer;
import io.opencensus.trace.EndSpanOptions;
@ -40,11 +39,7 @@ import io.opencensus.trace.Span;
import io.opencensus.trace.SpanContext;
import io.opencensus.trace.Status;
import io.opencensus.trace.Tracer;
import io.opencensus.trace.Tracing;
import io.opencensus.trace.export.SampledSpanStore;
import io.opencensus.trace.propagation.BinaryFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import java.util.logging.Level;
import java.util.logging.Logger;
@ -74,24 +69,6 @@ final class CensusTracingModule {
private final TracingClientInterceptor clientInterceptor = new TracingClientInterceptor();
private final ServerTracerFactory serverTracerFactory = new ServerTracerFactory();
private static final boolean isRegistered = registerCensusTracer();
private static boolean registerCensusTracer() {
InternalMethodDescriptor.setRegisterCallback(new RegisterForTracingCallback() {
@Override
public void onRegister(MethodDescriptor<?, ?> md) {
SampledSpanStore sampledStore = Tracing.getExportComponent().getSampledSpanStore();
if (sampledStore != null) {
List<String> spanNames = new ArrayList<String>(2);
spanNames.add(generateTraceSpanName(false, md.getFullMethodName()));
spanNames.add(generateTraceSpanName(true, md.getFullMethodName()));
sampledStore.registerSpanNamesForCollection(spanNames);
}
}
});
return true;
}
CensusTracingModule(
Tracer censusTracer, final BinaryFormat censusPropagationBinaryFormat) {
this.censusTracer = checkNotNull(censusTracer, "censusTracer");
@ -119,8 +96,8 @@ final class CensusTracingModule {
* Creates a {@link ClientCallTracer} for a new call.
*/
@VisibleForTesting
ClientCallTracer newClientCallTracer(@Nullable Span parentSpan, String fullMethodName) {
return new ClientCallTracer(parentSpan, fullMethodName);
ClientCallTracer newClientCallTracer(@Nullable Span parentSpan, MethodDescriptor<?, ?> method) {
return new ClientCallTracer(parentSpan, method);
}
/**
@ -201,8 +178,12 @@ final class CensusTracingModule {
return status;
}
private static EndSpanOptions createEndSpanOptions(io.grpc.Status status) {
return EndSpanOptions.builder().setStatus(convertStatus(status)).build();
private static EndSpanOptions createEndSpanOptions(
io.grpc.Status status, boolean sampledToLocalTracing) {
return EndSpanOptions.builder()
.setStatus(convertStatus(status))
.setSampleToLocalSpanStore(sampledToLocalTracing)
.build();
}
private static void recordNetworkEvent(
@ -222,14 +203,16 @@ final class CensusTracingModule {
final class ClientCallTracer extends ClientStreamTracer.Factory {
volatile int callEnded;
private final boolean isSampledToLocalTracing;
private final Span span;
ClientCallTracer(@Nullable Span parentSpan, String fullMethodName) {
checkNotNull(fullMethodName, "fullMethodName");
ClientCallTracer(@Nullable Span parentSpan, MethodDescriptor<?, ?> method) {
checkNotNull(method, "method");
this.isSampledToLocalTracing = method.isSampledToLocalTracing();
this.span =
censusTracer
.spanBuilderWithExplicitParent(
generateTraceSpanName(false, fullMethodName),
generateTraceSpanName(false, method.getFullMethodName()),
parentSpan)
.setRecordEvents(true)
.startSpan();
@ -252,7 +235,7 @@ final class CensusTracingModule {
if (callEndedUpdater.getAndSet(this, 1) != 0) {
return;
}
span.end(createEndSpanOptions(status));
span.end(createEndSpanOptions(status, isSampledToLocalTracing));
}
}
@ -281,6 +264,7 @@ final class CensusTracingModule {
private final class ServerTracer extends ServerStreamTracer {
private final Span span;
volatile boolean isSampledToLocalTracing;
volatile int streamClosed;
ServerTracer(String fullMethodName, @Nullable SpanContext remoteSpan) {
@ -294,6 +278,11 @@ final class CensusTracingModule {
.startSpan();
}
@Override
public void serverCallStarted(ServerCall<?, ?> call) {
isSampledToLocalTracing = call.getMethodDescriptor().isSampledToLocalTracing();
}
/**
* Record a finished stream and mark the current time as the end time.
*
@ -305,7 +294,7 @@ final class CensusTracingModule {
if (streamClosedUpdater.getAndSet(this, 1) != 0) {
return;
}
span.end(createEndSpanOptions(status));
span.end(createEndSpanOptions(status, isSampledToLocalTracing));
}
@Override
@ -353,8 +342,7 @@ final class CensusTracingModule {
// Safe usage of the unsafe trace API because CONTEXT_SPAN_KEY.get() returns the same value
// as Tracer.getCurrentSpan() except when no value available when the return value is null
// for the direct access and BlankSpan when Tracer API is used.
final ClientCallTracer tracerFactory =
newClientCallTracer(CONTEXT_SPAN_KEY.get(), method.getFullMethodName());
final ClientCallTracer tracerFactory = newClientCallTracer(CONTEXT_SPAN_KEY.get(), method);
ClientCall<ReqT, RespT> call =
next.newCall(method, callOptions.withStreamTracerFactory(tracerFactory));
return new SimpleForwardingClientCall<ReqT, RespT>(call) {

View File

@ -94,4 +94,36 @@ public class MethodDescriptorTest {
// Never reached
assert discard == null;
}
@Test
public void sampledToLocalTracing() {
MethodDescriptor<String, String> md1 = MethodDescriptor.<String, String>newBuilder()
.setType(MethodType.SERVER_STREAMING)
.setFullMethodName("/package.service/method")
.setRequestMarshaller(new StringMarshaller())
.setResponseMarshaller(new StringMarshaller())
.setSampledToLocalTracing(true)
.build();
assertTrue(md1.isSampledToLocalTracing());
MethodDescriptor<String, String> md2 = md1.toBuilder()
.setFullMethodName("/package.service/method2")
.build();
assertTrue(md2.isSampledToLocalTracing());
// Same method name as md1, but not setting sampledToLocalTracing
MethodDescriptor<String, String> md3 = MethodDescriptor.<String, String>newBuilder()
.setType(MethodType.SERVER_STREAMING)
.setFullMethodName("/package.service/method")
.setRequestMarshaller(new StringMarshaller())
.setResponseMarshaller(new StringMarshaller())
.build();
assertFalse(md3.isSampledToLocalTracing());
MethodDescriptor<String, String> md4 = md3.toBuilder()
.setFullMethodName("/package.service/method2")
.setSampledToLocalTracing(true)
.build();
assertTrue(md4.isSampledToLocalTracing());
}
}

View File

@ -133,6 +133,9 @@ public class CensusModulesTest {
.setResponseMarshaller(MARSHALLER)
.setFullMethodName("package1.service2/method3")
.build();
private final MethodDescriptor<String, String> sampledMethod =
method.toBuilder().setSampledToLocalTracing(true).build();
private final FakeClock fakeClock = new FakeClock();
private final FakeStatsContextFactory statsCtxFactory = new FakeStatsContextFactory();
private final Random random = new Random(1234);
@ -305,6 +308,7 @@ public class CensusModulesTest {
.setStatus(
io.opencensus.trace.Status.PERMISSION_DENIED
.withDescription("No you don't"))
.setSampleToLocalSpanStore(false)
.build());
verify(spyClientSpan, never()).end();
}
@ -362,7 +366,7 @@ public class CensusModulesTest {
@Test
public void clientBasicTracingDefaultSpan() {
CensusTracingModule.ClientCallTracer callTracer =
censusTracing.newClientCallTracer(null, method.getFullMethodName());
censusTracing.newClientCallTracer(null, method);
Metadata headers = new Metadata();
ClientStreamTracer clientStreamTracer =
callTracer.newClientStreamTracer(CallOptions.DEFAULT, headers);
@ -394,11 +398,28 @@ public class CensusModulesTest {
.build(),
events.get(2));
inOrder.verify(spyClientSpan).end(
EndSpanOptions.builder().setStatus(io.opencensus.trace.Status.OK).build());
EndSpanOptions.builder()
.setStatus(io.opencensus.trace.Status.OK)
.setSampleToLocalSpanStore(false)
.build());
verifyNoMoreInteractions(spyClientSpan);
verifyNoMoreInteractions(tracer);
}
@Test
public void clientTracingSampledToLocalSpanStore() {
CensusTracingModule.ClientCallTracer callTracer =
censusTracing.newClientCallTracer(null, sampledMethod);
Metadata headers = new Metadata();
callTracer.callEnded(Status.OK);
verify(spyClientSpan).end(
EndSpanOptions.builder()
.setStatus(io.opencensus.trace.Status.OK)
.setSampleToLocalSpanStore(true)
.build());
}
@Test
public void clientStreamNeverCreatedStillRecordStats() {
CensusStatsModule.ClientCallTracer callTracer =
@ -431,7 +452,7 @@ public class CensusModulesTest {
@Test
public void clientStreamNeverCreatedStillRecordTracing() {
CensusTracingModule.ClientCallTracer callTracer =
censusTracing.newClientCallTracer(fakeClientParentSpan, method.getFullMethodName());
censusTracing.newClientCallTracer(fakeClientParentSpan, method);
verify(tracer).spanBuilderWithExplicitParent(
eq("Sent.package1.service2.method3"), same(fakeClientParentSpan));
verify(spyClientSpanBuilder).setRecordEvents(eq(true));
@ -442,6 +463,7 @@ public class CensusModulesTest {
.setStatus(
io.opencensus.trace.Status.DEADLINE_EXCEEDED
.withDescription("3 seconds"))
.setSampleToLocalSpanStore(false)
.build());
verifyNoMoreInteractions(spyClientSpan);
}
@ -567,7 +589,7 @@ public class CensusModulesTest {
@Test
public void traceHeadersPropagateSpanContext() throws Exception {
CensusTracingModule.ClientCallTracer callTracer =
censusTracing.newClientCallTracer(fakeClientParentSpan, method.getFullMethodName());
censusTracing.newClientCallTracer(fakeClientParentSpan, method);
Metadata headers = new Metadata();
callTracer.newClientStreamTracer(CallOptions.DEFAULT, headers);
@ -684,6 +706,8 @@ public class CensusModulesTest {
Context filteredContext = serverStreamTracer.filterContext(Context.ROOT);
assertSame(spyServerSpan, ContextUtils.CONTEXT_SPAN_KEY.get(filteredContext));
serverStreamTracer.serverCallStarted(new FakeServerCall<String, String>(method));
verify(spyServerSpan, never()).end(any(EndSpanOptions.class));
serverStreamTracer.outboundMessage(0);
@ -710,10 +734,44 @@ public class CensusModulesTest {
events.get(2));
inOrder.verify(spyServerSpan).end(
EndSpanOptions.builder()
.setStatus(io.opencensus.trace.Status.CANCELLED).build());
.setStatus(io.opencensus.trace.Status.CANCELLED)
.setSampleToLocalSpanStore(false)
.build());
verifyNoMoreInteractions(spyServerSpan);
}
@Test
public void serverTracingSampledToLocalSpanStore() {
ServerStreamTracer.Factory tracerFactory = censusTracing.getServerTracerFactory();
ServerStreamTracer serverStreamTracer =
tracerFactory.newServerStreamTracer(sampledMethod.getFullMethodName(), new Metadata());
serverStreamTracer.filterContext(Context.ROOT);
serverStreamTracer.serverCallStarted(new FakeServerCall<String, String>(sampledMethod));
serverStreamTracer.streamClosed(Status.CANCELLED);
verify(spyServerSpan).end(
EndSpanOptions.builder()
.setStatus(io.opencensus.trace.Status.CANCELLED)
.setSampleToLocalSpanStore(true)
.build());
}
@Test
public void serverTracingNotSampledToLocalSpanStore_whenServerCallNotCreated() {
ServerStreamTracer.Factory tracerFactory = censusTracing.getServerTracerFactory();
ServerStreamTracer serverStreamTracer =
tracerFactory.newServerStreamTracer(sampledMethod.getFullMethodName(), new Metadata());
serverStreamTracer.streamClosed(Status.CANCELLED);
verify(spyServerSpan).end(
EndSpanOptions.builder()
.setStatus(io.opencensus.trace.Status.CANCELLED)
.setSampleToLocalSpanStore(false)
.build());
}
@Test
public void convertToTracingStatus() {
// Without description
@ -767,4 +825,42 @@ public class CensusModulesTest {
assertNull(record.getMetric(RpcConstants.RPC_CLIENT_UNCOMPRESSED_REQUEST_BYTES));
assertNull(record.getMetric(RpcConstants.RPC_CLIENT_UNCOMPRESSED_RESPONSE_BYTES));
}
private static class FakeServerCall<ReqT, RespT> extends ServerCall<ReqT, RespT> {
final MethodDescriptor<ReqT, RespT> method;
FakeServerCall(MethodDescriptor<ReqT, RespT> method) {
this.method = method;
}
@Override
public void request(int numMessages) {
throw new AssertionError("Should not be called");
}
@Override
public void sendHeaders(Metadata headers) {
throw new AssertionError("Should not be called");
}
@Override
public void sendMessage(RespT message) {
throw new AssertionError("Should not be called");
}
@Override
public void close(Status status, Metadata trailers) {
throw new AssertionError("Should not be called");
}
@Override
public boolean isCancelled() {
throw new AssertionError("Should not be called");
}
@Override
public MethodDescriptor<ReqT, RespT> getMethodDescriptor() {
return method;
}
}
}

View File

@ -47,7 +47,7 @@ public final class LoadBalancerGrpc {
.setType(io.grpc.MethodDescriptor.MethodType.BIDI_STREAMING)
.setFullMethodName(generateFullMethodName(
"grpc.lb.v1.LoadBalancer", "BalanceLoad"))
.setRegisterForTracing(true)
.setSampledToLocalTracing(true)
.setRequestMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
io.grpc.grpclb.LoadBalanceRequest.getDefaultInstance()))
.setResponseMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(

View File

@ -47,7 +47,7 @@ public final class MetricsServiceGrpc {
.setType(io.grpc.MethodDescriptor.MethodType.SERVER_STREAMING)
.setFullMethodName(generateFullMethodName(
"grpc.testing.MetricsService", "GetAllGauges"))
.setRegisterForTracing(true)
.setSampledToLocalTracing(true)
.setRequestMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
io.grpc.testing.integration.Metrics.EmptyMessage.getDefaultInstance()))
.setResponseMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
@ -79,7 +79,7 @@ public final class MetricsServiceGrpc {
.setType(io.grpc.MethodDescriptor.MethodType.UNARY)
.setFullMethodName(generateFullMethodName(
"grpc.testing.MetricsService", "GetGauge"))
.setRegisterForTracing(true)
.setSampledToLocalTracing(true)
.setRequestMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
io.grpc.testing.integration.Metrics.GaugeRequest.getDefaultInstance()))
.setResponseMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(

View File

@ -50,7 +50,7 @@ public final class ReconnectServiceGrpc {
.setType(io.grpc.MethodDescriptor.MethodType.UNARY)
.setFullMethodName(generateFullMethodName(
"grpc.testing.ReconnectService", "Start"))
.setRegisterForTracing(true)
.setSampledToLocalTracing(true)
.setRequestMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
com.google.protobuf.EmptyProtos.Empty.getDefaultInstance()))
.setResponseMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
@ -82,7 +82,7 @@ public final class ReconnectServiceGrpc {
.setType(io.grpc.MethodDescriptor.MethodType.UNARY)
.setFullMethodName(generateFullMethodName(
"grpc.testing.ReconnectService", "Stop"))
.setRegisterForTracing(true)
.setSampledToLocalTracing(true)
.setRequestMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
com.google.protobuf.EmptyProtos.Empty.getDefaultInstance()))
.setResponseMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(

View File

@ -51,7 +51,7 @@ public final class TestServiceGrpc {
.setType(io.grpc.MethodDescriptor.MethodType.UNARY)
.setFullMethodName(generateFullMethodName(
"grpc.testing.TestService", "EmptyCall"))
.setRegisterForTracing(true)
.setSampledToLocalTracing(true)
.setRequestMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
com.google.protobuf.EmptyProtos.Empty.getDefaultInstance()))
.setResponseMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
@ -83,7 +83,7 @@ public final class TestServiceGrpc {
.setType(io.grpc.MethodDescriptor.MethodType.UNARY)
.setFullMethodName(generateFullMethodName(
"grpc.testing.TestService", "UnaryCall"))
.setRegisterForTracing(true)
.setSampledToLocalTracing(true)
.setRequestMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
io.grpc.testing.integration.Messages.SimpleRequest.getDefaultInstance()))
.setResponseMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
@ -115,7 +115,7 @@ public final class TestServiceGrpc {
.setType(io.grpc.MethodDescriptor.MethodType.UNARY)
.setFullMethodName(generateFullMethodName(
"grpc.testing.TestService", "CacheableUnaryCall"))
.setRegisterForTracing(true)
.setSampledToLocalTracing(true)
.setRequestMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
io.grpc.testing.integration.Messages.SimpleRequest.getDefaultInstance()))
.setResponseMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
@ -147,7 +147,7 @@ public final class TestServiceGrpc {
.setType(io.grpc.MethodDescriptor.MethodType.SERVER_STREAMING)
.setFullMethodName(generateFullMethodName(
"grpc.testing.TestService", "StreamingOutputCall"))
.setRegisterForTracing(true)
.setSampledToLocalTracing(true)
.setRequestMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
io.grpc.testing.integration.Messages.StreamingOutputCallRequest.getDefaultInstance()))
.setResponseMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
@ -179,7 +179,7 @@ public final class TestServiceGrpc {
.setType(io.grpc.MethodDescriptor.MethodType.CLIENT_STREAMING)
.setFullMethodName(generateFullMethodName(
"grpc.testing.TestService", "StreamingInputCall"))
.setRegisterForTracing(true)
.setSampledToLocalTracing(true)
.setRequestMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
io.grpc.testing.integration.Messages.StreamingInputCallRequest.getDefaultInstance()))
.setResponseMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
@ -211,7 +211,7 @@ public final class TestServiceGrpc {
.setType(io.grpc.MethodDescriptor.MethodType.BIDI_STREAMING)
.setFullMethodName(generateFullMethodName(
"grpc.testing.TestService", "FullDuplexCall"))
.setRegisterForTracing(true)
.setSampledToLocalTracing(true)
.setRequestMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
io.grpc.testing.integration.Messages.StreamingOutputCallRequest.getDefaultInstance()))
.setResponseMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
@ -243,7 +243,7 @@ public final class TestServiceGrpc {
.setType(io.grpc.MethodDescriptor.MethodType.BIDI_STREAMING)
.setFullMethodName(generateFullMethodName(
"grpc.testing.TestService", "HalfDuplexCall"))
.setRegisterForTracing(true)
.setSampledToLocalTracing(true)
.setRequestMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
io.grpc.testing.integration.Messages.StreamingOutputCallRequest.getDefaultInstance()))
.setResponseMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
@ -275,7 +275,7 @@ public final class TestServiceGrpc {
.setType(io.grpc.MethodDescriptor.MethodType.UNARY)
.setFullMethodName(generateFullMethodName(
"grpc.testing.TestService", "UnimplementedCall"))
.setRegisterForTracing(true)
.setSampledToLocalTracing(true)
.setRequestMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
com.google.protobuf.EmptyProtos.Empty.getDefaultInstance()))
.setResponseMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(

View File

@ -51,7 +51,7 @@ public final class UnimplementedServiceGrpc {
.setType(io.grpc.MethodDescriptor.MethodType.UNARY)
.setFullMethodName(generateFullMethodName(
"grpc.testing.UnimplementedService", "UnimplementedCall"))
.setRegisterForTracing(true)
.setSampledToLocalTracing(true)
.setRequestMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
com.google.protobuf.EmptyProtos.Empty.getDefaultInstance()))
.setResponseMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(

View File

@ -47,7 +47,7 @@ public final class HealthGrpc {
.setType(io.grpc.MethodDescriptor.MethodType.UNARY)
.setFullMethodName(generateFullMethodName(
"grpc.health.v1.Health", "Check"))
.setRegisterForTracing(true)
.setSampledToLocalTracing(true)
.setRequestMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
io.grpc.health.v1.HealthCheckRequest.getDefaultInstance()))
.setResponseMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(

View File

@ -47,7 +47,7 @@ public final class MonitoringGrpc {
.setType(io.grpc.MethodDescriptor.MethodType.UNARY)
.setFullMethodName(generateFullMethodName(
"grpc.instrumentation.v1alpha.Monitoring", "GetCanonicalRpcStats"))
.setRegisterForTracing(true)
.setSampledToLocalTracing(true)
.setRequestMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
com.google.protobuf.Empty.getDefaultInstance()))
.setResponseMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
@ -79,7 +79,7 @@ public final class MonitoringGrpc {
.setType(io.grpc.MethodDescriptor.MethodType.UNARY)
.setFullMethodName(generateFullMethodName(
"grpc.instrumentation.v1alpha.Monitoring", "GetStats"))
.setRegisterForTracing(true)
.setSampledToLocalTracing(true)
.setRequestMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
io.grpc.instrumentation.v1alpha.StatsRequest.getDefaultInstance()))
.setResponseMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
@ -111,7 +111,7 @@ public final class MonitoringGrpc {
.setType(io.grpc.MethodDescriptor.MethodType.SERVER_STREAMING)
.setFullMethodName(generateFullMethodName(
"grpc.instrumentation.v1alpha.Monitoring", "WatchStats"))
.setRegisterForTracing(true)
.setSampledToLocalTracing(true)
.setRequestMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
io.grpc.instrumentation.v1alpha.StatsRequest.getDefaultInstance()))
.setResponseMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
@ -143,7 +143,7 @@ public final class MonitoringGrpc {
.setType(io.grpc.MethodDescriptor.MethodType.UNARY)
.setFullMethodName(generateFullMethodName(
"grpc.instrumentation.v1alpha.Monitoring", "GetRequestTraces"))
.setRegisterForTracing(true)
.setSampledToLocalTracing(true)
.setRequestMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
io.grpc.instrumentation.v1alpha.TraceRequest.getDefaultInstance()))
.setResponseMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
@ -175,7 +175,7 @@ public final class MonitoringGrpc {
.setType(io.grpc.MethodDescriptor.MethodType.UNARY)
.setFullMethodName(generateFullMethodName(
"grpc.instrumentation.v1alpha.Monitoring", "GetCustomMonitoringData"))
.setRegisterForTracing(true)
.setSampledToLocalTracing(true)
.setRequestMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
io.grpc.instrumentation.v1alpha.MonitoringDataGroup.getDefaultInstance()))
.setResponseMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(

View File

@ -47,7 +47,7 @@ public final class ServerReflectionGrpc {
.setType(io.grpc.MethodDescriptor.MethodType.BIDI_STREAMING)
.setFullMethodName(generateFullMethodName(
"grpc.reflection.v1alpha.ServerReflection", "ServerReflectionInfo"))
.setRegisterForTracing(true)
.setSampledToLocalTracing(true)
.setRequestMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
io.grpc.reflection.v1alpha.ServerReflectionRequest.getDefaultInstance()))
.setResponseMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(

View File

@ -50,7 +50,7 @@ public final class AnotherDynamicServiceGrpc {
.setType(io.grpc.MethodDescriptor.MethodType.UNARY)
.setFullMethodName(generateFullMethodName(
"grpc.reflection.testing.AnotherDynamicService", "Method"))
.setRegisterForTracing(true)
.setSampledToLocalTracing(true)
.setRequestMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
io.grpc.reflection.testing.DynamicRequest.getDefaultInstance()))
.setResponseMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(

View File

@ -50,7 +50,7 @@ public final class DynamicServiceGrpc {
.setType(io.grpc.MethodDescriptor.MethodType.UNARY)
.setFullMethodName(generateFullMethodName(
"grpc.reflection.testing.DynamicService", "Method"))
.setRegisterForTracing(true)
.setSampledToLocalTracing(true)
.setRequestMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
io.grpc.reflection.testing.DynamicRequest.getDefaultInstance()))
.setResponseMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(

View File

@ -47,7 +47,7 @@ public final class ReflectableServiceGrpc {
.setType(io.grpc.MethodDescriptor.MethodType.UNARY)
.setFullMethodName(generateFullMethodName(
"grpc.reflection.testing.ReflectableService", "Method"))
.setRegisterForTracing(true)
.setSampledToLocalTracing(true)
.setRequestMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
io.grpc.reflection.testing.Request.getDefaultInstance()))
.setResponseMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(

View File

@ -13,9 +13,7 @@ buildscript {
dependencies {
compile project(':grpc-protobuf'),
project(':grpc-stub')
testCompile libraries.opencensus_api,
libraries.truth
testRuntime libraries.opencensus_impl
testCompile libraries.truth
}
configureProtoCompilation()

View File

@ -50,7 +50,7 @@ public final class SimpleServiceGrpc {
.setType(io.grpc.MethodDescriptor.MethodType.UNARY)
.setFullMethodName(generateFullMethodName(
"grpc.testing.SimpleService", "UnaryRpc"))
.setRegisterForTracing(true)
.setSampledToLocalTracing(true)
.setRequestMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
io.grpc.testing.protobuf.SimpleRequest.getDefaultInstance()))
.setResponseMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
@ -82,7 +82,7 @@ public final class SimpleServiceGrpc {
.setType(io.grpc.MethodDescriptor.MethodType.CLIENT_STREAMING)
.setFullMethodName(generateFullMethodName(
"grpc.testing.SimpleService", "ClientStreamingRpc"))
.setRegisterForTracing(true)
.setSampledToLocalTracing(true)
.setRequestMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
io.grpc.testing.protobuf.SimpleRequest.getDefaultInstance()))
.setResponseMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
@ -114,7 +114,7 @@ public final class SimpleServiceGrpc {
.setType(io.grpc.MethodDescriptor.MethodType.SERVER_STREAMING)
.setFullMethodName(generateFullMethodName(
"grpc.testing.SimpleService", "ServerStreamingRpc"))
.setRegisterForTracing(true)
.setSampledToLocalTracing(true)
.setRequestMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
io.grpc.testing.protobuf.SimpleRequest.getDefaultInstance()))
.setResponseMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
@ -146,7 +146,7 @@ public final class SimpleServiceGrpc {
.setType(io.grpc.MethodDescriptor.MethodType.BIDI_STREAMING)
.setFullMethodName(generateFullMethodName(
"grpc.testing.SimpleService", "BidiStreamingRpc"))
.setRegisterForTracing(true)
.setSampledToLocalTracing(true)
.setRequestMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
io.grpc.testing.protobuf.SimpleRequest.getDefaultInstance()))
.setResponseMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(

View File

@ -16,19 +16,14 @@
package io.grpc.testing.protobuf;
import static com.google.common.truth.Truth.assertThat;
import static io.grpc.MethodDescriptor.MethodType.BIDI_STREAMING;
import static io.grpc.MethodDescriptor.MethodType.CLIENT_STREAMING;
import static io.grpc.MethodDescriptor.MethodType.SERVER_STREAMING;
import static io.grpc.MethodDescriptor.MethodType.UNARY;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import io.grpc.MethodDescriptor;
import io.opencensus.trace.Tracing;
import io.opencensus.trace.export.SampledSpanStore;
import java.util.ArrayList;
import java.util.Set;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@ -59,33 +54,7 @@ public class SimpleServiceTest {
}
@Test
public void registerSampledMethodsForTracing() throws Exception {
// Make sure SimpleServiceGrpc and CensusTracingModule classes are loaded.
assertNotNull(Class.forName(SimpleServiceGrpc.class.getName()));
assertNotNull(Class.forName("io.grpc.internal.CensusTracingModule"));
String[] methodNames = new String[] {
"grpc.testing.SimpleService/UnaryRpc",
"grpc.testing.SimpleService/ClientStreamingRpc",
"grpc.testing.SimpleService/ServerStreamingRpc",
"grpc.testing.SimpleService/BidiStreamingRpc"};
ArrayList<String> expectedSpans = new ArrayList<String>();
for (String methodName : methodNames) {
expectedSpans.add(generateTraceSpanName(false, methodName));
expectedSpans.add(generateTraceSpanName(true, methodName));
}
SampledSpanStore sampledStore = Tracing.getExportComponent().getSampledSpanStore();
Set<String> registeredSpans = sampledStore.getRegisteredSpanNamesForCollection();
assertThat(registeredSpans).containsAllIn(expectedSpans);
}
/**
* Copy of {@link io.grpc.internal.CensusTracingModule#generateTraceSpanName} to break dependency.
*/
private static String generateTraceSpanName(boolean isServer, String fullMethodName) {
String prefix = isServer ? "Recv" : "Sent";
return prefix + "." + fullMethodName.replace('/', '.');
public void generatedMethodsAreSampledToLocalTracing() throws Exception {
assertTrue(SimpleServiceGrpc.getUnaryRpcMethod().isSampledToLocalTracing());
}
}