Skip to content

Commit

Permalink
Merge branch 'open-telemetry:main' into develop/HttpURLConnection-aut…
Browse files Browse the repository at this point in the history
…o-instrumentation
  • Loading branch information
surbhiia authored Nov 16, 2023
2 parents e2516ac + 2942e5f commit d2cb68b
Show file tree
Hide file tree
Showing 20 changed files with 672 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,9 @@ android {
sourceCompatibility(javaVersion)
targetCompatibility(javaVersion)
}
}

val libs = extensions.getByType<VersionCatalogsExtension>().named("libs")
dependencies {
implementation(libs.findLibrary("findbugs-jsr305").get())
}
4 changes: 3 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ opentelemetry-java = "1.32.0"
opentelemetry-java-instrumentation = "1.31.0"
opentelemetry-java-instrumentation-alpha = "1.31.0-alpha"
opentelemetry-semconv = "1.21.0-alpha"
opentelemetry-contrib = "1.31.0-alpha"
mockito = "5.7.0"
junit = "5.10.1"
byteBuddy = "1.14.9"
Expand All @@ -27,7 +28,8 @@ opentelemetry-context = { module = "io.opentelemetry:opentelemetry-context", ver
opentelemetry-sdk = { module = "io.opentelemetry:opentelemetry-sdk" }
opentelemetry-exporter-zipkin = { module = "io.opentelemetry:opentelemetry-exporter-zipkin" }
opentelemetry-exporter-logging = { module = "io.opentelemetry:opentelemetry-exporter-logging" }
zipkin-sender-okhttp3 = { module = "io.zipkin.reporter2:zipkin-sender-okhttp3", version.ref = "zipkin-reporter"}
zipkin-sender-okhttp3 = { module = "io.zipkin.reporter2:zipkin-sender-okhttp3", version.ref = "zipkin-reporter" }
opentelemetry-diskBuffering = { module = "io.opentelemetry.contrib:opentelemetry-disk-buffering", version.ref = "opentelemetry-contrib" }

#Test tools
opentelemetry-sdk-testing = { module = "io.opentelemetry:opentelemetry-sdk-testing", version.ref = "opentelemetry-java" }
Expand Down
1 change: 1 addition & 0 deletions instrumentation/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ dependencies {
implementation(libs.opentelemetry.exporter.logging)
implementation(libs.opentelemetry.instrumentation.api)
implementation(libs.opentelemetry.semconv)
implementation(libs.opentelemetry.diskBuffering)

testImplementation(libs.bundles.mockito)
testImplementation(libs.bundles.junit)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package io.opentelemetry.android;

import android.app.Application;
import io.opentelemetry.android.config.OtelRumConfig;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.logs.SdkLoggerProvider;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import static java.util.Objects.requireNonNull;

import android.app.Application;
import io.opentelemetry.android.config.OtelRumConfig;
import io.opentelemetry.android.instrumentation.InstrumentedApplication;
import io.opentelemetry.android.instrumentation.activity.VisibleScreenTracker;
import io.opentelemetry.android.instrumentation.network.CurrentNetworkProvider;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import io.opentelemetry.sdk.trace.ReadableSpan;
import io.opentelemetry.sdk.trace.SpanProcessor;

class ScreenAttributesSpanProcessor implements SpanProcessor {
public final class ScreenAttributesSpanProcessor implements SpanProcessor {

private final VisibleScreenTracker visibleScreenTracker;

Expand Down
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.android;
package io.opentelemetry.android.config;

import io.opentelemetry.android.ScreenAttributesSpanProcessor;
import io.opentelemetry.android.instrumentation.network.CurrentNetworkProvider;
import io.opentelemetry.api.common.Attributes;
import java.util.function.Supplier;
Expand All @@ -20,6 +21,8 @@ public class OtelRumConfig {
private boolean includeNetworkAttributes = true;
private boolean generateSdkInitializationEvents = true;
private boolean includeScreenAttributes = true;
private DiskBufferingConfiguration diskBufferingConfiguration =
DiskBufferingConfiguration.builder().build();

/**
* Configures the set of global attributes to emit with every span and event. Any existing
Expand All @@ -34,12 +37,12 @@ public OtelRumConfig setGlobalAttributes(Supplier<Attributes> globalAttributesSu
return this;
}

boolean hasGlobalAttributes() {
public boolean hasGlobalAttributes() {
Attributes attributes = globalAttributesSupplier.get();
return attributes != null && !attributes.isEmpty();
}

Supplier<Attributes> getGlobalAttributesSupplier() {
public Supplier<Attributes> getGlobalAttributesSupplier() {
return globalAttributesSupplier;
}

Expand Down Expand Up @@ -90,4 +93,14 @@ public OtelRumConfig disableScreenAttributes() {
public boolean shouldIncludeScreenAttributes() {
return includeScreenAttributes;
}

public DiskBufferingConfiguration getDiskBufferingConfiguration() {
return diskBufferingConfiguration;
}

/** Sets the parameters for caching signals in disk in order to export them later. */
public void setDiskBufferingConfiguration(
DiskBufferingConfiguration diskBufferingConfiguration) {
this.diskBufferingConfiguration = diskBufferingConfiguration;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

package io.opentelemetry.android.instrumentation.startup;

import io.opentelemetry.android.OtelRumConfig;
import io.opentelemetry.android.config.OtelRumConfig;

public interface InitializationEvents {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

package io.opentelemetry.android.instrumentation.startup;

import io.opentelemetry.android.OtelRumConfig;
import io.opentelemetry.android.config.OtelRumConfig;

public class SdkInitializationEvents implements InitializationEvents {

Expand Down
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();
}
}
}
}
Loading

0 comments on commit d2cb68b

Please sign in to comment.