Skip to content

Commit 0f61739

Browse files
authored
Issue 298 (#300)
* Remove reliance on default encoding. * Boolean environmentVariable(String key) method no longer returns null.
1 parent 7c6b832 commit 0f61739

File tree

4 files changed

+23
-9
lines changed

4 files changed

+23
-9
lines changed

powertools-logging/src/main/java/software/amazon/lambda/powertools/logging/internal/LambdaLoggingAspect.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.aspectj.lang.annotation.Pointcut;
3838
import software.amazon.lambda.powertools.logging.Logging;
3939

40+
import static java.nio.charset.StandardCharsets.UTF_8;
4041
import static java.util.Optional.empty;
4142
import static java.util.Optional.of;
4243
import static java.util.Optional.ofNullable;
@@ -165,8 +166,8 @@ private Object[] logFromInputStream(final ProceedingJoinPoint pjp) {
165166
Object[] args = pjp.getArgs();
166167

167168
try (ByteArrayOutputStream out = new ByteArrayOutputStream();
168-
OutputStreamWriter writer = new OutputStreamWriter(out);
169-
InputStreamReader reader = new InputStreamReader((InputStream) pjp.getArgs()[0])) {
169+
OutputStreamWriter writer = new OutputStreamWriter(out, UTF_8);
170+
InputStreamReader reader = new InputStreamReader((InputStream) pjp.getArgs()[0], UTF_8)) {
170171

171172
IOUtils.copy(reader, writer);
172173
writer.flush();

powertools-tracing/src/main/java/software/amazon/lambda/powertools/tracing/internal/LambdaTracingAspect.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ public Object around(ProceedingJoinPoint pjp,
7878
private boolean captureResponse(Tracing powerToolsTracing) {
7979
switch (powerToolsTracing.captureMode()) {
8080
case ENVIRONMENT_VAR:
81-
Boolean captureResponse = environmentVariable("POWERTOOLS_TRACER_CAPTURE_RESPONSE");
82-
return null != captureResponse ? captureResponse : powerToolsTracing.captureResponse();
81+
boolean captureResponse = environmentVariable("POWERTOOLS_TRACER_CAPTURE_RESPONSE");
82+
return isEnvironmentVariableSet("POWERTOOLS_TRACER_CAPTURE_RESPONSE") ? captureResponse : powerToolsTracing.captureResponse();
8383
case RESPONSE:
8484
case RESPONSE_AND_ERROR:
8585
return true;
@@ -92,8 +92,8 @@ private boolean captureResponse(Tracing powerToolsTracing) {
9292
private boolean captureError(Tracing powerToolsTracing) {
9393
switch (powerToolsTracing.captureMode()) {
9494
case ENVIRONMENT_VAR:
95-
Boolean captureError = environmentVariable("POWERTOOLS_TRACER_CAPTURE_ERROR");
96-
return null != captureError ? captureError : powerToolsTracing.captureError();
95+
boolean captureError = environmentVariable("POWERTOOLS_TRACER_CAPTURE_ERROR");
96+
return isEnvironmentVariableSet("POWERTOOLS_TRACER_CAPTURE_ERROR") ? captureError : powerToolsTracing.captureError();
9797
case ERROR:
9898
case RESPONSE_AND_ERROR:
9999
return true;
@@ -117,8 +117,11 @@ private boolean placedOnHandlerMethod(ProceedingJoinPoint pjp) {
117117
&& (placedOnRequestHandler(pjp) || placedOnStreamHandler(pjp));
118118
}
119119

120-
private Boolean environmentVariable(String POWERTOOLS_TRACER_CAPTURE_RESPONSE) {
121-
return null != SystemWrapper.getenv(POWERTOOLS_TRACER_CAPTURE_RESPONSE)
122-
? Boolean.valueOf(SystemWrapper.getenv(POWERTOOLS_TRACER_CAPTURE_RESPONSE)) : null;
120+
private boolean environmentVariable(String key) {
121+
return Boolean.parseBoolean(SystemWrapper.getenv(key));
122+
}
123+
124+
private boolean isEnvironmentVariableSet(String key) {
125+
return SystemWrapper.containsKey(key);
123126
}
124127
}

powertools-tracing/src/main/java/software/amazon/lambda/powertools/tracing/internal/SystemWrapper.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,8 @@ public SystemWrapper() {
77
public static String getenv(String name) {
88
return System.getenv(name);
99
}
10+
11+
public static boolean containsKey(String key) {
12+
return System.getenv().containsKey(key);
13+
}
1014
}

powertools-tracing/src/test/java/software/amazon/lambda/powertools/tracing/internal/LambdaTracingAspectTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ static void beforeAll() {
6161
try (MockedStatic<SystemWrapper> mocked = mockStatic(SystemWrapper.class)) {
6262
mocked.when(() -> SystemWrapper.getenv("POWERTOOLS_TRACER_CAPTURE_RESPONSE")).thenReturn(null);
6363
mocked.when(() -> SystemWrapper.getenv("POWERTOOLS_TRACER_CAPTURE_ERROR")).thenReturn(null);
64+
mocked.when(() -> SystemWrapper.containsKey("POWERTOOLS_TRACER_CAPTURE_RESPONSE")).thenReturn(false);
65+
mocked.when(() -> SystemWrapper.containsKey("POWERTOOLS_TRACER_CAPTURE_ERROR")).thenReturn(false);
6466
}
6567
}
6668

@@ -227,6 +229,7 @@ void shouldCaptureTracesWithNoMetadataDeprecated() {
227229
@Test
228230
void shouldNotCaptureTracesIfDisabledViaEnvironmentVariable() {
229231
try (MockedStatic<SystemWrapper> mocked = mockStatic(SystemWrapper.class)) {
232+
mocked.when(() -> SystemWrapper.containsKey("POWERTOOLS_TRACER_CAPTURE_RESPONSE")).thenReturn(true);
230233
mocked.when(() -> SystemWrapper.getenv("POWERTOOLS_TRACER_CAPTURE_RESPONSE")).thenReturn("false");
231234

232235
requestHandler.handleRequest(new Object(), context);
@@ -269,7 +272,9 @@ void shouldCaptureTracesIfExplicitlyEnabledAndEnvironmentVariableIsDisabled() {
269272
@Test
270273
void shouldCaptureTracesIfExplicitlyEnabledBothAndEnvironmentVariableIsDisabled() {
271274
try (MockedStatic<SystemWrapper> mocked = mockStatic(SystemWrapper.class)) {
275+
mocked.when(() -> SystemWrapper.containsKey("POWERTOOLS_TRACER_CAPTURE_RESPONSE")).thenReturn(true);
272276
mocked.when(() -> SystemWrapper.getenv("POWERTOOLS_TRACER_CAPTURE_RESPONSE")).thenReturn("false");
277+
mocked.when(() -> SystemWrapper.containsKey("POWERTOOLS_TRACER_CAPTURE_ERROR")).thenReturn(true);
273278
mocked.when(() -> SystemWrapper.getenv("POWERTOOLS_TRACER_CAPTURE_ERROR")).thenReturn("false");
274279
requestHandler = new PowerTracerToolEnabledExplicitlyForResponseAndError();
275280

@@ -292,6 +297,7 @@ void shouldCaptureTracesIfExplicitlyEnabledBothAndEnvironmentVariableIsDisabled(
292297
@Test
293298
void shouldNotCaptureTracesWithExceptionMetaDataIfDisabledViaEnvironmentVariable() {
294299
try (MockedStatic<SystemWrapper> mocked = mockStatic(SystemWrapper.class)) {
300+
mocked.when(() -> SystemWrapper.containsKey("POWERTOOLS_TRACER_CAPTURE_ERROR")).thenReturn(true);
295301
mocked.when(() -> SystemWrapper.getenv("POWERTOOLS_TRACER_CAPTURE_ERROR")).thenReturn("false");
296302
requestHandler = new PowerTracerToolEnabledWithException();
297303

0 commit comments

Comments
 (0)