1

I'm working on a POC and was able to integrate 2 microservices with JaegerUI.

Request to an endpoint in serviceA calls an endpoint in serviceB and returns a response.

I have used below dependencies:

spring.boot.version : 2.1.4.RELEASE

<groupId>io.opentracing.contrib</groupId>
            <artifactId>opentracing-spring-jaeger-web-starter</artifactId>
            <version>3.1.1</version>

 <groupId>io.opentracing.contrib</groupId>
        <artifactId>opentracing-spring-cloud-starter</artifactId>
        <version>0.4.0</version>

Spring autoconfiguration takes care of everything so just added the required properties:

opentracing.jaeger.http-sender.url

opentracing.jaeger.service-name

opentracing.jaeger.enabled

opentracing.spring.cloud.async.enabled

I want to achieve the below:

  1. I want to add application logs to the span so that they are visible in JaegerUI.
  2. I want to add some fields to span tags so that it's easy to search in JaegerUI.
  3. Also, I want the spanId and traceId to the application log.
  4. Is it possible to search in JaegerUI based on spanId/traceId? If yes, how?

Based on the answer in below SO question:

How to enrich Jaeger opentracing data with the application logs (produced by slf4j) for Spring Boot?

opentracing-spring-cloud-starter dependency should automatically take care of sending app logs to span in JaegerUI.

I have a log statement like below in serviceA:

logger.info("sending request to serviceB.");

But above log is not getting captured in corresponding span and not visible in JaegerUI.

Any suggestions on how to achieve the above scenarios are appreciated!

Rahul
  • 637
  • 5
  • 16

2 Answers2

1

I was studying Opentracing and Jeager and I've used this tutorial to get familiar with the basic possibilities: https://github.com/yurishkuro/opentracing-tutorial/tree/master/java

If you take a look in the case 1 (Hello World), it explains how to "Annotate the Trace with Tags and Logs". That would answer your questions 1, 2 and 3, as with that you can add all the info that you would like within spans and logs.

Here is a snippet from the repository (but I'd recommend checking there, as it has a more detailed explanation):

Span span = tracer.buildSpan("say-hello").start();
span.setTag("hello-to", helloTo);

In this case helloTo is a variable containing a name, to whom the app will say hello. It would create a span tag called hello-to with the value that is coming from the execution.

Below we have an example for the logs case, where the whole helloStr message is added to the logs:

// this goes inside the sayHello method
String helloStr = String.format("Hello, %s!", helloTo);
span.log(ImmutableMap.of("event", "string-format", "value", helloStr));

System.out.println(helloStr);
span.log(ImmutableMap.of("event", "println"));

Regarding the last question, that would be easier, you can use the Jaeger UI to search for the trace you would like, there is a field for that on the top left corner:

enter image description here

Juliano Costa
  • 2,175
  • 2
  • 18
  • 30
  • Thanks for your response. Your answer is relevant. However, I wanted to use the auto configured tracer instance provided by the Spring-Boot. I have figured it out. Basically, we can inject the Spring provided tracer and use it for accessing the active spans. I will be adding an answer of my own. – Rahul Jul 20 '20 at 15:57
1

There you go.

  1. I want to add application logs to the span so that they are visible in JaegerUI.
Span span = tracer.buildSpan("my-span-name").start();
span.setTag("my-tag-name", "my-tag-value");

There are various overloaded methods as follows

Span setTag(String key, String value);
Span setTag(String key, boolean value);
Span setTag(String key, Number value);
  1. I want to add some fields to span tags so that it's easy to search in JaegerUI.

    Jaeger API provides log method to log multiple fields that needs to be added to a map, the method signature is as follows,

Span log(Map<String, ?> fields);

eg:

span.log(
   ImmutableMap.Builder<String, Object>()
     .put("event", "soft error")
     .put("type", "cache timeout")
     .put("waited.millis", 1500)
     .build()
); 
  1. Also, I want the spanId and traceId to the application log.

spanId and traceId are stored in JaegerSpanContext class, which can be obtained from context() method of Span class.

    JaegerSpanContext spanContext = (JaegerSpanContext)sprintSpan.context();
    long spanId = spanContext.getSpanId();
    long traceId = spanContext.getTraceId();
  1. Is it possible to search in JaegerUI based on spanId/traceId? If yes, how?

There is a search box in the navigation bar of Jaeger UI where you can search traces by trace ID.

enter image description here

Krushnat Khavale
  • 416
  • 2
  • 4
  • 14