Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@
import com.amazonaws.xray.entities.Subsegment;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.function.Consumer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* A class of helper functions to add additional functionality and ease
* of use.
*/
public final class TracingUtils {
private static final Logger LOG = LoggerFactory.getLogger(TracingUtils.class);
private static ObjectMapper objectMapper;

/**
Expand All @@ -36,6 +39,10 @@ public final class TracingUtils {
* @param value the value of the annotation
*/
public static void putAnnotation(String key, String value) {
if (!isValidAnnotationKey(key)) {
LOG.warn("Ignoring annotation with unsupported characters in key: {}", key);
return;
}
AWSXRay.getCurrentSubsegmentOptional()
.ifPresent(segment -> segment.putAnnotation(key, value));
}
Expand All @@ -47,10 +54,24 @@ public static void putAnnotation(String key, String value) {
* @param value the value of the annotation
*/
public static void putAnnotation(String key, Number value) {
if (!isValidAnnotationKey(key)) {
LOG.warn("Ignoring annotation with unsupported characters in key: {}", key);
return;
}
AWSXRay.getCurrentSubsegmentOptional()
.ifPresent(segment -> segment.putAnnotation(key, value));
}

/**
Make sure that the annotation key is valid according to
<a href='https://docs.aws.amazon.com/xray/latest/devguide/xray-api-segmentdocuments.html#api-segmentdocuments-annotations'>the documentation</a>.

Annotation keys that are added that are invalid are ignored by x-ray.
**/
private static boolean isValidAnnotationKey(String key) {
return key.matches("^[a-zA-Z0-9_]+$");
}

/**
* Put an annotation to the current subsegment with a Boolean value.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.junit.jupiter.api.Test;

class TracingUtilsTest {

@BeforeEach
void setUp() {
AWSXRay.beginSegment("test");
Expand Down Expand Up @@ -123,6 +122,24 @@ void shouldInvokeCodeBlockWrappedWithinSubsegment() {
});
}

@Test
void shouldNotAddAnnotationIfInvalidCharacterInKey() {
AWSXRay.beginSubsegment("subSegment");
String inputKey = "stringKey with spaces";
TracingUtils.putAnnotation(inputKey, "val");
AWSXRay.getCurrentSubsegmentOptional()
.ifPresent(segment -> assertThat(segment.getAnnotations()).size().isEqualTo(0));
}

@Test
void shouldAddAnnotationIfValidCharactersInKey() {
AWSXRay.beginSubsegment("subSegment");
String inputKey = "validKey";
TracingUtils.putAnnotation(inputKey, "val");
AWSXRay.getCurrentSubsegmentOptional()
.ifPresent(segment -> assertThat(segment.getAnnotations()).size().isEqualTo(1));
}

@Test
void shouldInvokeCodeBlockWrappedWithinNamespacedSubsegment() {
Context test = mock(Context.class);
Expand Down Expand Up @@ -221,4 +238,4 @@ void shouldInvokeCodeBlockWrappedWithinNamespacedEntitySubsegment() throws Inter
.containsEntry("key", "val");
});
}
}
}