Skip to content

Commit

Permalink
fixup Insights integration for dynamic attach
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewazores committed Feb 26, 2024
1 parent 9b74455 commit 5afbcbc
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 17 deletions.
10 changes: 6 additions & 4 deletions src/main/java/io/cryostat/agent/Agent.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import com.sun.tools.attach.VirtualMachine;
import com.sun.tools.attach.VirtualMachineDescriptor;
import dagger.Component;
import org.eclipse.microprofile.config.Config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import picocli.CommandLine;
Expand All @@ -74,8 +75,6 @@ public class Agent implements Callable<Integer>, Consumer<AgentArgs> {
private static final String ALL_PIDS = "*";
static final String AUTO_ATTACH_PID = "0";

private static InsightsAgentHelper insights;

@Parameters(
index = "0",
defaultValue = "0",
Expand Down Expand Up @@ -151,8 +150,7 @@ public static void premain(String args, Instrumentation instrumentation) {
// JVM application
public static void agentmain(String args, Instrumentation instrumentation) {
log.trace("agentmain");
insights = new InsightsAgentHelper(instrumentation);
AgentArgs aa = AgentArgs.from(args);
AgentArgs aa = AgentArgs.from(instrumentation, args);
Agent agent = new Agent();
Thread t =
new Thread(
Expand Down Expand Up @@ -212,6 +210,8 @@ public void accept(AgentArgs args) {
AgentExitHandler agentExitHandler = null;
try {
final Client client = DaggerAgent_Client.builder().build();
InsightsAgentHelper insights =
new InsightsAgentHelper(client.config(), args.getInstrumentation());

URI baseUri = client.baseUri();
URIRange uriRange = client.uriRange();
Expand Down Expand Up @@ -318,6 +318,8 @@ private static void setupInsightsIfEnabled(
@Singleton
@Component(modules = {MainModule.class})
interface Client {
Config config();

@Named(ConfigModule.CRYOSTAT_AGENT_BASEURI)
URI baseUri();

Expand Down
18 changes: 15 additions & 3 deletions src/main/java/io/cryostat/agent/AgentArgs.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package io.cryostat.agent;

import java.lang.instrument.Instrumentation;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -32,14 +33,25 @@

class AgentArgs {
private static final String DELIMITER = "!";
private final Instrumentation instrumentation;
private final Map<String, String> properties;
private final String smartTriggers;

public AgentArgs(Map<String, String> properties, String smartTriggers) {
public AgentArgs(
Instrumentation instrumentation, Map<String, String> properties, String smartTriggers) {
this.instrumentation = instrumentation;
this.properties = Optional.ofNullable(properties).orElse(Collections.emptyMap());
this.smartTriggers = StringUtils.defaultValue(smartTriggers, "");
}

public AgentArgs(Map<String, String> properties, String smartTriggers) {
this(null, properties, smartTriggers);
}

public Instrumentation getInstrumentation() {
return instrumentation;
}

public Map<String, String> getProperties() {
return properties;
}
Expand All @@ -48,7 +60,7 @@ public String getSmartTriggers() {
return smartTriggers;
}

public static AgentArgs from(String agentmainArg) {
public static AgentArgs from(Instrumentation instrumentation, String agentmainArg) {
Map<String, String> properties = new HashMap<>();
String smartTriggers = "";
if (StringUtils.isNotBlank(agentmainArg)) {
Expand All @@ -70,7 +82,7 @@ public static AgentArgs from(String agentmainArg) {
}
smartTriggers = parts.poll();
}
return new AgentArgs(properties, smartTriggers);
return new AgentArgs(instrumentation, properties, smartTriggers);
}

public String toAgentMain() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import com.redhat.insights.agent.shaded.tls.PEMSupport;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.ConfigProvider;

public class InsightsAgentHelper {

Expand All @@ -54,9 +53,9 @@ public class InsightsAgentHelper {

private final Config config;

public InsightsAgentHelper(Instrumentation instrumentation) {
public InsightsAgentHelper(Config config, Instrumentation instrumentation) {
this.config = config;
this.instrumentation = instrumentation;
this.config = ConfigProvider.getConfig();
}

public boolean isInsightsEnabled(PluginInfo pluginInfo) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import com.redhat.insights.agent.shaded.InsightsReportController;
import com.redhat.insights.agent.shaded.http.InsightsHttpClient;
import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.ConfigProvider;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -61,16 +60,12 @@ public class InsightsAgentHelperTest {
@Mock InsightsReportController controller;
@Captor ArgumentCaptor<AgentConfiguration> configCaptor;
@Captor ArgumentCaptor<Supplier<InsightsHttpClient>> clientSupplierCaptor;
private MockedStatic<ConfigProvider> providerStatic;
MockedStatic<AgentBasicReport> reportStatic;
MockedStatic<InsightsReportController> controllerStatic;
InsightsAgentHelper helper;

@BeforeEach
void setupEach() {
providerStatic = Mockito.mockStatic(ConfigProvider.class);
providerStatic.when(() -> ConfigProvider.getConfig()).thenReturn(config);

reportStatic = Mockito.mockStatic(AgentBasicReport.class);
reportStatic.when(() -> AgentBasicReport.of(any())).thenReturn(report);

Expand All @@ -83,12 +78,11 @@ void setupEach() {
Collections.singletonMap("INSIGHTS_SVC", "http://insights-proxy.example.com:8080");
when(pluginInfo.getEnv()).thenReturn(env);

this.helper = new InsightsAgentHelper(instrumentation);
this.helper = new InsightsAgentHelper(config, instrumentation);
}

@AfterEach
void teardownEach() {
providerStatic.close();
reportStatic.close();
controllerStatic.close();
}
Expand Down

0 comments on commit 5afbcbc

Please sign in to comment.