Skip to content

Latest commit

 

History

History
83 lines (62 loc) · 2.33 KB

File metadata and controls

83 lines (62 loc) · 2.33 KB

Sleuth Module

Sleuth module is library to simplify sleuth integration

Setup Dependency

<dependency>
  <groupId>com.blibli.oss</groupId>
  <artifactId>blibli-backend-framework-sleuth</artifactId>
</dependency>

Extra Fields

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");

Tracer on Web Filter

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);
  }
}