Skip to content

Commit 30c8d33

Browse files
authored
chore: Addition of Warn Message If Invalid Annotation Key While Tracing #1511 (#1512)
1 parent de547d0 commit 30c8d33

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

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

+21
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,15 @@
2121
import com.amazonaws.xray.entities.Subsegment;
2222
import com.fasterxml.jackson.databind.ObjectMapper;
2323
import 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
*/
2931
public 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
*

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

+19-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import org.junit.jupiter.api.Test;
2929

3030
class TracingUtilsTest {
31-
3231
@BeforeEach
3332
void setUp() {
3433
AWSXRay.beginSegment("test");
@@ -123,6 +122,24 @@ void shouldInvokeCodeBlockWrappedWithinSubsegment() {
123122
});
124123
}
125124

125+
@Test
126+
void shouldNotAddAnnotationIfInvalidCharacterInKey() {
127+
AWSXRay.beginSubsegment("subSegment");
128+
String inputKey = "stringKey with spaces";
129+
TracingUtils.putAnnotation(inputKey, "val");
130+
AWSXRay.getCurrentSubsegmentOptional()
131+
.ifPresent(segment -> assertThat(segment.getAnnotations()).size().isEqualTo(0));
132+
}
133+
134+
@Test
135+
void shouldAddAnnotationIfValidCharactersInKey() {
136+
AWSXRay.beginSubsegment("subSegment");
137+
String inputKey = "validKey";
138+
TracingUtils.putAnnotation(inputKey, "val");
139+
AWSXRay.getCurrentSubsegmentOptional()
140+
.ifPresent(segment -> assertThat(segment.getAnnotations()).size().isEqualTo(1));
141+
}
142+
126143
@Test
127144
void shouldInvokeCodeBlockWrappedWithinNamespacedSubsegment() {
128145
Context test = mock(Context.class);
@@ -221,4 +238,4 @@ void shouldInvokeCodeBlockWrappedWithinNamespacedEntitySubsegment() throws Inter
221238
.containsEntry("key", "val");
222239
});
223240
}
224-
}
241+
}

0 commit comments

Comments
 (0)