Skip to content

Commit

Permalink
Better log messages (Azure#36064)
Browse files Browse the repository at this point in the history
Co-authored-by: heyams <[email protected]>
  • Loading branch information
trask and heyams authored Jul 26, 2023
1 parent d0b8fbf commit b3307ed
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,7 @@ private TelemetryItemExporter initExporterBuilder() {
File tempDir =
TempDirs.getApplicationInsightsTempDir(
LOGGER,
"Telemetry will not be stored to disk and retried later"
+ " on sporadic network failures");
"Telemetry will not be stored to disk and retried on sporadic network failures");

TelemetryItemExporter telemetryItemExporter;
if (tempDir != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ class LocalFileSender implements Runnable {
this.telemetryPipeline = telemetryPipeline;

diagnosticListener =
suppressWarnings
? TelemetryPipelineListener.noop()
: new DiagnosticTelemetryPipelineListener(
"Sending telemetry to the ingestion service (retry from disk)", false);
suppressWarnings ? TelemetryPipelineListener.noop() :
new DiagnosticTelemetryPipelineListener(
"Sending telemetry to the ingestion service (retry from disk)",
true,
" (will be retried again)");

scheduledExecutor.scheduleWithFixedDelay(
this, intervalSeconds, intervalSeconds, TimeUnit.SECONDS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

package com.azure.monitor.opentelemetry.exporter.implementation.localstorage;

import com.azure.monitor.opentelemetry.exporter.implementation.logging.DiagnosticTelemetryPipelineListener;
import com.azure.monitor.opentelemetry.exporter.implementation.logging.OperationLogger;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

Expand Down Expand Up @@ -53,11 +54,11 @@ final class LocalFileWriter {
value = "SECPTI", // Potential Path Traversal
justification =
"The constructed file path cannot be controlled by an end user of the instrumented application")
void writeToDisk(String connectionString, List<ByteBuffer> buffers) {
void writeToDisk(String connectionString, List<ByteBuffer> buffers, String originalErrorMessage) {
long size = getTotalSizeOfPersistedFiles(telemetryFolder);
if (size >= diskPersistenceMaxSizeBytes) {
operationLogger.recordFailure(
"Local persistent storage capacity has been reached. It's currently at ("
operationLogger.recordFailure(originalErrorMessage
+ ". Local persistent storage capacity has been reached. It's currently at ("
+ (size / 1024)
+ "KB). Telemetry will be lost.",
DISK_PERSISTENCE_WRITER_ERROR);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

package com.azure.monitor.opentelemetry.exporter.implementation.localstorage;

import com.azure.monitor.opentelemetry.exporter.implementation.logging.DiagnosticTelemetryPipelineListener;
import com.azure.monitor.opentelemetry.exporter.implementation.pipeline.TelemetryPipeline;
import com.azure.monitor.opentelemetry.exporter.implementation.pipeline.TelemetryPipelineListener;
import com.azure.monitor.opentelemetry.exporter.implementation.pipeline.TelemetryPipelineRequest;
Expand Down Expand Up @@ -46,14 +47,15 @@ public LocalStorageTelemetryPipelineListener(
@Override
public void onResponse(TelemetryPipelineRequest request, TelemetryPipelineResponse response) {
if (StatusCode.isRetryable(response.getStatusCode())) {
localFileWriter.writeToDisk(request.getConnectionString(), request.getTelemetry());
localFileWriter.writeToDisk(
request.getConnectionString(), request.getTelemetry(), getOriginalErrorMessage(response));
}
}

@Override
public void onException(
TelemetryPipelineRequest request, String errorMessage, Throwable throwable) {
localFileWriter.writeToDisk(request.getConnectionString(), request.getTelemetry());
localFileWriter.writeToDisk(request.getConnectionString(), request.getTelemetry(), errorMessage);
}

@Override
Expand All @@ -66,4 +68,14 @@ public CompletableResultCode shutdown() {
}
return CompletableResultCode.ofSuccess();
}

private static String getOriginalErrorMessage(TelemetryPipelineResponse response) {
int statusCode = response.getStatusCode();
if (statusCode == 401 || statusCode == 403) {
return DiagnosticTelemetryPipelineListener
.getErrorMessageFromCredentialRelatedResponse(statusCode, response.getBody());
} else {
return "Received response code " + statusCode;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ public class DiagnosticTelemetryPipelineListener implements TelemetryPipelineLis
private static final AtomicBoolean friendlyExceptionThrown = new AtomicBoolean();

private final OperationLogger operationLogger;
private final boolean suppressWarningsOnRetryableFailures;
private final boolean logRetryableFailures;
private final String retryableFailureSuffix;

// e.g. "Sending telemetry to the ingestion service"
public DiagnosticTelemetryPipelineListener(
String operation, boolean suppressWarningsOnRetryableFailures) {
String operation, boolean logRetryableFailures, String retryableFailureSuffix) {
operationLogger = new OperationLogger(FOR_CLASS, operation);
this.suppressWarningsOnRetryableFailures = suppressWarningsOnRetryableFailures;
this.logRetryableFailures = logRetryableFailures;
this.retryableFailureSuffix = retryableFailureSuffix;
}

@Override
Expand All @@ -62,7 +64,7 @@ public void onResponse(TelemetryPipelineRequest request, TelemetryPipelineRespon
break;
case 401: // breeze returns if aad enabled and no authentication token provided
case 403: // breeze returns if aad enabled or disabled (both cases) and
if (!suppressWarningsOnRetryableFailures) {
if (logRetryableFailures) {
operationLogger.recordFailure(
getErrorMessageFromCredentialRelatedResponse(responseCode, response.getBody()),
INGESTION_ERROR);
Expand All @@ -71,12 +73,12 @@ public void onResponse(TelemetryPipelineRequest request, TelemetryPipelineRespon
case 408: // REQUEST TIMEOUT
case 429: // TOO MANY REQUESTS
case 500: // INTERNAL SERVER ERROR
case 502: // BAD GATEWAY
case 503: // SERVICE UNAVAILABLE
if (!suppressWarningsOnRetryableFailures) {
case 504: // GATEWAY TIMEOUT
if (logRetryableFailures) {
operationLogger.recordFailure(
"Received response code "
+ responseCode
+ " (telemetry will be stored to disk and retried later)",
"Received response code " + responseCode + retryableFailureSuffix,
INGESTION_ERROR);
}
break;
Expand All @@ -99,9 +101,9 @@ public void onResponse(TelemetryPipelineRequest request, TelemetryPipelineRespon
public void onException(TelemetryPipelineRequest request, String reason, Throwable throwable) {
if (!NetworkFriendlyExceptions.logSpecialOneTimeFriendlyException(
throwable, request.getUrl().toString(), friendlyExceptionThrown, logger)) {
if (!suppressWarningsOnRetryableFailures) {
if (logRetryableFailures) {
operationLogger.recordFailure(
reason + " (telemetry will be stored to disk and retried later)",
reason + retryableFailureSuffix,
throwable,
INGESTION_ERROR);
}
Expand Down Expand Up @@ -129,7 +131,7 @@ private static Set<String> getErrors(String body) {
.collect(Collectors.toSet());
}

private static String getErrorMessageFromCredentialRelatedResponse(
public static String getErrorMessageFromCredentialRelatedResponse(
int responseCode, String responseBody) {
JsonNode jsonNode;
try {
Expand All @@ -148,6 +150,6 @@ private static String getErrorMessageFromCredentialRelatedResponse(
jsonNode.get("errors").forEach(errors::add);
return errors.get(0).get("message").asText()
+ action
+ " (telemetry will be stored to disk and retried later)";
+ " (telemetry will be stored to disk and retried)";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ public void testDeleteFilePermanentlyOnSuccess() throws Exception {
// persist 10 files to disk
for (int i = 0; i < 10; i++) {
localFileWriter.writeToDisk(
CONNECTION_STRING, singletonList(ByteBuffer.wrap("hello world".getBytes(UTF_8))));
CONNECTION_STRING,
singletonList(ByteBuffer.wrap("hello world".getBytes(UTF_8))),
"original error message");
}

assertThat(localFileCache.getPersistedFilesCache().size()).isEqualTo(10);
Expand Down Expand Up @@ -141,7 +143,9 @@ public void testDeleteFilePermanentlyOnFailure() throws Exception {
// persist 10 files to disk
for (int i = 0; i < 10; i++) {
localFileWriter.writeToDisk(
CONNECTION_STRING, singletonList(ByteBuffer.wrap("hello world".getBytes(UTF_8))));
CONNECTION_STRING,
singletonList(ByteBuffer.wrap("hello world".getBytes(UTF_8))),
"original error message");
}

assertThat(localFileCache.getPersistedFilesCache().size()).isEqualTo(10);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ public void testPurgedExpiredFiles() throws Exception {
for (int i = 0; i < 100; i++) {
writer.writeToDisk(
"InstrumentationKey=00000000-0000-0000-0000-0FEEDDADBEE;IngestionEndpoint=http://foo.bar/",
singletonList(ByteBuffer.wrap(text.getBytes(UTF_8))));
singletonList(ByteBuffer.wrap(text.getBytes(UTF_8))),
"original error message");
}

List<File> files = FileUtil.listTrnFiles(tempFolder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ public void testWriteByteBuffersList() throws IOException {
assertThat(byteBuffers.size()).isEqualTo(10);

LocalFileWriter writer = new LocalFileWriter(50, localFileCache, tempFolder, null, false);
writer.writeToDisk(CONNECTION_STRING, byteBuffers);
writer.writeToDisk(CONNECTION_STRING, byteBuffers, "original error message");
assertThat(localFileCache.getPersistedFilesCache().size()).isEqualTo(1);
}

@Test
public void testWriteRawByteArray() throws IOException {
LocalFileWriter writer = new LocalFileWriter(50, localFileCache, tempFolder, null, false);
byte[] content = Resources.readBytes("write-transmission.txt");
writer.writeToDisk(CONNECTION_STRING, singletonList(ByteBuffer.wrap(content)));
writer.writeToDisk(CONNECTION_STRING, singletonList(ByteBuffer.wrap(content)), "original error message");
assertThat(localFileCache.getPersistedFilesCache().size()).isEqualTo(1);
}

Expand All @@ -79,7 +79,9 @@ public void testWriteUnderMultipleThreadsEnvironment() throws InterruptedExcepti
LocalFileWriter writer =
new LocalFileWriter(50, localFileCache, tempFolder, null, false);
writer.writeToDisk(
CONNECTION_STRING, singletonList(ByteBuffer.wrap(telemetry.getBytes(UTF_8))));
CONNECTION_STRING,
singletonList(ByteBuffer.wrap(telemetry.getBytes(UTF_8))),
"original error message");
}
});
}
Expand Down

0 comments on commit b3307ed

Please sign in to comment.