import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
+import static org.sonar.core.config.TelemetryProperties.PROP_URL;
+
@ServerSide
public class TelemetryClient {
private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
private static final Logger LOG = Loggers.get(TelemetryClient.class);
private final OkHttpClient okHttpClient;
- private final TelemetryUrl serverUrl;
+ private final Configuration config;
public TelemetryClient(OkHttpClient okHttpClient, Configuration config) {
this.okHttpClient = okHttpClient;
- this.serverUrl = new TelemetryUrl(config);
+ this.config = config;
}
void send(String json) {
void optOut(String json) {
Request.Builder request = new Request.Builder();
- request.url(serverUrl.get());
+ request.url(serverUrl());
RequestBody body = RequestBody.create(JSON, json);
request.delete(body);
private Request buildHttpRequest(String json) {
Request.Builder request = new Request.Builder();
- request.url(serverUrl.get());
+ request.url(serverUrl());
RequestBody body = RequestBody.create(JSON, json);
request.post(body);
return request.build();
}
+ private String serverUrl() {
+ return config.get(PROP_URL).orElseThrow(() -> new IllegalStateException(String.format("Setting '%s' must be provided.", PROP_URL)));
+ }
+
}
import static org.sonar.api.utils.DateUtils.formatDate;
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.core.config.TelemetryProperties.PROP_URL;
@ServerSide
private final InternalProperties internalProperties;
private final Server server;
private final System2 system2;
- private final TelemetryFrequency frequencyInSeconds;
private ScheduledExecutorService executorService;
public TelemetryDaemon(TelemetryClient telemetryClient, Configuration config, InternalProperties internalProperties, Server server, System2 system2) {
this.telemetryClient = telemetryClient;
this.config = config;
- this.frequencyInSeconds = new TelemetryFrequency(config);
this.internalProperties = internalProperties;
this.server = server;
this.system2 = system2;
.setNameFormat(THREAD_NAME_PREFIX + "%d")
.setPriority(Thread.MIN_PRIORITY)
.build());
+ int frequencyInSeconds = frequency();
executorService.scheduleWithFixedDelay(() -> {
try {
Optional<Long> lastPing = internalProperties.read(I_PROP_LAST_PING).map(Long::valueOf);
// fail silently
}
// do not check at start up to exclude test instance which are not up for a long time
- }, frequencyInSeconds.get(), frequencyInSeconds.get(), TimeUnit.SECONDS);
+ }, frequencyInSeconds, frequencyInSeconds, TimeUnit.SECONDS);
}
@Override
private static long startOfDay(long now) {
return parseDate(formatDate(now)).getTime();
}
+
+ private int frequency() {
+ return config.getInt(PROP_FREQUENCY).orElseThrow(() -> new IllegalStateException(String.format("Setting '%s' must be provided.", PROP_FREQUENCY)));
+ }
}
+++ /dev/null
-/*
- * 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.server.telemetry;
-
-import org.sonar.api.config.Configuration;
-
-class TelemetryFrequency {
- private final Configuration config;
- private Long frequency;
-
- TelemetryFrequency(Configuration config) {
- this.config = config;
- }
-
- long get() {
- if (frequency == null) {
- 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;
- }
-}
+++ /dev/null
-/*
- * 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.server.telemetry;
-
-import org.sonar.api.config.Configuration;
-
-import static org.sonar.core.config.TelemetryProperties.PROP_URL;
-
-class TelemetryUrl {
- private final Configuration config;
- private String url;
-
- TelemetryUrl(Configuration config) {
- this.config = config;
- }
-
- String get() {
- if (url == null) {
- url = config.get(PROP_URL).orElseThrow(() -> new IllegalStateException(String.format("Setting '%s' must be provided.", PROP_URL)));
- }
-
- return url;
- }
-}
+++ /dev/null
-/*
- * 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.server.telemetry;
-
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.ExpectedException;
-import org.sonar.api.config.internal.MapSettings;
-
-import static org.assertj.core.api.Assertions.assertThat;
-
-public class TelemetryUrlTest {
-
- @Rule
- public ExpectedException expectedException = ExpectedException.none();
-
- private MapSettings settings = new MapSettings();
-
- private TelemetryUrl underTest;
-
- @Test
- public void return_url_as_is_when_no_ending_slash() {
- settings.setProperty("sonar.telemetry.url", "http://localhost:9001");
- underTest = new TelemetryUrl(settings.asConfig());
-
- assertThat(underTest.get()).isEqualTo("http://localhost:9001");
- }
-
- @Test
- public void fail_when_no_settings_to_define_muppet_url() {
- underTest = new TelemetryUrl(settings.asConfig());
-
- expectedException.expect(IllegalStateException.class);
- expectedException.expectMessage("Setting 'sonar.telemetry.url' must be provided.");
-
- underTest.get();
- }
-}