Skip to content

Commit

Permalink
Merge pull request quarkusio#34274 from brunobat/build-analytics-part3
Browse files Browse the repository at this point in the history
Build analytics - Improvements II
  • Loading branch information
maxandersen authored Jun 27, 2023
2 parents 53a64ea + b55b186 commit 881ab1b
Show file tree
Hide file tree
Showing 16 changed files with 203 additions and 146 deletions.
8 changes: 4 additions & 4 deletions docs/src/main/asciidoc/build-analytics.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,9 @@ Analytics configurations are stored at the `.redhat` folder in the user's home d

`anonymousId` is the locally generated anonymous user id.

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

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

== Show me the code!

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

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

* 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:
* Globally, by editing the `io.quarkus.analytics.localconfig` file in the '.redhat' folder of your user’s home directory. Update the file as follows:
[source,json]
----
{"active":false}
{"disabled":true}
----

* Per project, by using the system property `quarkus.analytics.disabled=true` when building the project.
Expand Down
6 changes: 6 additions & 0 deletions independent-projects/tools/analytics-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>uk.org.webcompere</groupId>
<artifactId>system-stubs-jupiter</artifactId>
<version>${system-stubs-jupiter.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
import io.quarkus.analytics.dto.segment.TrackProperties;
import io.quarkus.analytics.rest.RestClient;
import io.quarkus.analytics.util.FileUtils;
import io.quarkus.analytics.util.PropertyUtils;
import io.quarkus.bootstrap.model.ApplicationModel;
import io.quarkus.devtools.messagewriter.MessageWriter;
import io.quarkus.maven.dependency.ArtifactCoords;
Expand Down Expand Up @@ -122,7 +123,7 @@ public void close() {
try {
// complete all. Normally, the queue should have only 1 element.
CompletableFuture.allOf(postFutures.toArray(new CompletableFuture[0])).get(
Integer.getInteger("quarkus.analytics.timeout", DEFAULT_TIMEOUT),
PropertyUtils.getProperty("quarkus.analytics.timeout", DEFAULT_TIMEOUT),
TimeUnit.MILLISECONDS);
if (log.isDebugEnabled() && !postFutures.isEmpty()) {
log.debug("[Quarkus build analytics] Build analytics sent successfully. Sent event can be seen at .../target/" +
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.quarkus.analytics;

import static io.quarkus.analytics.util.PropertyUtils.getProperty;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -74,8 +76,8 @@ public ConfigService(final ConfigClient client, final AnonymousUserId userId, fi
}

public void userAcceptance(Function<String, String> analyticsEnabledSupplier) {
final int timeout = Integer.getInteger(QUARKUS_ANALYTICS_PROMPT_TIMEOUT, 10);
if (Files.exists(localConfigFile) || Boolean.getBoolean(QUARKUS_ANALYTICS_DISABLED_LOCAL_PROP)) {
final int timeout = getProperty(QUARKUS_ANALYTICS_PROMPT_TIMEOUT, 10);
if (Files.exists(localConfigFile) || getProperty(QUARKUS_ANALYTICS_DISABLED_LOCAL_PROP, false)) {
return; // ask nothing
} else {
try {
Expand All @@ -89,7 +91,8 @@ public void userAcceptance(Function<String, String> analyticsEnabledSupplier) {
}
final boolean isActive = userInput.equals("y") || userInput.equals("yes") || userInput.startsWith("yy");
FileUtils.createFileAndParent(localConfigFile);
FileUtils.write(new LocalConfig(isActive), localConfigFile);
final boolean isDisabled = !isActive;// just to make it explicit
FileUtils.write(new LocalConfig(isDisabled), localConfigFile);
log.info("[Quarkus build analytics] Quarkus Build Analytics " + (isActive ? "enabled" : "disabled")
+ " by the user." + NEW_LINE);
} catch (TimeoutException e) {
Expand Down Expand Up @@ -122,12 +125,12 @@ public boolean isActive() {
if (!Files.exists(localConfigFile)) {
return false; // disabled because user has not decided yet
} else if (!loadConfig(LocalConfig.class, localConfigFile)
.map(LocalConfig::isActive)
.map(localConfig -> !localConfig.isDisabled())
.orElse(true)) {
return false; // disabled by the user and recorded on the local config
}

if (Boolean.getBoolean(QUARKUS_ANALYTICS_DISABLED_LOCAL_PROP)) {
if (getProperty(QUARKUS_ANALYTICS_DISABLED_LOCAL_PROP, false)) {
return false; // disabled by local property
}
AnalyticsRemoteConfig analyticsRemoteConfig = getRemoteConfig();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ public class FileLocationsImpl implements FileLocations {
".redhat");

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

// singleton
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ public interface AnalyticsLocalConfig {
* @return true if the analytics is enabled
* @return
*/
boolean isActive();
boolean isDisabled();
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
import java.io.Serializable;

public class LocalConfig implements AnalyticsLocalConfig, Serializable {
private boolean active;
private boolean disabled;

public LocalConfig(boolean active) {
this.active = active;
public LocalConfig(boolean disabled) {
this.disabled = disabled;
}

public LocalConfig() {
}

@Override
public boolean isActive() {
return active;
public boolean isDisabled() {
return disabled;
}

public void setActive(boolean active) {
this.active = active;
public void setDisabled(boolean disabled) {
this.disabled = disabled;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.quarkus.analytics.rest;

import static io.quarkus.analytics.util.PropertyUtils.getProperty;
import static io.quarkus.analytics.util.StringUtils.getObjectMapper;
import static java.util.concurrent.TimeUnit.MILLISECONDS;

Expand Down Expand Up @@ -64,11 +65,11 @@ static String getAuthHeader(final String key) {

private final URI segmentTraceUri;

private final int timeoutMs = Integer.getInteger("quarkus.analytics.timeout", DEFAULT_TIMEOUT);
private final int timeoutMs = getProperty("quarkus.analytics.timeout", DEFAULT_TIMEOUT);

public RestClient(MessageWriter log) {
this.log = log;
final String segmentBaseUri = System.getProperty("quarkus.analytics.uri.base", "https://api.segment.io/");
final String segmentBaseUri = getProperty("quarkus.analytics.uri.base", "https://api.segment.io/");
this.segmentIdentityUri = getUri(segmentBaseUri + IDENTITY_ENDPOINT);
this.segmentTraceUri = getUri(segmentBaseUri + TRACK_ENDPOINT);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package io.quarkus.analytics.util;

public class PropertyUtils {

public static Integer getProperty(String propertyName, int defaultValue) {
if (propertyName == null) {
throw new IllegalArgumentException("Property name cannot be null");
}
Integer result = Integer.getInteger(propertyName);
try {
if (result == null) {
String stringValue = System.getenv(transformToEnvVarName(propertyName));
if (stringValue != null) {
result = Integer.parseInt(stringValue);
} else {
result = defaultValue;
}
}
} catch (NumberFormatException e) {
result = defaultValue;
}
return result;
}

public static String getProperty(String propertyName, String defaultValue) {
if (propertyName == null) {
throw new IllegalArgumentException("Property name cannot be null");
}
String result = System.getProperty(propertyName);
try {
if (result == null) {
String stringValue = System.getenv(transformToEnvVarName(propertyName));
if (stringValue != null) {
result = stringValue;
} else {
result = defaultValue;
}
}
} catch (NumberFormatException e) {
result = defaultValue;
}
return result;
}

public static boolean getProperty(String propertyName, boolean defaultValue) {
if (propertyName == null) {
throw new IllegalArgumentException("Property name cannot be null");
}
boolean result;
String systemValue = System.getProperty(propertyName);
try {
if (systemValue == null) {
String envValue = System.getenv(transformToEnvVarName(propertyName));
if (envValue != null) {
result = Boolean.parseBoolean(envValue);
} else {
result = defaultValue;
}
} else {
result = Boolean.parseBoolean(systemValue);
}
} catch (NumberFormatException e) {
result = defaultValue;
}
return result;
}

private static String transformToEnvVarName(String propertyName) {
return propertyName.toUpperCase().replace('.', '_');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ void testConsoleQuestion_yes() throws IOException {
Optional<LocalConfig> localConfig = FileUtils.read(LocalConfig.class, fileLocations.getLocalConfigFile(),
MessageWriter.info());
assertTrue(localConfig.isPresent());
assertTrue(localConfig.get().isActive());
assertFalse(localConfig.get().isDisabled());
}

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

@Test
Expand Down

This file was deleted.

Loading

0 comments on commit 881ab1b

Please sign in to comment.