下面列出了io.grpc.MethodDescriptor#extractFullServiceName ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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);
}
@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);
}
/**
* 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);
}
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());
}
/**
* 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);
}
/**
* 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);
}
/**
* 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);
}
/**
* 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());
}
/**
* 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());
}