KouShenhai / KCloud-Platform-IoT

KCloud-Platform-IoT(阻塞式)(老寇IoT云平台)是一个企业级微服务架构的IoT云平台。采用DDD(领域驱动设计)思想,基于Spring Boot 3.4.0、Spring Cloud 2024.0.0、Spring Cloud Alibaba 2023.0.1.3 最新版本开发的云服务多租户IoT平台,家人们,点个star!拜托啦~
https://koushenhai.github.io/KCloud-Platform-IoT
Apache License 2.0
472 stars 120 forks source link

Refactor MDC Handling and Simplify WrapperTrace Class #2912

Closed sourcery-ai[bot] closed 4 hours ago

sourcery-ai[bot] commented 5 hours ago

Issue: Complexity in MDC Handling and Redundant Wrapper Class

The current implementation involves repeated MDC (Mapped Diagnostic Context) handling logic and an unused WrapperListener class, which adds unnecessary complexity to the codebase.

Proposed Solution:

  1. Extract MDC Handling into a Reusable Helper Method:

    • Introduce a helper method executeWithTrace to encapsulate the MDC handling logic. This method will manage the MDC context by putting the trace ID and span ID before executing an action and clearing the context afterward.
    private void executeWithTrace(TraceLogV traceLog, Runnable action) {
       try {
           MDCUtil.put(traceLog.traceId(), traceLog.spanId());
           action.run();
       } finally {
           MDCUtil.clear();
       }
    }
  2. Simplify the WrapperTrace Class:

    • Refactor the WrapperTrace class to utilize the new executeWithTrace method, thereby reducing code duplication and improving maintainability.
    private static class WrapperTrace<R, S> extends ForwardingServerCallListener.SimpleForwardingServerCallListener<R> {
       private final TraceLogV traceLog;
    
       protected WrapperTrace(ServerCall<R, S> serverCall, ServerCallHandler<R, S> serverCallHandler,
               Metadata metadata, TraceLogV traceLog) {
           super(serverCallHandler.startCall(serverCall, metadata));
           this.traceLog = traceLog;
       }
    
       @Override
       public void onMessage(R message) {
           executeWithTrace(traceLog, () -> {
               log.info("onMessage...");
               super.onMessage(message);
           });
       }
    
       @Override
       public void onHalfClose() {
           executeWithTrace(traceLog, () -> {
               log.info("onHalfClose...");
               super.onHalfClose();
           });
       }
    
       @Override
       public void onComplete() {
           executeWithTrace(traceLog, () -> {
               log.info("onComplete...");
               super.onComplete();
           });
       }
    }

Benefits:

Action Items:

This refactoring will streamline the code and enhance its maintainability while preserving existing functionality. Please review and consider implementing these changes.


I created this issue for @KouShenhai from https://github.com/KouShenhai/KCloud-Platform-IoT/pull/2911#discussion_r1845679108.

Tips and commands #### Interacting with Sourcery - **Generate a plan of action:** Comment `@sourcery-ai plan` on this issue. - **Generate a pull request for this issue:** Comment `@sourcery-ai develop` to generate a PR that addresses this issue. #### Getting Help - [Contact our support team](mailto:support@sourcery.ai) for questions or feedback. - Visit our [documentation](https://docs.sourcery.ai) for detailed guides and information. - Keep in touch with the Sourcery team by following us on [X/Twitter](https://x.com/SourceryAI), [LinkedIn](https://www.linkedin.com/company/sourcery-ai/) or [GitHub](https://github.com/sourcery-ai).
KouShenhai commented 4 hours ago

2913