-
-
Notifications
You must be signed in to change notification settings - Fork 440
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: Attach Java vendor and version to events and transactions. (#1703)
- Loading branch information
1 parent
b52a0f0
commit b8854fe
Showing
7 changed files
with
160 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
sentry/src/main/java/io/sentry/SentryRuntimeEventProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package io.sentry; | ||
|
||
import io.sentry.protocol.SentryRuntime; | ||
import io.sentry.protocol.SentryTransaction; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
/** Attaches Java vendor and version to events and transactions. */ | ||
final class SentryRuntimeEventProcessor implements EventProcessor { | ||
private final @Nullable String javaVersion; | ||
private final @Nullable String javaVendor; | ||
|
||
public SentryRuntimeEventProcessor( | ||
final @Nullable String javaVersion, final @Nullable String javaVendor) { | ||
this.javaVersion = javaVersion; | ||
this.javaVendor = javaVendor; | ||
} | ||
|
||
public SentryRuntimeEventProcessor() { | ||
this(System.getProperty("java.version"), System.getProperty("java.vendor")); | ||
} | ||
|
||
@Override | ||
public @NotNull SentryEvent process( | ||
final @NotNull SentryEvent event, final @Nullable Object hint) { | ||
return process(event); | ||
} | ||
|
||
@Override | ||
public @NotNull SentryTransaction process( | ||
final @NotNull SentryTransaction transaction, final @Nullable Object hint) { | ||
return process(transaction); | ||
} | ||
|
||
private <T extends SentryBaseEvent> @NotNull T process(final @NotNull T event) { | ||
if (event.getContexts().getRuntime() == null) { | ||
event.getContexts().setRuntime(new SentryRuntime()); | ||
} | ||
final SentryRuntime runtime = event.getContexts().getRuntime(); | ||
if (runtime != null && runtime.getName() == null && runtime.getVersion() == null) { | ||
runtime.setName(javaVendor); | ||
runtime.setVersion(javaVersion); | ||
} | ||
return event; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package io.sentry.util; | ||
|
||
import org.jetbrains.annotations.ApiStatus; | ||
|
||
@ApiStatus.Internal | ||
public final class Platform { | ||
private static boolean isAndroid; | ||
|
||
static { | ||
// System#getProperty can throw an exception if there is a security manager is configured and | ||
// does not allow accessing system properties | ||
try { | ||
// All system properties on Android: | ||
// https://developer.android.com/reference/java/lang/System#getProperties() | ||
isAndroid = "The Android Project".equals(System.getProperty("java.vendor")); | ||
} catch (Exception e) { | ||
isAndroid = false; | ||
} | ||
} | ||
|
||
public static boolean isAndroid() { | ||
return isAndroid; | ||
} | ||
|
||
public static boolean isJvm() { | ||
return !isAndroid; | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
sentry/src/test/java/io/sentry/SentryRuntimeEventProcessorTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package io.sentry | ||
|
||
import io.sentry.protocol.SentryRuntime | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertNotNull | ||
import kotlin.test.assertNull | ||
|
||
class SentryRuntimeEventProcessorTest { | ||
|
||
private val eventProcessor = SentryRuntimeEventProcessor("16", "OpenJDK") | ||
|
||
@Test | ||
fun `when event does not have a runtime, sets runtime`() { | ||
val event = eventProcessor.process(SentryEvent(), null) | ||
assertNotNull(event) { | ||
assertNotNull(it.contexts.runtime) { runtime -> | ||
assertEquals("OpenJDK", runtime.name) | ||
assertEquals("16", runtime.version) | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
fun `when event has runtime with null name and null version, sets runtime`() { | ||
val event = SentryEvent() | ||
event.contexts.setRuntime(SentryRuntime()) | ||
val result = eventProcessor.process(event, null) | ||
assertNotNull(result) { | ||
assertNotNull(it.contexts.runtime) { runtime -> | ||
assertEquals("OpenJDK", runtime.name) | ||
assertEquals("16", runtime.version) | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
fun `when event has runtime with null name and version set, does not change runtime`() { | ||
val event = SentryEvent() | ||
val runtime = SentryRuntime() | ||
runtime.version = "1.1" | ||
event.contexts.setRuntime(runtime) | ||
val result = eventProcessor.process(event, null) | ||
assertNotNull(result) { | ||
assertNotNull(it.contexts.runtime) { runtime -> | ||
assertNull(runtime.name) | ||
assertEquals("1.1", runtime.version) | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
fun `when event has runtime with null version and name set, does not change runtime`() { | ||
val event = SentryEvent() | ||
val runtime = SentryRuntime() | ||
runtime.name = "Java" | ||
event.contexts.setRuntime(runtime) | ||
val result = eventProcessor.process(event, null) | ||
assertNotNull(result) { | ||
assertNotNull(it.contexts.runtime) { runtime -> | ||
assertNull(runtime.version) | ||
assertEquals("Java", runtime.name) | ||
} | ||
} | ||
} | ||
} |