Java Code Examples for io.grpc.MethodDescriptor#extractFullServiceName()

The following examples show how to use io.grpc.MethodDescriptor#extractFullServiceName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ProtoFullName.java    From karate-grpc with MIT License 6 votes vote down vote up
/**
 * format: <package>.<service>/<method>
 */
public static ProtoName parse(String fullName) {
    String fullServiceName = MethodDescriptor.extractFullServiceName(fullName);
    if (fullServiceName == null) {
        throw new IllegalArgumentException("Can't extract full service from " + fullName);
    }

    int serviceLength = fullServiceName.length();
    if (serviceLength + 1 >= fullName.length() || fullName.charAt(serviceLength) != '/') {
        throw new IllegalArgumentException("Can't extract method name from " + fullName);
    }
    String methodName = fullName.substring(fullServiceName.length() + 1);

    int index = fullServiceName.lastIndexOf('.');
    if (index == -1) {
        throw new IllegalArgumentException("Can't extract package name from " + fullServiceName);
    }
    String packageName = fullServiceName.substring(0, index);

    if (index + 1 >= fullServiceName.length() || fullServiceName.charAt(index) != '.') {
        throw new IllegalArgumentException("Can't extract service from " + fullServiceName);
    }
    String serviceName = fullServiceName.substring(index + 1);

    return new ProtoName(packageName, serviceName, methodName);
}
 
