Skip to content

Commit

Permalink
Merge pull request #43274 from gayaldassanayake/add-central-telemetry…
Browse files Browse the repository at this point in the history
…-header-8.x

[2201.8.x] Add telemetry header for TEST_MODE_ACTIVE=true
  • Loading branch information
Dilhasha authored Aug 13, 2024
2 parents 7566f8d + bbb9557 commit 3e1bef6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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() {
}

Expand Down Expand Up @@ -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();
Expand All @@ -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";
Expand All @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit 3e1bef6

Please sign in to comment.