In order to add baggage you need to have a span. Spring Cloud Sleuth and Spring Boot create a span for you when the controller is invoked. If you want to do the same using CLI application, you need to create span yourself.
You have two options.
Using API calls:
Span span = this.tracer.nextSpan().name("mySpan");
// do some work
span.end(); // best to put it in finally to make sure span is always ended
Or you can use annotations:
@NewSpan
public void doWork() {
}
If you use the annotation, please keep in mind the AOP proxies limitations. In particular self invocations (calls using this
) would not work.
@SpringBootApplication
public class ConsoleApplication
implements CommandLineRunner {
@Override
public void run(String... args) {
doWork(); //this is the same as this.doWork();
}
@NewSpan
public void doWork() {
}
}
This is not going to work as doWork
is not invoked through the AOP proxy. Make sure that you annotate a component managed by Spring and then use an injected instance.
@SpringBootApplication
public class ConsoleApplication
implements CommandLineRunner {
@Autowired
private MyService myService;
@Override
public void run(String... args) {
myService.doWork();
}
}
@Component
class MyService {
@NewSpan
public void doWork() {
}
}
In this case myService
is not instance of MyService
, but rather an instrumented proxy.