GrpcCallableFactory { @InternalApi("Visible for testing") static SpanName getSpanName(@Nonnull MethodDescriptor<?, ?> methodDescriptor) { Matcher matcher = FULL_METHOD_NAME_REGEX.matcher(methodDescriptor.getFullMethodName()); Preconditions.checkArgument(matcher.matches(), "Invalid fullMethodName"); return SpanName.of(matcher.group(1), matcher.group(2)); } private  GrpcCallableFactory(); static UnaryCallable<RequestT, ResponseT> createBaseUnaryCallable(
      GrpcCallSettings<RequestT, ResponseT> grpcCallSettings,
      UnaryCallSettings<?, ?> callSettings,
      ClientContext clientContext); static UnaryCallable<RequestT, ResponseT> createUnaryCallable(
      GrpcCallSettings<RequestT, ResponseT> grpcCallSettings,
      UnaryCallSettings<RequestT, ResponseT> callSettings,
      ClientContext clientContext); static UnaryCallable<RequestT, PagedListResponseT> createPagedCallable(
          GrpcCallSettings<RequestT, ResponseT> grpcCallSettings,
          PagedCallSettings<RequestT, ResponseT, PagedListResponseT> pagedCallSettings,
          ClientContext clientContext); @BetaApi("The surface for batching is not stable yet and may change in the future.") static UnaryCallable<RequestT, ResponseT> createBatchingCallable(
      GrpcCallSettings<RequestT, ResponseT> grpcCallSettings,
      BatchingCallSettings<RequestT, ResponseT> batchingCallSettings,
      ClientContext clientContext); @BetaApi( "The surface for long-running operations is not stable yet and may change in the future.") static OperationCallable<RequestT, ResponseT, MetadataT> createOperationCallable(
          GrpcCallSettings<RequestT, Operation> grpcCallSettings,
          OperationCallSettings<RequestT, ResponseT, MetadataT> operationCallSettings,
          ClientContext clientContext,
          OperationsStub operationsStub); @BetaApi("The surface for streaming is not stable yet and may change in the future.") static BidiStreamingCallable<RequestT, ResponseT> createBidiStreamingCallable(
          GrpcCallSettings<RequestT, ResponseT> grpcCallSettings,
          StreamingCallSettings<RequestT, ResponseT> streamingCallSettings,
          ClientContext clientContext); @Deprecated @BetaApi("The surface for streaming is not stable yet and may change in the future.") static ServerStreamingCallable<RequestT, ResponseT> createServerStreamingCallable(
          GrpcCallSettings<RequestT, ResponseT> grpcCallSettings,
          StreamingCallSettings<RequestT, ResponseT> streamingCallSettings,
          ClientContext clientContext); @BetaApi("The surface for streaming is not stable yet and may change in the future.") static ServerStreamingCallable<RequestT, ResponseT> createServerStreamingCallable(
          GrpcCallSettings<RequestT, ResponseT> grpcCallSettings,
          ServerStreamingCallSettings<RequestT, ResponseT> streamingCallSettings,
          ClientContext clientContext); @BetaApi("The surface for streaming is not stable yet and may change in the future.") static ClientStreamingCallable<RequestT, ResponseT> createClientStreamingCallable(
          GrpcCallSettings<RequestT, ResponseT> grpcCallSettings,
          StreamingCallSettings<RequestT, ResponseT> streamingCallSettings,
          ClientContext clientContext);  }
@Test public void testGetSpanName() { @SuppressWarnings("unchecked") MethodDescriptor descriptor = MethodDescriptor.newBuilder() .setType(MethodType.SERVER_STREAMING) .setFullMethodName("google.bigtable.v2.Bigtable/ReadRows") .setRequestMarshaller(Mockito.mock(Marshaller.class)) .setResponseMarshaller(Mockito.mock(Marshaller.class)) .build(); SpanName actualSpanName = GrpcCallableFactory.getSpanName(descriptor); assertThat(actualSpanName).isEqualTo(SpanName.of("Bigtable", "ReadRows")); }
@Test public void testGetSpanNameUnqualified() { @SuppressWarnings("unchecked") MethodDescriptor descriptor = MethodDescriptor.newBuilder() .setType(MethodType.SERVER_STREAMING) .setFullMethodName("UnqualifiedService/ReadRows") .setRequestMarshaller(Mockito.mock(Marshaller.class)) .setResponseMarshaller(Mockito.mock(Marshaller.class)) .build(); SpanName actualSpanName = GrpcCallableFactory.getSpanName(descriptor); assertThat(actualSpanName).isEqualTo(SpanName.of("UnqualifiedService", "ReadRows")); }
@Test public void testGetSpanNameInvalid() { List<String> invalidNames = ImmutableList.of("BareMethod", "/MethodWithoutService"); for (String invalidName : invalidNames) { @SuppressWarnings("unchecked") MethodDescriptor descriptor = MethodDescriptor.newBuilder() .setType(MethodType.SERVER_STREAMING) .setFullMethodName(invalidName) .setRequestMarshaller(Mockito.mock(Marshaller.class)) .setResponseMarshaller(Mockito.mock(Marshaller.class)) .build(); IllegalArgumentException actualError = null; try { SpanName spanName = GrpcCallableFactory.getSpanName(descriptor); Truth.assertWithMessage( "Invalid method descriptor should not have a valid span name: %s should not generate the spanName: %s", invalidName, spanName) .fail(); } catch (IllegalArgumentException e) { actualError = e; } assertThat(actualError).isNotNull(); } }