Skip to content

Commit 1f86db1

Browse files
authored
Merge b01298b into a474402
2 parents a474402 + b01298b commit 1f86db1

File tree

10 files changed

+117
-65
lines changed

10 files changed

+117
-65
lines changed

sentry-log4j2/src/test/kotlin/io/sentry/log4j2/SentryAppenderTest.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ class SentryAppenderTest {
7878
@BeforeTest
7979
fun `clear MDC`() {
8080
ThreadContext.clearAll()
81+
Sentry.close()
8182
}
8283

8384
@Test

sentry-quartz/api/sentry-quartz.api

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ public final class io/sentry/quartz/BuildConfig {
55

66
public final class io/sentry/quartz/SentryJobListener : org/quartz/JobListener {
77
public static final field SENTRY_CHECK_IN_ID_KEY Ljava/lang/String;
8+
public static final field SENTRY_LIFECYCLE_TOKEN_KEY Ljava/lang/String;
89
public static final field SENTRY_SLUG_KEY Ljava/lang/String;
910
public fun <init> ()V
1011
public fun <init> (Lio/sentry/IScopes;)V

sentry-quartz/src/main/java/io/sentry/quartz/SentryJobListener.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
import io.sentry.CheckIn;
55
import io.sentry.CheckInStatus;
66
import io.sentry.IScopes;
7+
import io.sentry.ISentryLifecycleToken;
78
import io.sentry.ScopesAdapter;
89
import io.sentry.SentryIntegrationPackageStorage;
910
import io.sentry.SentryLevel;
1011
import io.sentry.protocol.SentryId;
12+
import io.sentry.util.LifecycleHelper;
1113
import io.sentry.util.Objects;
1214
import io.sentry.util.TracingUtils;
1315
import org.jetbrains.annotations.ApiStatus;
@@ -23,6 +25,7 @@ public final class SentryJobListener implements JobListener {
2325

2426
public static final String SENTRY_CHECK_IN_ID_KEY = "sentry-checkin-id";
2527
public static final String SENTRY_SLUG_KEY = "sentry-slug";
28+
public static final String SENTRY_LIFECYCLE_TOKEN_KEY = "sentry-lifecycle";
2629

2730
private final @NotNull IScopes scopes;
2831

@@ -49,13 +52,14 @@ public void jobToBeExecuted(final @NotNull JobExecutionContext context) {
4952
if (maybeSlug == null) {
5053
return;
5154
}
52-
scopes.pushScope();
55+
final @NotNull ISentryLifecycleToken lifecycleToken = scopes.pushIsolationScope();
5356
TracingUtils.startNewTrace(scopes);
5457
final @NotNull String slug = maybeSlug;
5558
final @NotNull CheckIn checkIn = new CheckIn(slug, CheckInStatus.IN_PROGRESS);
5659
final @NotNull SentryId checkInId = scopes.captureCheckIn(checkIn);
5760
context.put(SENTRY_CHECK_IN_ID_KEY, checkInId);
5861
context.put(SENTRY_SLUG_KEY, slug);
62+
context.put(SENTRY_LIFECYCLE_TOKEN_KEY, lifecycleToken);
5963
} catch (Throwable t) {
6064
scopes
6165
.getOptions()
@@ -103,7 +107,7 @@ public void jobWasExecuted(JobExecutionContext context, JobExecutionException jo
103107
.getLogger()
104108
.log(SentryLevel.ERROR, "Unable to capture check-in in jobWasExecuted.", t);
105109
} finally {
106-
scopes.popScope();
110+
LifecycleHelper.close(context.get(SENTRY_LIFECYCLE_TOKEN_KEY));
107111
}
108112
}
109113
}

sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/checkin/SentryCheckInAdvice.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import io.sentry.CheckInStatus;
66
import io.sentry.DateUtils;
77
import io.sentry.IScopes;
8+
import io.sentry.ISentryLifecycleToken;
89
import io.sentry.ScopesAdapter;
910
import io.sentry.SentryLevel;
1011
import io.sentry.protocol.SentryId;
@@ -86,7 +87,7 @@ public Object invoke(final @NotNull MethodInvocation invocation) throws Throwabl
8687
return invocation.proceed();
8788
}
8889

89-
scopes.pushScope();
90+
final @NotNull ISentryLifecycleToken lifecycleToken = scopes.pushIsolationScope();
9091
TracingUtils.startNewTrace(scopes);
9192

9293
@Nullable SentryId checkInId = null;
@@ -106,7 +107,7 @@ public Object invoke(final @NotNull MethodInvocation invocation) throws Throwabl
106107
CheckIn checkIn = new CheckIn(checkInId, monitorSlug, status);
107108
checkIn.setDuration(DateUtils.millisToSeconds(System.currentTimeMillis() - startTime));
108109
scopes.captureCheckIn(checkIn);
109-
scopes.popScope();
110+
lifecycleToken.close();
110111
}
111112
}
112113

sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentryCheckInAdviceTest.kt

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package io.sentry.spring.jakarta
33
import io.sentry.CheckIn
44
import io.sentry.CheckInStatus
55
import io.sentry.IScopes
6+
import io.sentry.ISentryLifecycleToken
67
import io.sentry.Sentry
78
import io.sentry.SentryOptions
89
import io.sentry.protocol.SentryId
@@ -56,10 +57,13 @@ class SentryCheckInAdviceTest {
5657
@Autowired
5758
lateinit var scopes: IScopes
5859

60+
val lifecycleToken = mock<ISentryLifecycleToken>()
61+
5962
@BeforeTest
6063
fun setup() {
6164
reset(scopes)
6265
whenever(scopes.options).thenReturn(SentryOptions())
66+
whenever(scopes.pushIsolationScope()).thenReturn(lifecycleToken)
6367
}
6468

6569
@Test
@@ -79,10 +83,10 @@ class SentryCheckInAdviceTest {
7983
assertEquals("monitor_slug_1", doneCheckIn.monitorSlug)
8084
assertEquals(CheckInStatus.OK.apiName(), doneCheckIn.status)
8185

82-
val order = inOrder(scopes)
83-
order.verify(scopes).pushScope()
86+
val order = inOrder(scopes, lifecycleToken)
87+
order.verify(scopes).pushIsolationScope()
8488
order.verify(scopes, times(2)).captureCheckIn(any())
85-
order.verify(scopes).popScope()
89+
order.verify(lifecycleToken).close()
8690
}
8791

8892
@Test
@@ -103,10 +107,10 @@ class SentryCheckInAdviceTest {
103107
assertEquals("monitor_slug_1e", doneCheckIn.monitorSlug)
104108
assertEquals(CheckInStatus.ERROR.apiName(), doneCheckIn.status)
105109

106-
val order = inOrder(scopes)
107-
order.verify(scopes).pushScope()
110+
val order = inOrder(scopes, lifecycleToken)
111+
order.verify(scopes).pushIsolationScope()
108112
order.verify(scopes, times(2)).captureCheckIn(any())
109-
order.verify(scopes).popScope()
113+
order.verify(lifecycleToken).close()
110114
}
111115

112116
@Test
@@ -123,10 +127,10 @@ class SentryCheckInAdviceTest {
123127
assertEquals(CheckInStatus.OK.apiName(), doneCheckIn.status)
124128
assertNotNull(doneCheckIn.duration)
125129

126-
val order = inOrder(scopes)
127-
order.verify(scopes).pushScope()
130+
val order = inOrder(scopes, lifecycleToken)
131+
order.verify(scopes).pushIsolationScope()
128132
order.verify(scopes).captureCheckIn(any())
129-
order.verify(scopes).popScope()
133+
order.verify(lifecycleToken).close()
130134
}
131135

132136
@Test
@@ -144,10 +148,10 @@ class SentryCheckInAdviceTest {
144148
assertEquals(CheckInStatus.ERROR.apiName(), doneCheckIn.status)
145149
assertNotNull(doneCheckIn.duration)
146150

147-
val order = inOrder(scopes)
148-
order.verify(scopes).pushScope()
151+
val order = inOrder(scopes, lifecycleToken)
152+
order.verify(scopes).pushIsolationScope()
149153
order.verify(scopes).captureCheckIn(any())
150-
order.verify(scopes).popScope()
154+
order.verify(lifecycleToken).close()
151155
}
152156

153157
@Test
@@ -178,10 +182,10 @@ class SentryCheckInAdviceTest {
178182
assertEquals(CheckInStatus.OK.apiName(), doneCheckIn.status)
179183
assertNotNull(doneCheckIn.duration)
180184

181-
val order = inOrder(scopes)
182-
order.verify(scopes).pushScope()
185+
val order = inOrder(scopes, lifecycleToken)
186+
order.verify(scopes).pushIsolationScope()
183187
order.verify(scopes).captureCheckIn(any())
184-
order.verify(scopes).popScope()
188+
order.verify(lifecycleToken).close()
185189
}
186190

187191
@Test
@@ -198,10 +202,10 @@ class SentryCheckInAdviceTest {
198202
assertEquals(CheckInStatus.OK.apiName(), doneCheckIn.status)
199203
assertNotNull(doneCheckIn.duration)
200204

201-
val order = inOrder(scopes)
202-
order.verify(scopes).pushScope()
205+
val order = inOrder(scopes, lifecycleToken)
206+
order.verify(scopes).pushIsolationScope()
203207
order.verify(scopes).captureCheckIn(any())
204-
order.verify(scopes).popScope()
208+
order.verify(lifecycleToken).close()
205209
}
206210

207211
@Test
@@ -218,10 +222,10 @@ class SentryCheckInAdviceTest {
218222
assertEquals(CheckInStatus.OK.apiName(), doneCheckIn.status)
219223
assertNotNull(doneCheckIn.duration)
220224

221-
val order = inOrder(scopes)
222-
order.verify(scopes).pushScope()
225+
val order = inOrder(scopes, lifecycleToken)
226+
order.verify(scopes).pushIsolationScope()
223227
order.verify(scopes).captureCheckIn(any())
224-
order.verify(scopes).popScope()
228+
order.verify(lifecycleToken).close()
225229
}
226230

227231
@Configuration

sentry-spring/src/main/java/io/sentry/spring/checkin/SentryCheckInAdvice.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import io.sentry.CheckInStatus;
66
import io.sentry.DateUtils;
77
import io.sentry.IScopes;
8+
import io.sentry.ISentryLifecycleToken;
89
import io.sentry.ScopesAdapter;
910
import io.sentry.SentryLevel;
1011
import io.sentry.protocol.SentryId;
@@ -89,7 +90,7 @@ public Object invoke(final @NotNull MethodInvocation invocation) throws Throwabl
8990
return invocation.proceed();
9091
}
9192

