2929 */
3030package com .google .api .gax .grpc ;
3131
32- import com .google .api .core .InternalApi ;
3332import com .google .api .gax .longrunning .OperationSnapshot ;
3433import com .google .api .gax .rpc .BatchingCallSettings ;
3534import com .google .api .gax .rpc .BidiStreamingCallable ;
3635import com .google .api .gax .rpc .Callables ;
3736import com .google .api .gax .rpc .ClientContext ;
3837import com .google .api .gax .rpc .ClientStreamingCallable ;
38+ import com .google .api .gax .rpc .LibraryMetadata ;
3939import com .google .api .gax .rpc .LongRunningClient ;
4040import com .google .api .gax .rpc .OperationCallSettings ;
4141import com .google .api .gax .rpc .OperationCallable ;
4646import com .google .api .gax .rpc .StreamingCallSettings ;
4747import com .google .api .gax .rpc .UnaryCallSettings ;
4848import com .google .api .gax .rpc .UnaryCallable ;
49+ import com .google .api .gax .tracing .ApiTracerContext ;
4950import com .google .api .gax .tracing .SpanName ;
5051import com .google .api .gax .tracing .TracedBatchingCallable ;
5152import com .google .api .gax .tracing .TracedBidiCallable ;
5455import com .google .api .gax .tracing .TracedOperationInitialCallable ;
5556import com .google .api .gax .tracing .TracedServerStreamingCallable ;
5657import com .google .api .gax .tracing .TracedUnaryCallable ;
57- import com .google .common .base . Preconditions ;
58+ import com .google .common .annotations . VisibleForTesting ;
5859import com .google .common .collect .ImmutableSet ;
5960import com .google .longrunning .Operation ;
6061import com .google .longrunning .stub .OperationsStub ;
6162import io .grpc .MethodDescriptor ;
62- import java .util .regex .Matcher ;
63- import java .util .regex .Pattern ;
6463import javax .annotation .Nonnull ;
6564
6665/** Class with utility methods to create grpc-based direct callables. */
6766public class GrpcCallableFactory {
68- // Used to extract service and method name from a grpc MethodDescriptor.
69- private static final Pattern FULL_METHOD_NAME_REGEX = Pattern .compile ("^.*?([^./]+)/([^./]+)$" );
7067
7168 private GrpcCallableFactory () {}
7269
@@ -112,7 +109,7 @@ public static <RequestT, ResponseT> UnaryCallable<RequestT, ResponseT> createUna
112109 new TracedUnaryCallable <>(
113110 callable ,
114111 clientContext .getTracerFactory (),
115- getSpanName (grpcCallSettings .getMethodDescriptor ()));
112+ getApiTracerContext (grpcCallSettings .getMethodDescriptor ()));
116113
117114 return callable .withDefaultCallContext (clientContext .getDefaultCallContext ());
118115 }
@@ -161,7 +158,7 @@ public static <RequestT, ResponseT> UnaryCallable<RequestT, ResponseT> createBat
161158 new TracedBatchingCallable <>(
162159 baseCallable ,
163160 clientContext .getTracerFactory (),
164- getSpanName (grpcCallSettings .getMethodDescriptor ()),
161+ getApiTracerContext (grpcCallSettings .getMethodDescriptor ()),
165162 batchingCallSettings .getBatchingDescriptor ());
166163
167164 UnaryCallable <RequestT , ResponseT > batchingCallable =
@@ -187,7 +184,8 @@ OperationCallable<RequestT, ResponseT, MetadataT> createOperationCallable(
187184 ClientContext clientContext ,
188185 OperationsStub operationsStub ) {
189186
190- SpanName initialSpanName = getSpanName (grpcCallSettings .getMethodDescriptor ());
187+ ApiTracerContext tracerContext = getApiTracerContext (grpcCallSettings .getMethodDescriptor ());
188+ SpanName initialSpanName = SpanName .of (tracerContext );
191189 SpanName operationSpanName =
192190 SpanName .of (initialSpanName .getClientName (), initialSpanName .getMethodName () + "Operation" );
193191
@@ -237,7 +235,7 @@ BidiStreamingCallable<RequestT, ResponseT> createBidiStreamingCallable(
237235 new TracedBidiCallable <>(
238236 callable ,
239237 clientContext .getTracerFactory (),
240- getSpanName (grpcCallSettings .getMethodDescriptor ()));
238+ getApiTracerContext (grpcCallSettings .getMethodDescriptor ()));
241239
242240 return callable .withDefaultCallContext (clientContext .getDefaultCallContext ());
243241 }
@@ -295,7 +293,7 @@ ServerStreamingCallable<RequestT, ResponseT> createServerStreamingCallable(
295293 new TracedServerStreamingCallable <>(
296294 callable ,
297295 clientContext .getTracerFactory (),
298- getSpanName (grpcCallSettings .getMethodDescriptor ()));
296+ getApiTracerContext (grpcCallSettings .getMethodDescriptor ()));
299297
300298 return callable .withDefaultCallContext (clientContext .getDefaultCallContext ());
301299 }
@@ -323,16 +321,17 @@ ClientStreamingCallable<RequestT, ResponseT> createClientStreamingCallable(
323321 new TracedClientStreamingCallable <>(
324322 callable ,
325323 clientContext .getTracerFactory (),
326- getSpanName (grpcCallSettings .getMethodDescriptor ()));
324+ getApiTracerContext (grpcCallSettings .getMethodDescriptor ()));
327325
328326 return callable .withDefaultCallContext (clientContext .getDefaultCallContext ());
329327 }
330328
331- @ InternalApi ("Visible for testing" )
332- static SpanName getSpanName (@ Nonnull MethodDescriptor <?, ?> methodDescriptor ) {
333- Matcher matcher = FULL_METHOD_NAME_REGEX .matcher (methodDescriptor .getFullMethodName ());
334-
335- Preconditions .checkArgument (matcher .matches (), "Invalid fullMethodName" );
336- return SpanName .of (matcher .group (1 ), matcher .group (2 ));
329+ @ VisibleForTesting
330+ static ApiTracerContext getApiTracerContext (@ Nonnull MethodDescriptor <?, ?> methodDescriptor ) {
331+ return ApiTracerContext .newBuilder ()
332+ .setFullMethodName (methodDescriptor .getFullMethodName ())
333+ .setTransport (ApiTracerContext .Transport .GRPC )
334+ .setLibraryMetadata (LibraryMetadata .empty ())
335+ .build ();
337336 }
338337}
0 commit comments