File I/O Integration
Learn how to capture tracing information from file I/O operations.
Capturing transactions requires that you first set up tracing if you haven't already.
Sentry file I/O integration provides the SentryFileInputStream
/SentryFileOutputStream
and SentryFileReader
/SentryFileWriter
, which create a span for each File read/write operation.
The file I/O Integration is available under the core Java SDK package.
Copied
<dependency>
<groupId>io.sentry</groupId>
<artifactId>sentry</artifactId>
<version>7.17.0</version>
</dependency>
For other dependency managers, see the central Maven repository.
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").
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").