Skip to content

Commit

Permalink
fix: refactoring according review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
binarycoded committed Nov 19, 2024
1 parent d1671e6 commit 2e27d4b
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}

/**
Expand All @@ -40,17 +48,17 @@ private static SimpleRequestBuilder buildRequestWithHeaders(
SimpleRequestBuilder requestBuilder, Agent agent) {
Map<String, String> 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;
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down

0 comments on commit 2e27d4b

Please sign in to comment.