pinpoint-apm / pinpoint

APM, (Application Performance Management) tool for large-scale distributed systems.
https://pinpoint-apm.gitbook.io/
Apache License 2.0
13.41k stars 3.75k forks source link

How can I get transactionId in springboot application, not in log #7742

Open ighack opened 3 years ago

ighack commented 3 years ago

How can I get transactionId in springboot application, like skywalking

import org.apache.skywalking.apm.toolkit.trace.TraceContext;

   @ResponseBody
    @PostMapping("/Hello")
    public FdMessageVO Hello(@RequestBody Map<String,String> param) throws IOException {
        String traceid = TraceContext.traceId();
       // insert into mysql
        return  fdMessageVo;
    }

I want to insert transactionId to mysql

yjqg6666 commented 3 years ago

@ighack This PR #6801 might be what you want.

emeroad commented 3 years ago

Maybe you need the SDK.

SDK PR is currently under development, but transactionId export feature is not included. https://github.com/pinpoint-apm/pinpoint/issues/7654

@yjqg6666 The design pattern of SDK PR seems like a very good idea. A similar pattern seems to be possible with transactionid export. What do you think?

yjqg6666 commented 3 years ago

@emeroad If possible, i would prefer to migrate this feature into the official SDK. It wouldn't be hard. IMHO, the official SDK should have minimal dependency to avoid dependency conflicts and no dependency on pinpoint other modules should be recommended.

yjqg6666 commented 3 years ago

It would be so much better to get the transaction info from a stable api.

ljgstudy commented 2 years ago

When will the SDK be released?

yjqg6666 commented 2 years ago

Working on the migration ( move application-interaction module in PR #6801 to agent-sdk). A new PR would be make in a few days later.

yjqg6666 commented 2 years ago

@ighack @ljgstudy @emeroad The feature is migrated to the PR #9175 branch. You could try it out.

aalinyu commented 2 years ago

Class.forName is very performance consuming, cache the result maybe better?

aalinyu commented 2 years ago

@yjqg6666 I have a question, why not use a plugin to enhancement the agent sdk to set the txid in, it may be more efficient

yjqg6666 commented 2 years ago

Class.forName is very performance consuming, cache the result maybe better?

@aalinyu Class.forName is called only 4 times per a HTTP request. According to this benchmarking, it may be not necessary.

emeroad commented 2 years ago

Alternative

Non-invasive implementation that does not require SDK

The current implementation has a problem of high complexity across ClassLoader, ThreadLocal, and SDK. Here are some ideas for this

public class Sample extends HttpServlet{

protected void doGet(HttpServletRequest request, HttpServletResponse response) {
       // This information is injected by the pinpoint interceptor
        String transactionId = request.getAttribute("PINPOINT_TRANSACTIONID")
    Long spanId = request.getAttribute("PINPOINT_SPANID")
}

}


- Pros
Simple implementation.
Simple Object Lifecycle, Easy thread propagation. Most frameworks handle propagation of request automatically.
```java
public class HttpServletRequestAdaptor implements RequestAdaptor<HttpServletRequest> {
     @Override
     public void setAttribute(HttpServletRequest request, String name, Object o) {
          request.setAttribute(name, o);
      }
}