pinpoint icon indicating copy to clipboard operation
pinpoint copied to clipboard

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

Open ighack opened this issue 3 years ago • 10 comments

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

ighack avatar Mar 16 '21 03:03 ighack

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

yjqg6666 avatar Mar 19 '21 08:03 yjqg6666

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?

emeroad avatar Mar 19 '21 09:03 emeroad

@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 avatar Mar 19 '21 09:03 yjqg6666

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

yjqg6666 avatar Mar 19 '21 09:03 yjqg6666

When will the SDK be released?

ljgstudy avatar Jul 29 '22 06:07 ljgstudy

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 avatar Sep 09 '22 09:09 yjqg6666

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

yjqg6666 avatar Sep 10 '22 02:09 yjqg6666

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

aalinyu avatar Sep 13 '22 02:09 aalinyu

@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

aalinyu avatar Sep 13 '22 02:09 aalinyu

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.

yjqg6666 avatar Sep 13 '22 05:09 yjqg6666

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

  • ServletRequest.getAttribute() approach

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.
public class HttpServletRequestAdaptor implements RequestAdaptor<HttpServletRequest> {
     @Override
     public void setAttribute(HttpServletRequest request, String name, Object o) {
          request.setAttribute(name, o);
      }
}
  • Cons Some modules do not have setAttributes(). (grpc stream, reactive netty request....) Solution? : A module without setAttribute() does not implement the feature.

emeroad avatar Sep 29 '22 07:09 emeroad