File I/O
Learn more about the Sentry file I/O integration for the Android SDK.
Supported in Sentry's Android SDK version 5.5.0
and above.
Supported in Sentry Android Gradle Plugin version 3.0.0
and above.
The Sentry Android Gradle Plugin provides file I/O support through bytecode manipulation. The source can be found on GitHub.
On this page, we get you up and running with Sentry's file I/O integration, so that it will automatically start a span from an active transaction that's bound to the scope of each file input/output stream operation.
To use the file I/O integration, add the Sentry Android Gradle plugin in build.gradle
:
plugins {
id "io.sentry.android.gradle" version "4.14.1"
}
Make sure, that tracing is enabled.
In general, no further configuration is required as the auto-instrumentation is enabled by default. If you would like to disable the file I/O instrumentation feature, we expose a configuration option for that:
import io.sentry.android.gradle.extensions.InstrumentationFeature
sentry {
tracingInstrumentation {
enabled = true
features = EnumSet.allOf(InstrumentationFeature) - InstrumentationFeature.FILE_IO
}
}
Assuming you have the following (reduced) code snippet performing a file I/O operation:
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.activity.ComponentActivity
import io.sentry.Sentry
import io.sentry.SpanStatus
import java.io.File
class LyricsActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
findViewById<Button>(R.id.load_lyrics).setOnClickListener {
val transaction = Sentry.startTransaction(
name = "Track Interaction",
operation = "ui.action.lyrics",
bindToScope = true
)
val file = File(context.filesDir, "lyrics.txt")
val lyricsTextView = findViewById<TextView>(R.id.lyrics)
lyricsTextView.setText(file.readText())
transaction.finish(SpanStatus.OK)
}
}
}
To view the recorded transaction, log into sentry.io and open your project. Clicking Performance will open a page with transactions, where you can select the just recorded transaction with the name Track Interaction
. The event will look similar to this:
At the moment, we only support java.io.FileInputStream
, java.io.FileOutputStream
, java.io.FileReader
and java.io.FileWriter
classes for auto-instrumentation.
Alternatively, if you are not using our Gradle Plugin, the Android SDK provides the capability to manually instrument file I/O operations through the custom Sentry-specific implementations.
To instrument FileInputStream
/FileOutputStream
the SDK provides drop-in replacements such as SentryFileInputStream
/SentryFileOutputStream
.
Old:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
File file1 = new File("file1.txt");
File file2 = new File("file2.txt");
try (FileInputStream fis = new FileInputStream(file1)) {
byte[] buffer = new byte[1024];
try (FileOutputStream fos = new FileOutputStream(file2)) {
int read;
while (true) {
read = fis.read(buffer);
if (read == -1) {
break;
}
fos.write(buffer, 0, read);
}
}
}
New:
import io.sentry.instrumentation.file.SentryFileInputStream;
import io.sentry.instrumentation.file.SentryFileOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
File file1 = new File("file1.txt");
File file2 = new File("file2.txt");
try (FileInputStream fis = new SentryFileInputStream(file1)) {
byte[] buffer = new byte[1024];
try (FileOutputStream fos = new SentryFileOutputStream(file2)) {
int read;
while (true) {
read = fis.read(buffer);
if (read == -1) {
break;
}
fos.write(buffer, 0, read);
}
}
}
Similarly, FileReader
/FileWriter
can be instrumented through SentryFileReader
/SentryFileWriter
:
Old:
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
File file1 = new File("file1.txt");
File file2 = new File("file2.txt");
try (FileReader reader = new FileReader(file1)) {
char[] buffer = new char[1024];
try (FileWriter writer = new FileWriter(file2, true)) {
int read;
while (true) {
read = reader.read(buffer, 0, buffer.length);
if (read == -1) {
break;
}
writer.write(buffer, 0, buffer.length);
}
writer.flush();
}
}
New:
import io.sentry.instrumentation.file.SentryFileReader;
import io.sentry.instrumentation.file.SentryFileWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
File file1 = new File("file1.txt");
File file2 = new File("file2.txt");
try (FileReader reader = new SentryFileReader(file1)) {
char[] buffer = new char[1024];
try (FileWriter writer = new SentryFileWriter(file2, true)) {
int read;
while (true) {
read = reader.read(buffer, 0, buffer.length);
if (read == -1) {
break;
}
writer.write(buffer, 0, buffer.length);
}
writer.flush();
}
}
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").