1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
/*
* SonarQube
* Copyright (C) 2009-2024 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.telemetry;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okio.BufferedSink;
import okio.GzipSink;
import okio.Okio;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.Startable;
import org.sonar.api.config.Configuration;
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 {
private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
private static final Logger LOG = LoggerFactory.getLogger(TelemetryClient.class);
private final OkHttpClient okHttpClient;
private final Configuration config;
private String serverUrl;
private String metricsServerUrl;
private boolean compression;
public TelemetryClient(OkHttpClient okHttpClient, Configuration config) {
this.config = config;
this.okHttpClient = okHttpClient;
}
void upload(String json) throws IOException {
Request request = buildHttpRequest(serverUrl, json);
execute(okHttpClient.newCall(request));
}
void uploadMetric(String json) throws IOException {
Request request = buildHttpRequest(metricsServerUrl, json);
execute(okHttpClient.newCall(request));
}
void optOut(String json) {
Request.Builder request = new Request.Builder();
request.url(serverUrl);
RequestBody body = RequestBody.create(JSON, json);
request.delete(body);
try {
execute(okHttpClient.newCall(request.build()));
} catch (IOException e) {
LOG.debug("Error when sending opt-out usage statistics: {}", e.getMessage());
}
}
private Request buildHttpRequest(String serverUrl, String json) {
Request.Builder request = new Request.Builder();
request.addHeader("Content-Encoding", "gzip");
request.addHeader("Content-Type", "application/json");
request.url(serverUrl);
RequestBody body = RequestBody.create(JSON, json);
if (compression) {
request.post(gzip(body));
} else {
request.post(body);
}
return request.build();
}
private static RequestBody gzip(final RequestBody body) {
return new RequestBody() {
@Override
public MediaType contentType() {
return body.contentType();
}
@Override
public long contentLength() {
// We don't know the compressed length in advance!
return -1;
}
@Override
public void writeTo(BufferedSink sink) throws IOException {
BufferedSink gzipSink = Okio.buffer(new GzipSink(sink));
body.writeTo(gzipSink);
gzipSink.close();
}
};
}
private static void execute(Call call) throws IOException {
try (Response ignored = call.execute()) {
// auto close connection to avoid leaked connection
}
}
@Override
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);
}
@Override
public void stop() {
// Nothing to do
}
}
|