This extension to spring-cloud-sleuth gives you the ability to create "local" spans at points not instrumented by spring-cloud-sleuth by default. For example you could want to create local spans for database calls. With this extension you can create a new Span simply by adding an annotation to your method.
The following method would be wrapped by a local span with name "TestClass/findProducts"
public class TestClass {
...
@CreateSleuthSpan
public List<Products> findProducts(Long tenantId, Integer minAmount) {
...
}
}
To use this library, you have to add the following two annotations to one of your configuration files:
@EnableAspectJAutoProxy
@Import(SleuthAnnotationConfiguration.class)
You can customize the name of the created span by adding the "name" attribute to @CreateSleuthSpan
annotation.
Example: The following will change the span name to "psql/findProductsByTenantAndAmount".
public class TestClass {
...
@CreateSleuthSpan(name = "psql/findProductsByTenantAndAmount")
public List<Products> findProducts(Long tenantId, Integer minAmount) {
...
}
}
There are 3 different ways to add tags to a span. All of them are controlled by the SleuthSpanTag
annotation. Precedence is Way 3 -> Way 2 -> Way 1
The following method adds a tag with name "tenantId" and value of "tenantId.toString()" to the span:
public class TestClass {
...
@CreateSleuthSpan(name = "psql/findProductsByTenantAndAmount")
public List<Products> findProducts(@SleuthSpanTag("tenantId") Long tenantId, Integer minAmount) {
...
}
}
The following method adds a tag with name "tenantId" and value of "tenantId + 1" to the span:
public class TestClass {
...
@CreateSleuthSpan(name = "psql/findProductsByTenantAndAmount")
public List<Products> findProducts(@SleuthSpanTag(value = "tenantId", tagValueExpression = "param + 1") Long tenantId, Integer minAmount) {
...
}
}
The value of the tag for following method will be computed by an implementation of SleuthTagValueResolver interface.
public class TestClass {
...
@CreateSleuthSpan(name = "psql/findProductsByTenantAndAmount")
public List<Products> findProducts(@SleuthSpanTag(value = "tenantId", tagValueResolverBeanName = "tenantIdTagValueResolver") Long tenantId, Integer minAmount) {
...
}
}