Skip to content

Commit 3e816ba

Browse files
committed
Changes in ApolloAuditScope for managing last active span.
1 parent c45db51 commit 3e816ba

File tree

4 files changed

+42
-27
lines changed

4 files changed

+42
-27
lines changed

apollo-audit/apollo-audit-impl/src/main/java/com/ctrip/framework/apollo/audit/context/ApolloAuditScope.java

+11-3
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@
1818

1919
public class ApolloAuditScope implements AutoCloseable {
2020

21-
private ApolloAuditSpan activeSpan;
21+
private final ApolloAuditSpan activeSpan;
22+
private final ApolloAuditScope previousScope;
2223
private String lastSpanId;
2324

24-
public ApolloAuditScope(ApolloAuditSpan activeSpan) {
25+
public ApolloAuditScope(ApolloAuditSpan activeSpan, ApolloAuditScope previousScope) {
2526
this.activeSpan = activeSpan;
27+
this.previousScope = previousScope;
2628
this.lastSpanId = null;
2729
}
2830

@@ -32,7 +34,9 @@ public ApolloAuditSpan activeSpan() {
3234

3335
@Override
3436
public void close() {
35-
// Closing span becomes parent-scope's last span, managed externally
37+
if (previousScope != null) {
38+
previousScope.setLastSpanId(activeSpan.spanId());
39+
}
3640
}
3741

3842
public String getLastSpanId() {
@@ -42,4 +46,8 @@ public String getLastSpanId() {
4246
public void setLastSpanId(String lastSpanId) {
4347
this.lastSpanId = lastSpanId;
4448
}
49+
50+
public ApolloAuditScope getPreviousScope() {
51+
return previousScope;
52+
}
4553
}

apollo-audit/apollo-audit-impl/src/main/java/com/ctrip/framework/apollo/audit/context/ApolloAuditScopeManager.java

+12-14
Original file line numberDiff line numberDiff line change
@@ -20,31 +20,29 @@
2020

2121
public class ApolloAuditScopeManager {
2222

23-
private ApolloAuditScope scope;
23+
private ApolloAuditScope currentScope;
2424

25-
public ApolloAuditScopeManager() {}
25+
public ApolloAuditScopeManager() {
26+
}
2627

2728
public ApolloAuditScope activate(ApolloAuditSpan span) {
28-
// Directly manage the scope, no need to reference the scope's previous parent
29-
this.scope = new ApolloAuditScope(span);
30-
return this.scope;
29+
this.currentScope = new ApolloAuditScope(span, currentScope);
30+
return currentScope;
3131
}
3232

33-
public void deactivate() {
34-
if (this.scope != null) {
35-
this.scope.close(); // Close the scope, but no need for reference to parent scope
33+
public void deactivate() throws IOException {
34+
if (currentScope != null) {
35+
ApolloAuditScope previousScope = currentScope.getPreviousScope();
36+
currentScope.close();
37+
currentScope = previousScope; // Restore previous scope after closing
3638
}
3739
}
3840

3941
public ApolloAuditSpan activeSpan() {
40-
return getScope() == null ? null : getScope().activeSpan();
42+
return currentScope == null ? null : currentScope.activeSpan();
4143
}
4244

4345
public ApolloAuditScope getScope() {
44-
return scope;
45-
}
46-
47-
public void setScope(ApolloAuditScope scope) {
48-
this.scope = scope;
46+
return currentScope;
4947
}
5048
}

apollo-audit/apollo-audit-impl/src/test/java/com/ctrip/framework/apollo/audit/component/ApolloAuditLogApiJpaImplTest.java

+10-4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package com.ctrip.framework.apollo.audit.component;
1818

19+
import static org.junit.Assert.assertTrue;
1920
import static org.junit.jupiter.api.Assertions.assertEquals;
2021
import static org.junit.jupiter.api.Assertions.assertNull;
2122
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -104,10 +105,13 @@ public void testAppendAuditLog() {
104105
activeSpan.setOpName(opName);
105106
activeSpan.setContext(new ApolloAuditSpanContext(traceId, spanId));
106107

107-
// Create the ApolloAuditScope using the active span
108-
ApolloAuditScope scope = new ApolloAuditScope(activeSpan);
108+
// Create an ApolloAuditScopeManager to manage scopes
109+
ApolloAuditScopeManager scopeManager = new ApolloAuditScopeManager();
109110

110-
// Mock the behavior of the tracer to return the scope when startActiveSpan is called
111+
// Activate a new audit scope
112+
ApolloAuditScope scope = scopeManager.activate(activeSpan);
113+
114+
// Mock the behavior of the tracer to return a scope when startActiveSpan is called
111115
Mockito.when(tracer.startActiveSpan(Mockito.eq(create), Mockito.eq(opName), Mockito.eq(description)))
112116
.thenReturn(scope);
113117

@@ -119,14 +123,16 @@ public void testAppendAuditLog() {
119123
Mockito.verify(tracer, Mockito.times(1))
120124
.startActiveSpan(Mockito.eq(create), Mockito.eq(opName), Mockito.eq(description));
121125

122-
// Verify that the scope returned has the correct values
126+
// Verify that the returned scope has the correct values
127+
assertTrue(returnedScope!=null);
123128
assertEquals(create, returnedScope.activeSpan().getOpType());
124129
assertEquals(opName, returnedScope.activeSpan().getOpName());
125130
assertEquals(traceId, returnedScope.activeSpan().traceId());
126131
assertEquals(spanId, returnedScope.activeSpan().spanId());
127132
}
128133

129134

135+
130136
@Test
131137
public void testAppendDataInfluenceCaseCreateOrUpdate() {
132138
{

apollo-audit/apollo-audit-impl/src/test/java/com/ctrip/framework/apollo/audit/component/ApolloAuditScopeManagerTest.java

+9-6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package com.ctrip.framework.apollo.audit.component;
1818

19+
import static org.junit.Assert.assertNull;
1920
import static org.junit.jupiter.api.Assertions.assertEquals;
2021
import static org.mockito.Mockito.eq;
2122
import static org.mockito.Mockito.mock;
@@ -40,16 +41,18 @@ public class ApolloAuditScopeManagerTest {
4041
public void testActivate() {
4142
ApolloAuditSpan mockSpan = mock(ApolloAuditSpan.class);
4243

43-
// Create an ApolloAuditScope directly using the mock span
44-
ApolloAuditScope activeScope = new ApolloAuditScope(mockSpan);
45-
46-
// Call the activate method of ApolloAuditScopeManager (manager)
44+
// Create an ApolloAuditScopeManager instance
4745
ApolloAuditScopeManager manager = new ApolloAuditScopeManager();
48-
manager.setScope(activeScope); // Set the scope manually since there's no manager dependency
4946

50-
// Verify the scope has been set
47+
// Activate the span using the manager, which should create a new scope
48+
ApolloAuditScope activeScope = manager.activate(mockSpan);
49+
50+
// Verify that the active scope is correctly set in the manager
5151
assertEquals(activeScope, manager.getScope());
5252
assertEquals(mockSpan, activeScope.activeSpan());
53+
54+
// Verify that the previous scope is null (since it's the first activation)
55+
assertNull(activeScope.getPreviousScope());
5356
}
5457

5558

0 commit comments

Comments
 (0)