Skip to content

Commit 58c6970

Browse files
Copy options tags to transactions. (#1198)
Fixes #1163
1 parent 3eaa67f commit 58c6970

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* Enhancement: Enable Kotlin map-like access on CustomSamplingContext (#1192)
1717
* Enhancement: Auto register custom ITransportFactory in Spring integration (#1194)
1818
* Enhancement: Improve Kotlin property access in Performance API (#1193)
19+
* Enhancement: Copy options tags to transactions (#1198)
1920
* Enhancement: Add convenient method for accessing event's throwable (1202)
2021

2122
# 4.0.0-alpha.3

sentry/src/main/java/io/sentry/SentryClient.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,15 @@ public void captureSession(final @NotNull Session session, final @Nullable Objec
400400
if (transaction.getEnvironment() == null) {
401401
transaction.setEnvironment(options.getEnvironment());
402402
}
403+
if (transaction.getTags() == null) {
404+
transaction.setTags(new HashMap<>(options.getTags()));
405+
} else {
406+
for (Map.Entry<String, String> item : options.getTags().entrySet()) {
407+
if (!transaction.getTags().containsKey(item.getKey())) {
408+
transaction.setTag(item.getKey(), item.getValue());
409+
}
410+
}
411+
}
403412
return transaction;
404413
}
405414

sentry/src/test/java/io/sentry/SentryClientTest.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,27 @@ class SentryClientTest {
824824
assertEquals("transactionEnvironment", transaction.environment)
825825
}
826826

827+
@Test
828+
fun `when transaction does not have tags, and tags are set on options, options values are applied to transactions`() {
829+
fixture.sentryOptions.setTag("tag1", "value1")
830+
val sut = fixture.getSut()
831+
val transaction = SentryTransaction("name")
832+
sut.captureTransaction(transaction)
833+
assertEquals(mapOf("tag1" to "value1"), transaction.tags)
834+
}
835+
836+
@Test
837+
fun `when transaction has tags, and tags are set on options, options tags are added to transactions`() {
838+
fixture.sentryOptions.setTag("tag1", "value1")
839+
fixture.sentryOptions.setTag("tag2", "value2")
840+
val sut = fixture.getSut()
841+
val transaction = SentryTransaction("name")
842+
transaction.setTag("tag3", "value3")
843+
transaction.setTag("tag2", "transaction-tag")
844+
sut.captureTransaction(transaction)
845+
assertEquals(mapOf("tag1" to "value1", "tag2" to "transaction-tag", "tag3" to "value3"), transaction.tags)
846+
}
847+
827848
private fun createScope(): Scope {
828849
return Scope(SentryOptions()).apply {
829850
addBreadcrumb(Breadcrumb().apply {

0 commit comments

Comments
 (0)