Skip to content

NullPointerException thrown by instrumented metrics code in unit tests. #2019

@slavaschmidt

Description

@slavaschmidt

Version used: 2.2.1

There seem to be an issue with using @FlushMetrics annotation that leads to the NullPointerException during automated testing.

Our project is managed with maven.

This is how out test is implemented:

    @Test
    void Can_handle_request() {
        final APIGatewayProxyRequestEvent apiGatewayProxyRequestEvent = mock(APIGatewayProxyRequestEvent.class);
        final Context context = mock(Context.class);
        given(context.getAwsRequestId()).willReturn("request-id");

        final APIGatewayProxyResponseEvent actual = requestHandler.handleRequest(apiGatewayProxyRequestEvent, context);
        // assertions

    }

and the implementation:

    @Override
    @FlushMetrics(namespace = "namespace", service = "service", captureColdStart = true)
    public APIGatewayProxyResponseEvent handleRequest(
        final APIGatewayProxyRequestEvent apiGatewayProxyRequestEvent,
        final Context context
    ) {
// actual implementation
}

We pass environment variables to disable metrics as in the following (pom.xml):

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <environmentVariables>
            <POWERTOOLS_SERVICE_NAME>test</POWERTOOLS_SERVICE_NAME>
            <POWERTOOLS_METRICS_DISABLED>true</POWERTOOLS_METRICS_DISABLED>
            <POWERTOOLS_METRICS_NAMESPACE>test</POWERTOOLS_METRICS_NAMESPACE>
            <POWERTOOLS_METRICS_FUNCTION_NAME>test</POWERTOOLS_METRICS_FUNCTION_NAME>
            <POWERTOOLS_DISABLE_METRICS>true</POWERTOOLS_DISABLE_METRICS>
        </environmentVariables>
    </configuration>
</plugin>

Still, when tests are executed a NullPointerException is thrown by the instrumented code.

java.lang.NullPointerException
        at java.base/java.util.concurrent.ConcurrentHashMap.putVal(ConcurrentHashMap.java:1011)
        at java.base/java.util.concurrent.ConcurrentHashMap.put(ConcurrentHashMap.java:1006)
        at software.amazon.cloudwatchlogs.emf.model.Metadata.putCustomMetadata(Metadata.java:89)
        at software.amazon.cloudwatchlogs.emf.model.MetricsContext.putMetadata(MetricsContext.java:273)
        at software.amazon.cloudwatchlogs.emf.logger.MetricsLogger.lambda$putMetadata$5(MetricsLogger.java:277)
        at software.amazon.cloudwatchlogs.emf.logger.MetricsLogger.applyReadLock(MetricsLogger.java:328)
        at software.amazon.cloudwatchlogs.emf.logger.MetricsLogger.putMetadata(MetricsLogger.java:275)
        at software.amazon.lambda.powertools.metrics.internal.EmfMetricsLogger.addMetadata(EmfMetricsLogger.java:94)
        at software.amazon.lambda.powertools.metrics.internal.LambdaMetricsAspect.captureColdStartMetricIfEnabled(LambdaMetricsAspect.java:116)
        at software.amazon.lambda.powertools.metrics.internal.LambdaMetricsAspect.around(LambdaMetricsAspect.java:97)
        at com.acme.RequestHandler.handleRequest(RequestHandler.java:67)
        at com.acme.RequestHandlerTest.Can_handle_request(RequestHandlerTest.java:116)
        at java.base/java.lang.reflect.Method.invoke(Method.java:580)
        at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
        at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)

Metadata

Metadata

Assignees

Labels

documentationImprovements or additions to documentationmetrics

Type

No type

Projects

Status

Working on it

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions