-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'open-telemetry:main' into develop/HttpURLConnection-aut…
…o-instrumentation
- Loading branch information
Showing
20 changed files
with
672 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
...rumentation/src/main/java/io/opentelemetry/android/config/DiskBufferingConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.android.config; | ||
|
||
/** Configuration for disk buffering. */ | ||
public final class DiskBufferingConfiguration { | ||
private final boolean enabled; | ||
private final int maxCacheSize; | ||
private static final int DEFAULT_MAX_CACHE_SIZE = 60 * 1024 * 1024; | ||
private static final int MAX_FILE_SIZE = 1024 * 1024; | ||
|
||
private DiskBufferingConfiguration(Builder builder) { | ||
this.enabled = builder.enabled; | ||
this.maxCacheSize = builder.maxCacheSize; | ||
} | ||
|
||
public static Builder builder() { | ||
return new Builder(false, DEFAULT_MAX_CACHE_SIZE); | ||
} | ||
|
||
public boolean isEnabled() { | ||
return enabled; | ||
} | ||
|
||
public int getMaxCacheSize() { | ||
return maxCacheSize; | ||
} | ||
|
||
public int getMaxCacheFileSize() { | ||
return MAX_FILE_SIZE; | ||
} | ||
|
||
public static final class Builder { | ||
private boolean enabled; | ||
private int maxCacheSize; | ||
|
||
private Builder(boolean enabled, int maxCacheSize) { | ||
this.enabled = enabled; | ||
this.maxCacheSize = maxCacheSize; | ||
} | ||
|
||
/** Enables or disables disk buffering. */ | ||
public Builder setEnabled(boolean enabled) { | ||
this.enabled = enabled; | ||
return this; | ||
} | ||
|
||
/** | ||
* Sets the maximum amount of bytes that this tool can use to store cached signals in disk. | ||
* A smaller amount of space will be used if there's not enough space in disk to allocate | ||
* the value provided here. | ||
*/ | ||
public Builder setMaxCacheSize(int maxCacheSize) { | ||
this.maxCacheSize = maxCacheSize; | ||
return this; | ||
} | ||
|
||
public DiskBufferingConfiguration build() { | ||
return new DiskBufferingConfiguration(this); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
...ion/src/main/java/io/opentelemetry/android/internal/features/persistence/DiskManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.android.internal.features.persistence; | ||
|
||
import io.opentelemetry.android.config.DiskBufferingConfiguration; | ||
import io.opentelemetry.android.config.OtelRumConfig; | ||
import io.opentelemetry.android.internal.services.CacheStorageService; | ||
import io.opentelemetry.android.internal.services.PreferencesService; | ||
import io.opentelemetry.android.internal.services.ServiceManager; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
/** | ||
* This class is internal and not for public use. Its APIs are unstable and can change at any time. | ||
*/ | ||
public final class DiskManager { | ||
private static final String MAX_FOLDER_SIZE_KEY = "max_signal_folder_size"; | ||
private static final Logger logger = Logger.getLogger("DiskManager"); | ||
private final CacheStorageService cacheStorageService; | ||
private final PreferencesService preferencesService; | ||
private final DiskBufferingConfiguration diskBufferingConfiguration; | ||
|
||
public static DiskManager create(OtelRumConfig config) { | ||
ServiceManager serviceManager = ServiceManager.get(); | ||
return new DiskManager( | ||
serviceManager.getService(CacheStorageService.class), | ||
serviceManager.getService(PreferencesService.class), | ||
config.getDiskBufferingConfiguration()); | ||
} | ||
|
||
private DiskManager( | ||
CacheStorageService cacheStorageService, | ||
PreferencesService preferencesService, | ||
DiskBufferingConfiguration diskBufferingConfiguration) { | ||
this.cacheStorageService = cacheStorageService; | ||
this.preferencesService = preferencesService; | ||
this.diskBufferingConfiguration = diskBufferingConfiguration; | ||
} | ||
|
||
public File getSignalsBufferDir() throws IOException { | ||
File dir = new File(cacheStorageService.getCacheDir(), "opentelemetry/signals"); | ||
if (!dir.exists()) { | ||
if (!dir.mkdirs()) { | ||
throw new IOException("Could not create dir " + dir); | ||
} | ||
} | ||
return dir; | ||
} | ||
|
||
public File getTemporaryDir() throws IOException { | ||
File dir = new File(cacheStorageService.getCacheDir(), "opentelemetry/temp"); | ||
if (!dir.exists()) { | ||
if (!dir.mkdirs()) { | ||
throw new IOException("Could not create dir " + dir); | ||
} | ||
} | ||
deleteFiles(dir); | ||
return dir; | ||
} | ||
|
||
/** | ||
* It checks for the available cache space in disk, then it attempts to divide by 3 in order to | ||
* get each signal's folder max size. The resulting value is subtracted the max file size value | ||
* in order to account for temp files used during the reading process. | ||
* | ||
* <p> | ||
* | ||
* @return If the calculated size is < the max file size value, it returns 0. The calculated | ||
* size is stored in the preferences and returned otherwise. | ||
*/ | ||
public int getMaxFolderSize() { | ||
int storedSize = preferencesService.retrieveInt(MAX_FOLDER_SIZE_KEY, -1); | ||
if (storedSize > 0) { | ||
logger.log( | ||
Level.FINER, | ||
String.format("Returning max folder size from preferences: %s", storedSize)); | ||
return storedSize; | ||
} | ||
int requestedSize = diskBufferingConfiguration.getMaxCacheSize(); | ||
int availableCacheSize = (int) cacheStorageService.ensureCacheSpaceAvailable(requestedSize); | ||
// Divides the available cache size by 3 (for each signal's folder) and then subtracts the | ||
// size of a single file to be aware of a temp file used when reading data back from the | ||
// disk. | ||
int maxCacheFileSize = getMaxCacheFileSize(); | ||
int calculatedSize = (availableCacheSize / 3) - maxCacheFileSize; | ||
if (calculatedSize < maxCacheFileSize) { | ||
logger.log( | ||
Level.WARNING, | ||
String.format( | ||
"Insufficient folder cache size: %s, it must be at least: %s", | ||
calculatedSize, maxCacheFileSize)); | ||
return 0; | ||
} | ||
preferencesService.store(MAX_FOLDER_SIZE_KEY, calculatedSize); | ||
|
||
logger.log( | ||
Level.FINER, | ||
String.format( | ||
"Requested cache size: %s, available cache size: %s, folder size: %s", | ||
requestedSize, availableCacheSize, calculatedSize)); | ||
return calculatedSize; | ||
} | ||
|
||
public int getMaxCacheFileSize() { | ||
return diskBufferingConfiguration.getMaxCacheFileSize(); | ||
} | ||
|
||
private static void deleteFiles(File dir) { | ||
File[] files = dir.listFiles(); | ||
if (files != null) { | ||
for (File file : files) { | ||
if (file.isDirectory()) { | ||
deleteFiles(file); | ||
} | ||
file.delete(); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.