Skip to content

Commit

Permalink
Add SentryFileReader/SentryFileWriter
Browse files Browse the repository at this point in the history
  • Loading branch information
romtsn committed Dec 13, 2021
1 parent a3b6103 commit ff28d76
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 0 deletions.
14 changes: 14 additions & 0 deletions sentry/api/sentry.api
Original file line number Diff line number Diff line change
Expand Up @@ -1471,6 +1471,20 @@ public final class io/sentry/instrumentation/file/SentryFileOutputStream$Factory
public static fun create (Ljava/io/FileOutputStream;Ljava/lang/String;Z)Ljava/io/FileOutputStream;
}

public final class io/sentry/instrumentation/file/SentryFileReader : java/io/InputStreamReader {
public fun <init> (Ljava/io/File;)V
public fun <init> (Ljava/io/FileDescriptor;)V
public fun <init> (Ljava/lang/String;)V
}

public final class io/sentry/instrumentation/file/SentryFileWriter : java/io/OutputStreamWriter {
public fun <init> (Ljava/io/File;)V
public fun <init> (Ljava/io/File;Z)V
public fun <init> (Ljava/io/FileDescriptor;)V
public fun <init> (Ljava/lang/String;)V
public fun <init> (Ljava/lang/String;Z)V
}

public final class io/sentry/protocol/App : io/sentry/IUnknownPropertiesConsumer {
public static final field TYPE Ljava/lang/String;
public fun <init> ()V
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.sentry.instrumentation.file;

import io.sentry.IHub;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import org.jetbrains.annotations.NotNull;

public final class SentryFileReader extends InputStreamReader {
public SentryFileReader(final @NotNull String fileName) throws FileNotFoundException {
super(new SentryFileInputStream(fileName));
}

public SentryFileReader(final @NotNull File file) throws FileNotFoundException {
super(new SentryFileInputStream(file));
}

public SentryFileReader(final @NotNull FileDescriptor fd) {
super(new SentryFileInputStream(fd));
}

SentryFileReader(final @NotNull File file, final @NotNull IHub hub) throws FileNotFoundException {
super(new SentryFileInputStream(file, hub));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.sentry.instrumentation.file;

import io.sentry.IHub;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileNotFoundException;
import java.io.OutputStreamWriter;
import org.jetbrains.annotations.NotNull;

public final class SentryFileWriter extends OutputStreamWriter {
public SentryFileWriter(final @NotNull String fileName) throws FileNotFoundException {
super(new SentryFileOutputStream(fileName));
}

public SentryFileWriter(final @NotNull String fileName, final boolean append)
throws FileNotFoundException {
super(new SentryFileOutputStream(fileName, append));
}

public SentryFileWriter(final @NotNull File file) throws FileNotFoundException {
super(new SentryFileOutputStream(file));
}

public SentryFileWriter(final @NotNull File file, final boolean append)
throws FileNotFoundException {
super(new SentryFileOutputStream(file, append));
}

public SentryFileWriter(final @NotNull FileDescriptor fd) {
super(new SentryFileOutputStream(fd));
}

SentryFileWriter(final @NotNull File file, final @NotNull IHub hub) throws FileNotFoundException {
super(new SentryFileOutputStream(file, hub));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package io.sentry.instrumentation.file

import com.nhaarman.mockitokotlin2.mock
import com.nhaarman.mockitokotlin2.whenever
import io.sentry.IHub
import io.sentry.SentryOptions
import io.sentry.SentryTracer
import io.sentry.SpanStatus.OK
import io.sentry.TransactionContext
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
import java.io.File
import kotlin.test.assertEquals

class SentryFileReaderTest {
class Fixture {
val hub = mock<IHub>()
val sentryTracer = SentryTracer(TransactionContext("name", "op"), hub)

internal fun getSut(
tmpFile: File,
activeTransaction: Boolean = true,
): SentryFileReader {
tmpFile.writeText("TEXT")
whenever(hub.options).thenReturn(SentryOptions())
if (activeTransaction) {
whenever(hub.span).thenReturn(sentryTracer)
}
return SentryFileReader(tmpFile, hub)
}
}

@get:Rule
val tmpDir = TemporaryFolder()

private val fixture = Fixture()

private val tmpFile: File get() = tmpDir.newFile("test.txt")

@Test
fun `captures a span`() {
val reader = fixture.getSut(tmpFile)
reader.readText()
reader.close()

assertEquals(fixture.sentryTracer.children.size, 1)
val fileIOSpan = fixture.sentryTracer.children.first()
assertEquals(fileIOSpan.spanContext.operation, "file.read")
assertEquals(fileIOSpan.spanContext.description, "test.txt (4 B)")
assertEquals(fileIOSpan.data["file.size"], 4L)
assertEquals(fileIOSpan.throwable, null)
assertEquals(fileIOSpan.isFinished, true)
assertEquals(fileIOSpan.status, OK)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package io.sentry.instrumentation.file

import com.nhaarman.mockitokotlin2.mock
import com.nhaarman.mockitokotlin2.whenever
import io.sentry.IHub
import io.sentry.SentryOptions
import io.sentry.SentryTracer
import io.sentry.SpanStatus.OK
import io.sentry.TransactionContext
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
import java.io.File
import kotlin.test.assertEquals

class SentryFileWriterTest {
class Fixture {
val hub = mock<IHub>()
val sentryTracer = SentryTracer(TransactionContext("name", "op"), hub)

internal fun getSut(
tmpFile: File,
activeTransaction: Boolean = true,
): SentryFileWriter {
whenever(hub.options).thenReturn(SentryOptions())
if (activeTransaction) {
whenever(hub.span).thenReturn(sentryTracer)
}
return SentryFileWriter(tmpFile, hub)
}
}

@get:Rule
val tmpDir = TemporaryFolder()

private val fixture = Fixture()

private val tmpFile: File get() = tmpDir.newFile("test.txt")

@Test
fun `captures a span`() {
val writer = fixture.getSut(tmpFile)
writer.write("TEXT")
writer.close()

assertEquals(fixture.sentryTracer.children.size, 1)
val fileIOSpan = fixture.sentryTracer.children.first()
assertEquals(fileIOSpan.spanContext.operation, "file.write")
assertEquals(fileIOSpan.spanContext.description, "test.txt (4 B)")
assertEquals(fileIOSpan.data["file.size"], 4L)
assertEquals(fileIOSpan.throwable, null)
assertEquals(fileIOSpan.isFinished, true)
assertEquals(fileIOSpan.status, OK)
}
}

0 comments on commit ff28d76

Please sign in to comment.