Skip to content

Commit 1cd0408

Browse files
authored
Add logging when purging old telemetry files (#1957)
* Add logging when purging old telemetry files * more * better message * Better operation logging
1 parent 5357377 commit 1cd0408

File tree

3 files changed

+27
-21
lines changed

3 files changed

+27
-21
lines changed

agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/localstorage/LocalFileLoader.java

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ public class LocalFileLoader {
5252
private static final OperationLogger operationLogger =
5353
new OperationLogger(LocalFileLoader.class, "Loading telemetry from disk");
5454

55+
private static final OperationLogger updateOperationLogger =
56+
new OperationLogger(LocalFileLoader.class, "Updating local telemetry on disk");
57+
5558
public LocalFileLoader(
5659
LocalFileCache localFileCache,
5760
File telemetryFolder,
@@ -161,43 +164,37 @@ private static void readFully(FileInputStream fileInputStream, byte[] byteArray,
161164
// either delete it permanently on success or add it back to cache to be processed again later on
162165
// failure
163166
public void updateProcessedFileStatus(boolean success, File file) {
167+
if (!file.exists()) {
168+
// not sure why this would happen
169+
updateOperationLogger.recordFailure("File no longer exists: " + file.getName());
170+
return;
171+
}
164172
if (success) {
165-
deleteFilePermanentlyOnSuccess(file);
173+
// delete a file on the queue permanently when http response returns success.
174+
if (!LocalStorageUtils.deleteFileWithRetries(file)) {
175+
// TODO (heya) track file deletion failure via Statsbeat
176+
updateOperationLogger.recordFailure("Fail to delete " + file.getName());
177+
} else {
178+
updateOperationLogger.recordSuccess();
179+
}
166180
} else {
167181
// rename the temp file back to .trn source file extension
168182
File sourceFile =
169183
new File(telemetryFolder, FilenameUtils.getBaseName(file.getName()) + ".trn");
170184
try {
171185
FileUtils.moveFile(file, sourceFile);
172186
} catch (IOException ex) {
173-
operationLogger.recordFailure(
187+
updateOperationLogger.recordFailure(
174188
"Fail to rename " + file.getName() + " to have a .trn extension.", ex);
175189
return;
176190
}
191+
updateOperationLogger.recordSuccess();
177192

178193
// add the source filename back to local file cache to be processed later.
179194
localFileCache.addPersistedFilenameToMap(sourceFile.getName());
180195
}
181196
}
182197

183-
// delete a file on the queue permanently when http response returns success.
184-
private static void deleteFilePermanentlyOnSuccess(File file) {
185-
if (!file.exists()) {
186-
return;
187-
}
188-
189-
deleteFile(file);
190-
}
191-
192-
private static void deleteFile(File file) {
193-
if (!LocalStorageUtils.deleteFileWithRetries(file)) {
194-
// TODO (heya) track file deletion failure via Statsbeat
195-
operationLogger.recordFailure("Fail to delete " + file.getName());
196-
} else {
197-
operationLogger.recordSuccess();
198-
}
199-
}
200-
201198
private void incrementReadFailureCount() {
202199
if (nonessentialStatsbeat != null) {
203200
nonessentialStatsbeat.incrementReadFailureCount();

agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/localstorage/LocalFilePurger.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,23 @@ public void run() {
7979

8080
private void purgedExpiredFiles(File folder) {
8181
Collection<File> files = FileUtils.listFiles(folder, new String[] {"trn"}, false);
82+
int numDeleted = 0;
8283
for (File file : files) {
8384
if (expired(file.getName())) {
8485
if (!LocalStorageUtils.deleteFileWithRetries(file)) {
8586
logger.warn(
8687
"Fail to delete the expired {} from folder '{}'.", file.getName(), folder.getName());
88+
} else {
89+
numDeleted++;
8790
}
8891
}
8992
}
93+
if (numDeleted > 0) {
94+
logger.warn(
95+
"{} local telemetry file(s) in folder '{}' expired after 48 hours and were deleted",
96+
numDeleted,
97+
folder.getName());
98+
}
9099
}
91100

92101
// files that are older than expiredIntervalSeconds (default 48 hours) are expired and need to

agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/telemetry/TelemetryChannel.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public class TelemetryChannel {
6969
private static final OperationLogger operationLogger =
7070
new OperationLogger(
7171
TelemetryChannel.class,
72-
"Sending telemetry to the ingestion service (telemetry will be stored to disk on failure):");
72+
"Sending telemetry to the ingestion service (telemetry will be stored to disk on failure and retried later):");
7373

7474
// TODO (kryalama) do we still need this AtomicBoolean, or can we use throttling built in to the
7575
// operationLogger?

0 commit comments

Comments
 (0)