Skip to content

Commit 7c3130b

Browse files
committed
Log subsequent registration errors on DEBUG level
In order to reduce the noise in the application log, only the first failure when registering is logged on the WARN level. All further attempts are logged using the DEBUG level. closes #490
1 parent 2848a63 commit 7c3130b

File tree

1 file changed

+102
-88
lines changed

1 file changed

+102
-88
lines changed
Lines changed: 102 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/*
2-
* Copyright 2014 the original author or authors.
2+
* Copyright 2014-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -15,10 +15,12 @@
1515
*/
1616
package de.codecentric.boot.admin.client.registration;
1717

18+
import de.codecentric.boot.admin.client.config.AdminProperties;
19+
1820
import java.util.Collections;
1921
import java.util.Map;
22+
import java.util.concurrent.atomic.AtomicInteger;
2023
import java.util.concurrent.atomic.AtomicReference;
21-
2224
import org.slf4j.Logger;
2325
import org.slf4j.LoggerFactory;
2426
import org.springframework.http.HttpEntity;
@@ -28,101 +30,113 @@
2830
import org.springframework.http.ResponseEntity;
2931
import org.springframework.web.client.RestTemplate;
3032

31-
import de.codecentric.boot.admin.client.config.AdminProperties;
32-
3333
/**
3434
* Registers the client application at spring-boot-admin-server
3535
*/
3636
public class ApplicationRegistrator {
37+
private static final Logger LOGGER = LoggerFactory.getLogger(ApplicationRegistrator.class);
38+
private static final HttpHeaders HTTP_HEADERS = createHttpHeaders();
39+
private final AtomicInteger unsuccessfulAttempts = new AtomicInteger(0);
40+
private final AtomicReference<String> registeredId = new AtomicReference<>();
41+
private final AdminProperties admin;
42+
private final RestTemplate template;
43+
private final ApplicationFactory applicationFactory;
3744

38-
private static final Logger LOGGER = LoggerFactory.getLogger(ApplicationRegistrator.class);
39-
private static final HttpHeaders HTTP_HEADERS = createHttpHeaders();
40-
private final AtomicReference<String> registeredId = new AtomicReference<>();
41-
private final AdminProperties admin;
42-
private final RestTemplate template;
43-
private final ApplicationFactory applicationFactory;
44-
45-
public ApplicationRegistrator(RestTemplate template, AdminProperties admin, ApplicationFactory applicationFactory) {
46-
this.admin = admin;
47-
this.template = template;
48-
this.applicationFactory = applicationFactory;
49-
}
45+
public ApplicationRegistrator(RestTemplate template, AdminProperties admin, ApplicationFactory applicationFactory) {
46+
this.admin = admin;
47+
this.template = template;
48+
this.applicationFactory = applicationFactory;
49+
}
5050

51-
private static HttpHeaders createHttpHeaders() {
52-
HttpHeaders headers = new HttpHeaders();
53-
headers.setContentType(MediaType.APPLICATION_JSON);
54-
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
55-
return HttpHeaders.readOnlyHttpHeaders(headers);
56-
}
51+
private static HttpHeaders createHttpHeaders() {
52+
HttpHeaders headers = new HttpHeaders();
53+
headers.setContentType(MediaType.APPLICATION_JSON);
54+
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
55+
return HttpHeaders.readOnlyHttpHeaders(headers);
56+
}
5757

58-
/**
59-
* Registers the client application at spring-boot-admin-server.
60-
*
61-
* @return true if successful registration on at least one admin server
62-
*/
63-
public boolean register() {
64-
boolean isRegistrationSuccessful = false;
65-
Application self = createApplication();
66-
for (String adminUrl : admin.getAdminUrl()) {
67-
try {
68-
@SuppressWarnings("rawtypes")
69-
ResponseEntity<Map> response = template.postForEntity(adminUrl,
70-
new HttpEntity<>(self, HTTP_HEADERS), Map.class);
58+
/**
59+
* Registers the client application at spring-boot-admin-server.
60+
*
61+
* @return true if successful registration on at least one admin server
62+
*/
63+
public boolean register() {
64+
boolean isRegistrationSuccessful = false;
65+
Application self = createApplication();
66+
for (String adminUrl : admin.getAdminUrl()) {
67+
try {
68+
@SuppressWarnings("rawtypes") ResponseEntity<Map> response = template.postForEntity(adminUrl,
69+
new HttpEntity<>(self, HTTP_HEADERS), Map.class);
7170

72-
if (response.getStatusCode().equals(HttpStatus.CREATED)) {
73-
if (registeredId.compareAndSet(null, response.getBody().get("id").toString())) {
74-
LOGGER.info("Application registered itself as {}", response.getBody());
75-
} else {
76-
LOGGER.debug("Application refreshed itself as {}", response.getBody());
77-
}
71+
if (response.getStatusCode().equals(HttpStatus.CREATED)) {
72+
if (registeredId.compareAndSet(null, response.getBody().get("id").toString())) {
73+
LOGGER.info("Application registered itself as {}", response.getBody());
74+
} else {
75+
LOGGER.debug("Application refreshed itself as {}", response.getBody());
76+
}
7877

79-
isRegistrationSuccessful = true;
80-
if (admin.isRegisterOnce()) {
81-
break;
82-
}
83-
} else {
84-
LOGGER.warn("Application failed to registered itself as {}. Response: {}", self,
85-
response.toString());
86-
}
87-
} catch (Exception ex) {
88-
LOGGER.warn("Failed to register application as {} at spring-boot-admin ({}): {}",
89-
self, admin.getAdminUrl(), ex.getMessage());
90-
}
91-
}
78+
isRegistrationSuccessful = true;
79+
if (admin.isRegisterOnce()) {
80+
break;
81+
}
82+
} else {
83+
if (unsuccessfulAttempts.get() == 0) {
84+
LOGGER.warn(
85+
"Application failed to registered itself as {}. Response: {}. Further attempts are logged on DEBUG level",
86+
self, response.toString());
87+
} else {
88+
LOGGER.debug("Application failed to registered itself as {}. Response: {}", self,
89+
response.toString());
90+
}
91+
}
92+
} catch (Exception ex) {
93+
if (unsuccessfulAttempts.get() == 0) {
94+
LOGGER.warn(
95+
"Failed to register application as {} at spring-boot-admin ({}): {}. Further attempts are logged on DEBUG level",
96+
self, admin.getAdminUrl(), ex.getMessage());
97+
} else {
98+
LOGGER.debug("Failed to register application as {} at spring-boot-admin ({}): {}", self,
99+
admin.getAdminUrl(), ex.getMessage());
100+
}
101+
}
102+
}
103+
if (!isRegistrationSuccessful) {
104+
unsuccessfulAttempts.incrementAndGet();
105+
} else {
106+
unsuccessfulAttempts.set(0);
107+
}
108+
return isRegistrationSuccessful;
109+
}
92110

93-
return isRegistrationSuccessful;
94-
}
111+
public void deregister() {
112+
String id = registeredId.get();
113+
if (id != null) {
114+
for (String adminUrl : admin.getAdminUrl()) {
115+
try {
116+
template.delete(adminUrl + "/" + id);
117+
registeredId.compareAndSet(id, null);
118+
if (admin.isRegisterOnce()) {
119+
break;
120+
}
121+
} catch (Exception ex) {
122+
LOGGER.warn("Failed to deregister application (id={}) at spring-boot-admin ({}): {}", id, adminUrl,
123+
ex.getMessage());
124+
}
125+
}
126+
}
127+
}
95128

96-
public void deregister() {
97-
String id = registeredId.get();
98-
if (id != null) {
99-
for (String adminUrl : admin.getAdminUrl()) {
100-
try {
101-
template.delete(adminUrl + "/" + id);
102-
registeredId.compareAndSet(id, null);
103-
if (admin.isRegisterOnce()) {
104-
break;
105-
}
106-
} catch (Exception ex) {
107-
LOGGER.warn(
108-
"Failed to deregister application (id={}) at spring-boot-admin ({}): {}",
109-
id, adminUrl, ex.getMessage());
110-
}
111-
}
112-
}
113-
}
114-
115-
/**
116-
* Returns the id of this client as given by the admin server.
117-
* Returns null if the client has not registered against the admin server yet.
118-
*
119-
* @return
120-
*/
121-
public String getRegisteredId() {
122-
return registeredId.get();
123-
}
129+
/**
130+
* Returns the id of this client as given by the admin server.
131+
* Returns null if the client has not registered against the admin server yet.
132+
*
133+
* @return
134+
*/
135+
public String getRegisteredId() {
136+
return registeredId.get();
137+
}
124138

125-
protected Application createApplication() {
126-
return applicationFactory.createApplication();
127-
}
139+
protected Application createApplication() {
140+
return applicationFactory.createApplication();
141+
}
128142
}

0 commit comments

Comments
 (0)