Skip to content

Commit

Permalink
chore: Addition of Warn Message If Invalid Annotation Key While Tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
jdoherty committed Nov 14, 2023
1 parent de547d0 commit 30c8d33
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
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");
});
}
}
}

0 comments on commit 30c8d33

Please sign in to comment.