Skip to content

Commit d07a819

Browse files
committed
feat: add repeat record log
1 parent f82362e commit d07a819

File tree

47 files changed

+97
-76
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+97
-76
lines changed

arex-instrumentation-api/src/main/java/io/arex/inst/runtime/context/RepeatedCollectManager.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import io.arex.agent.bootstrap.ctx.ArexThreadLocal;
44
import io.arex.agent.bootstrap.internal.CallDepth;
5+
import io.arex.inst.runtime.config.Config;
6+
import io.arex.inst.runtime.log.LogManager;
57

68
/**
79
* Avoid collecting data multiple times on the call chain
@@ -22,7 +24,7 @@ public static boolean validate() {
2224
return Context.get() == null;
2325
}
2426

25-
public static boolean exitAndValidate() {
27+
public static boolean exitAndValidate(String repeatMessage) {
2628
CallDepth callDepth = Context.get();
2729
if (callDepth == null) {
2830
return true;
@@ -32,6 +34,10 @@ public static boolean exitAndValidate() {
3234
Context.remove();
3335
return true;
3436
}
37+
38+
if (Config.get().isEnableDebug()) {
39+
LogManager.info("repeat", repeatMessage);
40+
}
3541
return false;
3642
}
3743

arex-instrumentation-api/src/test/java/io/arex/inst/runtime/context/RepeatedCollectManagerTest.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,23 @@
22

33
import static org.junit.jupiter.api.Assertions.*;
44

5+
import io.arex.inst.runtime.config.ConfigBuilder;
6+
import io.arex.inst.runtime.log.LogManager;
57
import org.junit.jupiter.api.AfterAll;
68
import org.junit.jupiter.api.BeforeAll;
79
import org.junit.jupiter.api.Test;
10+
import org.mockito.MockedStatic;
811
import org.mockito.Mockito;
912

1013
/**
1114
* @since 2024/1/12
1215
*/
1316
class RepeatedCollectManagerTest {
17+
private static MockedStatic<LogManager> logManagerMockedStatic;
1418
@BeforeAll
1519
static void setUp() {
1620
Mockito.mockStatic(ContextManager.class);
21+
logManagerMockedStatic = Mockito.mockStatic(LogManager.class);
1722
}
1823
@AfterAll
1924
static void tearDown() {
@@ -23,12 +28,15 @@ static void tearDown() {
2328
@Test
2429
void test() {
2530
assertTrue(RepeatedCollectManager.validate());
26-
assertTrue(RepeatedCollectManager.exitAndValidate());
31+
assertTrue(RepeatedCollectManager.exitAndValidate("test"));
2732

2833
Mockito.when(ContextManager.needRecord()).thenReturn(true);
2934
RepeatedCollectManager.enter();
3035
RepeatedCollectManager.enter();
31-
assertFalse(RepeatedCollectManager.exitAndValidate());
32-
assertTrue(RepeatedCollectManager.exitAndValidate());
36+
// enable debug
37+
ConfigBuilder.create("test").enableDebug(true).build();
38+
assertFalse(RepeatedCollectManager.exitAndValidate("test"));
39+
logManagerMockedStatic.verify(() -> LogManager.info("repeat", "test"));
40+
assertTrue(RepeatedCollectManager.exitAndValidate("test"));
3341
}
3442
}

arex-instrumentation/database/arex-database-hibernate/src/main/java/io/arex/inst/database/hibernate/AbstractEntityPersisterInstrumentation.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public static void onExit(
8989
@Advice.Thrown(readOnly = false) Throwable throwable,
9090
@Advice.Local("mockResult") MockResult mockResult,
9191
@Advice.Local("extractor") DatabaseExtractor extractor) {
92-
if (!RepeatedCollectManager.exitAndValidate()) {
92+
if (!RepeatedCollectManager.exitAndValidate("db insert repeat record")) {
9393
return;
9494
}
9595

@@ -139,7 +139,7 @@ public static void onExit(
139139
@Advice.Argument(8) String sql,
140140
@Advice.Thrown(readOnly = false) Throwable throwable,
141141
@Advice.Local("mockResult") MockResult mockResult) {
142-
if (!RepeatedCollectManager.exitAndValidate()) {
142+
if (!RepeatedCollectManager.exitAndValidate("db update repeat record")) {
143143
return;
144144
}
145145

@@ -184,7 +184,7 @@ public static void onExit(
184184
@Advice.Argument(4) String sql,
185185
@Advice.Thrown(readOnly = false) Throwable throwable,
186186
@Advice.Local("mockResult") MockResult mockResult) {
187-
if (!RepeatedCollectManager.exitAndValidate()) {
187+
if (!RepeatedCollectManager.exitAndValidate("db delete repeat record")) {
188188
return;
189189
}
190190

arex-instrumentation/database/arex-database-hibernate/src/main/java/io/arex/inst/database/hibernate/LoaderInstrumentation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public static void onExit(@Advice.This Loader loader,
6363
@Advice.Return(readOnly = false) List<?> list,
6464
@Advice.Local("mockResult") MockResult mockResult,
6565
@Advice.Local("extractor") DatabaseExtractor extractor) throws HibernateException {
66-
if (!RepeatedCollectManager.exitAndValidate()) {
66+
if (!RepeatedCollectManager.exitAndValidate("db query repeat record")) {
6767
return;
6868
}
6969

arex-instrumentation/database/arex-database-hibernate/src/test/java/io/arex/inst/database/hibernate/AbstractEntityPersisterInstrumentationTest.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import static org.junit.jupiter.api.Assertions.*;
2727
import static org.junit.jupiter.params.provider.Arguments.arguments;
28+
import static org.mockito.ArgumentMatchers.anyString;
2829
import static org.mockito.ArgumentMatchers.isA;
2930

3031
@ExtendWith(MockitoExtension.class)
@@ -77,10 +78,10 @@ void onInsertExit(Runnable mocker, Throwable throwable, MockResult mockResult, P
7778

7879
static Stream<Arguments> onInsertExitCase() {
7980
Runnable emptyMocker = () -> {
80-
Mockito.when(RepeatedCollectManager.exitAndValidate()).thenReturn(false);
81+
Mockito.when(RepeatedCollectManager.exitAndValidate(anyString())).thenReturn(false);
8182
};
8283
Runnable exitAndValidate = () -> {
83-
Mockito.when(RepeatedCollectManager.exitAndValidate()).thenReturn(true);
84+
Mockito.when(RepeatedCollectManager.exitAndValidate(anyString())).thenReturn(true);
8485
};
8586
Runnable needRecord = () -> {
8687
Mockito.when(ContextManager.needRecord()).thenReturn(true);
@@ -126,7 +127,7 @@ static Stream<Arguments> onUpdateOrInsertEnterCase() {
126127
@MethodSource("onUpdateOrInsertExitCase")
127128
void onUpdateOrInsertExit(Runnable recordType, Object mockResult) {
128129
AtomicReference<DatabaseExtractor> mo = new AtomicReference<>();
129-
Mockito.when(RepeatedCollectManager.exitAndValidate()).thenReturn(true);
130+
Mockito.when(RepeatedCollectManager.exitAndValidate(anyString())).thenReturn(true);
130131
Mockito.when(ContextManager.needRecord()).thenReturn(true);
131132
try (MockedConstruction<DatabaseExtractor> mocked = Mockito.mockConstruction(DatabaseExtractor.class, (mock, context) -> {
132133
mo.set(mock);
@@ -149,7 +150,7 @@ static Stream<Arguments> onUpdateOrInsertExitCase() {
149150
void updateOrInsertExitReplayThrowable() {
150151
MockResult mock = Mockito.mock(MockResult.class);
151152
Mockito.when(ContextManager.needReplay()).thenReturn(true);
152-
Mockito.when(RepeatedCollectManager.exitAndValidate()).thenReturn(true);
153+
Mockito.when(RepeatedCollectManager.exitAndValidate(anyString())).thenReturn(true);
153154
Throwable throwable = new Throwable();
154155
Mockito.doReturn(true).when(mock).notIgnoreMockResult();
155156
Mockito.doReturn(throwable).when(mock).getThrowable();
@@ -184,7 +185,7 @@ static Stream<Arguments> onDeleteEnterCase() {
184185
@MethodSource("onDeleteExitCase")
185186
void onDeleteExit(Runnable recordType, Object mockResult) {
186187
AtomicReference<DatabaseExtractor> mo = new AtomicReference<>();
187-
Mockito.when(RepeatedCollectManager.exitAndValidate()).thenReturn(true);
188+
Mockito.when(RepeatedCollectManager.exitAndValidate(anyString())).thenReturn(true);
188189
Mockito.when(ContextManager.needRecord()).thenReturn(true);
189190
try (MockedConstruction<DatabaseExtractor> mocked = Mockito.mockConstruction(DatabaseExtractor.class, (mock, context) -> {
190191
mo.set(mock);
@@ -207,11 +208,11 @@ static Stream<Arguments> onDeleteExitCase() {
207208
void deleteExitReplayThrowable() {
208209
MockResult mock = Mockito.mock(MockResult.class);
209210
Mockito.when(ContextManager.needReplay()).thenReturn(true);
210-
Mockito.when(RepeatedCollectManager.exitAndValidate()).thenReturn(true);
211+
Mockito.when(RepeatedCollectManager.exitAndValidate(anyString())).thenReturn(true);
211212
Throwable throwable = new Throwable();
212213
Mockito.doReturn(true).when(mock).notIgnoreMockResult();
213214
Mockito.doReturn(throwable).when(mock).getThrowable();
214215
AbstractEntityPersisterInstrumentation.DeleteAdvice.onExit(null, null, throwable, mock);
215216
Mockito.verify(mock, Mockito.times(2)).getThrowable();
216217
}
217-
}
218+
}

arex-instrumentation/database/arex-database-hibernate/src/test/java/io/arex/inst/database/hibernate/LoaderInstrumentationTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ void onExit(Runnable mocker, HibernateException exception, MockResult mockResult
7777

7878
static Stream<Arguments> onExitCase() {
7979
Runnable emptyMocker = () -> {};
80-
Runnable exitAndValidate = () -> Mockito.when(RepeatedCollectManager.exitAndValidate()).thenReturn(true);
80+
Runnable exitAndValidate = () -> Mockito.when(RepeatedCollectManager.exitAndValidate(Mockito.anyString())).thenReturn(true);
8181
Runnable needRecord = () -> Mockito.when(ContextManager.needRecord()).thenReturn(true);
8282
Predicate<HibernateException> predicate1 = Objects::isNull;
8383
Predicate<HibernateException> predicate2 = Objects::nonNull;
@@ -88,4 +88,4 @@ static Stream<Arguments> onExitCase() {
8888
arguments(needRecord, null, null, predicate1)
8989
);
9090
}
91-
}
91+
}

arex-instrumentation/database/arex-database-mongo/src/main/java/io/arex/inst/database/mongo/AggregateInstrumentation.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ public static void onExit(@Enter boolean needReplay,
6767
}
6868
return;
6969
}
70-
if (ContextManager.needRecord() && RepeatedCollectManager.exitAndValidate()) {
70+
71+
if (ContextManager.needRecord() && RepeatedCollectManager.exitAndValidate("mongo aggregate repeat record")) {
7172
MongoHelper.record("Aggregate", namespace, pipeline, result, throwable);
7273
}
7374
}

arex-instrumentation/database/arex-database-mongo/src/main/java/io/arex/inst/database/mongo/ListIndexesInstrumentation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public static void onExit(@Advice.Enter boolean needReplay,
6161
}
6262
return;
6363
}
64-
if (ContextManager.needRecord() && RepeatedCollectManager.exitAndValidate()) {
64+
if (ContextManager.needRecord() && RepeatedCollectManager.exitAndValidate("mongo listIndexes repeat record.")) {
6565
MongoHelper.record("ListIndexes", namespace, null, result, throwable);
6666
}
6767
}

arex-instrumentation/database/arex-database-mongo/src/main/java/io/arex/inst/database/mongo/ReadOperationInstrumentation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public static void onExit(@Advice.Enter boolean needReplay,
7272
}
7373
return;
7474
}
75-
if (ContextManager.needRecord() && RepeatedCollectManager.exitAndValidate()) {
75+
if (ContextManager.needRecord() && RepeatedCollectManager.exitAndValidate("mongo read operation repeat record.")) {
7676
MongoHelper.record(thiz.getClass().getSimpleName(), namespace, filter, result, throwable);
7777
}
7878
}

arex-instrumentation/database/arex-database-mongo/src/main/java/io/arex/inst/database/mongo/WriteOperationInstrumentation.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public static boolean onEnter() {
5858
@Advice.OnMethodExit(suppress = Throwable.class)
5959
public static void onExit() {
6060
if (ContextManager.needRecord()) {
61-
RepeatedCollectManager.exitAndValidate();
61+
RepeatedCollectManager.exitAndValidate("mongo void operation repeat record");
6262
}
6363
}
6464
}
@@ -97,7 +97,7 @@ public static void onExit(@Advice.Origin Method method,
9797
return;
9898
}
9999

100-
if (ContextManager.needRecord() && RepeatedCollectManager.exitAndValidate()) {
100+
if (ContextManager.needRecord() && RepeatedCollectManager.exitAndValidate("mongo write operation repeat record")) {
101101
MongoHelper.record(methodName, namespace, filter, result, throwable);
102102
}
103103
}

0 commit comments

Comments
 (0)