Skip to content

chore: Logging and SQS utility Optimisations #484

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
Jul 27, 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 @@ -56,19 +56,19 @@ public static boolean placedOnStreamHandler(final ProceedingJoinPoint pjp) {
&& pjp.getArgs()[2] instanceof Context;
}

public static Optional<Context> extractContext(final ProceedingJoinPoint pjp) {
public static Context extractContext(final ProceedingJoinPoint pjp) {

if (isHandlerMethod(pjp)) {
if (placedOnRequestHandler(pjp)) {
return of((Context) pjp.getArgs()[1]);
return (Context) pjp.getArgs()[1];
}

if (placedOnStreamHandler(pjp)) {
return of((Context) pjp.getArgs()[2]);
return (Context) pjp.getArgs()[2];
}
}

return empty();
return null;
}

public static String serviceName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* {@see Logging}
*/
public final class LoggingUtils {
private static ObjectMapper objectMapper = new ObjectMapper();
private static ObjectMapper objectMapper;

private LoggingUtils() {
}
Expand Down Expand Up @@ -93,6 +93,10 @@ public static void defaultObjectMapper(ObjectMapper objectMapper) {
}

public static ObjectMapper objectMapper() {
if(null == objectMapper) {
objectMapper = new ObjectMapper();
}

return objectMapper;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.Optional;
import java.util.Random;

import com.amazonaws.services.lambda.runtime.Context;
import com.fasterxml.jackson.core.JsonPointer;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
Expand Down Expand Up @@ -85,12 +86,13 @@ public Object around(ProceedingJoinPoint pjp,

setLogLevelBasedOnSamplingRate(pjp, logging);

extractContext(pjp)
.ifPresent(context -> {
appendKeys(DefaultLambdaFields.values(context));
appendKey("coldStart", isColdStart() ? "true" : "false");
appendKey("service", serviceName());
});
Context extractedContext = extractContext(pjp);

if(null != extractedContext) {
appendKeys(DefaultLambdaFields.values(extractedContext));
appendKey("coldStart", isColdStart() ? "true" : "false");
appendKey("service", serviceName());
}

getXrayTraceId().ifPresent(xRayTraceId -> appendKey("xray_trace_id", xRayTraceId));

Expand Down Expand Up @@ -120,9 +122,9 @@ private static void resetLogLevels(Level logLevel) {

private void setLogLevelBasedOnSamplingRate(final ProceedingJoinPoint pjp,
final Logging logging) {
double samplingRate = samplingRate(logging);

if (isHandlerMethod(pjp)) {
float sample = SAMPLER.nextFloat();
double samplingRate = samplingRate(logging);

if (samplingRate < 0 || samplingRate > 1) {
LOG.debug("Skipping sampling rate configuration because of invalid value. Sampling rate: {}", samplingRate);
Expand All @@ -131,6 +133,12 @@ private void setLogLevelBasedOnSamplingRate(final ProceedingJoinPoint pjp,

appendKey("samplingRate", String.valueOf(samplingRate));

if (samplingRate == 0) {
return;
}

float sample = SAMPLER.nextFloat();

if (samplingRate > sample) {
resetLogLevels(Level.DEBUG);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.lang.reflect.Field;

import com.amazonaws.services.lambda.runtime.Context;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
Expand Down Expand Up @@ -53,10 +54,12 @@ public Object around(ProceedingJoinPoint pjp,

logger.setNamespace(namespace(metrics));

extractContext(pjp).ifPresent((context) -> {
coldStartSingleMetricIfApplicable(context.getAwsRequestId(), context.getFunctionName(), metrics);
logger.putProperty(REQUEST_ID_PROPERTY, context.getAwsRequestId());
});
Context extractedContext = extractContext(pjp);

if( null != extractedContext) {
coldStartSingleMetricIfApplicable(extractedContext.getAwsRequestId(), extractedContext.getFunctionName(), metrics);
logger.putProperty(REQUEST_ID_PROPERTY, extractedContext.getAwsRequestId());
}

LambdaHandlerProcessor.getXrayTraceId()
.ifPresent(traceId -> logger.putProperty(TRACE_ID_PROPERTY, traceId));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public final class SqsUtils {
private static final Logger LOG = LoggerFactory.getLogger(SqsUtils.class);

private static final ObjectMapper objectMapper = new ObjectMapper();
private static SqsClient client = SqsClient.create();
private static SqsClient client;

private SqsUtils() {
}
Expand Down Expand Up @@ -231,6 +231,10 @@ public static <R> List<R> batchProcessor(final SQSEvent event,
final SqsMessageHandler<R> handler) {
final List<R> handlerReturn = new ArrayList<>();

if(client == null) {
client = SqsClient.create();
}

BatchContext batchContext = new BatchContext(client);

for (SQSMessage message : event.getRecords()) {
Expand Down