xue2lang opened a new issue, #17834:
URL: https://github.com/apache/shardingsphere/issues/17834

   ## Question
   now, i use shardingsphere-agent in sharding-proxy to record trace, the trace 
plugin is zipkin, i want record traceid into log,  but don not know how to bury 
the code , please see the steps:
   
   
   * step 1 : add log support dependency
   ```java
   
       @Override
       public void start(final PluginConfiguration pluginConfig) {
           if (!checkConfig(pluginConfig)) {
               throw new PluginConfigurationException("zipkin config error, 
host is null or port is %s", pluginConfig.getPort());
           }
           Properties props = pluginConfig.getProps();
           String urlVersion = 
Optional.ofNullable(props.getProperty("URL_VERSION")).orElse("/api/v2/spans");
           String serviceName = 
Optional.ofNullable(props.getProperty("SERVICE_NAME")).orElse("shardingsphere-agent");
           String token = 
Optional.ofNullable(props.getProperty("TOKEN")).orElse("");
           sender = OkHttpSender.create(String.format("http://%s:%s%s%s";, 
pluginConfig.getHost(), pluginConfig.getPort(), token != null && token.length() 
> 0 ? "/" + token : token, urlVersion));
           Sampler sampler = createSampler(pluginConfig);
           zipkinSpanHandler = AsyncZipkinSpanHandler.create(sender);
           tracing = 
Tracing.newBuilder().localServiceName(serviceName).sampler(sampler).addSpanHandler(zipkinSpanHandler)
                 
                   //add MDCScopeDecorator for add trace to MDC 
                      .currentTraceContext(
                           ThreadLocalCurrentTraceContext.newBuilder()
                           .addScopeDecorator(MDCScopeDecorator.get())
                           .build()
                   )
   
   
               .build();
       }
   ```
   * step 2:how to bury the code  in agent plugin of zipkin ,have any suggest 
????
   if we want record trace , we must get the currentTraceContext from Tracing, 
then execute code like this 
   ```java
   
   /**
    * Command executor task advice.
    */
   public final class CommandExecutorTaskAdvice implements 
InstanceMethodAroundAdvice {
       
       private static final String OPERATION_NAME = 
"/ShardingSphere/rootInvoke/";
       
       @Override
       public void beforeMethod(final AdviceTargetObject target, final Method 
method, final Object[] args, final MethodInvocationResult result) {
           Span span = Tracing.currentTracer().newTrace().name(OPERATION_NAME);
           span.tag(ZipkinConstants.Tags.COMPONENT, 
ZipkinConstants.COMPONENT_NAME).kind(Span.Kind.CLIENT)
                   .tag(ZipkinConstants.Tags.DB_TYPE, 
ZipkinConstants.DB_TYPE_VALUE).start();
           ExecutorDataMap.getValue().put(ZipkinConstants.ROOT_SPAN, span);
   
    //save scope to thread local
           CurrentTraceContext.Scope scope = 
Tracing.current().currentTraceContext().newScope(span.context());
           TraceScopeThreadLocal.INSTANCE.set(scope);
       }
       
       @SneakyThrows
       @Override
       public void afterMethod(final AdviceTargetObject target, final Method 
method, final Object[] args, final MethodInvocationResult result) {
           Field field = 
CommandExecutorTask.class.getDeclaredField("connectionSession");
           field.setAccessible(true);
           JDBCBackendConnection connection = (JDBCBackendConnection) 
((ConnectionSession) field.get(target)).getBackendConnection();
           Span span = (Span) 
ExecutorDataMap.getValue().remove(ZipkinConstants.ROOT_SPAN);
           span.tag(ZipkinConstants.Tags.CONNECTION_COUNT, 
String.valueOf(connection.getConnectionSize()));
           span.finish();
   
   //close
           closeScope();
       }
       
       @Override
       public void onThrowing(final AdviceTargetObject target, final Method 
method, final Object[] args, final Throwable throwable) {
           ((Span) 
ExecutorDataMap.getValue().get(ZipkinConstants.ROOT_SPAN)).error(throwable);
           closeScope();
       }
   
       private void closeScope() {
           TraceScopeThreadLocal.INSTANCE.get().close();
           TraceScopeThreadLocal.INSTANCE.remove();
       }
   }
   
   
   ```
   
   
   * step 3:how to get the traceId from the upstream system (for eg: 
go、php,this system has access to the zipkin for sdk ),have any suggest ?????
   
   my idea: extended database connection communication protocol (eg go) , and 
in proxy shoud add handler in netty to accept the traceid from upstream ,then 
put it to MDC 
   
   
   
   
   
   **For English only**, other languages will not accept.
   
   Before asking a question, make sure you have:
   
   - Googled your question.
   - Searched open and closed [GitHub 
issues](https://github.com/apache/shardingsphere/issues).
   - Read documentation: [ShardingSphere 
Doc](https://shardingsphere.apache.org/document/current/en/overview).
   
   Please pay attention on issues you submitted, because we maybe need more 
details. 
   If no response anymore and we cannot reproduce it on current information, we 
will **close it**.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: 
[email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to