Sleuth module is library to simplify sleuth integration
<dependency>
<groupId>com.blibli.oss</groupId>
<artifactId>blibli-backend-framework-sleuth</artifactId>
</dependency>
Sleuth support baggage or extra fields. This is field that we can store to sleuth context,
and can be accessed anywhere. Default spring boot already support extra fields using properties.
But sometimes we want to add extra field using code or in multiple place. Sleuth module support
extra fields using spring bean. We only need to create bean of class SleuthExtraFields
@Component
public static class HelloWorldSleuthExtraFields implements SleuthExtraFields {
@Override
public List<String> getFields() {
return Arrays.asList("Hello", "World");
}
}
@Component
public static class MandatoryParamSleuthExtraFields implements SleuthExtraFields {
@Override
public List<String> getFields() {
return Arrays.asList("StoreId", "ClientId");
}
}
Sleuth module will automatically register all fields, so we can set and get extra field to sleuth context.
@Autowired
private Tracer tracer;
// set extra field
ExtraFieldPropagation.set(span.context(), "Hello", "Value");
ExtraFieldPropagation.set(span.context(), "World", "Value");
ExtraFieldPropagation.set(span.context(), "ClientId", "Value");
ExtraFieldPropagation.set(span.context(), "StoreId", "Value");
// get extra field
String hello = ExtraFieldPropagation.get(span.context(), "Hello");
String world = ExtraFieldPropagation.get(span.context(), "World");
String clientId = ExtraFieldPropagation.get(span.context(), "ClientId");
String storeId = ExtraFieldPropagation.get(span.context(), "StoreId");
Using Tracer on WebFilter is not easy. By default current span is not active on web filter.
To simplify this, we can use class SleuthWebFilter
as base interface. The filter method will have Span
parameter.
@Component
public class ExampleSleuthWebFilter implements SleuthWebFilter {
@Autowired
@Getter
private Tracer tracer;
@Override
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain, Span currentSpan) {
// do something with currentSpan
ExtraFieldPropagation.set(currentSpan.context(), "Key", "Value");
return chain.filter(exchange);
}
}