Skip to content

Issue 297: Add AwsRequestId to each EMF log. #301

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 1 commit 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
@@ -1,9 +1,7 @@
package software.amazon.lambda.powertools.metrics.internal;

import java.lang.reflect.Field;
import java.util.Optional;

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 All @@ -25,7 +23,6 @@
import static software.amazon.lambda.powertools.core.internal.LambdaHandlerProcessor.placedOnStreamHandler;
import static software.amazon.lambda.powertools.core.internal.LambdaHandlerProcessor.serviceName;
import static software.amazon.lambda.powertools.metrics.MetricsUtils.metricsLogger;
import static software.amazon.lambda.powertools.metrics.MetricsUtils.withSingleMetric;

@Aspect
public class LambdaMetricsAspect {
Expand All @@ -50,7 +47,10 @@ public Object around(ProceedingJoinPoint pjp,
logger.setNamespace(namespace(metrics))
.putDimensions(DimensionSet.of("Service", service(metrics)));

coldStartSingleMetricIfApplicable(pjp, metrics);
extractContext(pjp).ifPresent((context) -> {
coldStartSingleMetricIfApplicable(context.getAwsRequestId(), context.getFunctionName(), metrics);
logger.putProperty("AwsRequestId", context.getAwsRequestId());
});

try {
return pjp.proceed(proceedArgs);
Expand All @@ -66,20 +66,19 @@ public Object around(ProceedingJoinPoint pjp,
return pjp.proceed(proceedArgs);
}

private void coldStartSingleMetricIfApplicable(final ProceedingJoinPoint pjp,
private void coldStartSingleMetricIfApplicable(final String awsRequestId,
final String functionName,
final Metrics metrics) {
if (metrics.captureColdStart()
&& isColdStart()) {

Optional<Context> contextOptional = extractContext(pjp);

if (contextOptional.isPresent()) {
Context context = contextOptional.orElseThrow(() -> new IllegalStateException("Context not found"));

withSingleMetric("ColdStart", 1, Unit.COUNT, namespace(metrics), (logger) ->
logger.setDimensions(DimensionSet.of("Service", service(metrics), "FunctionName", context.getFunctionName())));
}
MetricsLogger metricsLogger = new MetricsLogger();
metricsLogger.setNamespace(namespace(metrics));
metricsLogger.putMetric("ColdStart", 1, Unit.COUNT);
metricsLogger.setDimensions(DimensionSet.of("Service", service(metrics), "FunctionName", functionName));
metricsLogger.putProperty("AwsRequestId", awsRequestId);
metricsLogger.flush();
}

}

private void validateBeforeFlushingMetrics(Metrics metrics) {
Expand All @@ -103,7 +102,6 @@ private String service(Metrics metrics) {

private void validateMetricsAndRefreshOnFailure(Metrics metrics) {
try {

validateBeforeFlushingMetrics(metrics);
} catch (ValidationException e){
refreshMetricsContext();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public void metricsWithoutColdStart() {
assertThat(logAsJson)
.containsEntry("Metric1", 1.0)
.containsEntry("Service", "booking")
.containsEntry("AwsRequestId", "123ABC")
.containsKey("_aws");
});
}
Expand All @@ -105,6 +106,7 @@ public void metricsWithColdStart() {
.doesNotContainKey("Metric1")
.containsEntry("ColdStart", 1.0)
.containsEntry("Service", "booking")
.containsEntry("AwsRequestId", "123ABC")
.containsKey("_aws");

logAsJson = readAsJson(s[1]);
Expand All @@ -113,6 +115,7 @@ public void metricsWithColdStart() {
.doesNotContainKey("ColdStart")
.containsEntry("Metric1", 1.0)
.containsEntry("Service", "booking")
.containsEntry("AwsRequestId", "123ABC")
.containsKey("_aws");
});
}
Expand All @@ -136,6 +139,7 @@ public void noColdStartMetricsWhenColdStartDone() {
.doesNotContainKey("Metric1")
.containsEntry("ColdStart", 1.0)
.containsEntry("Service", "booking")
.containsEntry("AwsRequestId", "123ABC")
.containsKey("_aws");

logAsJson = readAsJson(s[1]);
Expand All @@ -144,6 +148,7 @@ public void noColdStartMetricsWhenColdStartDone() {
.doesNotContainKey("ColdStart")
.containsEntry("Metric1", 1.0)
.containsEntry("Service", "booking")
.containsEntry("AwsRequestId", "123ABC")
.containsKey("_aws");

logAsJson = readAsJson(s[2]);
Expand All @@ -152,6 +157,7 @@ public void noColdStartMetricsWhenColdStartDone() {
.doesNotContainKey("ColdStart")
.containsEntry("Metric1", 1.0)
.containsEntry("Service", "booking")
.containsEntry("AwsRequestId", "123ABC")
.containsKey("_aws");
});
}
Expand All @@ -173,6 +179,7 @@ public void metricsWithStreamHandler() throws IOException {
assertThat(logAsJson)
.containsEntry("Metric1", 1.0)
.containsEntry("Service", "booking")
.containsEntry("AwsRequestId", "123ABC")
.containsKey("_aws");
});
}
Expand Down Expand Up @@ -249,6 +256,7 @@ public void metricsPublishedEvenHandlerThrowsException() {
assertThat(logAsJson)
.containsEntry("CoolMetric", 1.0)
.containsEntry("Service", "booking")
.containsEntry("AwsRequestId", "123ABC")
.containsKey("_aws");
});
}
Expand All @@ -259,6 +267,7 @@ private void setupContext() {
when(context.getInvokedFunctionArn()).thenReturn("testArn");
when(context.getFunctionVersion()).thenReturn("1");
when(context.getMemoryLimitInMB()).thenReturn(10);
when(context.getAwsRequestId()).thenReturn("123ABC");
}

private Map<String, Object> readAsJson(String s) {
Expand Down