2121import com .amazonaws .xray .entities .Subsegment ;
2222import com .fasterxml .jackson .databind .ObjectMapper ;
2323import java .util .function .Consumer ;
24+ import org .slf4j .Logger ;
25+ import org .slf4j .LoggerFactory ;
2426
2527/**
2628 * A class of helper functions to add additional functionality and ease
2729 * of use.
2830 */
2931public final class TracingUtils {
32+ private static final Logger LOG = LoggerFactory .getLogger (TracingUtils .class );
3033 private static ObjectMapper objectMapper ;
3134
3235 /**
@@ -36,6 +39,10 @@ public final class TracingUtils {
3639 * @param value the value of the annotation
3740 */
3841 public static void putAnnotation (String key , String value ) {
42+ if (!isValidAnnotationKey (key )) {
43+ LOG .warn ("Ignoring annotation with unsupported characters in key: {}" , key );
44+ return ;
45+ }
3946 AWSXRay .getCurrentSubsegmentOptional ()
4047 .ifPresent (segment -> segment .putAnnotation (key , value ));
4148 }
@@ -47,10 +54,24 @@ public static void putAnnotation(String key, String value) {
4754 * @param value the value of the annotation
4855 */
4956 public static void putAnnotation (String key , Number value ) {
57+ if (!isValidAnnotationKey (key )) {
58+ LOG .warn ("Ignoring annotation with unsupported characters in key: {}" , key );
59+ return ;
60+ }
5061 AWSXRay .getCurrentSubsegmentOptional ()
5162 .ifPresent (segment -> segment .putAnnotation (key , value ));
5263 }
5364
65+ /**
66+ Make sure that the annotation key is valid according to
67+ <a href='https://docs.aws.amazon.com/xray/latest/devguide/xray-api-segmentdocuments.html#api-segmentdocuments-annotations'>the documentation</a>.
68+
69+ Annotation keys that are added that are invalid are ignored by x-ray.
70+ **/
71+ private static boolean isValidAnnotationKey (String key ) {
72+ return key .matches ("^[a-zA-Z0-9_]+$" );
73+ }
74+
5475 /**
5576 * Put an annotation to the current subsegment with a Boolean value.
5677 *
0 commit comments