Skip to content

Commit

Permalink
Update log messages to make it easier for monitoring (#1316)
Browse files Browse the repository at this point in the history
* Update log messages to make it easier for monitoring

* More updates to the log messages

* More updates to the log messages

* More updates to the log messages

* updated additional Consul Service Discovery related log messages to make alerting simpler and messages more informative

Co-authored-by: haijohn-su <[email protected]>
Co-authored-by: Michael Christoff <[email protected]>
  • Loading branch information
3 people authored Aug 4, 2022
1 parent 1172f50 commit 9c78901
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
32 changes: 16 additions & 16 deletions consul/src/main/java/com/networknt/consul/ConsulRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,10 @@ private ConcurrentHashMap<String, List<URL>> lookupServiceUpdate(String protocol
ConcurrentHashMap<String, List<URL>> serviceUrls = new ConcurrentHashMap<>();
if (response != null) {
List<ConsulService> services = response.getValue();
if(logger.isDebugEnabled()) try {logger.debug("services = " + Config.getInstance().getMapper().writeValueAsString(services));} catch (Exception e) {}
if(logger.isDebugEnabled()) try {logger.debug("Consul-registered services = " + Config.getInstance().getMapper().writeValueAsString(services));} catch (Exception e) {}
if (services != null && !services.isEmpty()
&& response.getConsulIndex() > lastConsulIndexId) {
logger.info("Got {} updated urls for service: {}", services.size(), serviceName);
logger.info("Got updated urls from Consul: {} instances of service {} found", services.size(), serviceName);
for (ConsulService service : services) {
try {
URL url = ConsulUtils.buildUrl(protocol, service);
Expand All @@ -221,24 +221,24 @@ private ConcurrentHashMap<String, List<URL>> lookupServiceUpdate(String protocol
urlList = new ArrayList<>();
serviceUrls.put(serviceName, urlList);
}
if(logger.isTraceEnabled()) logger.trace("lookupServiceUpdate url = " + url);
if(logger.isTraceEnabled()) logger.trace("Consul lookupServiceUpdate url = " + url);
urlList.add(url);
} catch (Exception e) {
logger.error("convert consul service to url fail! service:" + service, e);
logger.error("Failed to convert Consul service to url! service: " + service, e);
}
}
lookupServices.put(serviceName, response.getConsulIndex());
logger.info("Service {} consul index {} put into lookupServices", serviceName, response.getConsulIndex());
logger.info("Consul index put into lookupServices for service: {}, index={}", serviceName, response.getConsulIndex());
return serviceUrls;
} else if (response.getConsulIndex() < lastConsulIndexId) {
logger.info(serviceName + " lastIndex:" + lastConsulIndexId + "; response consul Index:" + response.getConsulIndex());
logger.info("Consul returned stale index: Index reset to 0 for service {} - Consul response index < last Consul index: {} < {}", serviceName, response.getConsulIndex(), lastConsulIndexId);
lookupServices.put(serviceName, 0L);
} else {
logger.info(serviceName + " no need update, lastIndex:" + lastConsulIndexId);
logger.info("Consul returned no service updates: No need to update local Consul discovery cache for service {}, lastIndex={}", serviceName, lastConsulIndexId);
}
} else {
serviceUrls.put(serviceName, new ArrayList<>());
logger.info("no response for service: {}, set urls to null", serviceName);
logger.info("No Consul response for service! Clearing local cache for service {}", serviceName);
}
return serviceUrls;
}
Expand Down Expand Up @@ -268,7 +268,7 @@ private void updateServiceCache(String serviceName, ConcurrentHashMap<String, Li
List<URL> cachedUrls = serviceCache.get(serviceName);
List<URL> newUrls = serviceUrls.get(serviceName);
try {
logger.trace("serviceUrls = {}", Config.getInstance().getMapper().writeValueAsString(serviceUrls));
logger.trace("Consul service URLs = {}", Config.getInstance().getMapper().writeValueAsString(serviceUrls));
} catch(Exception e) {
}
boolean change = true;
Expand All @@ -284,7 +284,7 @@ private void updateServiceCache(String serviceName, ConcurrentHashMap<String, Li
for (URL url : newUrls) {
sb.append(url.getUri()).append(";");
}
logger.info("consul notify urls:" + sb.toString());
logger.info("Consul notify URLs:" + sb.toString());
}
}
}
Expand All @@ -300,18 +300,18 @@ public ServiceLookupThread(String protocol, String serviceName) {

@Override
public void run() {
logger.info("start service lookup thread. lookup interval: " + lookupInterval + "ms, service: " + serviceName);
logger.info("Start Consul lookupServiceUpdate thread - Lookup interval: {}ms, service {}", lookupInterval, serviceName);
while (true) {
try {
logger.info("Start to sleep {} ms for the next lookupServiceUpdate", lookupInterval);
logger.info("Consul lookupServiceUpdate Thread - SLEEP: Start to sleep {}ms for service {}", lookupInterval, serviceName);
sleep(lookupInterval);
logger.info("Woke up from the sleep for the next lookupServiceUpdate");
logger.info("Consul lookupServiceUpdate Thread - WAKE UP: Woke up from sleep for lookupServiceUpdate for service {}", serviceName);
ConcurrentHashMap<String, List<URL>> serviceUrls = lookupServiceUpdate(protocol, serviceName);
logger.info("Got {} serviceUrls from lookupServiceUpdate({}, {})", serviceUrls.size(), protocol, serviceName);
logger.info("Got service URLs from Consul lookupServiceUpdate: {} service URLs found for service {} ({})", serviceUrls.size(), serviceName, protocol);
updateServiceCache(serviceName, serviceUrls, true);
logger.info("Service cache updated with the serviceUrls from lookupServiceUpdate");
logger.info("Local Consul service cache updated with service URLs from lookupServiceUpdate for {}", serviceName);
} catch (Throwable e) {
logger.error("service lookup thread fail!", e);
logger.error("Consul lookupServiceUpdate thread failed!", e);
try {
Thread.sleep(2000);
} catch (InterruptedException ignored) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,13 @@ public class ConsulClientImpl implements ConsulClient {
public ConsulClientImpl() {
String consulUrl = config.getConsulUrl().toLowerCase();
optionMap = isHttp2() ? OptionMap.create(UndertowOptions.ENABLE_HTTP2, true) : OptionMap.EMPTY;
logger.debug("url = {}", consulUrl);
logger.debug("Consul URL = {}", consulUrl);
if(config.getWait() != null && config.getWait().length() > 2) wait = config.getWait();
logger.debug("wait = {}", wait);
try {
uri = new URI(consulUrl);
} catch (URISyntaxException e) {
logger.error("Invalid URI " + consulUrl, e);
logger.error("Consul URL generated invalid URI! Consul URL: " + consulUrl, e);
throw new RuntimeException("Invalid URI " + consulUrl, e);
}
}
Expand Down Expand Up @@ -177,19 +177,19 @@ public ConsulResponse<List<ConsulService>> lookupHealthService(String serviceNam
if(tag != null) {
path = path + "&tag=" + tag;
}
logger.trace("path = {}", path);
logger.trace("Consul health service path = {}", path);
try {
logger.debug("Getting connection from pool with {}", uri);
connection = client.borrowConnection(uri, Http2Client.WORKER, Http2Client.SSL, Http2Client.BUFFER_POOL, optionMap).get();
logger.info("Got connection: {} from pool and send request to {}", connection, path);
AtomicReference<ClientResponse> reference = send(connection, Methods.GET, path, token, null);
int statusCode = reference.get().getResponseCode();
logger.info("Got status code: {} from the consul query", statusCode);
logger.info("Got Consul Query status code: {}", statusCode);
if(statusCode >= UNUSUAL_STATUS_CODE){
throw new Exception("Failed to unregister on Consul: " + statusCode);
} else {
String body = reference.get().getAttachment(Http2Client.RESPONSE_BODY);
logger.debug("Got response body: {} from the consul query", body);
logger.debug("Got Consul Query response body: {}", body);
List<Map<String, Object>> services = Config.getInstance().getMapper().readValue(body, new TypeReference<List<Map<String, Object>>>(){});
List<ConsulService> ConsulServcies = new ArrayList<>(
services.size());
Expand Down Expand Up @@ -239,7 +239,7 @@ AtomicReference<ClientResponse> send(ClientConnection connection, HttpString met
ClientRequest request = new ClientRequest().setMethod(method).setPath(path);
request.getRequestHeaders().put(Headers.HOST, "localhost");
if (token != null) request.getRequestHeaders().put(HttpStringConstants.CONSUL_TOKEN, token);
if (logger.isTraceEnabled()) logger.trace("The request sent to consul: {} = request header: {}, request body is empty", uri.toString(), request.toString());
if (logger.isTraceEnabled()) logger.trace("The request sent to Consul URI {} - request header: {}, request body is empty", uri.toString(), request.toString());
if(StringUtils.isBlank(json)) {
connection.sendRequest(request, client.createClientCallback(reference, latch));
} else {
Expand All @@ -255,7 +255,7 @@ AtomicReference<ClientResponse> send(ClientConnection connection, HttpString met
// is borrowed from the pool, a new connection will be created as the one returned is not open.
if(connection != null && connection.isOpen()) IoUtils.safeClose(connection);
throw new RuntimeException(
String.format("The request to Consul: %s timed out after %d seconds", uri, waitInSecond));
String.format("The request to Consul timed out after %d seconds to: %s", waitInSecond, uri));
}
return reference;
}
Expand Down

0 comments on commit 9c78901

Please sign in to comment.