Skip to content

Commit

Permalink
SONAR-9721 Telemetry properties defined in a specific PropertyDefinit…
Browse files Browse the repository at this point in the history
…ion class
  • Loading branch information
teryk committed Aug 30, 2017
1 parent 5c2b4dc commit 4cc72a7
Show file tree
Hide file tree
Showing 13 changed files with 99 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public void real_start_without_cluster() throws IOException {
+ 25 // level 1
+ 47 // content of DaoModule
+ 3 // content of EsSearchModule
+ 58 // content of CorePropertyDefinitions
+ 61 // content of CorePropertyDefinitions
);
assertThat(
picoContainer.getComponentAdapters().stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@

import static org.sonar.api.utils.DateUtils.formatDate;
import static org.sonar.api.utils.DateUtils.parseDate;
import static org.sonar.server.telemetry.TelemetryProperties.PROP_ENABLE;
import static org.sonar.server.telemetry.TelemetryProperties.PROP_URL;
import static org.sonar.core.config.TelemetryProperties.PROP_ENABLE;
import static org.sonar.core.config.TelemetryProperties.PROP_URL;

@ServerSide
public class TelemetryDaemon implements Startable {
private static final String THREAD_NAME_PREFIX = "sq-telemetry-service-";
private static final int SEVEN_DAYS = 7 * 24 * 60 * 60 * 1_000;
private static final String I_PROP_LAST_PING = "sonar.telemetry.lastPing";
private static final String I_PROP_OPT_OUT = "sonar.telemetry.optOut";
static final String I_PROP_LAST_PING = "telemetry.lastPing";
static final String I_PROP_OPT_OUT = "telemetry.optOut";
private static final Logger LOG = Loggers.get(TelemetryDaemon.class);

private final TelemetryClient telemetryClient;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ class TelemetryFrequency {

long get() {
if (frequency == null) {
frequency = config.getLong(TelemetryProperties.PROP_FREQUENCY)
.orElseThrow(() -> new IllegalStateException(String.format("Setting '%s' must be provided.", TelemetryProperties.PROP_FREQUENCY)));
frequency = config.getLong(org.sonar.core.config.TelemetryProperties.PROP_FREQUENCY)
.orElseThrow(() -> new IllegalStateException(String.format("Setting '%s' must be provided.", org.sonar.core.config.TelemetryProperties.PROP_FREQUENCY)));
}

return frequency;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public class TelemetryModule extends Module {
@Override
protected void configureModule() {
add(
TelemetryProperties.class,
TelemetryDaemon.class,
TelemetryClient.class);
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

import org.sonar.api.config.Configuration;

import static org.sonar.server.telemetry.TelemetryProperties.PROP_URL;
import static org.sonar.core.config.TelemetryProperties.PROP_URL;

class TelemetryUrl {
private final Configuration config;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.sonar.api.config.PropertyDefinitions;
import org.sonar.api.config.internal.MapSettings;
import org.sonar.api.utils.internal.TestSystem2;
import org.sonar.core.config.TelemetryProperties;
import org.sonar.server.property.InternalProperties;
import org.sonar.server.property.MapInternalProperties;

Expand All @@ -36,6 +37,9 @@
import static org.mockito.Mockito.timeout;
import static org.mockito.Mockito.verify;
import static org.sonar.api.utils.DateUtils.parseDate;
import static org.sonar.core.config.TelemetryProperties.PROP_ENABLE;
import static org.sonar.core.config.TelemetryProperties.PROP_FREQUENCY;
import static org.sonar.server.telemetry.TelemetryDaemon.I_PROP_LAST_PING;

public class TelemetryDaemonTest {

Expand All @@ -52,15 +56,15 @@ public class TelemetryDaemonTest {

@Before
public void setUp() throws Exception {
settings = new MapSettings(new PropertyDefinitions(TelemetryProperties.class));
settings = new MapSettings(new PropertyDefinitions(TelemetryProperties.all()));
system2.setNow(System.currentTimeMillis());

underTest = new TelemetryDaemon(client, settings.asConfig(), internalProperties, server, system2);
}

@Test
public void send_data_via_client_at_startup_after_initial_delay() {
settings.setProperty("sonar.telemetry.frequency", "1");
settings.setProperty(PROP_FREQUENCY, "1");
underTest.start();

verify(client, timeout(1_000).atLeastOnce()).send(anyString());
Expand All @@ -71,18 +75,18 @@ public void check_if_should_send_data_periodically() {
long now = system2.now();
long sixDaysAgo = now - (ONE_DAY * 6L);
long sevenDaysAgo = now - (ONE_DAY * 7L);
internalProperties.write("sonar.telemetry.lastPing", String.valueOf(sixDaysAgo));
settings.setProperty("sonar.telemetry.frequency", "1");
internalProperties.write(I_PROP_LAST_PING, String.valueOf(sixDaysAgo));
settings.setProperty(PROP_FREQUENCY, "1");
underTest.start();
verify(client, timeout(1_000).never()).send(anyString());
internalProperties.write("sonar.telemetry.lastPing", String.valueOf(sevenDaysAgo));
internalProperties.write(I_PROP_LAST_PING, String.valueOf(sevenDaysAgo));

verify(client, timeout(1_000).atLeastOnce()).send(anyString());
}

@Test
public void send_server_id() {
settings.setProperty("sonar.telemetry.frequency", "1");
settings.setProperty(PROP_FREQUENCY, "1");
String id = randomAlphanumeric(40);
server.setId(id);
underTest.start();
Expand All @@ -92,35 +96,35 @@ public void send_server_id() {

@Test
public void do_not_send_data_if_last_ping_earlier_than_one_week_ago() {
settings.setProperty("sonar.telemetry.frequency", "1");
settings.setProperty(PROP_FREQUENCY, "1");
long now = system2.now();
long sixDaysAgo = now - (ONE_DAY * 6L);

internalProperties.write("sonar.telemetry.lastPing", String.valueOf(sixDaysAgo));
internalProperties.write(I_PROP_LAST_PING, String.valueOf(sixDaysAgo));
underTest.start();

verify(client, timeout(2_000).never()).send(anyString());
}

@Test
public void send_data_if_last_ping_is_one_week_ago() {
settings.setProperty("sonar.telemetry.frequency", "1");
settings.setProperty(PROP_FREQUENCY, "1");
long today = parseDate("2017-08-01").getTime();
system2.setNow(today + 15 * ONE_HOUR);
long now = system2.now();
long sevenDaysAgo = now - (ONE_DAY * 7L);
internalProperties.write("sonar.telemetry.lastPing", String.valueOf(sevenDaysAgo));
internalProperties.write(I_PROP_LAST_PING, String.valueOf(sevenDaysAgo));

underTest.start();

verify(client, timeout(1_000).atLeastOnce()).send(anyString());
assertThat(internalProperties.read("sonar.telemetry.lastPing").get()).isEqualTo(String.valueOf(today));
assertThat(internalProperties.read(I_PROP_LAST_PING).get()).isEqualTo(String.valueOf(today));
}

@Test
public void opt_out_sent_once() {
settings.setProperty("sonar.telemetry.frequency", "1");
settings.setProperty("sonar.telemetry.enable", "false");
settings.setProperty(PROP_FREQUENCY, "1");
settings.setProperty(PROP_ENABLE, "false");
underTest.start();
underTest.start();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ public class TelemetryModuleTest {
public void verify_count_of_added_components() {
ComponentContainer container = new ComponentContainer();
new TelemetryModule().configure(container);
assertThat(container.size()).isEqualTo(3 + 2);
assertThat(container.size()).isEqualTo(2 + 2);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public static List<PropertyDefinition> all() {
defs.addAll(PurgeProperties.all());
defs.addAll(EmailSettings.definitions());
defs.addAll(WebhookProperties.all());
defs.addAll(TelemetryProperties.all());

defs.addAll(ImmutableList.of(
PropertyDefinition.builder(PROP_PASSWORD)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* SonarQube
* Copyright (C) 2009-2017 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.core.config;

import com.google.common.collect.ImmutableList;
import java.util.List;
import org.sonar.api.PropertyType;
import org.sonar.api.config.PropertyDefinition;

public class TelemetryProperties {

public static final String PROP_ENABLE = "sonar.telemetry.enable";
public static final String PROP_FREQUENCY = "sonar.telemetry.frequencyInSeconds";
public static final String PROP_URL = "sonar.telemetry.url";

private TelemetryProperties() {
// only static stuff
}

public static List<PropertyDefinition> all() {
return ImmutableList.of(
PropertyDefinition.builder(PROP_ENABLE)
.defaultValue(Boolean.toString(true))
.type(PropertyType.BOOLEAN)
.name("Share SonarQube statistics")
.description("By sharing anonymous SonarQube statistics, you help us understand how SonarQube is used so we can improve the plugin to work even better for you. " +
"We don't collect source code or IP addresses. And we don't share the data with anyone else.")
.hidden()
.build(),
PropertyDefinition.builder(PROP_FREQUENCY)
// 6 hours in seconds
.defaultValue("21600")
.type(PropertyType.INTEGER)
.name("Frequency of telemetry checks, in seconds")
.hidden()
.build(),
PropertyDefinition.builder(PROP_URL)
.defaultValue("https://telemetry.sonarsource.com/sonarqube")
.type(PropertyType.STRING)
.name("URL where telemetry data is sent")
.hidden()
.build()
);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class CorePropertyDefinitionsTest {
@Test
public void all() {
List<PropertyDefinition> defs = CorePropertyDefinitions.all();
assertThat(defs).hasSize(58);
assertThat(defs).hasSize(61);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

package org.sonar.server.telemetry;
package org.sonar.core.config;

import org.junit.Test;
import org.sonar.api.config.Configuration;
Expand All @@ -29,15 +29,12 @@

public class TelemetryPropertiesTest {

Configuration underTest = new MapSettings(new PropertyDefinitions(TelemetryProperties.class)).asConfig();
private Configuration underTest = new MapSettings(new PropertyDefinitions(TelemetryProperties.all())).asConfig();

@Test
public void default_frequency_is_6_hours_in_seconds() {
assertThat(underTest.getInt("sonar.telemetry.frequency")).contains(6 * 60 * 60);
}

@Test
public void default_url_point_to_telemetry_server() {
assertThat(underTest.get("sonar.telemetry.url")).contains("https://telemetry.sonarsource.com/sonarqube");
public void default_telemetry_properties() {
assertThat(underTest.getBoolean("sonar.telemetry.enable")).hasValue(true);
assertThat(underTest.getInt("sonar.telemetry.frequencyInSeconds")).hasValue(6 * 60 * 60);
assertThat(underTest.get("sonar.telemetry.url")).hasValue("https://telemetry.sonarsource.com/sonarqube");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void send_telemetry_data_at_startup() throws Exception {
orchestrator = Orchestrator.builderEnv()
.addPlugin(xooPlugin())
.setServerProperty("sonar.telemetry.url", url)
.setServerProperty("sonar.telemetry.frequency", "1")
.setServerProperty("sonar.telemetry.frequencyInSeconds", "1")
.setServerProperty("sonar.core.id", serverId)
.build();
orchestrator.start();
Expand All @@ -78,7 +78,7 @@ public void opt_out_of_telemetry() throws Exception {
.addPlugin(xooPlugin())
.setServerProperty("sonar.telemetry.enable", "false")
.setServerProperty("sonar.telemetry.url", url)
.setServerProperty("sonar.telemetry.frequency", "1")
.setServerProperty("sonar.telemetry.frequencyInSeconds", "1")
.setServerProperty("sonar.core.id", serverId)
.build();
orchestrator.start();
Expand Down

0 comments on commit 4cc72a7

Please sign in to comment.