Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Turn off Statsbeat when proxy is used or any exception from the server #2221

Merged
merged 17 commits into from
Apr 13, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
Expand All @@ -73,19 +74,17 @@ public class TelemetryChannel {

private static final AppInsightsByteBufferPool byteBufferPool = new AppInsightsByteBufferPool();

private static final int MAX_STATSBEAT_ERROR_COUNT = 10;

private final OperationLogger operationLogger;
private final OperationLogger retryOperationLogger;

// TODO (kryalama) do we still need this AtomicBoolean, or can we use throttling built in to the
// operationLogger?
private final AtomicBoolean friendlyExceptionThrown = new AtomicBoolean();

private final AtomicInteger statsbeatErrorCount = new AtomicInteger();
private final AtomicBoolean statsbeatShutdown = new AtomicBoolean();
private final AtomicInteger statsbeatUnableToReachBreezeCounter = new AtomicInteger();
trask marked this conversation as resolved.
Show resolved Hide resolved
private final AtomicBoolean statsbeatHasBeenShutdown = new AtomicBoolean();
trask marked this conversation as resolved.
Show resolved Hide resolved

private volatile boolean statsbeatAtLeastOneSuccess;
private volatile boolean statsbeatHasReachedBreezeAtLeastOnce;
trask marked this conversation as resolved.
Show resolved Hide resolved

@SuppressWarnings("CatchAndPrintStackTrace")
private static ObjectMapper createObjectMapper() {
Expand Down Expand Up @@ -122,7 +121,7 @@ public CompletableResultCode sendRawBytes(
String instrumentationKey,
Runnable onSuccess,
Consumer<Boolean> onFailure) {
if (isStatsbeat && statsbeatShutdown.get()) {
if (isStatsbeat && statsbeatHasBeenShutdown.get()) {
trask marked this conversation as resolved.
Show resolved Hide resolved
// let it be deleted from disk so that it won't keep getting retried
return CompletableResultCode.ofSuccess();
}
Expand Down Expand Up @@ -299,6 +298,11 @@ private CompletableResultCode internalSend(
return result;
}

// not including 401/403/503 in this list because those are commonly returned by proxy servers
// when they are not configured to allow traffic for westus-0
private static final Set<Integer> RESPONSE_CODES_INDICATING_REACHED_BREEZE =
new HashSet<>(asList(200, 206, 402, 408, 429, 439, 500));
trask marked this conversation as resolved.
Show resolved Hide resolved
trask marked this conversation as resolved.
Show resolved Hide resolved

private Consumer<HttpResponse> responseHandler(
String instrumentationKey,
long startTime,
Expand All @@ -313,11 +317,11 @@ private Consumer<HttpResponse> responseHandler(
.subscribe(
body -> {
int statusCode = response.getStatusCode();
if (isStatsbeat && !statsbeatAtLeastOneSuccess) {
if (statusCode == 200) {
statsbeatAtLeastOneSuccess = true;
} else if (statusCode != 439 && statusCode != 402) {
possiblyShutDownStatsbeat();
if (isStatsbeat && !statsbeatHasReachedBreezeAtLeastOnce) {
if (RESPONSE_CODES_INDICATING_REACHED_BREEZE.contains(statusCode)) {
statsbeatHasReachedBreezeAtLeastOnce = true;
} else {
statsbeatDidNotReachBreeze();
}
}
switch (statusCode) {
Expand Down Expand Up @@ -388,8 +392,8 @@ private Consumer<Throwable> errorHandler(
String instrumentationKey, Consumer<Boolean> onFailure, OperationLogger operationLogger) {

return error -> {
if (isStatsbeat && !statsbeatAtLeastOneSuccess) {
possiblyShutDownStatsbeat();
if (isStatsbeat && !statsbeatHasReachedBreezeAtLeastOnce) {
statsbeatDidNotReachBreeze();
}

if (!isStatsbeat
Expand All @@ -407,9 +411,9 @@ private Consumer<Throwable> errorHandler(
};
}

private void possiblyShutDownStatsbeat() {
if (statsbeatErrorCount.getAndIncrement() >= MAX_STATSBEAT_ERROR_COUNT
&& !statsbeatShutdown.getAndSet(true)) {
private void statsbeatDidNotReachBreeze() {
if (statsbeatUnableToReachBreezeCounter.getAndIncrement() >= 10
&& !statsbeatHasBeenShutdown.getAndSet(true)) {
trask marked this conversation as resolved.
Show resolved Hide resolved
// shutting down statsbeat because it's unlikely that it will ever get through at this point
// some possible reasons:
// * AMPLS
Expand Down