From bbb9557cc84fba8907275a7cf894073ef4d16eb7 Mon Sep 17 00:00:00 2001 From: Gayal Dassanayake Date: Fri, 9 Aug 2024 11:33:21 +0530 Subject: [PATCH] Add telemetry header for TEST_MODE_ACTIVE=true --- .../central/client/CentralAPIClient.java | 18 +++++++++--------- .../central/client/CentralClientConstants.java | 3 ++- .../ballerinalang/central/client/Utils.java | 17 ++++++++++------- 3 files changed, 21 insertions(+), 17 deletions(-) diff --git a/cli/central-client/src/main/java/org/ballerinalang/central/client/CentralAPIClient.java b/cli/central-client/src/main/java/org/ballerinalang/central/client/CentralAPIClient.java index a96eb8957067..91a68676590f 100644 --- a/cli/central-client/src/main/java/org/ballerinalang/central/client/CentralAPIClient.java +++ b/cli/central-client/src/main/java/org/ballerinalang/central/client/CentralAPIClient.java @@ -80,6 +80,7 @@ import static org.ballerinalang.central.client.CentralClientConstants.APPLICATION_OCTET_STREAM; import static org.ballerinalang.central.client.CentralClientConstants.AUTHORIZATION; import static org.ballerinalang.central.client.CentralClientConstants.BALA_URL; +import static org.ballerinalang.central.client.CentralClientConstants.BALLERINA_CENTRAL_TELEMETRY_DISABLED; import static org.ballerinalang.central.client.CentralClientConstants.BALLERINA_PLATFORM; import static org.ballerinalang.central.client.CentralClientConstants.CONTENT_DISPOSITION; import static org.ballerinalang.central.client.CentralClientConstants.CONTENT_TYPE; @@ -93,6 +94,7 @@ import static org.ballerinalang.central.client.CentralClientConstants.USER_AGENT; import static org.ballerinalang.central.client.CentralClientConstants.VERSION; import static org.ballerinalang.central.client.Utils.ProgressRequestBody; +import static org.ballerinalang.central.client.Utils.SET_TEST_MODE_ACTIVE; import static org.ballerinalang.central.client.Utils.createBalaInHomeRepo; import static org.ballerinalang.central.client.Utils.getAsList; import static org.ballerinalang.central.client.Utils.getBearerToken; @@ -1583,16 +1585,14 @@ protected void closeClient(OkHttpClient client) throws IOException { * @return Http request builder */ protected Request.Builder getNewRequest(String supportedPlatform, String ballerinaVersion) { - if (this.accessToken.isEmpty()) { - return new Request.Builder() - .addHeader(BALLERINA_PLATFORM, supportedPlatform) - .addHeader(USER_AGENT, ballerinaVersion); - } else { - return new Request.Builder() - .addHeader(BALLERINA_PLATFORM, supportedPlatform) - .addHeader(USER_AGENT, ballerinaVersion) - .addHeader(AUTHORIZATION, getBearerToken(this.accessToken)); + Request.Builder requestBuilder = new Request.Builder() + .addHeader(BALLERINA_PLATFORM, supportedPlatform) + .addHeader(USER_AGENT, ballerinaVersion) + .addHeader(BALLERINA_CENTRAL_TELEMETRY_DISABLED, String.valueOf(SET_TEST_MODE_ACTIVE)); + if (!this.accessToken.isEmpty()) { + requestBuilder.addHeader(AUTHORIZATION, getBearerToken(this.accessToken)); } + return requestBuilder; } public String accessToken() { diff --git a/cli/central-client/src/main/java/org/ballerinalang/central/client/CentralClientConstants.java b/cli/central-client/src/main/java/org/ballerinalang/central/client/CentralClientConstants.java index 3c94e0e177a2..1b8b23a48c4a 100644 --- a/cli/central-client/src/main/java/org/ballerinalang/central/client/CentralClientConstants.java +++ b/cli/central-client/src/main/java/org/ballerinalang/central/client/CentralClientConstants.java @@ -54,5 +54,6 @@ private CentralClientConstants() { static final String DEV_REPO = "dev-central.ballerina.io"; public static final String BALLERINA_STAGE_CENTRAL = "BALLERINA_STAGE_CENTRAL"; public static final String BALLERINA_DEV_CENTRAL = "BALLERINA_DEV_CENTRAL"; - + public static final String TEST_MODE_ACTIVE = "TEST_MODE_ACTIVE"; + public static final String BALLERINA_CENTRAL_TELEMETRY_DISABLED = "Ballerina-Central-Telemetry-Disabled"; } diff --git a/cli/central-client/src/main/java/org/ballerinalang/central/client/Utils.java b/cli/central-client/src/main/java/org/ballerinalang/central/client/Utils.java index 8bfa30db8e10..fc6e89cf1b72 100644 --- a/cli/central-client/src/main/java/org/ballerinalang/central/client/Utils.java +++ b/cli/central-client/src/main/java/org/ballerinalang/central/client/Utils.java @@ -65,6 +65,7 @@ import static org.ballerinalang.central.client.CentralClientConstants.PRODUCTION_REPO; import static org.ballerinalang.central.client.CentralClientConstants.RESOLVED_REQUESTED_URI; import static org.ballerinalang.central.client.CentralClientConstants.STAGING_REPO; +import static org.ballerinalang.central.client.CentralClientConstants.TEST_MODE_ACTIVE; /** * Utils class for this package. @@ -76,6 +77,8 @@ public class Utils { System.getenv(BALLERINA_STAGE_CENTRAL)); public static final boolean SET_BALLERINA_DEV_CENTRAL = Boolean.parseBoolean( System.getenv(BALLERINA_DEV_CENTRAL)); + public static final boolean SET_TEST_MODE_ACTIVE = Boolean.parseBoolean(System.getenv(TEST_MODE_ACTIVE)); + private Utils() { } @@ -447,13 +450,13 @@ public static class ProgressRequestBody extends RequestBody { private final RequestBody reqBody; private final String task; private final PrintStream out; - + public ProgressRequestBody(RequestBody reqBody, String task, PrintStream out) { this.reqBody = reqBody; this.task = task; this.out = out; } - + @Override public MediaType contentType() { return this.reqBody.contentType(); @@ -469,7 +472,7 @@ public void writeTo(BufferedSink sink) throws IOException { final long totalBytes = contentLength(); long byteConverter; String unitName; - + if (totalBytes < 1024 * 5) { // use bytes for progress bar if payload is less than 5 KB byteConverter = 1; unitName = " B"; @@ -480,7 +483,7 @@ public void writeTo(BufferedSink sink) throws IOException { byteConverter = 1024 * 1024; unitName = " MB"; } - + ProgressBar progressBar = new ProgressBar(task, contentLength(), 1000, out, ProgressBarStyle.ASCII, unitName, byteConverter); CountingSink countingSink = new CountingSink(sink, progressBar); @@ -490,16 +493,16 @@ public void writeTo(BufferedSink sink) throws IOException { progressBar.close(); } } - + private static class CountingSink extends ForwardingSink { private long bytesWritten = 0; private final ProgressBar progressBar; - + public CountingSink(Sink delegate, ProgressBar progressBar) { super(delegate); this.progressBar = progressBar; } - + @Override public void write(Buffer source, long byteCount) throws IOException { super.write(source, byteCount);