Fixed DefaultSpanNamer not picking the value from annotation

This commit is contained in:
Marcin Grzejszczak
2022-12-13 19:04:10 +01:00
parent 799b6598dc
commit dcfd714ee6
2 changed files with 19 additions and 6 deletions

View File

@@ -24,15 +24,15 @@ import org.springframework.core.annotation.AnnotationUtils;
/**
* Default implementation of SpanNamer that tries to get the span name as follows:
*
* <p>
* * from the @SpanName annotation on the class if one is present.
*
* <p>
* * from the @SpanName annotation on the method if passed object is of a {@link Method}.
* type
*
* <p>
* * from the toString() of the delegate if it's not the default
* {@link Object#toString()}.
*
* <p>
* * the default provided value.
*
* @author Marcin Grzejszczak
@@ -41,7 +41,7 @@ import org.springframework.core.annotation.AnnotationUtils;
*/
public class DefaultSpanNamer implements SpanNamer {
private static boolean isDefaultToString(Object delegate, String spanName) {
private static boolean isDefaultToString(Object delegate) {
try {
return delegate.getClass().getMethod("toString").getDeclaringClass() == Object.class;
}
@@ -55,7 +55,7 @@ public class DefaultSpanNamer implements SpanNamer {
SpanName annotation = annotation(object);
String spanName = annotation != null ? annotation.value() : object.toString();
// If there is no overridden toString method we'll put a constant value
if (isDefaultToString(object, spanName)) {
if (annotation == null && isDefaultToString(object)) {
return defaultValue;
}
return spanName;

View File

@@ -19,6 +19,8 @@ package org.springframework.cloud.sleuth.internal;
import org.assertj.core.api.BDDAssertions;
import org.junit.jupiter.api.Test;
import org.springframework.cloud.sleuth.SpanName;
class DefaultSpanNamerTest {
@Test
@@ -39,6 +41,12 @@ class DefaultSpanNamerTest {
BDDAssertions.then(defaultValue).isEqualTo("mytostring");
}
@Test
void nameWithNoToStringAndAnnotation() {
String defaultValue = new DefaultSpanNamer().name(new NoToStringOverrideAndAnnotation(), "default value");
BDDAssertions.then(defaultValue).isEqualTo("new-name");
}
static class NoToStringOverride {
}
@@ -56,4 +64,9 @@ class DefaultSpanNamerTest {
}
@SpanName("new-name")
static class NoToStringOverrideAndAnnotation {
}
}