|
20 | 20 | import static org.mockito.Mockito.verify;
|
21 | 21 | import static software.amazon.lambda.powertools.tracing.TracingUtils.withEntitySubsegment;
|
22 | 22 |
|
| 23 | +import ch.qos.logback.classic.Level; |
| 24 | +import ch.qos.logback.classic.Logger; |
| 25 | +import ch.qos.logback.classic.spi.ILoggingEvent; |
| 26 | +import ch.qos.logback.core.read.ListAppender; |
23 | 27 | import com.amazonaws.services.lambda.runtime.Context;
|
24 | 28 | import com.amazonaws.xray.AWSXRay;
|
25 | 29 | import com.amazonaws.xray.entities.Entity;
|
| 30 | +import java.util.List; |
26 | 31 | import org.junit.jupiter.api.AfterEach;
|
27 | 32 | import org.junit.jupiter.api.BeforeEach;
|
28 | 33 | import org.junit.jupiter.api.Test;
|
| 34 | +import org.slf4j.LoggerFactory; |
29 | 35 |
|
30 | 36 | class TracingUtilsTest {
|
31 | 37 |
|
@@ -123,6 +129,48 @@ void shouldInvokeCodeBlockWrappedWithinSubsegment() {
|
123 | 129 | });
|
124 | 130 | }
|
125 | 131 |
|
| 132 | + @Test |
| 133 | + void shouldEmitNoLogWarnIfValidCharacterInKey() { |
| 134 | + AWSXRay.beginSubsegment("subSegment"); |
| 135 | + Logger logger = (Logger) LoggerFactory.getLogger(TracingUtils.class); |
| 136 | + |
| 137 | + // create and start a ListAppender |
| 138 | + ListAppender<ILoggingEvent> listAppender = new ListAppender<>(); |
| 139 | + listAppender.start(); |
| 140 | + |
| 141 | + // add the appender to the logger |
| 142 | + logger.addAppender(listAppender); |
| 143 | + |
| 144 | + TracingUtils.putAnnotation("stringKey", "val"); |
| 145 | + |
| 146 | + List<ILoggingEvent> logsList = listAppender.list; |
| 147 | + assertThat(AWSXRay.getTraceEntity().getAnnotations()) |
| 148 | + .hasSize(1) |
| 149 | + .contains( |
| 150 | + entry("stringKey", "val") |
| 151 | + ); |
| 152 | + assertThat(logsList.size()).isZero(); |
| 153 | + } |
| 154 | + |
| 155 | + @Test |
| 156 | + void shouldEmitLogWarnIfInvalidCharacterInKey() { |
| 157 | + AWSXRay.beginSubsegment("subSegment"); |
| 158 | + Logger logger = (Logger) LoggerFactory.getLogger(TracingUtils.class); |
| 159 | + |
| 160 | + // create and start a ListAppender |
| 161 | + ListAppender<ILoggingEvent> listAppender = new ListAppender<>(); |
| 162 | + listAppender.start(); |
| 163 | + |
| 164 | + // add the appender to the logger |
| 165 | + logger.addAppender(listAppender); |
| 166 | + String inputKey = "stringKey with spaces"; |
| 167 | + TracingUtils.putAnnotation(inputKey, "val"); |
| 168 | + |
| 169 | + List<ILoggingEvent> logsList = listAppender.list; |
| 170 | + assertThat(logsList.get(0).getLevel()).isEqualTo(Level.WARN); |
| 171 | + assertThat(logsList.get(0).getMessage()).isEqualTo("ignoring annotation with unsupported characters in key: {}",inputKey); |
| 172 | + } |
| 173 | + |
126 | 174 | @Test
|
127 | 175 | void shouldInvokeCodeBlockWrappedWithinNamespacedSubsegment() {
|
128 | 176 | Context test = mock(Context.class);
|
|
0 commit comments