Skip to content

Commit 881ab1b

Browse files
authored
Merge pull request quarkusio#34274 from brunobat/build-analytics-part3
Build analytics - Improvements II
2 parents 53a64ea + b55b186 commit 881ab1b

File tree

16 files changed

+203
-146
lines changed

16 files changed

+203
-146
lines changed

docs/src/main/asciidoc/build-analytics.adoc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,9 @@ Analytics configurations are stored at the `.redhat` folder in the user's home d
130130

131131
`anonymousId` is the locally generated anonymous user id.
132132

133-
`com.redhat.devtools.quarkus.localconfig` holds if the user has agreed to send analytics data or not.
133+
`io.quarkus.analytics.localconfig` holds if the user has agreed to send analytics data or not.
134134

135-
`com.redhat.devtools.quarkus.remoteconfig` cache of the remote configuration that allows to disable analytics globally.
135+
`io.quarkus.analytics.remoteconfig` cache of the remote configuration that allows to disable analytics globally.
136136

137137
== Show me the code!
138138

@@ -146,10 +146,10 @@ The uploaded events are stored under the project's `/target` folder with the nam
146146

147147
If you have opted in and would like to disable build time analytics, you can do so in two ways:
148148

149-
* Globally, by editing the `com.redhat.devtools.quarkus.localconfig` file in the '.redhat' folder of your user’s home directory. Update the file as follows:
149+
* Globally, by editing the `io.quarkus.analytics.localconfig` file in the '.redhat' folder of your user’s home directory. Update the file as follows:
150150
[source,json]
151151
----
152-
{"active":false}
152+
{"disabled":true}
153153
----
154154

155155
* Per project, by using the system property `quarkus.analytics.disabled=true` when building the project.

independent-projects/tools/analytics-common/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@
6161
<artifactId>mockito-core</artifactId>
6262
<scope>test</scope>
6363
</dependency>
64+
<dependency>
65+
<groupId>uk.org.webcompere</groupId>
66+
<artifactId>system-stubs-jupiter</artifactId>
67+
<version>${system-stubs-jupiter.version}</version>
68+
<scope>test</scope>
69+
</dependency>
6470
</dependencies>
6571

6672
</project>

