Skip to content

Commit

Permalink
Add ability to enable debug logging for disk buffering (#753)
Browse files Browse the repository at this point in the history
* refactor to kotlin

* use config in demo

* enable debug mode and wire up to demo app

* extract constants

* extract constants
  • Loading branch information
breedx-splk authored Jan 17, 2025
1 parent 6ee3d1f commit a55b22a
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 167 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import io.opentelemetry.android.config.OtelRumConfig;
import io.opentelemetry.android.export.BufferDelegatingLogExporter;
import io.opentelemetry.android.export.BufferDelegatingSpanExporter;
import io.opentelemetry.android.features.diskbuffering.DiskBufferingConfiguration;
import io.opentelemetry.android.features.diskbuffering.DiskBufferingConfig;
import io.opentelemetry.android.features.diskbuffering.SignalFromDiskExporter;
import io.opentelemetry.android.features.diskbuffering.scheduler.DefaultExportScheduleHandler;
import io.opentelemetry.android.features.diskbuffering.scheduler.DefaultExportScheduler;
Expand Down Expand Up @@ -352,12 +352,11 @@ private void initializeExporters(
BufferDelegatingSpanExporter bufferDelegatingSpanExporter,
BufferDelegatingLogExporter bufferedDelegatingLogExporter) {

DiskBufferingConfiguration diskBufferingConfiguration =
config.getDiskBufferingConfiguration();
DiskBufferingConfig diskBufferingConfig = config.getDiskBufferingConfig();
SpanExporter spanExporter = buildSpanExporter();
LogRecordExporter logsExporter = buildLogsExporter();
SignalFromDiskExporter signalFromDiskExporter = null;
if (diskBufferingConfiguration.isEnabled()) {
if (diskBufferingConfig.getEnabled()) {
try {
StorageConfiguration storageConfiguration = createStorageConfiguration();
final SpanExporter originalSpanExporter = spanExporter;
Expand Down Expand Up @@ -420,7 +419,7 @@ public OpenTelemetryRumBuilder setExportScheduleHandler(
private StorageConfiguration createStorageConfiguration() throws IOException {
Preferences preferences = getServiceManager().getPreferences();
CacheStorage storage = getServiceManager().getCacheStorage();
DiskBufferingConfiguration config = this.config.getDiskBufferingConfiguration();
DiskBufferingConfig config = this.config.getDiskBufferingConfig();
DiskManager diskManager = new DiskManager(storage, preferences, config);
return StorageConfiguration.builder()
.setRootDir(diskManager.getSignalsBufferDir())
Expand All @@ -431,6 +430,7 @@ private StorageConfiguration createStorageConfiguration() throws IOException {
.setMinFileAgeForReadMillis(config.getMinFileAgeForReadMillis())
.setTemporaryFileProvider(
new SimpleTemporaryFileProvider(diskManager.getTemporaryDir()))
.setDebugEnabled(config.getDebugEnabled())
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
package io.opentelemetry.android.config;

import io.opentelemetry.android.ScreenAttributesSpanProcessor;
import io.opentelemetry.android.features.diskbuffering.DiskBufferingConfiguration;
import io.opentelemetry.android.features.diskbuffering.DiskBufferingConfig;
import io.opentelemetry.android.internal.services.network.CurrentNetworkProvider;
import io.opentelemetry.api.common.Attributes;
import java.time.Duration;
Expand All @@ -24,8 +24,7 @@ public class OtelRumConfig {
private boolean generateSdkInitializationEvents = true;
private boolean includeScreenAttributes = true;
private boolean discoverInstrumentations = true;
private DiskBufferingConfiguration diskBufferingConfiguration =
DiskBufferingConfiguration.builder().build();
private DiskBufferingConfig diskBufferingConfig = DiskBufferingConfig.create();
private Duration sessionTimeout = Duration.ofMinutes(15);

/**
Expand Down Expand Up @@ -98,8 +97,8 @@ public boolean shouldIncludeScreenAttributes() {
return includeScreenAttributes;
}

public DiskBufferingConfiguration getDiskBufferingConfiguration() {
return diskBufferingConfiguration;
public DiskBufferingConfig getDiskBufferingConfig() {
return diskBufferingConfig;
}

/**
Expand All @@ -125,9 +124,8 @@ public OtelRumConfig disableInstrumentationDiscovery() {
*
* @return this
*/
public OtelRumConfig setDiskBufferingConfiguration(
DiskBufferingConfiguration diskBufferingConfiguration) {
this.diskBufferingConfiguration = diskBufferingConfiguration;
public OtelRumConfig setDiskBufferingConfig(DiskBufferingConfig diskBufferingConfig) {
this.diskBufferingConfig = diskBufferingConfig;
return this;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.android.features.diskbuffering

import android.util.Log
import io.opentelemetry.android.common.RumConstants.OTEL_RUM_LOG_TAG
import java.util.concurrent.TimeUnit

const val DEFAULT_MAX_CACHE_SIZE: Int = 60 * 1024 * 1024
const val MAX_CACHE_FILE_SIZE: Int = 1024 * 1024
const val DEFAULT_MAX_FILE_AGE_FOR_WRITE_MS = 30L
const val DEFAULT_MIN_FILE_AGE_FOR_READ_MS = 33L
const val DEFAULT_MAX_FILE_AGE_FOR_READ_MS = 18L

data class DiskBufferingConfig
@JvmOverloads
constructor(
val enabled: Boolean = false,
val maxCacheSize: Int = DEFAULT_MAX_CACHE_SIZE,
val maxFileAgeForWriteMillis: Long = TimeUnit.SECONDS.toMillis(DEFAULT_MAX_FILE_AGE_FOR_WRITE_MS),
val minFileAgeForReadMillis: Long = TimeUnit.SECONDS.toMillis(DEFAULT_MIN_FILE_AGE_FOR_READ_MS),
val maxFileAgeForReadMillis: Long = TimeUnit.HOURS.toMillis(DEFAULT_MAX_FILE_AGE_FOR_READ_MS),
val maxCacheFileSize: Int = MAX_CACHE_FILE_SIZE,
val debugEnabled: Boolean = false,
) {
companion object {
/**
* Convenience factory method that validates the min/max and fixes
* those up if needed. Users should prefer this method over the
* vanilla non-validating constructor.
*/
@JvmOverloads
@JvmStatic
fun create(
enabled: Boolean = false,
maxCacheSize: Int = DEFAULT_MAX_CACHE_SIZE,
maxFileAgeForWriteMillis: Long = TimeUnit.SECONDS.toMillis(30),
minFileAgeForReadMillis: Long = TimeUnit.SECONDS.toMillis(33),
maxFileAgeForReadMillis: Long = TimeUnit.HOURS.toMillis(18),
maxCacheFileSize: Int = MAX_CACHE_FILE_SIZE,
debugEnabled: Boolean = false,
): DiskBufferingConfig {
var minRead = minFileAgeForReadMillis
if (minFileAgeForReadMillis <= maxFileAgeForWriteMillis) {
minRead = maxFileAgeForWriteMillis + 5
Log.w(OTEL_RUM_LOG_TAG, "minFileAgeForReadMillis must be greater than maxFileAgeForWriteMillis")
Log.w(OTEL_RUM_LOG_TAG, "overriding minFileAgeForReadMillis from $minFileAgeForReadMillis to $minRead")
}
return DiskBufferingConfig(
enabled = enabled,
maxCacheSize = maxCacheSize,
maxFileAgeForWriteMillis = maxFileAgeForWriteMillis,
minFileAgeForReadMillis = minRead,
maxFileAgeForReadMillis = maxFileAgeForReadMillis,
maxCacheFileSize = maxCacheFileSize,
debugEnabled = debugEnabled,
)
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package io.opentelemetry.android.internal.features.persistence

import android.util.Log
import io.opentelemetry.android.common.RumConstants
import io.opentelemetry.android.features.diskbuffering.DiskBufferingConfiguration
import io.opentelemetry.android.features.diskbuffering.DiskBufferingConfig
import io.opentelemetry.android.internal.services.CacheStorage
import io.opentelemetry.android.internal.services.Preferences
import java.io.File
Expand All @@ -16,7 +16,7 @@ import java.io.IOException
internal class DiskManager(
private val cacheStorage: CacheStorage,
private val preferences: Preferences,
private val diskBufferingConfiguration: DiskBufferingConfiguration,
private val diskBufferingConfig: DiskBufferingConfig,
) {
@get:Throws(IOException::class)
val signalsBufferDir: File
Expand Down Expand Up @@ -53,7 +53,7 @@ internal class DiskManager(
)
return storedSize
}
val requestedSize = diskBufferingConfiguration.maxCacheSize
val requestedSize = diskBufferingConfig.maxCacheSize
val availableCacheSize =
cacheStorage.ensureCacheSpaceAvailable(requestedSize.toLong()).toInt()
// Divides the available cache size by 3 (for each signal's folder) and then subtracts the
Expand Down Expand Up @@ -86,7 +86,7 @@ internal class DiskManager(
}

val maxCacheFileSize: Int
get() = diskBufferingConfiguration.maxCacheFileSize
get() = diskBufferingConfig.maxCacheFileSize

companion object {
private const val MAX_FOLDER_SIZE_KEY = "max_signal_folder_size"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import androidx.annotation.NonNull;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import io.opentelemetry.android.config.OtelRumConfig;
import io.opentelemetry.android.features.diskbuffering.DiskBufferingConfiguration;
import io.opentelemetry.android.features.diskbuffering.DiskBufferingConfig;
import io.opentelemetry.android.features.diskbuffering.SignalFromDiskExporter;
import io.opentelemetry.android.features.diskbuffering.scheduler.ExportScheduleHandler;
import io.opentelemetry.android.instrumentation.AndroidInstrumentation;
Expand Down Expand Up @@ -348,8 +348,7 @@ public void diskBufferingEnabled() {
doReturn(60 * 1024 * 1024L).when(cacheStorage).ensureCacheSpaceAvailable(anyLong());
OtelRumConfig config = buildConfig();
ExportScheduleHandler scheduleHandler = mock();
config.setDiskBufferingConfiguration(
DiskBufferingConfiguration.builder().setEnabled(true).build());
config.setDiskBufferingConfig(new DiskBufferingConfig(true));
ArgumentCaptor<SpanExporter> exporterCaptor = ArgumentCaptor.forClass(SpanExporter.class);

OpenTelemetryRum.builder(application, config)
Expand Down Expand Up @@ -384,8 +383,7 @@ public void diskBufferingEnabled_when_exception_thrown() {
.getCacheDir();
ArgumentCaptor<SpanExporter> exporterCaptor = ArgumentCaptor.forClass(SpanExporter.class);
OtelRumConfig config = buildConfig();
config.setDiskBufferingConfiguration(
DiskBufferingConfiguration.builder().setEnabled(true).build());
config.setDiskBufferingConfig(new DiskBufferingConfig(true));

OpenTelemetryRum.builder(application, config)
.setServiceManager(serviceManager)
Expand Down Expand Up @@ -422,8 +420,7 @@ public void diskBufferingDisabled() {
ExportScheduleHandler scheduleHandler = mock();

OtelRumConfig config = buildConfig();
config.setDiskBufferingConfiguration(
DiskBufferingConfiguration.builder().setEnabled(false).build());
config.setDiskBufferingConfig(new DiskBufferingConfig(false));

OpenTelemetryRum.builder(application, config)
.setExportScheduleHandler(scheduleHandler)
Expand Down
Loading

0 comments on commit a55b22a

Please sign in to comment.