Skip to content

Commit

Permalink
Add agent identifier generation
Browse files Browse the repository at this point in the history
  • Loading branch information
binarycoded committed Oct 29, 2024
1 parent 6fc5c59 commit 966305c
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import rocks.inspectit.gepard.agent.instrumentation.state.InstrumentationState;
import rocks.inspectit.gepard.agent.instrumentation.state.configuration.InspectitConfigurationHolder;
import rocks.inspectit.gepard.agent.instrumentation.state.configuration.resolver.ConfigurationResolver;
import rocks.inspectit.gepard.agent.internal.identity.IdentityManager;
import rocks.inspectit.gepard.agent.internal.otel.OpenTelemetryAccessor;
import rocks.inspectit.gepard.agent.notification.NotificationManager;
import rocks.inspectit.gepard.agent.transformation.TransformationManager;
Expand Down Expand Up @@ -68,6 +69,10 @@ public AgentBuilder extend(AgentBuilder agentBuilder, ConfigProperties config) {
instrumentationManager.startClassDiscovery();
instrumentationManager.startBatchInstrumentation(instrumentationState);

// Set agentId based on generation type
IdentityManager identityManager = IdentityManager.getInstance();
identityManager.setIdentity();

// Start loading the inspectIT configuration
ConfigurationManager configurationManager = ConfigurationManager.create();
configurationManager.loadConfiguration();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import rocks.inspectit.gepard.agent.configuration.persistence.file.ConfigurationFileReader;
import rocks.inspectit.gepard.agent.configuration.persistence.file.ConfigurationFileWriter;
import rocks.inspectit.gepard.agent.internal.file.FileAccessor;
import rocks.inspectit.gepard.agent.internal.identity.IdentityManager;
import rocks.inspectit.gepard.agent.internal.properties.PropertiesResolver;
import rocks.inspectit.gepard.agent.internal.schedule.InspectitScheduler;

Expand Down Expand Up @@ -46,7 +47,9 @@ public void loadConfiguration() {
*/
private void startHttpPolling(String serverUrl, ConfigurationPersistence persistence) {
InspectitScheduler scheduler = InspectitScheduler.getInstance();
HttpConfigurationPoller poller = new HttpConfigurationPoller(serverUrl, persistence);
IdentityManager identityManager = IdentityManager.getInstance();
HttpConfigurationPoller poller =
new HttpConfigurationPoller(serverUrl, identityManager.getAgentId(), persistence);
Duration pollingInterval = PropertiesResolver.getPollingInterval();
scheduler.startRunnable(poller, pollingInterval);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ private HttpConfigurationFactory() {}
* @return the HTTP post request, containing agent information
* @throws URISyntaxException invalid uri
*/
public static SimpleHttpRequest createConfigurationRequest(String baseUrl)
public static SimpleHttpRequest createConfigurationRequest(String baseUrl, String agentId)
throws URISyntaxException {
URI uri = new URIBuilder(baseUrl + "/agent-configuration").build();

return SimpleRequestBuilder.get(uri).build();
return SimpleRequestBuilder.get(uri).addParameter("id", agentId).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@ public class HttpConfigurationPoller implements NamedRunnable {

private final String serverUrl;

private final String agentId;

private final ConfigurationPersistence persistence;

/** Flag to mark first polling attempt */
private boolean isFirstAttempt = true;

public HttpConfigurationPoller(String serverUrl, ConfigurationPersistence persistence) {
public HttpConfigurationPoller(
String serverUrl, String agentId, ConfigurationPersistence persistence) {
this.serverUrl = serverUrl;
this.agentId = agentId;
this.persistence = persistence;
}

Expand Down Expand Up @@ -63,7 +67,7 @@ boolean pollConfiguration() {
private SimpleHttpRequest createConfigurationRequest() {
SimpleHttpRequest request = null;
try {
request = HttpConfigurationFactory.createConfigurationRequest(serverUrl);
request = HttpConfigurationFactory.createConfigurationRequest(serverUrl, agentId);
} catch (URISyntaxException e) {
log.error("Error building HTTP URI for configuration polling", e);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/* (C) 2024 */
package rocks.inspectit.gepard.agent.internal.identity;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Objects;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import rocks.inspectit.gepard.agent.internal.identity.type.IdGenerationType;
import rocks.inspectit.gepard.agent.internal.properties.PropertiesResolver;

public class IdentityManager {
private static final Logger log = LoggerFactory.getLogger(IdentityManager.class);

private String agentId;

private static IdentityManager instance;

private IdentityManager() {}

public static IdentityManager getInstance() {
if (Objects.isNull(instance)) instance = new IdentityManager();
return instance;
}

public String getAgentId() {
return agentId;
}

public void setIdentity() {
IdGenerationType idGenerationType = PropertiesResolver.getIdGenerationType();
try {
switch (idGenerationType) {
case FQDN -> agentId = hash(InetAddress.getLocalHost().getCanonicalHostName());
case IP_ADDRESS -> agentId = hash(InetAddress.getLocalHost().getHostAddress());
case SERVICE_PROCESS_NAME ->
ProcessHandle.current()
.info()
.commandLine()
.ifPresent(commandLine -> agentId = commandLine.trim());
}
} catch (UnknownHostException e) {
log.info("Could not determine hostname", e);
}
}

private static String hash(String input) {
MessageDigest messageDigest;
try {
messageDigest = MessageDigest.getInstance("SHA3-256");
} catch (NoSuchAlgorithmException e) {
throw new UnsupportedOperationException("SHA3-256 not supported", e);
}
byte[] bytes = messageDigest.digest(input.getBytes(StandardCharsets.UTF_8));
StringBuilder hexString = new StringBuilder(2 * bytes.length);
for (byte b : bytes) {
String hex = Integer.toHexString(0xff & b);
if (hex.length() == 1) {
hexString.append('0');
}
hexString.append(hex);
}
return hexString.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* (C) 2024 */
package rocks.inspectit.gepard.agent.internal.identity.type;

public enum IdGenerationType {
IP_ADDRESS,
FQDN,
SERVICE_PROCESS_NAME
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import rocks.inspectit.gepard.agent.internal.identity.type.IdGenerationType;

/**
* This resolver provides configurable properties or their default values. Currently, it is possible
Expand Down Expand Up @@ -37,6 +38,12 @@ public class PropertiesResolver {
"inspectit.config.http.attributes.";
public static final String ATTRIBUTES_ENV_PROPERTY_PREFIX = "INSPECTIT_CONFIG_HTTP_ATTRIBUTES_";

public static final String ID_GENERATION_TYPE_SYSTEM_PROPERTY =
"inspectit.config.id-generation-type";

public static final String ID_GENERATION_TYPE_ENV_PROPERTY =
"INSPECTIT_CONFIG_ID_GENERATION_TYPE";

private static final Logger log = LoggerFactory.getLogger(PropertiesResolver.class);

private PropertiesResolver() {}
Expand Down Expand Up @@ -167,4 +174,36 @@ private static Map<String, String> getAttributesFromSystemProperties() {
entry.getKey().toString().substring(ATTRIBUTES_SYSTEM_PROPERTY_PREFIX.length()),
entry -> entry.getValue().toString()));
}

/**
* TODO
*
* @return
*/
public static IdGenerationType getIdGenerationType() {
String idGenerationTypeSystemProperty = System.getProperty(ID_GENERATION_TYPE_SYSTEM_PROPERTY);
if (Objects.nonNull(idGenerationTypeSystemProperty))
return convertToIdGenerationType(idGenerationTypeSystemProperty);

String idGenerationTypeEnvProperty = System.getenv(ID_GENERATION_TYPE_ENV_PROPERTY);
return convertToIdGenerationType(idGenerationTypeEnvProperty);
}

/**
* TODO
*
* @param idGenerationType
* @return
*/
private static IdGenerationType convertToIdGenerationType(String idGenerationType) {
IdGenerationType idGeneration = IdGenerationType.FQDN;
if (Objects.nonNull(idGenerationType)) {
try {
idGeneration = IdGenerationType.valueOf(idGenerationType);
} catch (IllegalArgumentException e) {
log.info("Unknown id-generation type: {}. Using fallback: FQDN", idGenerationType);
}
}
return idGeneration;
}
}

0 comments on commit 966305c

Please sign in to comment.