Skip to content

Commit

Permalink
SONAR-9721 Drop use of TelemetryUrl and TelemetryFrequency
Browse files Browse the repository at this point in the history
  • Loading branch information
teryk committed Aug 30, 2017
1 parent 4cc72a7 commit 20d5318
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 146 deletions.
Expand Up @@ -31,17 +31,19 @@
import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers; import org.sonar.api.utils.log.Loggers;


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

@ServerSide @ServerSide
public class TelemetryClient { public class TelemetryClient {
private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8"); private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
private static final Logger LOG = Loggers.get(TelemetryClient.class); private static final Logger LOG = Loggers.get(TelemetryClient.class);


private final OkHttpClient okHttpClient; private final OkHttpClient okHttpClient;
private final TelemetryUrl serverUrl; private final Configuration config;


public TelemetryClient(OkHttpClient okHttpClient, Configuration config) { public TelemetryClient(OkHttpClient okHttpClient, Configuration config) {
this.okHttpClient = okHttpClient; this.okHttpClient = okHttpClient;
this.serverUrl = new TelemetryUrl(config); this.config = config;
} }


void send(String json) { void send(String json) {
Expand All @@ -55,7 +57,7 @@ void send(String json) {


void optOut(String json) { void optOut(String json) {
Request.Builder request = new Request.Builder(); Request.Builder request = new Request.Builder();
request.url(serverUrl.get()); request.url(serverUrl());
RequestBody body = RequestBody.create(JSON, json); RequestBody body = RequestBody.create(JSON, json);
request.delete(body); request.delete(body);


Expand All @@ -68,10 +70,14 @@ void optOut(String json) {


private Request buildHttpRequest(String json) { private Request buildHttpRequest(String json) {
Request.Builder request = new Request.Builder(); Request.Builder request = new Request.Builder();
request.url(serverUrl.get()); request.url(serverUrl());
RequestBody body = RequestBody.create(JSON, json); RequestBody body = RequestBody.create(JSON, json);
request.post(body); request.post(body);
return request.build(); return request.build();
} }


private String serverUrl() {
return config.get(PROP_URL).orElseThrow(() -> new IllegalStateException(String.format("Setting '%s' must be provided.", PROP_URL)));
}

} }
Expand Up @@ -38,6 +38,7 @@
import static org.sonar.api.utils.DateUtils.formatDate; import static org.sonar.api.utils.DateUtils.formatDate;
import static org.sonar.api.utils.DateUtils.parseDate; 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_ENABLE;
import static org.sonar.core.config.TelemetryProperties.PROP_FREQUENCY;
import static org.sonar.core.config.TelemetryProperties.PROP_URL; import static org.sonar.core.config.TelemetryProperties.PROP_URL;


@ServerSide @ServerSide
Expand All @@ -53,14 +54,12 @@ public class TelemetryDaemon implements Startable {
private final InternalProperties internalProperties; private final InternalProperties internalProperties;
private final Server server; private final Server server;
private final System2 system2; private final System2 system2;
private final TelemetryFrequency frequencyInSeconds;


private ScheduledExecutorService executorService; private ScheduledExecutorService executorService;


public TelemetryDaemon(TelemetryClient telemetryClient, Configuration config, InternalProperties internalProperties, Server server, System2 system2) { public TelemetryDaemon(TelemetryClient telemetryClient, Configuration config, InternalProperties internalProperties, Server server, System2 system2) {
this.telemetryClient = telemetryClient; this.telemetryClient = telemetryClient;
this.config = config; this.config = config;
this.frequencyInSeconds = new TelemetryFrequency(config);
this.internalProperties = internalProperties; this.internalProperties = internalProperties;
this.server = server; this.server = server;
this.system2 = system2; this.system2 = system2;
Expand Down Expand Up @@ -94,6 +93,7 @@ public void start() {
.setNameFormat(THREAD_NAME_PREFIX + "%d") .setNameFormat(THREAD_NAME_PREFIX + "%d")
.setPriority(Thread.MIN_PRIORITY) .setPriority(Thread.MIN_PRIORITY)
.build()); .build());
int frequencyInSeconds = frequency();
executorService.scheduleWithFixedDelay(() -> { executorService.scheduleWithFixedDelay(() -> {
try { try {
Optional<Long> lastPing = internalProperties.read(I_PROP_LAST_PING).map(Long::valueOf); Optional<Long> lastPing = internalProperties.read(I_PROP_LAST_PING).map(Long::valueOf);
Expand All @@ -114,7 +114,7 @@ public void start() {
// fail silently // fail silently
} }
// do not check at start up to exclude test instance which are not up for a long time // 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 @Override
Expand All @@ -130,4 +130,8 @@ public void stop() {
private static long startOfDay(long now) { private static long startOfDay(long now) {
return parseDate(formatDate(now)).getTime(); 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)));
}
} }

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit 20d5318

Please sign in to comment.