I am using the Spring Boot Open Telemetry Starter as mentioned here to trace calls between APIs.
/
A sample code looks like this:
@GetMapping("/trace/svc1")
public ResponseEntity<String> service1(@RequestHeader Map<String, String> headers) {
System.out.println(String.format("Service 1 Traceparent: "+headers.get("traceparent")));
ResponseEntity<String> response
= restTemplate.getForEntity("http://localhost:8081/trace/svc2", String.class);
return new ResponseEntity<String>("Completed Service 1", HttpStatus.OK);
}
@GetMapping("/trace/svc2")
public ResponseEntity<String> service2(@RequestHeader Map<String, String> headers) {
System.out.println(String.format("Service 2 Traceparent: "+headers.get("traceparent")));
ResponseEntity<String> response
= restTemplate.getForEntity("http://localhost:8081/trace/svc3", String.class);
return new ResponseEntity<String>("Completed Service 2", HttpStatus.OK);
}
@GetMapping("/trace/svc3")
public ResponseEntity<String> service3(@RequestHeader Map<String, String> headers) {
System.out.println(String.format("Service 3 Traceparent: "+headers.get("traceparent")));
return new ResponseEntity<String>("Completed Service 3", HttpStatus.OK);
}
This is printing the trace parent in Service 2 and Service 3 as expected: Service 2 Traceparent: 00-62995a5f4f5759db12af1f4efb20221e-5bdc0cd84b66ecdc-01 Service 3 Traceparent: 00-62995a5f4f5759db12af1f4efb20221e-cc1b7c935d711554-01
Now I plan to log the trace parent in our log aggregation solution and use the trace id to query the traces later. So I have the following questions:
Can the trace parent be printed in service 1 itself? which is the first place where the trace is generated. In some cases if service 1 is the only API in question, we do not get to see the trace id in the logs at all.
Can a custom trace id be injected in the trace parent? For example if a transaction id or a correlation id needs to be injected as a trace parent, can that be done?
For example, I have tried to create a span like this to ensure that mytraceid and myspanid are propagated, but I can see brand new trace id and span ids are generated,
Context context =
Context.current()
.with(
Span.wrap(
SpanContext.create(
Hex.encodeHexString("mytraceid".getBytes()),
Hex.encodeHexString("myspanid".getBytes()),
TraceFlags.getDefault(),
TraceState.getDefault())));
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745111886a4611897.html
评论列表(0条)