Skip to content

Commit

Permalink
feature: Add IdentityManager and create agentId based on PID and host…
Browse files Browse the repository at this point in the history
…name (#26)

* Add IdentityManager and create agentId based on PID and hostname

* Fix review findings

* Fix remaining review findings

* Fix AgentInfoTest
  • Loading branch information
binarycoded authored Oct 31, 2024
1 parent 6fc5c59 commit 7fc92b8
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/* (C) 2024 */
package rocks.inspectit.gepard.agent.internal.identity;

import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Objects;
import rocks.inspectit.gepard.agent.internal.identity.model.IdentityInfo;

/** Responsible for generating the agentId. */
public class IdentityManager {

private static IdentityManager instance;

private final IdentityInfo identityInfo;

private IdentityManager() {
RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();
String vmId = runtime.getName();
this.identityInfo = new IdentityInfo(vmId, hash(vmId));
}

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

public IdentityInfo getIdentityInfo() {
return this.identityInfo;
}

/**
* Hashes the given input with SHA3-256.
*
* @param input the <code>String</code> to be hashed.
* @return the SHA3-256 hashed <code>String</code>
*/
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
@@ -1,5 +1,5 @@
/* (C) 2024 */
package rocks.inspectit.gepard.agent.notification.model;
package rocks.inspectit.gepard.agent.internal.identity.model;

import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
import com.fasterxml.jackson.annotation.PropertyAccessor;
Expand All @@ -11,6 +11,7 @@
import java.lang.management.RuntimeMXBean;
import java.util.Map;
import java.util.Objects;
import rocks.inspectit.gepard.agent.internal.identity.IdentityManager;
import rocks.inspectit.gepard.agent.internal.properties.PropertiesResolver;

/** Meta-information about the current agent */
Expand All @@ -31,18 +32,24 @@ public class AgentInfo {

private final long startTime;

private final long pid;
private final String vmId;

private final String agentId;

private final Map<String, String> attributes;

private AgentInfo() {
IdentityManager identityManager = IdentityManager.getInstance();
IdentityInfo identityInfo = identityManager.getIdentityInfo();
RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();

this.serviceName = getServiceNameFromSdk();
this.gepardVersion = "0.0.1";
this.otelVersion = AgentVersion.VERSION;
this.javaVersion = System.getProperty("java.version");
RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();
this.startTime = runtime.getStartTime();
this.pid = runtime.getPid();
this.vmId = identityInfo.vmId();
this.agentId = identityInfo.agentId();
this.attributes = PropertiesResolver.getAttributes();
}

Expand All @@ -67,15 +74,4 @@ private String getServiceNameFromSdk() {
? "inspectit-gepard-agent"
: configuredServiceName;
}

/**
* @return the service name of the agent.
*/
public String getServiceName() {
return serviceName;
}

public long getPid() {
return pid;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/* (C) 2024 */
package rocks.inspectit.gepard.agent.internal.identity.model;

public final class IdentityInfo {

private final String vmId;
private final String agentId;

public IdentityInfo(String vmId, String agentId) {
this.vmId = vmId;
this.agentId = agentId;
}

public String vmId() {
return vmId;
}

public String agentId() {
return agentId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
import org.apache.hc.client5.http.async.methods.SimpleRequestBuilder;
import org.apache.hc.core5.http.ContentType;
import rocks.inspectit.gepard.agent.notification.model.AgentInfo;
import rocks.inspectit.gepard.agent.internal.identity.model.AgentInfo;

/** This factory should create different HTTP requests for the configuration server */
public class NotificationFactory {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* (C) 2024 */
package rocks.inspectit.gepard.agent.internal.identity;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import rocks.inspectit.gepard.agent.internal.identity.model.IdentityInfo;

@ExtendWith(MockitoExtension.class)
class IdentityManagerTest {

@Test
void testCreateIdentityManagerSuccessfully() {
RuntimeMXBean mockRuntimeMXBean = mock(RuntimeMXBean.class);
when(mockRuntimeMXBean.getName()).thenReturn("12345@mockedHostName");

try (MockedStatic<ManagementFactory> managementFactoryMockedStatic =
Mockito.mockStatic(ManagementFactory.class)) {
managementFactoryMockedStatic
.when(ManagementFactory::getRuntimeMXBean)
.thenReturn(mockRuntimeMXBean);

IdentityManager identityManager = IdentityManager.getInstance();
IdentityInfo identityInfo = identityManager.getIdentityInfo();

assertNotNull(identityInfo);
assertEquals("12345@mockedHostName", identityInfo.vmId());
assertEquals(
"d29aca592fc2071bcef6577d649071d4d54a8ae6cd5c0be0e51f28af2867f207",
identityInfo.agentId());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* (C) 2024 */
package rocks.inspectit.gepard.agent.notification.model;
package rocks.inspectit.gepard.agent.internal.identity.model;

import static org.junit.jupiter.api.Assertions.*;

Expand All @@ -23,7 +23,8 @@ void agentInformationContainsProperties() throws JsonProcessingException {
assertTrue(info.contains("otelVersion"));
assertTrue(info.contains("javaVersion"));
assertTrue(info.contains("startTime"));
assertTrue(info.contains("pid"));
assertTrue(info.contains("vmId"));
assertTrue(info.contains("agentId"));
assertTrue(info.contains("attributes"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.net.URISyntaxException;
import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
import org.junit.jupiter.api.Test;
import rocks.inspectit.gepard.agent.notification.model.AgentInfo;
import rocks.inspectit.gepard.agent.internal.identity.model.AgentInfo;

class NotificationFactoryTest {

Expand Down

0 comments on commit 7fc92b8

Please sign in to comment.