From a82a94ce598d3c38a4bfa7b4fdc7fc41aedfa103 Mon Sep 17 00:00:00 2001 From: luke <348358584@qq.com> Date: Sun, 9 Jun 2024 18:09:55 +0800 Subject: [PATCH 1/3] fix NPE bug --- .../spi/defaultimpl/ApolloAuditOperatorDefaultSupplier.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/apollo-audit/apollo-audit-impl/src/main/java/com/ctrip/framework/apollo/audit/spi/defaultimpl/ApolloAuditOperatorDefaultSupplier.java b/apollo-audit/apollo-audit-impl/src/main/java/com/ctrip/framework/apollo/audit/spi/defaultimpl/ApolloAuditOperatorDefaultSupplier.java index e52b2075c44..e7ad3a9f244 100644 --- a/apollo-audit/apollo-audit-impl/src/main/java/com/ctrip/framework/apollo/audit/spi/defaultimpl/ApolloAuditOperatorDefaultSupplier.java +++ b/apollo-audit/apollo-audit-impl/src/main/java/com/ctrip/framework/apollo/audit/spi/defaultimpl/ApolloAuditOperatorDefaultSupplier.java @@ -17,6 +17,7 @@ package com.ctrip.framework.apollo.audit.spi.defaultimpl; import com.ctrip.framework.apollo.audit.constants.ApolloAuditConstants; +import com.ctrip.framework.apollo.audit.context.ApolloAuditSpan; import com.ctrip.framework.apollo.audit.context.ApolloAuditTracer; import com.ctrip.framework.apollo.audit.spi.ApolloAuditOperatorSupplier; import org.springframework.web.context.request.RequestAttributes; @@ -31,7 +32,8 @@ public String getOperator() { Object tracer = requestAttributes.getAttribute(ApolloAuditConstants.TRACER, RequestAttributes.SCOPE_REQUEST); if (tracer != null) { - return ((ApolloAuditTracer) tracer).scopeManager().activeSpan().operator(); + ApolloAuditSpan activeSpan = ((ApolloAuditTracer) tracer).scopeManager().activeSpan(); + return activeSpan != null ? activeSpan.operator() : "anonymous"; } else { return null; } From 05a7fb2a950f226348af88f5b368c0dd435ebae9 Mon Sep 17 00:00:00 2001 From: luke <348358584@qq.com> Date: Sun, 9 Jun 2024 18:33:02 +0800 Subject: [PATCH 2/3] Remove inelegant API exposure to the outside --- .../ctrip/framework/apollo/audit/context/ApolloAuditTracer.java | 2 +- .../spi/defaultimpl/ApolloAuditOperatorDefaultSupplier.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/apollo-audit/apollo-audit-impl/src/main/java/com/ctrip/framework/apollo/audit/context/ApolloAuditTracer.java b/apollo-audit/apollo-audit-impl/src/main/java/com/ctrip/framework/apollo/audit/context/ApolloAuditTracer.java index bfa4e6c4cee..8e4128620de 100644 --- a/apollo-audit/apollo-audit-impl/src/main/java/com/ctrip/framework/apollo/audit/context/ApolloAuditTracer.java +++ b/apollo-audit/apollo-audit-impl/src/main/java/com/ctrip/framework/apollo/audit/context/ApolloAuditTracer.java @@ -43,7 +43,7 @@ public ApolloAuditTracer(ApolloAuditScopeManager manager, this.operatorSupplier = operatorSupplier; } - public ApolloAuditScopeManager scopeManager() { + protected ApolloAuditScopeManager scopeManager() { return manager; } diff --git a/apollo-audit/apollo-audit-impl/src/main/java/com/ctrip/framework/apollo/audit/spi/defaultimpl/ApolloAuditOperatorDefaultSupplier.java b/apollo-audit/apollo-audit-impl/src/main/java/com/ctrip/framework/apollo/audit/spi/defaultimpl/ApolloAuditOperatorDefaultSupplier.java index e7ad3a9f244..9ae0f8c3dbc 100644 --- a/apollo-audit/apollo-audit-impl/src/main/java/com/ctrip/framework/apollo/audit/spi/defaultimpl/ApolloAuditOperatorDefaultSupplier.java +++ b/apollo-audit/apollo-audit-impl/src/main/java/com/ctrip/framework/apollo/audit/spi/defaultimpl/ApolloAuditOperatorDefaultSupplier.java @@ -32,7 +32,7 @@ public String getOperator() { Object tracer = requestAttributes.getAttribute(ApolloAuditConstants.TRACER, RequestAttributes.SCOPE_REQUEST); if (tracer != null) { - ApolloAuditSpan activeSpan = ((ApolloAuditTracer) tracer).scopeManager().activeSpan(); + ApolloAuditSpan activeSpan = ((ApolloAuditTracer) tracer).getActiveSpan(); return activeSpan != null ? activeSpan.operator() : "anonymous"; } else { return null; From b300cbfb8edbf1812cf49f3c2ab90a6b60d12408 Mon Sep 17 00:00:00 2001 From: luke <348358584@qq.com> Date: Sun, 9 Jun 2024 18:49:09 +0800 Subject: [PATCH 3/3] supplementary unit testing for operator supplier --- .../spi/ApolloAuditOperatorSupplierTest.java | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 apollo-audit/apollo-audit-impl/src/test/java/com/ctrip/framework/apollo/audit/spi/ApolloAuditOperatorSupplierTest.java diff --git a/apollo-audit/apollo-audit-impl/src/test/java/com/ctrip/framework/apollo/audit/spi/ApolloAuditOperatorSupplierTest.java b/apollo-audit/apollo-audit-impl/src/test/java/com/ctrip/framework/apollo/audit/spi/ApolloAuditOperatorSupplierTest.java new file mode 100644 index 00000000000..48a3ee319ad --- /dev/null +++ b/apollo-audit/apollo-audit-impl/src/test/java/com/ctrip/framework/apollo/audit/spi/ApolloAuditOperatorSupplierTest.java @@ -0,0 +1,74 @@ +/* + * Copyright 2024 Apollo Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package com.ctrip.framework.apollo.audit.spi; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.ctrip.framework.apollo.audit.constants.ApolloAuditConstants; +import com.ctrip.framework.apollo.audit.context.ApolloAuditSpan; +import com.ctrip.framework.apollo.audit.context.ApolloAuditSpanContext; +import com.ctrip.framework.apollo.audit.context.ApolloAuditTracer; +import com.ctrip.framework.apollo.audit.spi.defaultimpl.ApolloAuditOperatorDefaultSupplier; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.boot.test.mock.mockito.SpyBean; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.web.context.request.RequestAttributes; +import org.springframework.web.context.request.RequestContextHolder; + +@SpringBootTest +@ContextConfiguration(classes = ApolloAuditOperatorSupplier.class) +public class ApolloAuditOperatorSupplierTest { + + @SpyBean + ApolloAuditOperatorDefaultSupplier defaultSupplier; + + @MockBean + RequestAttributes requestAttributes; + @MockBean + ApolloAuditTracer tracer; + + @BeforeEach + public void setUp() { + Mockito.when(requestAttributes.getAttribute( + Mockito.eq(ApolloAuditConstants.TRACER), + Mockito.eq(RequestAttributes.SCOPE_REQUEST)) + ).thenReturn(tracer); + RequestContextHolder.setRequestAttributes(requestAttributes); + } + + @Test + public void testGetOperatorCaseActiveSpanExist() { + final String operator = "test"; + { + ApolloAuditSpan activeSpan = new ApolloAuditSpan(); + activeSpan.setContext(new ApolloAuditSpanContext(null, null, operator, null, null)); + Mockito.when(tracer.getActiveSpan()).thenReturn(activeSpan); + } + + assertEquals(operator, defaultSupplier.getOperator()); + } + + @Test + public void testGetOperatorCaseActiveSpanNotExist() { + assertEquals("anonymous", defaultSupplier.getOperator()); + } + +}