diff --git a/multi/multi__managing_spans_with_annotations.html b/multi/multi__managing_spans_with_annotations.html index 0d3bbc129..cd81acde9 100644 --- a/multi/multi__managing_spans_with_annotations.html +++ b/multi/multi__managing_spans_with_annotations.html @@ -9,10 +9,10 @@ Also, we provide the @SpanTag annotation to add tag void testMethod();

Annotating the method without any parameter leads to creating a new span whose name equals the annotated method name.

@NewSpan("customNameOnTestMethod4")
 void testMethod4();

If you provide the value in the annotation (either directly or by setting the name parameter), the created span has the provided value as the name.

// method declaration
 @NewSpan(name = "customNameOnTestMethod5")
-void testMethod5();
+void testMethod5(@SpanTag("testTag") String param);
 
 // and method execution
-this.testBean.testMethod5();

You can combine both the name and a tag. Let’s focus on the latter. +this.testBean.testMethod5("test");

You can combine both the name and a tag. Let’s focus on the latter. In this case, the value of the annotated method’s parameter runtime value becomes the value of the tag. In our sample, the tag key is testTag, and the tag value is test.

@NewSpan(name = "customNameOnTestMethod3")
 @Override
@@ -21,23 +21,25 @@ In our sample, the tag key is testTag, and the tag
 If you override the interface’s method and provide a different value for the @NewSpan annotation, the most
 concrete one wins (in this case customNameOnTestMethod3 is set).

11.3 Continuing Spans

If you want to add tags and annotations to an existing span, you can use the @ContinueSpan annotation, as shown in the following example:

// method declaration
 @ContinueSpan(log = "testMethod11")
-void testMethod11();
+void testMethod11(@SpanTag("testTag11") String param);
 
 // method execution
-this.testBean.testMethod11();
+this.testBean.testMethod11("test");
 this.testBean.testMethod13();

(Note that, in contrast with the @NewSpan annotation ,you can also add logs with the log parameter.)

That way, the span gets continued and:

  • Log entries named testMethod11.before and testMethod11.after are created.
  • If an exception is thrown, a log entry named testMethod11.afterFailure is also created.
  • A tag with a key of testTag11 and a value of test is created.

11.4 Advanced Tag Setting

There are 3 different ways to add tags to a span. All of them are controlled by the SpanTag annotation. The precedence is as follows:

  1. Try with a bean of TagValueResolver type and a provided name.
  2. If the bean name has not been provided, try to evaluate an expression. We search for a TagValueExpressionResolver bean. The default implementation uses SPEL expression resolution. IMPORTANT You can only reference properties from the SPEL expression. Method execution is not allowed due to security constraints.
  3. If we do not find any expression to evaluate, return the toString() value of the parameter.

11.4.1 Custom extractor

The value of the tag for the following method is computed by an implementation of TagValueResolver interface. Its class name has to be passed as the value of the resolver attribute.

Consider the following annotated method:

