21
21
import com .amazonaws .xray .entities .Subsegment ;
22
22
import com .fasterxml .jackson .databind .ObjectMapper ;
23
23
import java .util .function .Consumer ;
24
+ import org .slf4j .Logger ;
25
+ import org .slf4j .LoggerFactory ;
24
26
25
27
/**
26
28
* A class of helper functions to add additional functionality and ease
27
29
* of use.
28
30
*/
29
31
public final class TracingUtils {
32
+ private static final Logger LOG = LoggerFactory .getLogger (TracingUtils .class );
30
33
private static ObjectMapper objectMapper ;
31
34
32
35
/**
@@ -36,6 +39,10 @@ public final class TracingUtils {
36
39
* @param value the value of the annotation
37
40
*/
38
41
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
+ }
39
46
AWSXRay .getCurrentSubsegmentOptional ()
40
47
.ifPresent (segment -> segment .putAnnotation (key , value ));
41
48
}
@@ -47,10 +54,24 @@ public static void putAnnotation(String key, String value) {
47
54
* @param value the value of the annotation
48
55
*/
49
56
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
+ }
50
61
AWSXRay .getCurrentSubsegmentOptional ()
51
62
.ifPresent (segment -> segment .putAnnotation (key , value ));
52
63
}
53
64
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
+
54
75
/**
55
76
* Put an annotation to the current subsegment with a Boolean value.
56
77
*
0 commit comments