Skip to content

Commit

Permalink
[SEDONA-638] Submit telemetry asynchronously without blocking the ini…
Browse files Browse the repository at this point in the history
…tialization of SedonaContext (#1541)
  • Loading branch information
Kontinuation committed Aug 5, 2024
1 parent e83a55b commit aef523b
Showing 1 changed file with 41 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,26 @@
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.concurrent.atomic.AtomicBoolean;

public class TelemetryCollector {

private static final String BASE_URL = "https://sedona.gateway.scarf.sh/packages/";
private static final AtomicBoolean telemetrySubmitted = new AtomicBoolean(false);

public static String send(String engineName, String language) {
HttpURLConnection conn = null;
String telemetrySubmitted = "";
String telemetrySubmitUrl = "";
if (!telemetrySubmitted.compareAndSet(false, true)) {
return telemetrySubmitUrl;
}
try {
String arch = URLEncoder.encode(System.getProperty("os.arch").replaceAll(" ", "_"), "UTF-8");
String os = URLEncoder.encode(System.getProperty("os.name").replaceAll(" ", "_"), "UTF-8");
String jvm =
URLEncoder.encode(System.getProperty("java.version").replaceAll(" ", "_"), "UTF-8");

// Construct URL
telemetrySubmitted =
telemetrySubmitUrl =
BASE_URL + language + "/" + engineName + "/" + arch + "/" + os + "/" + jvm;

// Check for user opt-out
Expand All @@ -47,26 +51,44 @@ public static String send(String engineName, String language) {
&& System.getProperty("SCARF_NO_ANALYTICS").equals("true")
|| System.getProperty("DO_NOT_TRACK") != null
&& System.getProperty("DO_NOT_TRACK").equals("true")) {
return telemetrySubmitted;
return telemetrySubmitUrl;
}

// Send GET request
URL url = new URL(telemetrySubmitted);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.connect();
int responseCode = conn.getResponseCode();
// Optionally check the response for successful execution
if (responseCode != 200) {
// Silent handling, no output or log
}
Thread telemetrySubmitThread = createThread(telemetrySubmitUrl);
telemetrySubmitThread.start();
} catch (Exception e) {
// Silent catch block
} finally {
if (conn != null) {
conn.disconnect();
}
}
return telemetrySubmitted;
return telemetrySubmitUrl;
}

private static Thread createThread(String telemetrySubmitUrl) {
Thread telemetrySubmitThread =
new Thread("telemetry-submit-thread") {
@Override
public void run() {
HttpURLConnection conn = null;
try {
// Send GET request
URL url = new URL(telemetrySubmitUrl);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.connect();
int responseCode = conn.getResponseCode();
// Optionally check the response for successful execution
if (responseCode != 200) {
// Silent handling, no output or log
}
} catch (Exception e) {
// Silent catch block
} finally {
if (conn != null) {
conn.disconnect();
}
}
}
};
telemetrySubmitThread.setDaemon(true);
return telemetrySubmitThread;
}
}

0 comments on commit aef523b

Please sign in to comment.