@NewSpan
-public void getAnnotationForTagValueResolver() {
+public void getAnnotationForTagValueResolver(
+		@SpanTag(key = "test", resolver = TagValueResolver.class) String test) {
 }

Now further consider the following TagValueResolver bean implementation:

@Bean(name = "myCustomTagValueResolver")
 public TagValueResolver tagValueResolver() {
-	return () -> "Value from myCustomTagValueResolver";
+	return parameter -> "Value from myCustomTagValueResolver";
 }

The two preceding examples lead to setting a tag value equal to Value from myCustomTagValueResolver.

11.4.2 Resolving Expressions for a Value

Consider the following annotated method:

@NewSpan
-public void getAnnotationForTagValueExpression() {
+public void getAnnotationForTagValueExpression(
+		@SpanTag(key = "test", expression = "'hello' + ' characters'") String test) {
 }

No custom implementation of a TagValueExpressionResolver leads to evaluation of the SPEL expression, and a tag with a value of 4 characters is set on the span. If you want to use some other expression resolution mechanism, you can create your own implementation of the bean.

11.4.3 Using the toString() method

Consider the following annotated method:

@NewSpan
-public void getAnnotationForArgumentToString() {
+public void getAnnotationForArgumentToString(@SpanTag("test") Long param) {
 }

Running the preceding method with a value of 15 leads to setting a tag with a String value of "15".

\ No newline at end of file diff --git a/single/spring-cloud-sleuth.html b/single/spring-cloud-sleuth.html index 767dfd85c..52a1a56d9 100644 --- a/single/spring-cloud-sleuth.html +++ b/single/spring-cloud-sleuth.html @@ -688,10 +688,10 @@ Also, we provide the @SpanTag annotation to add tag void testMethod();

Annotating the method without any parameter leads to creating a new span whose name equals the annotated method name.

@NewSpan("customNameOnTestMethod4")
 void testMethod4();

If you provide the value in the annotation (either directly or by setting the name parameter), the created span has the provided value as the name.

// method declaration
 @NewSpan(name = "customNameOnTestMethod5")
-void testMethod5();
+void testMethod5(@SpanTag("testTag") String param);
 
 // and method execution
-this.testBean.testMethod5();

You can combine both the name and a tag. Let’s focus on the latter. +this.testBean.testMethod5("test");

You can combine both the name and a tag. Let’s focus on the latter. In this case, the value of the annotated method’s parameter runtime value becomes the value of the tag. In our sample, the tag key is testTag, and the tag value is test.

@NewSpan(name = "customNameOnTestMethod3")
 @Override
@@ -700,25 +700,27 @@ In our sample, the tag key is testTag, and the tag
 If you override the interface’s method and provide a different value for the @NewSpan annotation, the most
 concrete one wins (in this case customNameOnTestMethod3 is set).

11.3 Continuing Spans

If you want to add tags and annotations to an existing span, you can use the @ContinueSpan annotation, as shown in the following example:

// method declaration
 @ContinueSpan(log = "testMethod11")
-void testMethod11();
+void testMethod11(@SpanTag("testTag11") String param);
 
 // method execution
-this.testBean.testMethod11();
+this.testBean.testMethod11("test");
 this.testBean.testMethod13();

(Note that, in contrast with the @NewSpan annotation ,you can also add logs with the log parameter.)

That way, the span gets continued and:

  • Log entries named testMethod11.before and testMethod11.after are created.
  • If an exception is thrown, a log entry named testMethod11.afterFailure is also created.
  • A tag with a key of testTag11 and a value of test is created.

11.4 Advanced Tag Setting

There are 3 different ways to add tags to a span. All of them are controlled by the SpanTag annotation. The precedence is as follows:

  1. Try with a bean of TagValueResolver type and a provided name.
  2. If the bean name has not been provided, try to evaluate an expression. We search for a TagValueExpressionResolver bean. The default implementation uses SPEL expression resolution. IMPORTANT You can only reference properties from the SPEL expression. Method execution is not allowed due to security constraints.
  3. If we do not find any expression to evaluate, return the toString() value of the parameter.

11.4.1 Custom extractor

The value of the tag for the following method is computed by an implementation of TagValueResolver interface. Its class name has to be passed as the value of the resolver attribute.

Consider the following annotated method:

@NewSpan
-public void getAnnotationForTagValueResolver() {
+public void getAnnotationForTagValueResolver(
+		@SpanTag(key = "test", resolver = TagValueResolver.class) String test) {
 }

Now further consider the following TagValueResolver bean implementation:

@Bean(name = "myCustomTagValueResolver")
 public TagValueResolver tagValueResolver() {
-	return () -> "Value from myCustomTagValueResolver";
+	return parameter -> "Value from myCustomTagValueResolver";
 }

The two preceding examples lead to setting a tag value equal to Value from myCustomTagValueResolver.

11.4.2 Resolving Expressions for a Value

Consider the following annotated method:

@NewSpan
-public void getAnnotationForTagValueExpression() {
+public void getAnnotationForTagValueExpression(
+		@SpanTag(key = "test", expression = "'hello' + ' characters'") String test) {
 }

No custom implementation of a TagValueExpressionResolver leads to evaluation of the SPEL expression, and a tag with a value of 4 characters is set on the span. If you want to use some other expression resolution mechanism, you can create your own implementation of the bean.

11.4.3 Using the toString() method

Consider the following annotated method:

@NewSpan
-public void getAnnotationForArgumentToString() {
+public void getAnnotationForArgumentToString(@SpanTag("test") Long param) {
 }

Running the preceding method with a value of 15 leads to setting a tag with a String value of "15".

12. Customizations

12.1 HTTP

If a customization of client / server parsing of the HTTP related spans is required, just register a bean of type brave.http.HttpClientParser or brave.http.HttpServerParser. If client /server sampling is required, just diff --git a/spring-cloud-sleuth.xml b/spring-cloud-sleuth.xml index fc640a464..b96067d62 100644 --- a/spring-cloud-sleuth.xml +++ b/spring-cloud-sleuth.xml @@ -1440,10 +1440,10 @@ void testMethod4(); If you provide the value in the annotation (either directly or by setting the name parameter), the created span has the provided value as the name. // method declaration @NewSpan(name = "customNameOnTestMethod5") -void testMethod5(); +void testMethod5(@SpanTag("testTag") String param); // and method execution -this.testBean.testMethod5(); +this.testBean.testMethod5("test"); You can combine both the name and a tag. Let’s focus on the latter. In this case, the value of the annotated method’s parameter runtime value becomes the value of the tag. In our sample, the tag key is testTag, and the tag value is test. @@ -1460,10 +1460,10 @@ concrete one wins (in this case customNameOnTestMethod3 is se If you want to add tags and annotations to an existing span, you can use the @ContinueSpan annotation, as shown in the following example: // method declaration @ContinueSpan(log = "testMethod11") -void testMethod11(); +void testMethod11(@SpanTag("testTag11") String param); // method execution -this.testBean.testMethod11(); +this.testBean.testMethod11("test"); this.testBean.testMethod13(); (Note that, in contrast with the @NewSpan annotation ,you can also add logs with the log parameter.) That way, the span gets continued and: @@ -1503,12 +1503,13 @@ The default implementation uses SPEL expression resolution. Its class name has to be passed as the value of the resolver attribute. Consider the following annotated method: @NewSpan -public void getAnnotationForTagValueResolver() { +public void getAnnotationForTagValueResolver( + @SpanTag(key = "test", resolver = TagValueResolver.class) String test) { } Now further consider the following TagValueResolver bean implementation: @Bean(name = "myCustomTagValueResolver") public TagValueResolver tagValueResolver() { - return () -> "Value from myCustomTagValueResolver"; + return parameter -> "Value from myCustomTagValueResolver"; } The two preceding examples lead to setting a tag value equal to Value from myCustomTagValueResolver. @@ -1516,7 +1517,8 @@ public TagValueResolver tagValueResolver() { Resolving Expressions for a Value Consider the following annotated method: @NewSpan -public void getAnnotationForTagValueExpression() { +public void getAnnotationForTagValueExpression( + @SpanTag(key = "test", expression = "'hello' + ' characters'") String test) { } No custom implementation of a TagValueExpressionResolver leads to evaluation of the SPEL expression, and a tag with a value of 4 characters is set on the span. If you want to use some other expression resolution mechanism, you can create your own implementation of the bean. @@ -1525,7 +1527,7 @@ If you want to use some other expression resolution mechanism, you can create yo Using the <literal>toString()</literal> method Consider the following annotated method: @NewSpan -public void getAnnotationForArgumentToString() { +public void getAnnotationForArgumentToString(@SpanTag("test") Long param) { } Running the preceding method with a value of 15 leads to setting a tag with a String value of "15".