Skip to content

Commit 217cb13

Browse files
jdohertyjeromevdl
authored andcommitted
updated following PR comments
1 parent bc1896c commit 217cb13

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

powertools-tracing/src/main/java/software/amazon/lambda/powertools/tracing/TracingUtils.java

+16-6
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ public final class TracingUtils {
3939
* @param value the value of the annotation
4040
*/
4141
public static void putAnnotation(String key, String value) {
42-
validateAnnotationKey(key);
42+
if (!isValidateAnnotationKey(key)) {
43+
LOG.warn("Ignoring annotation with unsupported characters in key: {}", key);
44+
return;
45+
}
4346
AWSXRay.getCurrentSubsegmentOptional()
4447
.ifPresent(segment -> segment.putAnnotation(key, value));
4548
}
@@ -51,15 +54,22 @@ public static void putAnnotation(String key, String value) {
5154
* @param value the value of the annotation
5255
*/
5356
public static void putAnnotation(String key, Number value) {
54-
validateAnnotationKey(key);
57+
if (!isValidateAnnotationKey(key)) {
58+
LOG.warn("Ignoring annotation with unsupported characters in key: {}", key);
59+
return;
60+
}
5561
AWSXRay.getCurrentSubsegmentOptional()
5662
.ifPresent(segment -> segment.putAnnotation(key, value));
5763
}
5864

59-
private static void validateAnnotationKey(String key) {
60-
if (!key.matches("^[a-zA-Z0-9_]+$")) {
61-
LOG.warn("ignoring annotation with unsupported characters in key: {}", key);
62-
}
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 isValidateAnnotationKey(String key) {
72+
return key.matches("^[a-zA-Z0-9_]+$");
6373
}
6474

6575
/**

powertools-tracing/src/test/java/software/amazon/lambda/powertools/tracing/TracingUtilsTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ void shouldEmitLogWarnIfInvalidCharacterInKey() {
168168

169169
List<ILoggingEvent> logsList = listAppender.list;
170170
assertThat(logsList.get(0).getLevel()).isEqualTo(Level.WARN);
171-
assertThat(logsList.get(0).getMessage()).isEqualTo("ignoring annotation with unsupported characters in key: {}",inputKey);
171+
assertThat(logsList.get(0).getMessage()).isEqualTo("Ignoring annotation with unsupported characters in key: {}",inputKey);
172172
}
173173

174174
@Test

0 commit comments

Comments
 (0)