diff --git a/inspectit-gepard-agent/src/main/java/rocks/inspectit/gepard/agent/configuration/http/HttpConfigurationCallback.java b/inspectit-gepard-agent/src/main/java/rocks/inspectit/gepard/agent/configuration/http/HttpConfigurationCallback.java index 167999c..81c2555 100644 --- a/inspectit-gepard-agent/src/main/java/rocks/inspectit/gepard/agent/configuration/http/HttpConfigurationCallback.java +++ b/inspectit-gepard-agent/src/main/java/rocks/inspectit/gepard/agent/configuration/http/HttpConfigurationCallback.java @@ -2,11 +2,8 @@ package rocks.inspectit.gepard.agent.configuration.http; import java.io.IOException; -import java.util.Objects; import org.apache.hc.client5.http.async.methods.SimpleHttpResponse; import org.apache.hc.core5.concurrent.FutureCallback; -import org.apache.hc.core5.http.Header; -import org.apache.hc.core5.http.ProtocolException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import rocks.inspectit.gepard.agent.internal.configuration.observer.ConfigurationReceivedSubject; @@ -49,25 +46,18 @@ public void cancelled() { private void logStatus(SimpleHttpResponse response) { int statusCode = response.getCode(); - log.info("Received status code {}", statusCode); - if (statusCode == 404) { - log.error("Configuration not found on configuration server"); - } else if (statusCode == 200) { - try { - Header registrationHeader = response.getHeader("x-gepard-service-registered"); - if (Objects.isNull(registrationHeader)) { - log.error("Configuration server did not return registration header!"); - } else if (registrationHeader.getValue().equals("true")) { - log.info("Connection to configuration server was successfully established."); - } else { - log.debug("Connection to configuration server reused."); - } - } catch (ProtocolException e) { - log.error( - "Error reading response header. There might be an issue with the config-server!", e); - } + if (statusCode == 200) { log.info("Configuration fetched successfully"); + } else if (statusCode == 201) { + log.info( + "Connection to configuration server was successfully established. Configuration fetched successfully"); + } else if (statusCode == 404) { + log.error("Configuration not found on configuration server"); + } else { + log.error( + "Unexpected status code: {}. Please check the configuration server connection.", + statusCode); } } } diff --git a/inspectit-gepard-agent/src/main/java/rocks/inspectit/gepard/agent/configuration/http/HttpConfigurationFactory.java b/inspectit-gepard-agent/src/main/java/rocks/inspectit/gepard/agent/configuration/http/HttpConfigurationFactory.java index 005e66b..feb62c8 100644 --- a/inspectit-gepard-agent/src/main/java/rocks/inspectit/gepard/agent/configuration/http/HttpConfigurationFactory.java +++ b/inspectit-gepard-agent/src/main/java/rocks/inspectit/gepard/agent/configuration/http/HttpConfigurationFactory.java @@ -16,6 +16,14 @@ */ public class HttpConfigurationFactory { + public static final String X_GEPARD_SERVICE_NAME = "x-gepard-service-name"; + public static final String X_GEPARD_VM_ID = "x-gepard-vm-id"; + public static final String X_GEPARD_GEPARD_VERSION = "x-gepard-gepard-version"; + public static final String X_GEPARD_OTEL_VERSION = "x-gepard-otel-version"; + public static final String X_GEPARD_JAVA_VERSION = "x-gepard-java-version"; + public static final String X_GEPARD_START_TIME = "x-gepard-start-time"; + public static final String X_GEPARD_ATTRIBUTE = "x-gepard-attribute-"; + private HttpConfigurationFactory() {} /** @@ -40,17 +48,17 @@ private static SimpleRequestBuilder buildRequestWithHeaders( SimpleRequestBuilder requestBuilder, Agent agent) { Map headers = Map.of( - "x-gepard-service-Name", agent.getServiceName(), - "x-gepard-vm-id", agent.getVmId(), - "x-gepard-gepard-version", agent.getGepardVersion(), - "x-gepard-otel-version", agent.getOtelVersion(), - "x-gepard-java-version", agent.getJavaVersion(), - "x-gepard-start-time", agent.getStartTime().toString()); + X_GEPARD_SERVICE_NAME, agent.getServiceName(), + X_GEPARD_VM_ID, agent.getVmId(), + X_GEPARD_GEPARD_VERSION, agent.getGepardVersion(), + X_GEPARD_OTEL_VERSION, agent.getOtelVersion(), + X_GEPARD_JAVA_VERSION, agent.getJavaVersion(), + X_GEPARD_START_TIME, agent.getStartTime().toString()); headers.forEach(requestBuilder::addHeader); agent .getAttributes() - .forEach((key, value) -> requestBuilder.addHeader("X-Gepard-Attribute-" + key, value)); + .forEach((key, value) -> requestBuilder.addHeader(X_GEPARD_ATTRIBUTE + key, value)); return requestBuilder; } diff --git a/inspectit-gepard-agent/src/main/java/rocks/inspectit/gepard/agent/notification/http/NotificationCallback.java b/inspectit-gepard-agent/src/main/java/rocks/inspectit/gepard/agent/notification/http/NotificationCallback.java deleted file mode 100644 index ebebefb..0000000 --- a/inspectit-gepard-agent/src/main/java/rocks/inspectit/gepard/agent/notification/http/NotificationCallback.java +++ /dev/null @@ -1,27 +0,0 @@ -/* (C) 2024 */ -package rocks.inspectit.gepard.agent.notification.http; - -import org.apache.hc.client5.http.async.methods.SimpleHttpResponse; -import org.apache.hc.core5.concurrent.FutureCallback; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** Callback for notifications to the configuration server. Currently, only used for logging. */ -public class NotificationCallback implements FutureCallback { - private static final Logger log = LoggerFactory.getLogger(NotificationCallback.class); - - @Override - public void completed(SimpleHttpResponse result) { - log.info("Notified configuration server and received status code {}", result.getCode()); - } - - @Override - public void failed(Exception ex) { - log.error("Failed to notify configuration server", ex); - } - - @Override - public void cancelled() { - log.info("Cancelled notification to configuration server"); - } -} diff --git a/inspectit-gepard-agent/src/main/java/rocks/inspectit/gepard/agent/notification/http/NotificationFactory.java b/inspectit-gepard-agent/src/main/java/rocks/inspectit/gepard/agent/notification/http/NotificationFactory.java index ddcc0c9..ad0f80a 100644 --- a/inspectit-gepard-agent/src/main/java/rocks/inspectit/gepard/agent/notification/http/NotificationFactory.java +++ b/inspectit-gepard-agent/src/main/java/rocks/inspectit/gepard/agent/notification/http/NotificationFactory.java @@ -24,27 +24,6 @@ public class NotificationFactory { private NotificationFactory() {} - /** - * Create an HTTP post request to notify the configuration server about the starting agent and - * it's information. - * - * @param baseUrl the base url of the configuration server - * @return the HTTP post request, containing agent information - * @throws URISyntaxException invalid uri - * @throws JsonProcessingException corrupted agent information - */ - public static SimpleHttpRequest createStartNotification(String baseUrl) - throws URISyntaxException, JsonProcessingException { - String agentId = AgentInfo.INFO.getAgentId(); - URI uri = new URI(baseUrl + "/connections/" + agentId); - String agentInfoString = mapper.writeValueAsString(AgentInfo.INFO.getAgent()); - - return SimpleRequestBuilder.post(uri) - .setBody(agentInfoString, ContentType.APPLICATION_JSON) - .setHeader("content-type", "application/json") - .build(); - } - /** * Create an HTTP put request to notify the configuration server about the shutting down agent. * diff --git a/inspectit-gepard-agent/src/test/java/rocks/inspectit/gepard/agent/notification/http/NotificationFactoryTest.java b/inspectit-gepard-agent/src/test/java/rocks/inspectit/gepard/agent/notification/http/NotificationFactoryTest.java index 46833e3..b2d0d14 100644 --- a/inspectit-gepard-agent/src/test/java/rocks/inspectit/gepard/agent/notification/http/NotificationFactoryTest.java +++ b/inspectit-gepard-agent/src/test/java/rocks/inspectit/gepard/agent/notification/http/NotificationFactoryTest.java @@ -22,27 +22,6 @@ class NotificationFactoryTest { private final String agentId = AgentInfo.INFO.getAgentId(); - @Test - void validUrlCreatesStartNotification() throws Exception { - String baseUrl = "http://localhost:8080"; - String url = "http://localhost:8080/connections/" + agentId; - String contentType = "application/json"; - String agent = mapper.writeValueAsString(AgentInfo.INFO.getAgent()); - - SimpleHttpRequest request = NotificationFactory.createStartNotification(baseUrl); - - assertEquals(url, request.getUri().toString()); - assertEquals(contentType, request.getHeader("content-type").getValue()); - assertEquals(agent, request.getBodyText()); - } - - @Test - void invalidStartNotificationUrlThrowsException() { - String url = "invalid url"; - - assertThrows(URISyntaxException.class, () -> NotificationFactory.createStartNotification(url)); - } - @Test void validUrlCreatesShutdownNotification() throws Exception { String baseUrl = "http://localhost:8080";