Skip to content

Commit

Permalink
removed logback testing solution and test the annotation not logging
Browse files Browse the repository at this point in the history
  • Loading branch information
jdoherty committed Nov 14, 2023
1 parent 391c9cf commit 5b33d85
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 52 deletions.
11 changes: 0 additions & 11 deletions powertools-tracing/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,6 @@
</developer>
</developers>

<properties>
<logback.version>1.2.11</logback.version>
</properties>

<distributionManagement>
<snapshotRepository>
<id>ossrh</id>
Expand Down Expand Up @@ -119,13 +115,6 @@
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
<scope>test</scope>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,14 @@
import static org.mockito.Mockito.verify;
import static software.amazon.lambda.powertools.tracing.TracingUtils.withEntitySubsegment;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.read.ListAppender;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.xray.AWSXRay;
import com.amazonaws.xray.entities.Entity;
import java.util.List;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.slf4j.LoggerFactory;

class TracingUtilsTest {

@BeforeEach
void setUp() {
AWSXRay.beginSegment("test");
Expand Down Expand Up @@ -130,45 +123,21 @@ void shouldInvokeCodeBlockWrappedWithinSubsegment() {
}

@Test
void shouldEmitNoLogWarnIfValidCharacterInKey() {
void shouldNotAddAnnotationIfInvalidCharacterInKey() {
AWSXRay.beginSubsegment("subSegment");
Logger logger = (Logger) LoggerFactory.getLogger(TracingUtils.class);

// create and start a ListAppender
ListAppender<ILoggingEvent> listAppender = new ListAppender<>();
listAppender.start();

// add the appender to the logger
logger.addAppender(listAppender);

TracingUtils.putAnnotation("stringKey", "val");

List<ILoggingEvent> logsList = listAppender.list;
assertThat(AWSXRay.getTraceEntity().getAnnotations())
.hasSize(1)
.contains(
entry("stringKey", "val")
);
assertThat(logsList.size()).isZero();
String inputKey = "stringKey with spaces";
TracingUtils.putAnnotation(inputKey, "val");
AWSXRay.getCurrentSubsegmentOptional()
.ifPresent(segment -> assertThat(segment.getAnnotations()).size().isEqualTo(0));
}

@Test
void shouldEmitLogWarnIfInvalidCharacterInKey() {
void shouldAddAnnotationIfValidCharactersInKey() {
AWSXRay.beginSubsegment("subSegment");
Logger logger = (Logger) LoggerFactory.getLogger(TracingUtils.class);

// create and start a ListAppender
ListAppender<ILoggingEvent> listAppender = new ListAppender<>();
listAppender.start();

// add the appender to the logger
logger.addAppender(listAppender);
String inputKey = "stringKey with spaces";
String inputKey = "validKey";
TracingUtils.putAnnotation(inputKey, "val");

List<ILoggingEvent> logsList = listAppender.list;
assertThat(logsList.get(0).getLevel()).isEqualTo(Level.WARN);
assertThat(logsList.get(0).getMessage()).isEqualTo("Ignoring annotation with unsupported characters in key: {}",inputKey);
AWSXRay.getCurrentSubsegmentOptional()
.ifPresent(segment -> assertThat(segment.getAnnotations()).size().isEqualTo(1));
}

@Test
Expand Down Expand Up @@ -269,4 +238,4 @@ void shouldInvokeCodeBlockWrappedWithinNamespacedEntitySubsegment() throws Inter
.containsEntry("key", "val");
});
}
}
}

0 comments on commit 5b33d85

Please sign in to comment.