问题
Log tracing is extremely important matter, so I played a little bit with this code, to match/connect request ids with the response ones. This works for tracing request responses from to/from my ws.rs resources:
@Provider
public class LoggingFilter implements ContainerRequestFilter, ContainerResponseFilter {
private static final Logger LOG = Logger.getLogger(LoggingFilter.class);
AtomicInteger _id = new AtomicInteger(0);
AtomicInteger requestId = new AtomicInteger(0);
@Context
UriInfo info;
@Context
HttpServerRequest request;
@Override
public void filter(ContainerRequestContext context) {
final String method = context.getMethod();
final String path = info.getPath();
final String address = request.remoteAddress().toString();
// request.ge
final long id = requestId.incrementAndGet();
context.setProperty("prop", id);
LOG.infof("Request %s %s from IP %s", method, path, address);
}
@Override
public void filter(final ContainerRequestContext requestContext, final ContainerResponseContext responseContext) {
String LOGGING_ID_PROPERTY = "prop";
String RESPONSE_PREFIX = "responsePrefix";
final Object requestId = requestContext.getProperty(LOGGING_ID_PROPERTY);
final long id = requestId != null ? (Long) requestId : _id.incrementAndGet();
final StringBuilder b = new StringBuilder();
b.append("text");
printResponseLine(b, "Client response received", id, responseContext.getStatus());
printPrefixedHeaders(b, id, RESPONSE_PREFIX, responseContext.getHeaders());
}
private void printResponseLine(StringBuilder b, String string, long id, int status) {
LOG.info(b.toString() + id);
}
private void printPrefixedHeaders(StringBuilder b, long id, String string, MultivaluedMap<String, Object> headerMap) {
LOG.info(b.toString() + id);
}
}
But if I call some rest end-points (using rest clients) from my application, the trace is lost. The expectation is to see the IDs throughout the log trance from the origin to downstream to the response to the calling client. As it is done with spring-cloud-sleuth for example;
Is there a way to do it in Quarkus already?
回答1:
Just to keep the format of SO, will put it in the form of the answer (I got partial answer, feel free to give the full answer!):
1.
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-smallrye-opentracing</artifactId>
</dependency>
2.
quarkus.jaeger.service-name=my-service
quarkus.jaeger.sampler-type=const quarkus.jaeger.sampler-param=1
quarkus.jaeger.reporter-log-spans=true // this will make it log to the logs.
3.
Make sure you add the @Traced annotation over those classes/services that needed to be traced. That calls the db or http.
As result you will get something like this:
2019-11-28 15:52:36,726 INFO [io.jae.int.rep.LoggingReporter] (hystrix-DefaultCommandGroup-1) Span reported: e4dc03bd6270f8fb:e62c79391515c053:e4dc03bd6270f8fb:1 - com.myconpany.myapp.infrastructure.repository.init.pg.MyStuffPgRepository.config
2019-11-28 15:52:36,726 INFO [io.jae.int.rep.LoggingReporter] (hystrix-DefaultCommandGroup-1) Span reported: e4dc03bd6270f8fb:d512b915e876ae4c:e4dc03bd6270f8fb:1 - com.myconpany.myapp.infrastructure.repository.init.pg.MyAnotherStuffPgRepository.findAll
as I can see only [io.ja.in.re.LoggingReporter] category contains spans.. not any others
Still did not answer this:
Is there a way to add span by LOG.info(...)
from the code whenever I pace it ?
I do not see any spans for that logs.
I use org.slf4j.LoggerFactory
and tried the org.jboss.logging.Logger
;
So basically that LoggingFilter above is still with no spans.
I would expect ContainerRequestContext to have them inside. But do not see any while debug.
来源:https://stackoverflow.com/questions/59072395/quarkus-log-tracing-and-equivalent-to-spring-cloud-sleuth