The main arguments for this features are
api-agnostic means to collaborate with a span
reduced surface area for basic span operations.
collaboration with runtime generated code
If you really don’t want to take care of creating local spans manually you can profit from the
@NewSpan
annotation. Also we give you the @SpanTag
annotation to add tags in an automated
fashion.
Let’s look at some examples of usage.
@NewSpan void testMethod();
Annotating the method without any parameter will lead to a creation of a new span whose name will be equal to annotated method name.
@NewSpan("customNameOnTestMethod4") void testMethod4();
If you provide the value in the annotation (either directly or via the name
parameter) then
the created span will have the name as the provided value.
// method declaration @NewSpan(name = "customNameOnTestMethod5") void testMethod5(@SpanTag("testTag") String param); // and method execution this.testBean.testMethod5("test");
You can combine both the name and a tag. Let’s focus on the latter. In this case whatever the value of
the annotated method’s parameter runtime value will be - that will be the value of the tag. In our sample
the tag key will be testTag
and the tag value will be test
.
@NewSpan(name = "customNameOnTestMethod3") @Override public void testMethod3() { }
You can place the @NewSpan
annotation on both the class and an interface. If you override the
interface’s method and provide a different value of the @NewSpan
annotation then the most
concrete one wins (in this case customNameOnTestMethod3
will be set).
If you want to just add tags and annotations to an existing span it’s enough
to use the @ContinueSpan
annotation as presented below. Note that in contrast
with the @NewSpan
annotation you can also add logs via the log
parameter:
// method declaration @ContinueSpan(log = "testMethod11") void testMethod11(@SpanTag("testTag11") String param); // method execution this.testBean.testMethod11("test");
That way the span will get continued and:
testMethod11.before
and testMethod11.after
will be createdtestMethod11.afterFailure
will also be createdtestTag11
and value test
will be createdThere are 3 different ways to add tags to a span. All of them are controlled by the SpanTag
annotation.
Precedence is:
TagValueResolver
type and provided nameTagValueExpressionResolver
bean.
The default implementation uses SPEL expression resolution. If we do not find any expression to evaluate, return the toString()
value of the parameter.
IMPORTANT You can only reference properties from the SPEL expression. Method execution is not allowed due to security constraints.toString()
value of the parameterThe value of the tag for following method will be computed by an implementation of TagValueResolver
interface.
Its class name has to be passed as the value of the resolver
attribute.
Having such an annotated method:
@NewSpan public void getAnnotationForTagValueResolver(@SpanTag(key = "test", resolver = TagValueResolver.class) String test) { }
and such a TagValueResolver
bean implementation
@Bean(name = "myCustomTagValueResolver") public TagValueResolver tagValueResolver() { return parameter -> "Value from myCustomTagValueResolver"; }
Will lead to setting of a tag value equal to Value from myCustomTagValueResolver
.
Having such an annotated method:
@NewSpan public void getAnnotationForTagValueExpression(@SpanTag(key = "test", expression = "'hello' + ' characters'") String test) { }
and no custom implementation of a TagValueExpressionResolver
will lead to evaluation of the SPEL expression and a tag with value 4 characters
will be set on the span.
If you want to use some other expression resolution mechanism you can create your own implementation
of the bean.