You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This issue was first raised in #764. When compiling a native image in Quarkus using the GraalVM Metadata files introduced as of #1753 CloudWatch EMF does not understand that it is running in a Lambda environment. It tries to ping a CloudWatch agent on localhost which breaks in Lambda.
This happens irrespective of setting environment variables at compile time and/or build time to indicated to the CloudWatch EMF library that it is running in a Lambda environment.
What were you trying to accomplish?
Running Powertools Metrics in Lambda when building with quarkus build --native.
@Named("test")
publicclassTestLambdaimplementsRequestHandler<InputObject, OutputObject> {
@InjectProcessingServiceservice;
privatefinalstaticLoggerlogger = Logger.getLogger(TestLambda.class);
privatefinalstaticMetricsLoggermetricsLogger = MetricsUtils.metricsLogger();
@Override@Metrics(namespace = "QuarkusApp", service = "powertools")
publicOutputObjecthandleRequest(InputObjectinput, Contextcontext) {
logger.info("Quarkus Jboss Log."); // Logging with Jboss is not yet supported by PowertoolslongstartTime = System.currentTimeMillis();
finalOutputObjectoutput = service.process(input).setRequestId(context.getAwsRequestId());
metricsLogger.putMetric("ExecutionTime", System.currentTimeMillis() - startTime, Unit.MILLISECONDS);
metricsLogger.putMetric("Invocation", 1, Unit.COUNT);
returnoutput;
}
}
When I compile this using Java version: 21.0.6+8-LTS, vendor version: Oracle GraalVM 21.0.6+8.1 it will raise an error at runtime coming from CloudWatch EMF. I compile on Amazon Linux 2 using quarkus build --native:
The CloudWatch EMF dependency seems to think we are running in an agent-based environment and tries to connect to a CloudWatch agent endpoint on localhost. If I instruct the MetricsLogger manually to use the LambdaEnvironment the error is gone and the metrics are emitted as expected when deployed as native image.
--- a/powertools-metrics/src/main/java/software/amazon/lambda/powertools/metrics/MetricsUtils.java+++ b/powertools-metrics/src/main/java/software/amazon/lambda/powertools/metrics/MetricsUtils.java@@ -22,8 +22,10 @@ import static software.amazon.lambda.powertools.metrics.internal.LambdaMetricsAs
import java.util.Arrays;
import java.util.Optional;
import java.util.function.Consumer;
+
import software.amazon.cloudwatchlogs.emf.config.SystemWrapper;
import software.amazon.cloudwatchlogs.emf.environment.EnvironmentProvider;
+import software.amazon.cloudwatchlogs.emf.environment.LambdaEnvironment;
import software.amazon.cloudwatchlogs.emf.logger.MetricsLogger;
import software.amazon.cloudwatchlogs.emf.model.DimensionSet;
import software.amazon.cloudwatchlogs.emf.model.MetricsContext;
@@ -37,7 +39,7 @@ import software.amazon.cloudwatchlogs.emf.model.Unit;
* {@see Metrics}
*/
public final class MetricsUtils {
- private static final MetricsLogger metricsLogger = new MetricsLogger();+ private static final MetricsLogger metricsLogger = new MetricsLogger(new LambdaEnvironment());
private static DimensionSet[] defaultDimensions;
private MetricsUtils() {
Setting the environment variables at both at runtime and at compile time does not change this behavior. Only overwriting the environment directly in the code seems to resolve this issue when using quarkus build --native.
phipag
changed the title
Powertools Metrics CloudWatch EMF does not work with Quarkus native image
Powertools Metrics CloudWatch EMF dependency does not work with Quarkus native image
Mar 14, 2025
This issue was first raised in #764. When compiling a native image in Quarkus using the GraalVM Metadata files introduced as of #1753 CloudWatch EMF does not understand that it is running in a Lambda environment. It tries to ping a CloudWatch agent on
localhost
which breaks in Lambda.This happens irrespective of setting environment variables at compile time and/or build time to indicated to the CloudWatch EMF library that it is running in a Lambda environment.
What were you trying to accomplish?
Running Powertools Metrics in Lambda when building with
quarkus build --native
.Current Behavior
For reference, this is my test Lambda handler following the Quarkus AWS Lambda tutorial here https://quarkus.io/guides/aws-lambda:
When I compile this using
Java version: 21.0.6+8-LTS, vendor version: Oracle GraalVM 21.0.6+8.1
it will raise an error at runtime coming from CloudWatch EMF. I compile on Amazon Linux 2 usingquarkus build --native
:Possible solution and expected behavior
The CloudWatch EMF dependency seems to think we are running in an agent-based environment and tries to connect to a CloudWatch agent endpoint on localhost. If I instruct the
MetricsLogger
manually to use theLambdaEnvironment
the error is gone and the metrics are emitted as expected when deployed as native image.Output:
Setting the environment variables at both at runtime and at compile time does not change this behavior. Only overwriting the environment directly in the code seems to resolve this issue when using
quarkus build --native
.Environment
2.0.0-SNAPSHOT
provided.al2023
The text was updated successfully, but these errors were encountered: