Skip to content

Issue 298 #300

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.aspectj.lang.annotation.Pointcut;
import software.amazon.lambda.powertools.logging.Logging;

import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Optional.empty;
import static java.util.Optional.of;
import static java.util.Optional.ofNullable;
Expand Down Expand Up @@ -165,8 +166,8 @@ private Object[] logFromInputStream(final ProceedingJoinPoint pjp) {
Object[] args = pjp.getArgs();

try (ByteArrayOutputStream out = new ByteArrayOutputStream();
OutputStreamWriter writer = new OutputStreamWriter(out);
InputStreamReader reader = new InputStreamReader((InputStream) pjp.getArgs()[0])) {
OutputStreamWriter writer = new OutputStreamWriter(out, UTF_8);
InputStreamReader reader = new InputStreamReader((InputStream) pjp.getArgs()[0], UTF_8)) {

IOUtils.copy(reader, writer);
writer.flush();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ public Object around(ProceedingJoinPoint pjp,
private boolean captureResponse(Tracing powerToolsTracing) {
switch (powerToolsTracing.captureMode()) {
case ENVIRONMENT_VAR:
Boolean captureResponse = environmentVariable("POWERTOOLS_TRACER_CAPTURE_RESPONSE");
return null != captureResponse ? captureResponse : powerToolsTracing.captureResponse();
boolean captureResponse = environmentVariable("POWERTOOLS_TRACER_CAPTURE_RESPONSE");
return isEnvironmentVariableSet("POWERTOOLS_TRACER_CAPTURE_RESPONSE") ? captureResponse : powerToolsTracing.captureResponse();
case RESPONSE:
case RESPONSE_AND_ERROR:
return true;
Expand All @@ -92,8 +92,8 @@ private boolean captureResponse(Tracing powerToolsTracing) {
private boolean captureError(Tracing powerToolsTracing) {
switch (powerToolsTracing.captureMode()) {
case ENVIRONMENT_VAR:
Boolean captureError = environmentVariable("POWERTOOLS_TRACER_CAPTURE_ERROR");
return null != captureError ? captureError : powerToolsTracing.captureError();
boolean captureError = environmentVariable("POWERTOOLS_TRACER_CAPTURE_ERROR");
return isEnvironmentVariableSet("POWERTOOLS_TRACER_CAPTURE_ERROR") ? captureError : powerToolsTracing.captureError();
case ERROR:
case RESPONSE_AND_ERROR:
return true;
Expand All @@ -117,8 +117,11 @@ private boolean placedOnHandlerMethod(ProceedingJoinPoint pjp) {
&& (placedOnRequestHandler(pjp) || placedOnStreamHandler(pjp));
}

private Boolean environmentVariable(String POWERTOOLS_TRACER_CAPTURE_RESPONSE) {
return null != SystemWrapper.getenv(POWERTOOLS_TRACER_CAPTURE_RESPONSE)
? Boolean.valueOf(SystemWrapper.getenv(POWERTOOLS_TRACER_CAPTURE_RESPONSE)) : null;
private boolean environmentVariable(String key) {
return Boolean.parseBoolean(SystemWrapper.getenv(key));
}

private boolean isEnvironmentVariableSet(String key) {
return SystemWrapper.containsKey(key);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@ public SystemWrapper() {
public static String getenv(String name) {
return System.getenv(name);
}

public static boolean containsKey(String key) {
return System.getenv().containsKey(key);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ static void beforeAll() {
try (MockedStatic<SystemWrapper> mocked = mockStatic(SystemWrapper.class)) {
mocked.when(() -> SystemWrapper.getenv("POWERTOOLS_TRACER_CAPTURE_RESPONSE")).thenReturn(null);
mocked.when(() -> SystemWrapper.getenv("POWERTOOLS_TRACER_CAPTURE_ERROR")).thenReturn(null);
mocked.when(() -> SystemWrapper.containsKey("POWERTOOLS_TRACER_CAPTURE_RESPONSE")).thenReturn(false);
mocked.when(() -> SystemWrapper.containsKey("POWERTOOLS_TRACER_CAPTURE_ERROR")).thenReturn(false);
}
}

Expand Down Expand Up @@ -227,6 +229,7 @@ void shouldCaptureTracesWithNoMetadataDeprecated() {
@Test
void shouldNotCaptureTracesIfDisabledViaEnvironmentVariable() {
try (MockedStatic<SystemWrapper> mocked = mockStatic(SystemWrapper.class)) {
mocked.when(() -> SystemWrapper.containsKey("POWERTOOLS_TRACER_CAPTURE_RESPONSE")).thenReturn(true);
mocked.when(() -> SystemWrapper.getenv("POWERTOOLS_TRACER_CAPTURE_RESPONSE")).thenReturn("false");

requestHandler.handleRequest(new Object(), context);
Expand Down Expand Up @@ -269,7 +272,9 @@ void shouldCaptureTracesIfExplicitlyEnabledAndEnvironmentVariableIsDisabled() {
@Test
void shouldCaptureTracesIfExplicitlyEnabledBothAndEnvironmentVariableIsDisabled() {
try (MockedStatic<SystemWrapper> mocked = mockStatic(SystemWrapper.class)) {
mocked.when(() -> SystemWrapper.containsKey("POWERTOOLS_TRACER_CAPTURE_RESPONSE")).thenReturn(true);
mocked.when(() -> SystemWrapper.getenv("POWERTOOLS_TRACER_CAPTURE_RESPONSE")).thenReturn("false");
mocked.when(() -> SystemWrapper.containsKey("POWERTOOLS_TRACER_CAPTURE_ERROR")).thenReturn(true);
mocked.when(() -> SystemWrapper.getenv("POWERTOOLS_TRACER_CAPTURE_ERROR")).thenReturn("false");
requestHandler = new PowerTracerToolEnabledExplicitlyForResponseAndError();

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

Expand Down