Skip to content

Commit 6e0d085

Browse files
authored
Read build tool info from sentry-debug-meta.properties and attach it to events (#4314)
1 parent 10ae067 commit 6e0d085

File tree

5 files changed

+125
-5
lines changed

5 files changed

+125
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
## Unreleased
44

5-
### Fixes
5+
### Features
66

77
- Send Timber logs through Sentry Logs ([#4490](https://github.com/getsentry/sentry-java/pull/4490))
88
- Enable the Logs feature in your `SentryOptions` or with the `io.sentry.logs.enabled` manifest option and the SDK will automatically send Timber logs to Sentry, if the TimberIntegration is enabled.
99
- The SDK will automatically detect Timber and use it to send logs to Sentry.
1010
- Send logcat through Sentry Logs ([#4487](https://github.com/getsentry/sentry-java/pull/4487))
1111
- Enable the Logs feature in your `SentryOptions` or with the `io.sentry.logs.enabled` manifest option and the SDK will automatically send logcat logs to Sentry, if the Sentry Android Gradle plugin is applied.
1212
- To set the logcat level check the [Logcat integration documentation](https://docs.sentry.io/platforms/android/integrations/logcat/#configure).
13+
- Read build tool info from `sentry-debug-meta.properties` and attach it to events ([#4314](https://github.com/getsentry/sentry-java/pull/4314))
1314

1415
### Dependencies
1516

sentry/api/sentry.api

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6743,7 +6743,10 @@ public abstract interface class io/sentry/util/CollectionUtils$Predicate {
67436743
public final class io/sentry/util/DebugMetaPropertiesApplier {
67446744
public static field DEBUG_META_PROPERTIES_FILENAME Ljava/lang/String;
67456745
public fun <init> ()V
6746+
public static fun apply (Lio/sentry/SentryOptions;Ljava/util/List;)V
67466747
public static fun applyToOptions (Lio/sentry/SentryOptions;Ljava/util/List;)V
6748+
public static fun getBuildTool (Ljava/util/Properties;)Ljava/lang/String;
6749+
public static fun getBuildToolVersion (Ljava/util/Properties;)Ljava/lang/String;
67476750
public static fun getProguardUuid (Ljava/util/Properties;)Ljava/lang/String;
67486751
}
67496752

sentry/src/main/java/io/sentry/Sentry.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,7 @@ private static void initConfigurations(final @NotNull SentryOptions options) {
645645
options.setDebugMetaLoader(new ResourcesDebugMetaLoader(options.getLogger()));
646646
}
647647
final @Nullable List<Properties> propertiesList = options.getDebugMetaLoader().loadDebugMeta();
648-
DebugMetaPropertiesApplier.applyToOptions(options, propertiesList);
648+
DebugMetaPropertiesApplier.apply(options, propertiesList);
649649

650650
final IThreadChecker threadChecker = options.getThreadChecker();
651651
// only override the ThreadChecker if it's not already set by Android

sentry/src/main/java/io/sentry/util/DebugMetaPropertiesApplier.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.sentry.util;
22

3+
import io.sentry.SentryIntegrationPackageStorage;
34
import io.sentry.SentryLevel;
45
import io.sentry.SentryOptions;
56
import java.util.List;
@@ -11,6 +12,14 @@ public final class DebugMetaPropertiesApplier {
1112

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

15+
public static void apply(
16+
final @NotNull SentryOptions options, final @Nullable List<Properties> debugMetaProperties) {
17+
if (debugMetaProperties != null) {
18+
applyToOptions(options, debugMetaProperties);
19+
applyBuildTool(options, debugMetaProperties);
20+
}
21+
}
22+
1423
public static void applyToOptions(
1524
final @NotNull SentryOptions options, final @Nullable List<Properties> debugMetaProperties) {
1625
if (debugMetaProperties != null) {
@@ -49,7 +58,35 @@ private static void applyProguardUuid(
4958
}
5059
}
5160

61+
private static void applyBuildTool(
62+
final @NotNull SentryOptions options, @NotNull List<Properties> debugMetaProperties) {
63+
for (Properties properties : debugMetaProperties) {
64+
final @Nullable String buildTool = getBuildTool(properties);
65+
if (buildTool != null) {
66+
@Nullable String buildToolVersion = getBuildToolVersion(properties);
67+
if (buildToolVersion == null) {
68+
buildToolVersion = "unknown";
69+
}
70+
options
71+
.getLogger()
72+
.log(
73+
SentryLevel.DEBUG, "Build tool found: %s, version %s", buildTool, buildToolVersion);
74+
SentryIntegrationPackageStorage.getInstance().addPackage(buildTool, buildToolVersion);
75+
break;
76+
}
77+
}
78+
}
79+
5280
public static @Nullable String getProguardUuid(final @NotNull Properties debugMetaProperties) {
5381
return debugMetaProperties.getProperty("io.sentry.ProguardUuids");
5482
}
83+
84+
public static @Nullable String getBuildTool(final @NotNull Properties debugMetaProperties) {
85+
return debugMetaProperties.getProperty("io.sentry.build-tool");
86+
}
87+
88+
public static @Nullable String getBuildToolVersion(
89+
final @NotNull Properties debugMetaProperties) {
90+
return debugMetaProperties.getProperty("io.sentry.build-tool-version");
91+
}
5592
}

sentry/src/test/java/io/sentry/internal/debugmeta/ResourcesDebugMetaLoaderTest.kt

Lines changed: 82 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
package io.sentry.internal.debugmeta
22

33
import io.sentry.ILogger
4+
import io.sentry.SentryIntegrationPackageStorage
45
import io.sentry.SentryOptions
6+
import io.sentry.protocol.SentryPackage
57
import io.sentry.util.DebugMetaPropertiesApplier
68
import java.net.URL
79
import java.nio.charset.Charset
810
import java.util.Collections
911
import kotlin.test.Test
12+
import kotlin.test.assertContains
1013
import kotlin.test.assertEquals
14+
import kotlin.test.assertFalse
1115
import kotlin.test.assertNotNull
1216
import kotlin.test.assertNull
1317
import org.mockito.kotlin.mock
@@ -59,7 +63,7 @@ class ResourcesDebugMetaLoaderTest {
5963

6064
val options = SentryOptions()
6165

62-
assertNotNull(sut.loadDebugMeta()) { DebugMetaPropertiesApplier.applyToOptions(options, it) }
66+
assertNotNull(sut.loadDebugMeta()) { DebugMetaPropertiesApplier.apply(options, it) }
6367

6468
assertEquals(options.bundleIds, setOf("88ba82db-cd26-4c09-8b31-21461d286b68"))
6569
assertEquals(options.proguardUuid, "34077988-a0e5-4839-9618-7400e1616d1b")
@@ -84,7 +88,7 @@ class ResourcesDebugMetaLoaderTest {
8488

8589
val options = SentryOptions()
8690

87-
assertNotNull(sut.loadDebugMeta()) { DebugMetaPropertiesApplier.applyToOptions(options, it) }
91+
assertNotNull(sut.loadDebugMeta()) { DebugMetaPropertiesApplier.apply(options, it) }
8892

8993
assertEquals(
9094
options.bundleIds,
@@ -120,7 +124,7 @@ class ResourcesDebugMetaLoaderTest {
120124

121125
val options = SentryOptions()
122126

123-
assertNotNull(sut.loadDebugMeta()) { DebugMetaPropertiesApplier.applyToOptions(options, it) }
127+
assertNotNull(sut.loadDebugMeta()) { DebugMetaPropertiesApplier.apply(options, it) }
124128

125129
assertEquals(
126130
options.bundleIds,
@@ -139,4 +143,79 @@ class ResourcesDebugMetaLoaderTest {
139143

140144
assertNull(sut.loadDebugMeta())
141145
}
146+
147+
@Test
148+
fun `reads build-tool and build-version and adds them to packages`() {
149+
val sut =
150+
fixture.getSut(
151+
content =
152+
listOf(
153+
"""
154+
#Generated by sentry-maven-plugin
155+
#Wed May 17 15:33:34 CEST 2023
156+
io.sentry.ProguardUuids=34077988-a0e5-4839-9618-7400e1616d1b
157+
io.sentry.bundle-ids=88ba82db-cd26-4c09-8b31-21461d286b68
158+
io.sentry.build-tool=maven
159+
io.sentry.build-tool-version=1.0
160+
"""
161+
.trimIndent()
162+
)
163+
)
164+
165+
val options = SentryOptions()
166+
assertNotNull(sut.loadDebugMeta()) { DebugMetaPropertiesApplier.apply(options, it) }
167+
168+
val expected = SentryPackage("maven", "1.0")
169+
assertContains(SentryIntegrationPackageStorage.getInstance().packages, expected)
170+
}
171+
172+
@Test
173+
fun `reads build-tool and adds it to packages with unknown version if build-tool-version is absent`() {
174+
val sut =
175+
fixture.getSut(
176+
content =
177+
listOf(
178+
"""
179+
#Generated by sentry-maven-plugin
180+
#Wed May 17 15:33:34 CEST 2023
181+
io.sentry.ProguardUuids=34077988-a0e5-4839-9618-7400e1616d1b
182+
io.sentry.bundle-ids=88ba82db-cd26-4c09-8b31-21461d286b68
183+
io.sentry.build-tool=maven
184+
"""
185+
.trimIndent()
186+
)
187+
)
188+
189+
val options = SentryOptions()
190+
assertNotNull(sut.loadDebugMeta()) { DebugMetaPropertiesApplier.apply(options, it) }
191+
192+
val expected = SentryPackage("maven", "unknown")
193+
assertContains(SentryIntegrationPackageStorage.getInstance().packages, expected)
194+
}
195+
196+
@Test
197+
fun `does not add build-tool to packages if absent`() {
198+
val sut =
199+
fixture.getSut(
200+
content =
201+
listOf(
202+
"""
203+
#Generated manually
204+
#Wed May 17 15:33:34 CEST 2023
205+
io.sentry.ProguardUuids=34077988-a0e5-4839-9618-7400e1616d1b
206+
io.sentry.bundle-ids=88ba82db-cd26-4c09-8b31-21461d286b68
207+
"""
208+
.trimIndent()
209+
)
210+
)
211+
212+
val options = SentryOptions()
213+
assertNotNull(sut.loadDebugMeta()) { DebugMetaPropertiesApplier.apply(options, it) }
214+
215+
assertFalse {
216+
SentryIntegrationPackageStorage.getInstance().packages.any {
217+
it.name.equals("io.sentry.build-tool")
218+
}
219+
}
220+
}
142221
}

0 commit comments

Comments
 (0)