independent-projects/tools/analytics-common/src/main/java/io/quarkus/analytics/AnalyticsService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
import io.quarkus.analytics.dto.segment.TrackProperties;
6161
import io.quarkus.analytics.rest.RestClient;
6262
import io.quarkus.analytics.util.FileUtils;
63+
import io.quarkus.analytics.util.PropertyUtils;
6364
import io.quarkus.bootstrap.model.ApplicationModel;
6465
import io.quarkus.devtools.messagewriter.MessageWriter;
6566
import io.quarkus.maven.dependency.ArtifactCoords;
@@ -122,7 +123,7 @@ public void close() {
122123
try {
123124
// complete all. Normally, the queue should have only 1 element.
124125
CompletableFuture.allOf(postFutures.toArray(new CompletableFuture[0])).get(
125-
Integer.getInteger("quarkus.analytics.timeout", DEFAULT_TIMEOUT),
126+
PropertyUtils.getProperty("quarkus.analytics.timeout", DEFAULT_TIMEOUT),
126127
TimeUnit.MILLISECONDS);
127128
if (log.isDebugEnabled() && !postFutures.isEmpty()) {
128129
log.debug("[Quarkus build analytics] Build analytics sent successfully. Sent event can be seen at .../target/" +

independent-projects/tools/analytics-common/src/main/java/io/quarkus/analytics/ConfigService.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.quarkus.analytics;
22

3+
import static io.quarkus.analytics.util.PropertyUtils.getProperty;
4+
35
import java.io.IOException;
46
import java.nio.file.Files;
57
import java.nio.file.Path;
@@ -74,8 +76,8 @@ public ConfigService(final ConfigClient client, final AnonymousUserId userId, fi
7476
}
7577

7678
public void userAcceptance(Function<String, String> analyticsEnabledSupplier) {
77-
final int timeout = Integer.getInteger(QUARKUS_ANALYTICS_PROMPT_TIMEOUT, 10);
78-
if (Files.exists(localConfigFile) || Boolean.getBoolean(QUARKUS_ANALYTICS_DISABLED_LOCAL_PROP)) {
79+
final int timeout = getProperty(QUARKUS_ANALYTICS_PROMPT_TIMEOUT, 10);
80+
if (Files.exists(localConfigFile) || getProperty(QUARKUS_ANALYTICS_DISABLED_LOCAL_PROP, false)) {
7981
return; // ask nothing
8082
} else {
8183
try {
@@ -89,7 +91,8 @@ public void userAcceptance(Function<String, String> analyticsEnabledSupplier) {
8991
}
9092
final boolean isActive = userInput.equals("y") || userInput.equals("yes") || userInput.startsWith("yy");
9193
FileUtils.createFileAndParent(localConfigFile);
92-
FileUtils.write(new LocalConfig(isActive), localConfigFile);
94+
final boolean isDisabled = !isActive;// just to make it explicit
95+
FileUtils.write(new LocalConfig(isDisabled), localConfigFile);
9396
log.info("[Quarkus build analytics] Quarkus Build Analytics " + (isActive ? "enabled" : "disabled")
9497
+ " by the user." + NEW_LINE);
9598
} catch (TimeoutException e) {
@@ -122,12 +125,12 @@ public boolean isActive() {
122125
if (!Files.exists(localConfigFile)) {
123126
return false; // disabled because user has not decided yet
124127
} else if (!loadConfig(LocalConfig.class, localConfigFile)
125-
.map(LocalConfig::isActive)
128+
.map(localConfig -> !localConfig.isDisabled())
126129
.orElse(true)) {
127130
return false; // disabled by the user and recorded on the local config
128131
}
129132

130-
if (Boolean.getBoolean(QUARKUS_ANALYTICS_DISABLED_LOCAL_PROP)) {
133+
if (getProperty(QUARKUS_ANALYTICS_DISABLED_LOCAL_PROP, false)) {
131134
return false; // disabled by local property
132135
}
133136
AnalyticsRemoteConfig analyticsRemoteConfig = getRemoteConfig();

independent-projects/tools/analytics-common/src/main/java/io/quarkus/analytics/config/FileLocationsImpl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ public class FileLocationsImpl implements FileLocations {
1515
".redhat");
1616

1717
private static final Path UUID_FILE = RED_HAT.resolve("anonymousId");
18-
private static final Path REMOTE_CONFIG_FILE = RED_HAT.resolve("com.redhat.devtools.quarkus.remoteconfig");
18+
private static final Path REMOTE_CONFIG_FILE = RED_HAT.resolve("io.quarkus.analytics.remoteconfig");
1919
private static final Path LAST_REMOTE_CONFIG_TRY_FILE = RED_HAT.resolve(
20-
"com.redhat.devtools.quarkus.analytics.lasttry");
21-
private static final Path LOCAL_CONFIG_FILE = RED_HAT.resolve("com.redhat.devtools.quarkus.localconfig");
20+
"io.quarkus.analytics.lasttry");
21+
private static final Path LOCAL_CONFIG_FILE = RED_HAT.resolve("io.quarkus.analytics.localconfig");
2222
private static final String BUILD_ANALYTICS_EVENT_FILE_NAME = "build-analytics-event.json";
2323

2424
// singleton

independent-projects/tools/analytics-common/src/main/java/io/quarkus/analytics/dto/config/AnalyticsLocalConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ public interface AnalyticsLocalConfig {
55
* @return true if the analytics is enabled
66
* @return
77
*/
8-
boolean isActive();
8+
boolean isDisabled();
99
}

independent-projects/tools/analytics-common/src/main/java/io/quarkus/analytics/dto/config/LocalConfig.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,21 @@
33
import java.io.Serializable;
44

55
public class LocalConfig implements AnalyticsLocalConfig, Serializable {
6-
private boolean active;
6+
private boolean disabled;
77

8-
public LocalConfig(boolean active) {
9-
this.active = active;
8+
public LocalConfig(boolean disabled) {
9+
this.disabled = disabled;
1010
}
1111

1212
public LocalConfig() {
1313
}
1414

1515
@Override
16-
public boolean isActive() {
17-
return active;
16+
public boolean isDisabled() {
17+
return disabled;
1818
}
1919

20-
public void setActive(boolean active) {
21-
this.active = active;
20+
public void setDisabled(boolean disabled) {
21+
this.disabled = disabled;
2222
}
2323
}

independent-projects/tools/analytics-common/src/main/java/io/quarkus/analytics/rest/RestClient.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.quarkus.analytics.rest;
22

3+
import static io.quarkus.analytics.util.PropertyUtils.getProperty;
34
import static io.quarkus.analytics.util.StringUtils.getObjectMapper;
45
import static java.util.concurrent.TimeUnit.MILLISECONDS;
56

@@ -64,11 +65,11 @@ static String getAuthHeader(final String key) {
6465

6566
private final URI segmentTraceUri;
6667

67-
private final int timeoutMs = Integer.getInteger("quarkus.analytics.timeout", DEFAULT_TIMEOUT);
68+
private final int timeoutMs = getProperty("quarkus.analytics.timeout", DEFAULT_TIMEOUT);
6869

6970
public RestClient(MessageWriter log) {
7071
this.log = log;
71-
final String segmentBaseUri = System.getProperty("quarkus.analytics.uri.base", "https://api.segment.io/");
72+
final String segmentBaseUri = getProperty("quarkus.analytics.uri.base", "https://api.segment.io/");
7273
this.segmentIdentityUri = getUri(segmentBaseUri + IDENTITY_ENDPOINT);
7374
this.segmentTraceUri = getUri(segmentBaseUri + TRACK_ENDPOINT);
7475
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package io.quarkus.analytics.util;
2+
3+
public class PropertyUtils {
4+
5+
public static Integer getProperty(String propertyName, int defaultValue) {
6+
if (propertyName == null) {
7+
throw new IllegalArgumentException("Property name cannot be null");
8+
}
9+
Integer result = Integer.getInteger(propertyName);
10+
try {
11+
if (result == null) {
12+
String stringValue = System.getenv(transformToEnvVarName(propertyName));
13+
if (stringValue != null) {
14+
result = Integer.parseInt(stringValue);
15+
} else {
16+
result = defaultValue;
17+
}
18+
}
19+
} catch (NumberFormatException e) {
20+
result = defaultValue;
21+
}
22+
return result;
23+
}
24+
25+
public static String getProperty(String propertyName, String defaultValue) {
26+
if (propertyName == null) {
27+
throw new IllegalArgumentException("Property name cannot be null");
28+
}
29+
String result = System.getProperty(propertyName);
30+
try {
31+
if (result == null) {
32+
String stringValue = System.getenv(transformToEnvVarName(propertyName));
33+
if (stringValue != null) {
34+
result = stringValue;
35+
} else {
36+
result = defaultValue;
37+
}
38+
}
39+
} catch (NumberFormatException e) {
40+
result = defaultValue;
41+
}
42+
return result;
43+
}
44+
45+
public static boolean getProperty(String propertyName, boolean defaultValue) {
46+
if (propertyName == null) {
47+
throw new IllegalArgumentException("Property name cannot be null");
48+
}
49+
boolean result;
50+
String systemValue = System.getProperty(propertyName);
51+
try {
52+
if (systemValue == null) {
53+
String envValue = System.getenv(transformToEnvVarName(propertyName));
54+
if (envValue != null) {
55+
result = Boolean.parseBoolean(envValue);
56+
} else {
57+
result = defaultValue;
58+
}
59+
} else {
60+
result = Boolean.parseBoolean(systemValue);
61+
}
62+
} catch (NumberFormatException e) {
63+
result = defaultValue;
64+
}
65+
return result;
66+
}
67+
68+
private static String transformToEnvVarName(String propertyName) {
69+
return propertyName.toUpperCase().replace('.', '_');
70+
}
71+
}

independent-projects/tools/analytics-common/src/test/java/io/quarkus/analytics/AnalyticsServicePromptTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ void testConsoleQuestion_yes() throws IOException {
4747
Optional<LocalConfig> localConfig = FileUtils.read(LocalConfig.class, fileLocations.getLocalConfigFile(),
4848
MessageWriter.info());
4949
assertTrue(localConfig.isPresent());
50-
assertTrue(localConfig.get().isActive());
50+
assertFalse(localConfig.get().isDisabled());
5151
}
5252

5353
@Test
@@ -61,7 +61,7 @@ void testConsoleQuestion_no() throws IOException {
6161
Optional<LocalConfig> localConfig = FileUtils.read(LocalConfig.class, fileLocations.getLocalConfigFile(),
6262
MessageWriter.info());
6363
assertTrue(localConfig.isPresent());
64-
assertFalse(localConfig.get().isActive());
64+
assertTrue(localConfig.get().isDisabled());
6565
}
6666

6767
@Test

0 commit comments

Comments
 (0)