use of com.tencent.polaris.api.rpc.ServiceCallResult in project polaris-java-agent by polarismesh.
the class PolarisServiceObservability method updateServiceCallResult.
public static void updateServiceCallResult(Long delay, Throwable throwable, Integer status, PolarisAgentProperties polarisAgentProperties) {
ServiceInstance instance = InvokeContextHolder.get().getServiceInstance();
if (instance == null) {
LOGGER.warn("fail to call updateServiceCallResult for polaris with instance empty");
return;
}
ServiceCallResult serviceCallResult = new ServiceCallResult();
serviceCallResult.setNamespace(polarisAgentProperties.getNamespace());
serviceCallResult.setService(instance.getServiceId());
serviceCallResult.setHost(instance.getHost());
serviceCallResult.setPort(instance.getPort());
serviceCallResult.setDelay(delay);
serviceCallResult.setRetStatus(null == throwable ? RetStatus.RetSuccess : RetStatus.RetFail);
serviceCallResult.setRetCode(null != status ? status : -1);
try {
PolarisAPIFactory.getConsumerApi().updateServiceCallResult(serviceCallResult);
LOGGER.info("success to call updateServiceCallResult for polaris with status:{}, code:{}", serviceCallResult.getRetStatus(), serviceCallResult.getRetCode());
} catch (PolarisException e) {
LOGGER.error(e.getMessage());
} finally {
InvokeContextHolder.remove();
}
}
use of com.tencent.polaris.api.rpc.ServiceCallResult in project spring-cloud-tencent by Tencent.
the class PolarisFeignClient method execute.
@Override
public Response execute(Request request, Options options) throws IOException {
final ServiceCallResult resultRequest = createServiceCallResult(request);
try {
Response response = delegate.execute(request, options);
// HTTP code greater than 500 is an exception
if (response.status() >= 500) {
resultRequest.setRetStatus(RetStatus.RetFail);
}
LOG.debug("Will report result of {}. Request=[{}]. Response=[{}].", resultRequest.getRetStatus().name(), request, response);
return response;
} catch (IOException origin) {
resultRequest.setRetStatus(RetStatus.RetFail);
LOG.debug("Will report result of {}. Request=[{}].", resultRequest.getRetStatus().name(), request, origin);
throw origin;
} finally {
consumerAPI.updateServiceCallResult(resultRequest);
}
}
use of com.tencent.polaris.api.rpc.ServiceCallResult in project spring-cloud-tencent by Tencent.
the class PolarisFeignClient method createServiceCallResult.
private ServiceCallResult createServiceCallResult(final Request request) {
ServiceCallResult resultRequest = new ServiceCallResult();
resultRequest.setNamespace(MetadataContext.LOCAL_NAMESPACE);
String serviceName = request.requestTemplate().feignTarget().name();
resultRequest.setService(serviceName);
URI uri = URI.create(request.url());
resultRequest.setMethod(uri.getPath());
resultRequest.setRetStatus(RetStatus.RetSuccess);
String sourceNamespace = MetadataContext.LOCAL_NAMESPACE;
String sourceService = MetadataContext.LOCAL_SERVICE;
if (StringUtils.isNotBlank(sourceNamespace) && StringUtils.isNotBlank(sourceService)) {
resultRequest.setCallerService(new ServiceKey(sourceNamespace, sourceService));
}
resultRequest.setHost(uri.getHost());
resultRequest.setPort(uri.getPort());
return resultRequest;
}
use of com.tencent.polaris.api.rpc.ServiceCallResult in project polaris-java by polarismesh.
the class Consumer method invokeByNameResolution.
private static String invokeByNameResolution(String namespace, String service, String value, ConsumerAPI consumerAPI) {
System.out.println("namespace " + namespace + ", service " + service);
// 1. we need to do naming resolution to get a load balanced host and port
GetOneInstanceRequest getOneInstanceRequest = new GetOneInstanceRequest();
getOneInstanceRequest.setNamespace(namespace);
getOneInstanceRequest.setService(service);
InstancesResponse oneInstance = consumerAPI.getOneInstance(getOneInstanceRequest);
Instance[] instances = oneInstance.getInstances();
System.out.println("instances count is " + instances.length);
Instance targetInstance = instances[0];
System.out.printf("target instance is %s:%d%n", targetInstance.getHost(), targetInstance.getPort());
// 2. invoke the server by the resolved address
String urlStr = String.format("http://%s:%d%s?value=%s", targetInstance.getHost(), targetInstance.getPort(), PATH, value);
long startMillis = System.currentTimeMillis();
HttpResult httpResult = httpGet(urlStr);
long delay = System.currentTimeMillis() - startMillis;
System.out.printf("invoke %s, code is %d, delay is %d%n", urlStr, httpResult.code, delay);
// 3. report the invoke result to polaris-java, to eliminate the fail address
RetStatus status = RetStatus.RetSuccess;
if (httpResult.code != 200) {
status = RetStatus.RetFail;
}
ServiceCallResult result = new ServiceCallResult();
result.setNamespace(namespace);
result.setService(service);
result.setHost(targetInstance.getHost());
result.setPort(targetInstance.getPort());
result.setRetCode(httpResult.code);
result.setDelay(delay);
result.setRetStatus(status);
consumerAPI.updateServiceCallResult(result);
System.out.println("success to call updateServiceCallResult");
return httpResult.message;
}
use of com.tencent.polaris.api.rpc.ServiceCallResult in project polaris-java by polarismesh.
the class APIFacade method updateServiceCallResult.
public static boolean updateServiceCallResult(String namespace, String service, String method, String host, int port, long delay, boolean success, int code) {
if (!inited.get()) {
LOGGER.info("polaris not inited, updateServiceCallResult fail");
return false;
}
ServiceCallResult serviceCallResult = new ServiceCallResult();
serviceCallResult.setNamespace(namespace);
serviceCallResult.setService(service);
serviceCallResult.setMethod(method);
serviceCallResult.setHost(host);
serviceCallResult.setPort(port);
serviceCallResult.setDelay(delay);
serviceCallResult.setRetStatus(success ? RetStatus.RetSuccess : RetStatus.RetFail);
serviceCallResult.setRetCode(code);
consumerAPI.updateServiceCallResult(serviceCallResult);
LOGGER.debug("success to call updateServiceCallResult, status:{}", serviceCallResult.getRetStatus());
return true;
}
Aggregations