Skip to content

Commit

Permalink
Sporadic failure (#2507)
Browse files Browse the repository at this point in the history
* Simplify

* Fix flush on shutdown hook
  • Loading branch information
trask authored Sep 9, 2022
1 parent 8b99ed4 commit 7e87ac1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -269,19 +269,17 @@ private static CompletableResultCode flushAll(TelemetryClient telemetryClient) {
CompletableResultCode initialResult = CompletableResultCode.ofAll(results);
initialResult.whenComplete(
() -> {
if (initialResult.isSuccess()) {
CompletableResultCode telemetryClientResult = telemetryClient.forceFlush();
telemetryClientResult.whenComplete(
() -> {
if (telemetryClientResult.isSuccess()) {
overallResult.succeed();
} else {
overallResult.fail();
}
});
} else {
overallResult.fail();
}
// IMPORTANT: the metric reader flush will fail if the periodic metric reader is already
// mid-exporter
CompletableResultCode telemetryClientResult = telemetryClient.forceFlush();
telemetryClientResult.whenComplete(
() -> {
if (initialResult.isSuccess() && telemetryClientResult.isSuccess()) {
overallResult.succeed();
} else {
overallResult.fail();
}
});
});
return overallResult;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,9 @@
package com.microsoft.applicationinsights.smoketestapp;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.management.ManagementFactory;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Executors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
Expand Down Expand Up @@ -43,13 +39,7 @@ public String spawnAnotherJavaProcess() throws Exception {
appJar.toString(),
"okhttp3");
ProcessBuilder builder = new ProcessBuilder(command);
Process process = builder.start();
ConsoleOutputPipe consoleOutputPipe =
new ConsoleOutputPipe(process.getInputStream(), System.out);
Executors.newSingleThreadExecutor().submit(consoleOutputPipe);
ConsoleOutputPipe consoleErrorPipe =
new ConsoleOutputPipe(process.getErrorStream(), System.err);
Executors.newSingleThreadExecutor().submit(consoleErrorPipe);
Process process = builder.inheritIO().start();
process.waitFor();
return "OK!";
}
Expand All @@ -63,31 +53,4 @@ private static File getAgentJarFile() {
}
throw new AssertionError("Agent jar not found on command line: " + String.join(" ", jvmArgs));
}

private static class ConsoleOutputPipe implements Runnable {

private final InputStream in;
private final OutputStream out;

private ConsoleOutputPipe(InputStream in, OutputStream out) {
this.in = in;
this.out = out;
}

@Override
public void run() {
byte[] buffer = new byte[100];
try {
while (true) {
int n = in.read(buffer);
if (n == -1) {
break;
}
out.write(buffer, 0, n);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

0 comments on commit 7e87ac1

Please sign in to comment.