File I/O

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:

Copied
plugins {
  id "io.sentry.android.gradle" version "4.5.0"
}

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:

Copied
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:

Copied
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:

File I/O performance 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:

Copied
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:

Copied
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:

Copied
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:

Copied
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();
  }
}
Help improve this content
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").