Skip to content

Commit

Permalink
feat: Add runtime classes for FileReader/FileWriter instrumentation (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
romtsn authored and maciejwalkowiak committed Jan 5, 2022
1 parent 72c2195 commit 1674273
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -7,6 +7,7 @@
* Feat: Add locale to device context and deprecate language (#1832)
* Ref: change `java.util.Random` to `java.security.SecureRandom` for possible security reasons (#1831)
* Feat: Add `SentryFileInputStream` and `SentryFileOutputStream` for File I/O performance instrumentation (#1826)
- Feat: Add `SentryFileReader` and `SentryFileWriter` for File I/O instrumentation (#1843)

## 5.4.3

Expand Down
14 changes: 14 additions & 0 deletions sentry/api/sentry.api
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
@@ -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));
}
}
@@ -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));
}
}
@@ -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)
}
}
@@ -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 1674273

Please sign in to comment.