aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-telemetry/src
diff options
context:
space:
mode:
authorMatteo Mara <matteo.mara@sonarsource.com>2024-07-19 17:45:56 +0200
committersonartech <sonartech@sonarsource.com>2024-07-24 20:02:49 +0000
commit514ed23156ba614ed959456e683339c53373821c (patch)
treef8be3fc25defe1620a4605dad3ce8eb2fb965936 /server/sonar-telemetry/src
parentaf66f272ea91ce1a132dc40260247ec720741270 (diff)
downloadsonarqube-514ed23156ba614ed959456e683339c53373821c.tar.gz
sonarqube-514ed23156ba614ed959456e683339c53373821c.zip
SONAR-22479 Leveraging independent URL for Telemetry Metrics
Diffstat (limited to 'server/sonar-telemetry/src')
-rw-r--r--server/sonar-telemetry/src/main/java/org/sonar/telemetry/TelemetryClient.java14
-rw-r--r--server/sonar-telemetry/src/main/java/org/sonar/telemetry/TelemetryDaemon.java2
-rw-r--r--server/sonar-telemetry/src/test/java/org/sonar/telemetry/TelemetryClientCompressionTest.java2
-rw-r--r--server/sonar-telemetry/src/test/java/org/sonar/telemetry/TelemetryClientTest.java29
4 files changed, 41 insertions, 6 deletions
diff --git a/server/sonar-telemetry/src/main/java/org/sonar/telemetry/TelemetryClient.java b/server/sonar-telemetry/src/main/java/org/sonar/telemetry/TelemetryClient.java
index 92fa3599f3a..c1a0a94ec7d 100644
--- a/server/sonar-telemetry/src/main/java/org/sonar/telemetry/TelemetryClient.java
+++ b/server/sonar-telemetry/src/main/java/org/sonar/telemetry/TelemetryClient.java
@@ -37,6 +37,7 @@ import org.sonar.api.server.ServerSide;
import static org.sonar.process.ProcessProperties.Property.SONAR_TELEMETRY_COMPRESSION;
import static org.sonar.process.ProcessProperties.Property.SONAR_TELEMETRY_URL;
+import static org.sonar.process.ProcessProperties.Property.SONAR_TELEMETRY_METRICS_URL;
@ServerSide
public class TelemetryClient implements Startable {
@@ -46,6 +47,7 @@ public class TelemetryClient implements Startable {
private final OkHttpClient okHttpClient;
private final Configuration config;
private String serverUrl;
+ private String metricsServerUrl;
private boolean compression;
public TelemetryClient(OkHttpClient okHttpClient, Configuration config) {
@@ -54,7 +56,12 @@ public class TelemetryClient implements Startable {
}
void upload(String json) throws IOException {
- Request request = buildHttpRequest(json);
+ Request request = buildHttpRequest(serverUrl, json);
+ execute(okHttpClient.newCall(request));
+ }
+
+ void uploadMetric(String json) throws IOException {
+ Request request = buildHttpRequest(metricsServerUrl, json);
execute(okHttpClient.newCall(request));
}
@@ -63,7 +70,6 @@ public class TelemetryClient implements Startable {
request.url(serverUrl);
RequestBody body = RequestBody.create(JSON, json);
request.delete(body);
-
try {
execute(okHttpClient.newCall(request.build()));
} catch (IOException e) {
@@ -71,7 +77,7 @@ public class TelemetryClient implements Startable {
}
}
- private Request buildHttpRequest(String json) {
+ private Request buildHttpRequest(String serverUrl, String json) {
Request.Builder request = new Request.Builder();
request.addHeader("Content-Encoding", "gzip");
request.addHeader("Content-Type", "application/json");
@@ -117,6 +123,8 @@ public class TelemetryClient implements Startable {
public void start() {
this.serverUrl = config.get(SONAR_TELEMETRY_URL.getKey())
.orElseThrow(() -> new IllegalStateException(String.format("Setting '%s' must be provided.", SONAR_TELEMETRY_URL)));
+ this.metricsServerUrl = config.get(SONAR_TELEMETRY_METRICS_URL.getKey())
+ .orElseThrow(() -> new IllegalStateException(String.format("Setting '%s' must be provided.", SONAR_TELEMETRY_METRICS_URL)));
this.compression = config.getBoolean(SONAR_TELEMETRY_COMPRESSION.getKey()).orElse(true);
}
diff --git a/server/sonar-telemetry/src/main/java/org/sonar/telemetry/TelemetryDaemon.java b/server/sonar-telemetry/src/main/java/org/sonar/telemetry/TelemetryDaemon.java
index f920ff8956a..70e64719224 100644
--- a/server/sonar-telemetry/src/main/java/org/sonar/telemetry/TelemetryDaemon.java
+++ b/server/sonar-telemetry/src/main/java/org/sonar/telemetry/TelemetryDaemon.java
@@ -162,7 +162,7 @@ public class TelemetryDaemon extends AbstractStoppableScheduledExecutorServiceIm
TelemetryMetricsLoader.Context context = telemetryMetricsLoader.loadData();
for (BaseMessage message : context.getMessages()) {
String jsonString = MessageSerializer.serialize(message);
- telemetryClient.upload(jsonString);
+ telemetryClient.uploadMetric(jsonString);
}
try (DbSession dbSession = dbClient.openSession(false)) {
diff --git a/server/sonar-telemetry/src/test/java/org/sonar/telemetry/TelemetryClientCompressionTest.java b/server/sonar-telemetry/src/test/java/org/sonar/telemetry/TelemetryClientCompressionTest.java
index 29121b50377..6ac5d708553 100644
--- a/server/sonar-telemetry/src/test/java/org/sonar/telemetry/TelemetryClientCompressionTest.java
+++ b/server/sonar-telemetry/src/test/java/org/sonar/telemetry/TelemetryClientCompressionTest.java
@@ -33,6 +33,7 @@ import org.junit.jupiter.api.Test;
import org.sonar.api.config.internal.MapSettings;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.sonar.process.ProcessProperties.Property.SONAR_TELEMETRY_METRICS_URL;
import static org.sonar.process.ProcessProperties.Property.SONAR_TELEMETRY_URL;
class TelemetryClientCompressionTest {
@@ -45,6 +46,7 @@ class TelemetryClientCompressionTest {
telemetryServer.enqueue(new MockResponse().setResponseCode(200));
MapSettings settings = new MapSettings();
settings.setProperty(SONAR_TELEMETRY_URL.getKey(), telemetryServer.url("/").toString());
+ settings.setProperty(SONAR_TELEMETRY_METRICS_URL.getKey(), telemetryServer.url("/").toString());
TelemetryClient underTest = new TelemetryClient(okHttpClient, settings.asConfig());
underTest.start();
underTest.upload("payload compressed with gzip");
diff --git a/server/sonar-telemetry/src/test/java/org/sonar/telemetry/TelemetryClientTest.java b/server/sonar-telemetry/src/test/java/org/sonar/telemetry/TelemetryClientTest.java
index 2323ee4e0e8..ffb94d77fac 100644
--- a/server/sonar-telemetry/src/test/java/org/sonar/telemetry/TelemetryClientTest.java
+++ b/server/sonar-telemetry/src/test/java/org/sonar/telemetry/TelemetryClientTest.java
@@ -24,6 +24,7 @@ import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okio.Buffer;
+import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.sonar.api.config.internal.MapSettings;
@@ -33,22 +34,29 @@ import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.sonar.process.ProcessProperties.Property.SONAR_TELEMETRY_COMPRESSION;
+import static org.sonar.process.ProcessProperties.Property.SONAR_TELEMETRY_METRICS_URL;
import static org.sonar.process.ProcessProperties.Property.SONAR_TELEMETRY_URL;
class TelemetryClientTest {
private static final String JSON = "{\"key\":\"value\"}";
private static final String TELEMETRY_URL = "https://telemetry.com/url";
+ private static final String METRICS_TELEMETRY_URL = "https://telemetry.com/url/metrics";
private final OkHttpClient okHttpClient = mock(OkHttpClient.class, RETURNS_DEEP_STUBS);
private final MapSettings settings = new MapSettings();
private final TelemetryClient underTest = new TelemetryClient(okHttpClient, settings.asConfig());
+ @BeforeEach
+ void setProperties() {
+ settings.setProperty(SONAR_TELEMETRY_URL.getKey(), TELEMETRY_URL);
+ settings.setProperty(SONAR_TELEMETRY_METRICS_URL.getKey(), METRICS_TELEMETRY_URL);
+ }
+
@Test
void upload() throws IOException {
ArgumentCaptor<Request> requestCaptor = ArgumentCaptor.forClass(Request.class);
- settings.setProperty(SONAR_TELEMETRY_URL.getKey(), TELEMETRY_URL);
settings.setProperty(SONAR_TELEMETRY_COMPRESSION.getKey(), false);
underTest.start();
@@ -65,9 +73,26 @@ class TelemetryClientTest {
}
@Test
+ void uploadMetric() throws IOException {
+ ArgumentCaptor<Request> requestCaptor = ArgumentCaptor.forClass(Request.class);
+ settings.setProperty(SONAR_TELEMETRY_COMPRESSION.getKey(), false);
+ underTest.start();
+
+ underTest.uploadMetric(JSON);
+
+ verify(okHttpClient).newCall(requestCaptor.capture());
+ Request request = requestCaptor.getValue();
+ assertThat(request.method()).isEqualTo("POST");
+ assertThat(request.body().contentType()).isEqualTo(MediaType.parse("application/json; charset=utf-8"));
+ Buffer body = new Buffer();
+ request.body().writeTo(body);
+ assertThat(body.readUtf8()).isEqualTo(JSON);
+ assertThat(request.url()).hasToString(METRICS_TELEMETRY_URL);
+ }
+
+ @Test
void opt_out() throws IOException {
ArgumentCaptor<Request> requestCaptor = ArgumentCaptor.forClass(Request.class);
- settings.setProperty(SONAR_TELEMETRY_URL.getKey(), TELEMETRY_URL);
underTest.start();
underTest.optOut(JSON);