Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read build tool info from sentry-debug-meta.properties and attach it to events #4314

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

### Internal

- Read build tool info from `sentry-debug-meta.properties` and attach it to events ([#4314](https://github.com/getsentry/sentry-java/pull/4314))

## 8.6.0

### Behavioral Changes
Expand Down
3 changes: 3 additions & 0 deletions sentry/api/sentry.api
Original file line number Diff line number Diff line change
Expand Up @@ -6364,7 +6364,10 @@ public abstract interface class io/sentry/util/CollectionUtils$Predicate {
public final class io/sentry/util/DebugMetaPropertiesApplier {
public static field DEBUG_META_PROPERTIES_FILENAME Ljava/lang/String;
public fun <init> ()V
public static fun apply (Lio/sentry/SentryOptions;Ljava/util/List;)V
public static fun applyToOptions (Lio/sentry/SentryOptions;Ljava/util/List;)V
public static fun getBuildTool (Ljava/util/Properties;)Ljava/lang/String;
public static fun getBuildToolVersion (Ljava/util/Properties;)Ljava/lang/String;
public static fun getProguardUuid (Ljava/util/Properties;)Ljava/lang/String;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.sentry.util;

import io.sentry.SentryIntegrationPackageStorage;
import io.sentry.SentryLevel;
import io.sentry.SentryOptions;
import java.util.List;
Expand All @@ -11,6 +12,14 @@ public final class DebugMetaPropertiesApplier {

public static @NotNull String DEBUG_META_PROPERTIES_FILENAME = "sentry-debug-meta.properties";

public static void apply(
final @NotNull SentryOptions options, final @Nullable List<Properties> debugMetaProperties) {
if (debugMetaProperties != null) {
applyToOptions(options, debugMetaProperties);
applyBuildTool(options, debugMetaProperties);
}
}

public static void applyToOptions(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This class and method are public and not marked as internal so I've created a new method to implement the new behavior

final @NotNull SentryOptions options, final @Nullable List<Properties> debugMetaProperties) {
if (debugMetaProperties != null) {
Expand Down Expand Up @@ -49,7 +58,35 @@ private static void applyProguardUuid(
}
}

private static void applyBuildTool(
final @NotNull SentryOptions options, @NotNull List<Properties> debugMetaProperties) {
for (Properties properties : debugMetaProperties) {
final @Nullable String buildTool = getBuildTool(properties);
if (buildTool != null) {
@Nullable String buildToolVersion = getBuildToolVersion(properties);
if (buildToolVersion == null) {
buildToolVersion = "unknown";
}
options
.getLogger()
.log(
SentryLevel.DEBUG, "Build tool found: %s, version %s", buildTool, buildToolVersion);
SentryIntegrationPackageStorage.getInstance().addPackage(buildTool, buildToolVersion);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like that we're abusing the sdk.packages field of the envelope.
Should we maybe send it as sdk.build-tool or something? Even though it's not officially supported by the protocol.

break;
}
}
}

public static @Nullable String getProguardUuid(final @NotNull Properties debugMetaProperties) {
return debugMetaProperties.getProperty("io.sentry.ProguardUuids");
}

public static @Nullable String getBuildTool(final @NotNull Properties debugMetaProperties) {
return debugMetaProperties.getProperty("io.sentry.build-tool");
}

public static @Nullable String getBuildToolVersion(
final @NotNull Properties debugMetaProperties) {
return debugMetaProperties.getProperty("io.sentry.build-tool-version");
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package io.sentry.internal.debugmeta

import io.sentry.ILogger
import io.sentry.SentryIntegrationPackageStorage
import io.sentry.SentryOptions
import io.sentry.protocol.SentryPackage
import io.sentry.util.DebugMetaPropertiesApplier
import org.mockito.kotlin.mock
import org.mockito.kotlin.whenever
import java.net.URL
import java.nio.charset.Charset
import java.util.Collections
import kotlin.test.Test
import kotlin.test.assertContains
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertNotNull
import kotlin.test.assertNull

Expand Down Expand Up @@ -132,4 +136,72 @@ class ResourcesDebugMetaLoaderTest {

assertNull(sut.loadDebugMeta())
}

@Test
fun `reads build-tool and build-version and adds them to packages`() {
val sut = fixture.getSut(
content = listOf(
"""
#Generated by sentry-maven-plugin
#Wed May 17 15:33:34 CEST 2023
io.sentry.ProguardUuids=34077988-a0e5-4839-9618-7400e1616d1b
io.sentry.bundle-ids=88ba82db-cd26-4c09-8b31-21461d286b68
io.sentry.build-tool=maven
io.sentry.build-tool-version=1.0
""".trimIndent()
)
)

val options = SentryOptions()
assertNotNull(sut.loadDebugMeta()) {
DebugMetaPropertiesApplier.apply(options, it)
}

val expected = SentryPackage("maven", "1.0")
assertContains(SentryIntegrationPackageStorage.getInstance().packages, expected)
}

@Test
fun `reads build-tool and adds it to packages with unknown version if build-tool-version is absent`() {
val sut = fixture.getSut(
content = listOf(
"""
#Generated by sentry-maven-plugin
#Wed May 17 15:33:34 CEST 2023
io.sentry.ProguardUuids=34077988-a0e5-4839-9618-7400e1616d1b
io.sentry.bundle-ids=88ba82db-cd26-4c09-8b31-21461d286b68
io.sentry.build-tool=maven
""".trimIndent()
)
)

val options = SentryOptions()
assertNotNull(sut.loadDebugMeta()) {
DebugMetaPropertiesApplier.apply(options, it)
}

val expected = SentryPackage("maven", "unknown")
assertContains(SentryIntegrationPackageStorage.getInstance().packages, expected)
}

@Test
fun `does not add build-tool to packages if absent`() {
val sut = fixture.getSut(
content = listOf(
"""
#Generated manually
#Wed May 17 15:33:34 CEST 2023
io.sentry.ProguardUuids=34077988-a0e5-4839-9618-7400e1616d1b
io.sentry.bundle-ids=88ba82db-cd26-4c09-8b31-21461d286b68
""".trimIndent()
)
)

val options = SentryOptions()
assertNotNull(sut.loadDebugMeta()) {
DebugMetaPropertiesApplier.apply(options, it)
}

assertFalse { SentryIntegrationPackageStorage.getInstance().packages.any { it.name.equals("io.sentry.build-tool") } }
}
}
Loading