Example 2
Source File: MonitoringInterceptor.java    From feast with Apache License 2.0 6 votes vote down vote up
@Override
public <ReqT, RespT> Listener<ReqT> interceptCall(
    ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {

  long startCallMillis = System.currentTimeMillis();
  String fullMethodName = call.getMethodDescriptor().getFullMethodName();
  String serviceName = MethodDescriptor.extractFullServiceName(fullMethodName);
  String methodName = fullMethodName.substring(fullMethodName.indexOf("/") + 1);

  return next.startCall(
      new SimpleForwardingServerCall<ReqT, RespT>(call) {
        @Override
        public void close(Status status, Metadata trailers) {
          GrpcMetrics.requestLatency
              .labels(serviceName, methodName, status.getCode().name())
              .observe((System.currentTimeMillis() - startCallMillis) / 1000f);
          super.close(status, trailers);
        }
      },
      headers);
}
 
Example 3
Source File: MutableHandlerRegistry.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
/**
 * Note: This does not actually honor the authority provided.  It will, eventually in the future.
 */
@Override
@Nullable
public ServerMethodDefinition<?, ?> lookupMethod(String methodName, @Nullable String authority) {
  String serviceName = MethodDescriptor.extractFullServiceName(methodName);
  if (serviceName == null) {
    return null;
  }
  ServerServiceDefinition service = services.get(serviceName);
  if (service == null) {
    return null;
  }
  return service.getMethod(methodName);
}
 
Example 4
Source File: GrpcMethod.java    From java-grpc-prometheus with Apache License 2.0 5 votes vote down vote up
static GrpcMethod of(MethodDescriptor<?, ?> method) {
  String serviceName = MethodDescriptor.extractFullServiceName(method.getFullMethodName());

  // Full method names are of the form: "full.serviceName/MethodName". We extract the last part.
  String methodName = method.getFullMethodName().substring(serviceName.length() + 1);
  return new GrpcMethod(serviceName, methodName, method.getType());
}
 
Example 5
Source File: MutableHandlerRegistry.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
/**
 * Note: This does not actually honor the authority provided.  It will, eventually in the future.
 */
@Override
@Nullable
public ServerMethodDefinition<?, ?> lookupMethod(String methodName, @Nullable String authority) {
  String serviceName = MethodDescriptor.extractFullServiceName(methodName);
  if (serviceName == null) {
    return null;
  }
  ServerServiceDefinition service = services.get(serviceName);
  if (service == null) {
    return null;
  }
  return service.getMethod(methodName);
}
 
Example 6
Source File: BinlogHelper.java    From grpc-nebula-java with Apache License 2.0 4 votes vote down vote up
/**
 * Accepts a string in the format specified by the binary log spec.
 */
@VisibleForTesting
FactoryImpl(BinaryLogSink sink, String configurationString) {
  checkNotNull(sink, "sink");
  BinlogHelper globalLog = null;
  Map<String, BinlogHelper> perServiceLogs = new HashMap<String, BinlogHelper>();
  Map<String, BinlogHelper> perMethodLogs = new HashMap<String, BinlogHelper>();
  Set<String> blacklistedMethods = new HashSet<String>();
  if (configurationString != null && configurationString.length() > 0) {
    for (String configuration : Splitter.on(',').split(configurationString)) {
      Matcher configMatcher = configRe.matcher(configuration);
      if (!configMatcher.matches()) {
        throw new IllegalArgumentException("Illegal log config pattern: " + configuration);
      }
      String methodOrSvc = configMatcher.group(1);
      String binlogOptionStr = configMatcher.group(2);
      if (methodOrSvc.equals("*")) {
        // parse config for "*"
        checkState(
            globalLog == null,
            "Duplicate entry, this is fatal: " + configuration);
        globalLog = createBinaryLog(sink, binlogOptionStr);
        logger.log(Level.INFO, "Global binlog: {0}", binlogOptionStr);
      } else if (isServiceGlob(methodOrSvc)) {
        // parse config for a service, e.g. "service/*"
        String service = MethodDescriptor.extractFullServiceName(methodOrSvc);
        checkState(
            !perServiceLogs.containsKey(service),
            "Duplicate entry, this is fatal: " + configuration);
        perServiceLogs.put(service, createBinaryLog(sink, binlogOptionStr));
        logger.log(
            Level.INFO,
            "Service binlog: service={0} config={1}",
            new Object[] {service, binlogOptionStr});
      } else if (methodOrSvc.startsWith("-")) {
        // parse config for a method, e.g. "-service/method"
        String blacklistedMethod = methodOrSvc.substring(1);
        if (blacklistedMethod.length() == 0) {
          continue;
        }
        checkState(
            !blacklistedMethods.contains(blacklistedMethod),
            "Duplicate entry, this is fatal: " + configuration);
        checkState(
            !perMethodLogs.containsKey(blacklistedMethod),
            "Duplicate entry, this is fatal: " + configuration);
        blacklistedMethods.add(blacklistedMethod);
      } else {
        // parse config for a fully qualified method, e.g "serice/method"
        checkState(
            !perMethodLogs.containsKey(methodOrSvc),
            "Duplicate entry, this is fatal: " + configuration);
        checkState(
            !blacklistedMethods.contains(methodOrSvc),
            "Duplicate entry, this method was blacklisted: " + configuration);
        perMethodLogs.put(methodOrSvc, createBinaryLog(sink, binlogOptionStr));
        logger.log(
            Level.INFO,
            "Method binlog: method={0} config={1}",
            new Object[] {methodOrSvc, binlogOptionStr});
      }
    }
  }
  this.globalLog = globalLog;
  this.perServiceLogs = Collections.unmodifiableMap(perServiceLogs);
  this.perMethodLogs = Collections.unmodifiableMap(perMethodLogs);
  this.blacklistedMethods = Collections.unmodifiableSet(blacklistedMethods);
}
 
Example 7
Source File: BinlogHelper.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
/**
 * Accepts a string in the format specified by the binary log spec.
 */
@VisibleForTesting
FactoryImpl(BinaryLogSink sink, String configurationString) {
  checkNotNull(sink, "sink");
  BinlogHelper globalLog = null;
  Map<String, BinlogHelper> perServiceLogs = new HashMap<>();
  Map<String, BinlogHelper> perMethodLogs = new HashMap<>();
  Set<String> blacklistedMethods = new HashSet<>();
  if (configurationString != null && configurationString.length() > 0) {
    for (String configuration : Splitter.on(',').split(configurationString)) {
      int leftCurly = configuration.indexOf('{');
      // '*' for global, 'service/*' for service glob, or 'service/method' for fully qualified
      String methodOrSvc;
      // An expression originally wrapped in curly braces; like {m:256,h:256}, {m:256}, {h:256}
      String binlogOptionStr;
      if (leftCurly == -1) {
        methodOrSvc = configuration;
        binlogOptionStr = null;
      } else {
        int rightCurly = configuration.indexOf('}', leftCurly);
        if (rightCurly != configuration.length() - 1) {
          throw new IllegalArgumentException("Illegal log config pattern: " + configuration);
        }
        methodOrSvc = configuration.substring(0, leftCurly);
        // option without the curly braces
        binlogOptionStr = configuration.substring(leftCurly + 1, configuration.length() - 1);
      }
      if (methodOrSvc.isEmpty()) {
        throw new IllegalArgumentException("Illegal log config pattern: " + configuration);
      }
      if (methodOrSvc.equals("*")) {
        // parse config for "*"
        checkState(
            globalLog == null,
            "Duplicate entry, this is fatal: " + configuration);
        globalLog = createBinaryLog(sink, binlogOptionStr);
        logger.log(Level.INFO, "Global binlog: {0}", binlogOptionStr);
      } else if (isServiceGlob(methodOrSvc)) {
        // parse config for a service, e.g. "service/*"
        String service = MethodDescriptor.extractFullServiceName(methodOrSvc);
        checkState(
            !perServiceLogs.containsKey(service),
            "Duplicate entry, this is fatal: " + configuration);
        perServiceLogs.put(service, createBinaryLog(sink, binlogOptionStr));
        logger.log(
            Level.INFO,
            "Service binlog: service={0} config={1}",
            new Object[] {service, binlogOptionStr});
      } else if (methodOrSvc.startsWith("-")) {
        // parse config for a method, e.g. "-service/method"
        String blacklistedMethod = methodOrSvc.substring(1);
        if (blacklistedMethod.length() == 0) {
          continue;
        }
        checkState(
            !blacklistedMethods.contains(blacklistedMethod),
            "Duplicate entry, this is fatal: " + configuration);
        checkState(
            !perMethodLogs.containsKey(blacklistedMethod),
            "Duplicate entry, this is fatal: " + configuration);
        blacklistedMethods.add(blacklistedMethod);
      } else {
        // parse config for a fully qualified method, e.g "serice/method"
        checkState(
            !perMethodLogs.containsKey(methodOrSvc),
            "Duplicate entry, this is fatal: " + configuration);
        checkState(
            !blacklistedMethods.contains(methodOrSvc),
            "Duplicate entry, this method was blacklisted: " + configuration);
        perMethodLogs.put(methodOrSvc, createBinaryLog(sink, binlogOptionStr));
        logger.log(
            Level.INFO,
            "Method binlog: method={0} config={1}",
            new Object[] {methodOrSvc, binlogOptionStr});
      }
    }
  }
  this.globalLog = globalLog;
  this.perServiceLogs = Collections.unmodifiableMap(perServiceLogs);
  this.perMethodLogs = Collections.unmodifiableMap(perMethodLogs);
  this.blacklistedMethods = Collections.unmodifiableSet(blacklistedMethods);
}
 
Example 8
Source File: GrpcUtils.java    From grpc-spring-boot-starter with MIT License 2 votes vote down vote up
/**
 * Extracts the service name from the given method.
 *
 * @param method The method to get the service name from.
 * @return The extracted service name.
 * @see MethodDescriptor#extractFullServiceName(String)
 * @see #extractMethodName(MethodDescriptor)
 */
public static String extractServiceName(final MethodDescriptor<?, ?> method) {
    return MethodDescriptor.extractFullServiceName(method.getFullMethodName());
}
 
Example 9
Source File: GrpcUtils.java    From grpc-spring-boot-starter with MIT License 2 votes vote down vote up
/**
 * Extracts the service name from the given method.
 *
 * @param method The method to get the service name from.
 * @return The extracted service name.
 * @see MethodDescriptor#extractFullServiceName(String)
 * @see #extractMethodName(MethodDescriptor)
 */
public static String extractServiceName(final MethodDescriptor<?, ?> method) {
    return MethodDescriptor.extractFullServiceName(method.getFullMethodName());
}