From 13033dcd75b571de76ab25ebbdd92d4b7fb67ea4 Mon Sep 17 00:00:00 2001 From: Elliott Baron Date: Mon, 27 Nov 2023 12:11:50 -0500 Subject: [PATCH] feat(insights): add config parameter to opt out of Insights (#267) --- README.md | 1 + .../agent/insights/InsightsAgentHelper.java | 6 +++++- .../agent/insights/InsightsAgentHelperTest.java | 14 ++++++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index a12c0d8d..e0417ed1 100644 --- a/README.md +++ b/README.md @@ -148,6 +148,7 @@ and how it advertises itself to a Cryostat server instance. Required properties - [ ] `cryostat.agent.harvester.max-size-b` [`long`]: the JFR `maxsize` setting, specified in bytes, to apply to periodic uploads during the application lifecycle. Defaults to `0`, which means `unlimited`. - [ ] `cryostat.agent.smart-trigger.definitions` [`String[]`]: a comma-separated list of Smart Trigger definitions to load at startup. Defaults to the empty string: no Smart Triggers. - [ ] `cryostat.agent.smart-trigger.evaluation.period-ms` [`long`]: the length of time between Smart Trigger evaluations. Default `1000`. +- [ ] `rht.insights.java.opt-out` [`boolean`]: for the Red Hat build of Cryostat, set this to true to disable data collection for Red Hat Insights. Defaults to `false`. Red Hat Insights data collection is always disabled for community builds of Cryostat. These properties can be set by JVM system properties or by environment variables. For example, the property `cryostat.agent.baseuri` can be set using `-Dcryostat.agent.baseuri=https://mycryostat.example.com:1234/` or diff --git a/src/main/java/io/cryostat/agent/insights/InsightsAgentHelper.java b/src/main/java/io/cryostat/agent/insights/InsightsAgentHelper.java index 7ff1c66b..dcc572be 100644 --- a/src/main/java/io/cryostat/agent/insights/InsightsAgentHelper.java +++ b/src/main/java/io/cryostat/agent/insights/InsightsAgentHelper.java @@ -45,6 +45,7 @@ public class InsightsAgentHelper { private static final String INSIGHTS_SVC = "INSIGHTS_SVC"; + static final String RHT_INSIGHTS_JAVA_OPT_OUT = "rht.insights.java.opt-out"; private static final InsightsLogger log = new SLF4JWrapper(LoggerFactory.getLogger(InsightsAgentHelper.class)); @@ -61,7 +62,10 @@ public InsightsAgentHelper(Instrumentation instrumentation) { } public boolean isInsightsEnabled(PluginInfo pluginInfo) { - return pluginInfo.getEnv().containsKey(INSIGHTS_SVC); + // Check if the user has opted out + boolean optingOut = + config.getOptionalValue(RHT_INSIGHTS_JAVA_OPT_OUT, boolean.class).orElse(false); + return pluginInfo.getEnv().containsKey(INSIGHTS_SVC) && !optingOut; } public void runInsightsAgent(PluginInfo pluginInfo) { diff --git a/src/test/java/io/cryostat/agent/insights/InsightsAgentHelperTest.java b/src/test/java/io/cryostat/agent/insights/InsightsAgentHelperTest.java index c81d95c7..7a7f5eaa 100644 --- a/src/test/java/io/cryostat/agent/insights/InsightsAgentHelperTest.java +++ b/src/test/java/io/cryostat/agent/insights/InsightsAgentHelperTest.java @@ -104,6 +104,20 @@ void testInsightsDisabled() { Assertions.assertFalse(helper.isInsightsEnabled(pluginInfo)); } + @Test + void testInsightsOptingOut() { + when(config.getOptionalValue("rht.insights.java.opt-out", boolean.class)) + .thenReturn(Optional.of(true)); + Assertions.assertFalse(helper.isInsightsEnabled(pluginInfo)); + } + + @Test + void testInsightsNotOptingOut() { + when(config.getOptionalValue("rht.insights.java.opt-out", boolean.class)) + .thenReturn(Optional.of(false)); + Assertions.assertTrue(helper.isInsightsEnabled(pluginInfo)); + } + @Test void testRunInsightsAgent() { when(config.getValue(ConfigModule.CRYOSTAT_AGENT_APP_NAME, String.class))