92-
scopes.pushScope();
93+
final @NotNull ISentryLifecycleToken lifecycleToken = scopes.pushIsolationScope();
9394
TracingUtils.startNewTrace(scopes);
9495

9596
@Nullable SentryId checkInId = null;
@@ -109,7 +110,7 @@ public Object invoke(final @NotNull MethodInvocation invocation) throws Throwabl
109110
CheckIn checkIn = new CheckIn(checkInId, monitorSlug, status);
110111
checkIn.setDuration(DateUtils.millisToSeconds(System.currentTimeMillis() - startTime));
111112
scopes.captureCheckIn(checkIn);
112-
scopes.popScope();
113+
lifecycleToken.close();
113114
}
114115
}
115116

sentry-spring/src/test/kotlin/io/sentry/spring/SentryCheckInAdviceTest.kt

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package io.sentry.spring
33
import io.sentry.CheckIn
44
import io.sentry.CheckInStatus
55
import io.sentry.IScopes
6+
import io.sentry.ISentryLifecycleToken
67
import io.sentry.Sentry
78
import io.sentry.SentryOptions
89
import io.sentry.protocol.SentryId
@@ -57,10 +58,13 @@ class SentryCheckInAdviceTest {
5758
@Autowired
5859
lateinit var scopes: IScopes
5960

61+
val lifecycleToken = mock<ISentryLifecycleToken>()
62+
6063
@BeforeTest
6164
fun setup() {
6265
reset(scopes)
6366
whenever(scopes.options).thenReturn(SentryOptions())
67+
whenever(scopes.pushIsolationScope()).thenReturn(lifecycleToken)
6468
}
6569

6670
@Test
@@ -80,10 +84,10 @@ class SentryCheckInAdviceTest {
8084
assertEquals("monitor_slug_1", doneCheckIn.monitorSlug)
8185
assertEquals(CheckInStatus.OK.apiName(), doneCheckIn.status)
8286

83-
val order = inOrder(scopes)
84-
order.verify(scopes).pushScope()
87+
val order = inOrder(scopes, lifecycleToken)
88+
order.verify(scopes).pushIsolationScope()
8589
order.verify(scopes, times(2)).captureCheckIn(any())
86-
order.verify(scopes).popScope()
90+
order.verify(lifecycleToken).close()
8791
}
8892

8993
@Test
@@ -104,10 +108,10 @@ class SentryCheckInAdviceTest {
104108
assertEquals("monitor_slug_1e", doneCheckIn.monitorSlug)
105109
assertEquals(CheckInStatus.ERROR.apiName(), doneCheckIn.status)
106110

107-
val order = inOrder(scopes)
108-
order.verify(scopes).pushScope()
111+
val order = inOrder(scopes, lifecycleToken)
112+
order.verify(scopes).pushIsolationScope()
109113
order.verify(scopes, times(2)).captureCheckIn(any())
110-
order.verify(scopes).popScope()
114+
order.verify(lifecycleToken).close()
111115
}
112116

113117
@Test
@@ -124,10 +128,10 @@ class SentryCheckInAdviceTest {
124128
assertEquals(CheckInStatus.OK.apiName(), doneCheckIn.status)
125129
assertNotNull(doneCheckIn.duration)
126130

127-
val order = inOrder(scopes)
128-
order.verify(scopes).pushScope()
131+
val order = inOrder(scopes, lifecycleToken)
132+
order.verify(scopes).pushIsolationScope()
129133
order.verify(scopes).captureCheckIn(any())
130-
order.verify(scopes).popScope()
134+
order.verify(lifecycleToken).close()
131135
}
132136

133137
@Test
@@ -145,10 +149,10 @@ class SentryCheckInAdviceTest {
145149
assertEquals(CheckInStatus.ERROR.apiName(), doneCheckIn.status)
146150
assertNotNull(doneCheckIn.duration)
147151

148-
val order = inOrder(scopes)
149-
order.verify(scopes).pushScope()
152+
val order = inOrder(scopes, lifecycleToken)
153+
order.verify(scopes).pushIsolationScope()
150154
order.verify(scopes).captureCheckIn(any())
151-
order.verify(scopes).popScope()
155+
order.verify(lifecycleToken).close()
152156
}
153157

154158
@Test
@@ -160,9 +164,9 @@ class SentryCheckInAdviceTest {
160164
assertEquals(1, result)
161165
assertEquals(0, checkInCaptor.allValues.size)
162166

163-
verify(scopes, never()).pushScope()
167+
verify(scopes, never()).pushIsolationScope()
164168
verify(scopes, never()).captureCheckIn(any())
165-
verify(scopes, never()).popScope()
169+
verify(lifecycleToken, never()).close()
166170
}
167171

168172
@Test
@@ -179,10 +183,10 @@ class SentryCheckInAdviceTest {
179183
assertEquals(CheckInStatus.OK.apiName(), doneCheckIn.status)
180184
assertNotNull(doneCheckIn.duration)
181185

182-
val order = inOrder(scopes)
183-
order.verify(scopes).pushScope()
186+
val order = inOrder(scopes, lifecycleToken)
187+
order.verify(scopes).pushIsolationScope()
184188
order.verify(scopes).captureCheckIn(any())
185-
order.verify(scopes).popScope()
189+
order.verify(lifecycleToken).close()
186190
}
187191

188192
@Test
@@ -199,10 +203,10 @@ class SentryCheckInAdviceTest {
199203
assertEquals(CheckInStatus.OK.apiName(), doneCheckIn.status)
200204
assertNotNull(doneCheckIn.duration)
201205

202-
val order = inOrder(scopes)
203-
order.verify(scopes).pushScope()
206+
val order = inOrder(scopes, lifecycleToken)
207+
order.verify(scopes).pushIsolationScope()
204208
order.verify(scopes).captureCheckIn(any())
205-
order.verify(scopes).popScope()
209+
order.verify(lifecycleToken).close()
206210
}
207211

208212
@Test
@@ -219,10 +223,10 @@ class SentryCheckInAdviceTest {
219223
assertEquals(CheckInStatus.OK.apiName(), doneCheckIn.status)
220224
assertNotNull(doneCheckIn.duration)
221225

222-
val order = inOrder(scopes)
223-
order.verify(scopes).pushScope()
226+
val order = inOrder(scopes, lifecycleToken)
227+
order.verify(scopes).pushIsolationScope()
224228
order.verify(scopes).captureCheckIn(any())
225-
order.verify(scopes).popScope()
229+
order.verify(lifecycleToken).close()
226230
}
227231

228232
@Configuration

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import io.sentry.CheckInStatus;
55
import io.sentry.DateUtils;
66
import io.sentry.IScopes;
7+
import io.sentry.ISentryLifecycleToken;
78
import io.sentry.MonitorConfig;
89
import io.sentry.Sentry;
910
import io.sentry.protocol.SentryId;
@@ -30,12 +31,11 @@ public static <U> U withCheckIn(
3031
final @Nullable MonitorConfig monitorConfig,
3132
final @NotNull Callable<U> callable)
3233
throws Exception {
34+
final @NotNull ISentryLifecycleToken lifecycleToken = Sentry.pushIsolationScope();
3335
final @NotNull IScopes scopes = Sentry.getCurrentScopes();
3436
final long startTime = System.currentTimeMillis();
3537
boolean didError = false;
3638

37-
// TODO fork instead
38-
scopes.pushScope();
3939
TracingUtils.startNewTrace(scopes);
4040

4141
CheckIn inProgressCheckIn = new CheckIn(monitorSlug, CheckInStatus.IN_PROGRESS);
@@ -53,7 +53,7 @@ public static <U> U withCheckIn(
5353
CheckIn checkIn = new CheckIn(checkInId, monitorSlug, status);
5454
checkIn.setDuration(DateUtils.millisToSeconds(System.currentTimeMillis() - startTime));
5555
scopes.captureCheckIn(checkIn);
56-
scopes.popScope();
56+
lifecycleToken.close();
5757
}
5858
}
5959

0 commit comments

Comments
 (0)