From c52551049d29e8fa4f4f999332af8f1f3b456108 Mon Sep 17 00:00:00 2001 From: Philipp Page Date: Thu, 7 Aug 2025 16:09:12 +0200 Subject: [PATCH 1/4] Add Bazel example. --- .../sam-bazel/.bazelrc | 27 +++++ .../sam-bazel/.gitignore | 20 ++++ .../sam-bazel/BUILD.bazel | 74 ++++++++++++ .../sam-bazel/MODULE.bazel | 27 +++++ .../sam-bazel/README.md | 63 ++++++++++ .../sam-bazel/events/event.json | 63 ++++++++++ .../src/main/java/helloworld/App.java | 113 ++++++++++++++++++ .../src/main/java/helloworld/AppStream.java | 58 +++++++++ .../sam-bazel/src/main/resources/log4j2.xml | 16 +++ .../sam-bazel/template.yaml | 66 ++++++++++ 10 files changed, 527 insertions(+) create mode 100644 examples/powertools-examples-core-utilities/sam-bazel/.bazelrc create mode 100644 examples/powertools-examples-core-utilities/sam-bazel/.gitignore create mode 100644 examples/powertools-examples-core-utilities/sam-bazel/BUILD.bazel create mode 100644 examples/powertools-examples-core-utilities/sam-bazel/MODULE.bazel create mode 100644 examples/powertools-examples-core-utilities/sam-bazel/README.md create mode 100644 examples/powertools-examples-core-utilities/sam-bazel/events/event.json create mode 100644 examples/powertools-examples-core-utilities/sam-bazel/src/main/java/helloworld/App.java create mode 100644 examples/powertools-examples-core-utilities/sam-bazel/src/main/java/helloworld/AppStream.java create mode 100644 examples/powertools-examples-core-utilities/sam-bazel/src/main/resources/log4j2.xml create mode 100644 examples/powertools-examples-core-utilities/sam-bazel/template.yaml diff --git a/examples/powertools-examples-core-utilities/sam-bazel/.bazelrc b/examples/powertools-examples-core-utilities/sam-bazel/.bazelrc new file mode 100644 index 000000000..9c723b23e --- /dev/null +++ b/examples/powertools-examples-core-utilities/sam-bazel/.bazelrc @@ -0,0 +1,27 @@ +# Java compilation settings +build --java_language_version=11 +build --java_runtime_version=11 + +# Enable worker strategy for better performance +build --strategy=Javac=worker +build --worker_sandboxing + +# Modern Java build optimizations +build --experimental_strict_java_deps=off +build --incompatible_java_common_parameters=false + +# Performance optimizations +build --experimental_reuse_sandbox_directories +build --worker_multiplex + +# Disk cache for better performance +build --disk_cache=~/.cache/bazel-disk-cache + +# Remote cache configuration (optional) +# build --remote_cache=grpc://localhost:9092 + +# Build event stream for better observability +# build --build_event_json_file=build_events.json + +# Strict visibility for better dependency management +build --incompatible_enforce_config_setting_visibility=false diff --git a/examples/powertools-examples-core-utilities/sam-bazel/.gitignore b/examples/powertools-examples-core-utilities/sam-bazel/.gitignore new file mode 100644 index 000000000..4975e987e --- /dev/null +++ b/examples/powertools-examples-core-utilities/sam-bazel/.gitignore @@ -0,0 +1,20 @@ +# Bazel build outputs +bazel-* +MODULE.bazel.lock + +# SAM build outputs +.aws-sam/ + +# JAR files +*.jar + +# Maven lock file (generated) +maven_install.json + +# IDE files +.idea/ +.vscode/ +*.iml + +# OS files +.DS_Store diff --git a/examples/powertools-examples-core-utilities/sam-bazel/BUILD.bazel b/examples/powertools-examples-core-utilities/sam-bazel/BUILD.bazel new file mode 100644 index 000000000..f2b3b26b3 --- /dev/null +++ b/examples/powertools-examples-core-utilities/sam-bazel/BUILD.bazel @@ -0,0 +1,74 @@ +load("@rules_java//java:defs.bzl", "java_binary", "java_library") + +java_library( + name = "powertools_sam_lib_base", + srcs = glob(["src/main/java/**/*.java"]), + resources = glob(["src/main/resources/**"]), + deps = [ + "@maven//:software_amazon_lambda_powertools_tracing", + "@maven//:software_amazon_lambda_powertools_logging_log4j", + "@maven//:software_amazon_lambda_powertools_metrics", + "@maven//:com_amazonaws_aws_lambda_java_core", + "@maven//:com_amazonaws_aws_lambda_java_events", + "@maven//:org_aspectj_aspectjrt", + ], +) + +genrule( + name = "aspectj_weave", + srcs = [ + ":powertools_sam_lib_base", + "@maven//:software_amazon_lambda_powertools_tracing", + "@maven//:software_amazon_lambda_powertools_logging", + "@maven//:software_amazon_lambda_powertools_metrics", + "@maven//:com_amazonaws_aws_lambda_java_core", + "@maven//:com_amazonaws_aws_lambda_java_events", + "@maven//:org_aspectj_aspectjrt", + ], + outs = ["powertools_sam_lib_woven.jar"], + tools = [ + "@maven//:org_aspectj_aspectjweaver", + "@maven//:org_aspectj_aspectjtools", + ], + cmd = """ + # Get dependency JARs for classpath + ASPECTJ_TOOLS="$$(echo $(locations @maven//:org_aspectj_aspectjtools) $(locations @maven//:org_aspectj_aspectjweaver) | tr ' ' ':')" + ASPECTJ_RT="$(locations @maven//:org_aspectj_aspectjrt)" + LAMBDA_JARS="$$(echo $(locations @maven//:com_amazonaws_aws_lambda_java_core) $(locations @maven//:com_amazonaws_aws_lambda_java_events) | tr ' ' ':')" + ALL_DEPS="$$ASPECTJ_TOOLS:$$ASPECTJ_RT:$$LAMBDA_JARS" + + BASE_JAR=$$(echo $(locations :powertools_sam_lib_base) | cut -d' ' -f1) + POWERTOOLS_JARS="$$(echo $(locations @maven//:software_amazon_lambda_powertools_tracing) $(locations @maven//:software_amazon_lambda_powertools_logging) $(locations @maven//:software_amazon_lambda_powertools_metrics) | tr ' ' ':')" + + java -cp "$$ALL_DEPS" \ + org.aspectj.tools.ajc.Main \ + -inpath "$$BASE_JAR" \ + -aspectpath "$$POWERTOOLS_JARS" \ + -outjar $(location powertools_sam_lib_woven.jar) \ + -source 11 -target 11 -nowarn + """, +) + +java_import( + name = "powertools_sam_lib", + jars = [":powertools_sam_lib_woven.jar"], + deps = [ + "@maven//:software_amazon_lambda_powertools_tracing", + "@maven//:software_amazon_lambda_powertools_logging", + "@maven//:software_amazon_lambda_powertools_logging_log4j", + "@maven//:software_amazon_lambda_powertools_metrics", + "@maven//:com_amazonaws_aws_lambda_java_core", + "@maven//:com_amazonaws_aws_lambda_java_events", + "@maven//:org_aspectj_aspectjrt", + ], +) + +java_binary( + name = "powertools_sam_deploy", + main_class = "helloworld.App", + runtime_deps = [":powertools_sam_lib"], + deploy_manifest_lines = [ + "Main-Class: helloworld.App", + ], + create_executable = False, +) diff --git a/examples/powertools-examples-core-utilities/sam-bazel/MODULE.bazel b/examples/powertools-examples-core-utilities/sam-bazel/MODULE.bazel new file mode 100644 index 000000000..704b0cf1a --- /dev/null +++ b/examples/powertools-examples-core-utilities/sam-bazel/MODULE.bazel @@ -0,0 +1,27 @@ +module(name = "powertools_sam_bazel", version = "1.0.0") + +bazel_dep(name = "rules_java", version = "8.12.0") +bazel_dep(name = "rules_jvm_external", version = "6.3") + +maven = use_extension("@rules_jvm_external//:extensions.bzl", "maven") + +maven.install( + artifacts = [ + "software.amazon.lambda:powertools-tracing:2.2.1", + "software.amazon.lambda:powertools-logging:2.2.1", + "software.amazon.lambda:powertools-logging-log4j:2.2.1", + "software.amazon.lambda:powertools-metrics:2.2.1", + "com.amazonaws:aws-lambda-java-core:1.3.0", + "com.amazonaws:aws-lambda-java-events:3.16.1", + "org.aspectj:aspectjrt:1.9.22", + "org.aspectj:aspectjweaver:1.9.22", + "org.aspectj:aspectjtools:1.9.22", + "org.slf4j:slf4j-api:2.0.16", + ], + repositories = [ + "https://repo1.maven.org/maven2", + ], + lock_file = "//:maven_install.json", +) + +use_repo(maven, "maven") diff --git a/examples/powertools-examples-core-utilities/sam-bazel/README.md b/examples/powertools-examples-core-utilities/sam-bazel/README.md new file mode 100644 index 000000000..8fd94e4c1 --- /dev/null +++ b/examples/powertools-examples-core-utilities/sam-bazel/README.md @@ -0,0 +1,63 @@ +# Powertools for AWS Lambda (Java) - Core Utilities Example with Bazel + +This project demonstrates the Lambda for Powertools Java module deployed using [Serverless Application Model](https://aws.amazon.com/serverless/sam/) and built with Bazel. + +For general information on the deployed example itself, you can refer to the parent [README](../README.md) + +## Configuration + +SAM uses [template.yaml](template.yaml) to define the application's AWS resources. This file defines the Lambda function to be deployed as well as API Gateway for it. + +## Deploy the sample application + +To deploy the example, check out the instructions for getting started with SAM in [the examples directory](../../README.md) + +## Build and deploy + +```bash +# Build the application +bazel build //:powertools_sam_deploy_deploy.jar + +# Deploy the application +sam deploy --guided +``` + +## Local testing + +```bash +# Build the application +bazel build //:powertools_sam_deploy_deploy.jar + +# Test a single function locally +sam local invoke HelloWorldFunction --event events/event.json + +# Start the local API +sam local start-api + +# Test the API endpoints +curl http://127.0.0.1:3000/hello +curl http://127.0.0.1:3000/hellostream +``` + +## Additional notes + +You can watch the trace information or log information using the SAM CLI: + +```bash +# Tail the logs +sam logs --tail $MY_STACK + +# Tail the traces +sam traces --tail +``` + +### Pinning Maven versions + +To ensure reproducible builds, you can pin Maven dependency versions: + +```bash +# Generate lock file for reproducible builds +bazel run @maven//:pin +``` + +This creates `maven_install.json` which locks dependency versions and should be committed to version control. diff --git a/examples/powertools-examples-core-utilities/sam-bazel/events/event.json b/examples/powertools-examples-core-utilities/sam-bazel/events/event.json new file mode 100644 index 000000000..3822fadaa --- /dev/null +++ b/examples/powertools-examples-core-utilities/sam-bazel/events/event.json @@ -0,0 +1,63 @@ +{ + "body": "{\"message\": \"hello world\"}", + "resource": "/{proxy+}", + "path": "/path/to/resource", + "httpMethod": "POST", + "isBase64Encoded": false, + "queryStringParameters": { + "foo": "bar" + }, + "pathParameters": { + "proxy": "/path/to/resource" + }, + "stageVariables": { + "baz": "qux" + }, + "headers": { + "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", + "Accept-Encoding": "gzip, deflate, sdch", + "Accept-Language": "en-US,en;q=0.8", + "Cache-Control": "max-age=0", + "CloudFront-Forwarded-Proto": "https", + "CloudFront-Is-Desktop-Viewer": "true", + "CloudFront-Is-Mobile-Viewer": "false", + "CloudFront-Is-SmartTV-Viewer": "false", + "CloudFront-Is-Tablet-Viewer": "false", + "CloudFront-Viewer-Country": "US", + "Host": "1234567890.execute-api.us-east-1.amazonaws.com", + "Upgrade-Insecure-Requests": "1", + "User-Agent": "Custom User Agent String", + "Via": "1.1 08f323deadbeefa7af34d5feb414ce27.cloudfront.net (CloudFront)", + "X-Amz-Cf-Id": "cDehVQoZnx43VYQb9j2-nvCh-9z396Uhbp027Y2JvkCPNLmGJHqlaA==", + "X-Forwarded-For": "127.0.0.1, 127.0.0.2", + "X-Forwarded-Port": "443", + "X-Forwarded-Proto": "https" + }, + "requestContext": { + "accountId": "123456789012", + "resourceId": "123456", + "stage": "prod", + "requestId": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef", + "requestTime": "09/Apr/2015:12:34:56 +0000", + "requestTimeEpoch": 1428582896000, + "identity": { + "cognitoIdentityPoolId": null, + "accountId": null, + "cognitoIdentityId": null, + "caller": null, + "accessKey": null, + "sourceIp": "127.0.0.1", + "cognitoAuthenticationType": null, + "cognitoAuthenticationProvider": null, + "userArn": null, + "userAgent": "Custom User Agent String", + "user": null + }, + "path": "/prod/path/to/resource", + "resourcePath": "/{proxy+}", + "httpMethod": "POST", + "apiId": "1234567890", + "protocol": "HTTP/1.1" + } + } + \ No newline at end of file diff --git a/examples/powertools-examples-core-utilities/sam-bazel/src/main/java/helloworld/App.java b/examples/powertools-examples-core-utilities/sam-bazel/src/main/java/helloworld/App.java new file mode 100644 index 000000000..1f7d88cbb --- /dev/null +++ b/examples/powertools-examples-core-utilities/sam-bazel/src/main/java/helloworld/App.java @@ -0,0 +1,113 @@ +/* + * Copyright 2023 Amazon.com, Inc. or its affiliates. + * Licensed under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package helloworld; + +import static software.amazon.lambda.powertools.logging.argument.StructuredArguments.entry; +import static software.amazon.lambda.powertools.tracing.TracingUtils.putMetadata; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.URL; +import java.util.HashMap; +import java.util.Map; +import java.util.stream.Collectors; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.slf4j.MDC; + +import com.amazonaws.services.lambda.runtime.Context; +import com.amazonaws.services.lambda.runtime.RequestHandler; +import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent; +import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent; + +import software.amazon.lambda.powertools.logging.Logging; +import software.amazon.lambda.powertools.metrics.FlushMetrics; +import software.amazon.lambda.powertools.metrics.Metrics; +import software.amazon.lambda.powertools.metrics.MetricsFactory; +import software.amazon.lambda.powertools.metrics.model.DimensionSet; +import software.amazon.lambda.powertools.metrics.model.MetricResolution; +import software.amazon.lambda.powertools.metrics.model.MetricUnit; +import software.amazon.lambda.powertools.tracing.CaptureMode; +import software.amazon.lambda.powertools.tracing.Tracing; +import software.amazon.lambda.powertools.tracing.TracingUtils; + +/** + * Handler for requests to Lambda function. + */ +public class App implements RequestHandler { + private static final Logger log = LoggerFactory.getLogger(App.class); + private static final Metrics metrics = MetricsFactory.getMetricsInstance(); + + @Logging(logEvent = true, samplingRate = 0.7) + @Tracing(captureMode = CaptureMode.RESPONSE_AND_ERROR) + @FlushMetrics(namespace = "ServerlessAirline", service = "payment", captureColdStart = true) + public APIGatewayProxyResponseEvent handleRequest(final APIGatewayProxyRequestEvent input, final Context context) { + Map headers = new HashMap<>(); + + headers.put("Content-Type", "application/json"); + headers.put("X-Custom-Header", "application/json"); + + metrics.addMetric("CustomMetric1", 1, MetricUnit.COUNT); + + DimensionSet dimensionSet = new DimensionSet(); + dimensionSet.addDimension("AnotherService", "CustomService"); + dimensionSet.addDimension("AnotherService1", "CustomService1"); + metrics.flushSingleMetric("CustomMetric2", 1, MetricUnit.COUNT, "Another", dimensionSet); + + metrics.addMetric("CustomMetric3", 1, MetricUnit.COUNT, MetricResolution.HIGH); + + MDC.put("test", "willBeLogged"); + + APIGatewayProxyResponseEvent response = new APIGatewayProxyResponseEvent() + .withHeaders(headers); + try { + final String pageContents = this.getPageContents("https://checkip.amazonaws.com"); + log.info("", entry("ip", pageContents)); + TracingUtils.putAnnotation("Test", "New"); + String output = String.format("{ \"message\": \"hello world\", \"location\": \"%s\" }", pageContents); + + TracingUtils.withSubsegment("loggingResponse", subsegment -> { + String sampled = "log something out"; + log.info(sampled); + log.info(output); + }); + + log.info("After output"); + return response + .withStatusCode(200) + .withBody(output); + } catch (RuntimeException | IOException e) { + return response + .withBody("{}") + .withStatusCode(500); + } + } + + @Tracing + private void log() { + log.info("inside threaded logging for function"); + } + + @Tracing(namespace = "getPageContents", captureMode = CaptureMode.DISABLED) + private String getPageContents(String address) throws IOException { + URL url = new URL(address); + putMetadata("getPageContents", address); + try (BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()))) { + return br.lines().collect(Collectors.joining(System.lineSeparator())); + } + } +} \ No newline at end of file diff --git a/examples/powertools-examples-core-utilities/sam-bazel/src/main/java/helloworld/AppStream.java b/examples/powertools-examples-core-utilities/sam-bazel/src/main/java/helloworld/AppStream.java new file mode 100644 index 000000000..e04641ddc --- /dev/null +++ b/examples/powertools-examples-core-utilities/sam-bazel/src/main/java/helloworld/AppStream.java @@ -0,0 +1,58 @@ +/* + * Copyright 2023 Amazon.com, Inc. or its affiliates. + * Licensed under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package helloworld; + +import com.amazonaws.services.lambda.runtime.Context; +import com.amazonaws.services.lambda.runtime.RequestStreamHandler; +import com.fasterxml.jackson.databind.ObjectMapper; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.nio.charset.StandardCharsets; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import software.amazon.lambda.powertools.logging.Logging; +import software.amazon.lambda.powertools.metrics.FlushMetrics; + +import java.io.InputStreamReader; +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.OutputStreamWriter; +import java.io.PrintWriter; + +public class AppStream implements RequestStreamHandler { + private static final ObjectMapper mapper = new ObjectMapper(); + private final static Logger log = LogManager.getLogger(AppStream.class); + + @Override + @Logging(logEvent = true) + @FlushMetrics(namespace = "ServerlessAirline", service = "payment", captureColdStart = true) + // RequestStreamHandler can be used instead of RequestHandler for cases when you'd like to deserialize request body or serialize response body yourself, instead of allowing that to happen automatically + // Note that you still need to return a proper JSON for API Gateway to handle + // See Lambda Response format for examples: https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html + public void handleRequest(InputStream input, OutputStream output, Context context) { + try (BufferedReader reader = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8)); + PrintWriter writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(output, StandardCharsets.UTF_8)))) { + + log.info("Received: " + mapper.writerWithDefaultPrettyPrinter().writeValueAsString(mapper.readTree(reader))); + + writer.write("{\"body\": \"" + System.currentTimeMillis() + "\"} "); + } catch (IOException e) { + log.error("Something has gone wrong: ", e); + } + } +} \ No newline at end of file diff --git a/examples/powertools-examples-core-utilities/sam-bazel/src/main/resources/log4j2.xml b/examples/powertools-examples-core-utilities/sam-bazel/src/main/resources/log4j2.xml new file mode 100644 index 000000000..e1fd14cea --- /dev/null +++ b/examples/powertools-examples-core-utilities/sam-bazel/src/main/resources/log4j2.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/examples/powertools-examples-core-utilities/sam-bazel/template.yaml b/examples/powertools-examples-core-utilities/sam-bazel/template.yaml new file mode 100644 index 000000000..92ee87c8a --- /dev/null +++ b/examples/powertools-examples-core-utilities/sam-bazel/template.yaml @@ -0,0 +1,66 @@ +AWSTemplateFormatVersion: "2010-09-09" +Transform: AWS::Serverless-2016-10-31 +Description: > + CoreUtilities + +# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst +Globals: + Function: + Timeout: 20 + Runtime: java11 + MemorySize: 512 + Tracing: Active # https://docs.aws.amazon.com/lambda/latest/dg/lambda-x-ray.html + Environment: + Variables: + # Powertools for AWS Lambda (Java) env vars: https://docs.powertools.aws.dev/lambda/java/#environment-variables + POWERTOOLS_LOG_LEVEL: INFO + POWERTOOLS_LOGGER_SAMPLE_RATE: 0.1 + POWERTOOLS_LOGGER_LOG_EVENT: true + POWERTOOLS_METRICS_NAMESPACE: Coreutilities + +Resources: + HelloWorldFunction: + Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction + Properties: + CodeUri: bazel-bin/powertools_sam_deploy_deploy.jar + Handler: helloworld.App::handleRequest + Environment: # More info about Env Vars: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#environment-object + Variables: + POWERTOOLS_SERVICE_NAME: hello + Events: + HelloWorld: + Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api + Properties: + Path: /hello + Method: get + + HelloWorldStreamFunction: + Type: AWS::Serverless::Function + Properties: + CodeUri: bazel-bin/powertools_sam_deploy_deploy.jar + Handler: helloworld.AppStream::handleRequest + MemorySize: 512 + Tracing: Active + Environment: + Variables: + POWERTOOLS_LOGGER_SAMPLE_RATE: 0.7 + Events: + HelloWorld: + Type: Api + Properties: + Path: /hellostream + Method: get + +Outputs: + # ServerlessRestApi is an implicit API created out of Events key under Serverless::Function + # Find out more about other implicit resources you can reference within SAM + # https://github.com/awslabs/serverless-application-model/blob/master/docs/internals/generated_resources.rst#api + HelloWorldApi: + Description: "API Gateway endpoint URL for Prod stage for Hello World function" + Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/" + HelloWorldFunction: + Description: "Hello World Lambda Function ARN" + Value: !GetAtt HelloWorldFunction.Arn + HelloWorldFunctionIamRole: + Description: "Implicit IAM Role created for Hello World function" + Value: !GetAtt HelloWorldFunctionRole.Arn From c4282945b738bdc3fbd77f854e5c2a020a73fe3f Mon Sep 17 00:00:00 2001 From: Philipp Page Date: Thu, 7 Aug 2025 16:50:40 +0200 Subject: [PATCH 2/4] Remove double _deploy suffix. --- .../powertools-examples-core-utilities/sam-bazel/BUILD.bazel | 2 +- .../powertools-examples-core-utilities/sam-bazel/README.md | 4 ++-- .../sam-bazel/template.yaml | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/examples/powertools-examples-core-utilities/sam-bazel/BUILD.bazel b/examples/powertools-examples-core-utilities/sam-bazel/BUILD.bazel index f2b3b26b3..7b888e62a 100644 --- a/examples/powertools-examples-core-utilities/sam-bazel/BUILD.bazel +++ b/examples/powertools-examples-core-utilities/sam-bazel/BUILD.bazel @@ -64,7 +64,7 @@ java_import( ) java_binary( - name = "powertools_sam_deploy", + name = "powertools_sam", main_class = "helloworld.App", runtime_deps = [":powertools_sam_lib"], deploy_manifest_lines = [ diff --git a/examples/powertools-examples-core-utilities/sam-bazel/README.md b/examples/powertools-examples-core-utilities/sam-bazel/README.md index 8fd94e4c1..a18af2dd5 100644 --- a/examples/powertools-examples-core-utilities/sam-bazel/README.md +++ b/examples/powertools-examples-core-utilities/sam-bazel/README.md @@ -16,7 +16,7 @@ To deploy the example, check out the instructions for getting started with SAM i ```bash # Build the application -bazel build //:powertools_sam_deploy_deploy.jar +bazel build //:powertools_sam_deploy.jar # Deploy the application sam deploy --guided @@ -26,7 +26,7 @@ sam deploy --guided ```bash # Build the application -bazel build //:powertools_sam_deploy_deploy.jar +bazel build //:powertools_sam_deploy.jar # Test a single function locally sam local invoke HelloWorldFunction --event events/event.json diff --git a/examples/powertools-examples-core-utilities/sam-bazel/template.yaml b/examples/powertools-examples-core-utilities/sam-bazel/template.yaml index 92ee87c8a..1e489b8eb 100644 --- a/examples/powertools-examples-core-utilities/sam-bazel/template.yaml +++ b/examples/powertools-examples-core-utilities/sam-bazel/template.yaml @@ -22,7 +22,7 @@ Resources: HelloWorldFunction: Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction Properties: - CodeUri: bazel-bin/powertools_sam_deploy_deploy.jar + CodeUri: bazel-bin/powertools_sam_deploy.jar Handler: helloworld.App::handleRequest Environment: # More info about Env Vars: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#environment-object Variables: @@ -37,7 +37,7 @@ Resources: HelloWorldStreamFunction: Type: AWS::Serverless::Function Properties: - CodeUri: bazel-bin/powertools_sam_deploy_deploy.jar + CodeUri: bazel-bin/powertools_sam_deploy.jar Handler: helloworld.AppStream::handleRequest MemorySize: 512 Tracing: Active From 212adc0caf502e41b55ed7798d18c5d86b5d92d3 Mon Sep 17 00:00:00 2001 From: Philipp Page Date: Thu, 7 Aug 2025 16:56:50 +0200 Subject: [PATCH 3/4] Use default .bazelrc setting and re-enable strict dependency checks. --- .../sam-bazel/.bazelrc | 24 ------------------- .../sam-bazel/BUILD.bazel | 4 ++++ 2 files changed, 4 insertions(+), 24 deletions(-) diff --git a/examples/powertools-examples-core-utilities/sam-bazel/.bazelrc b/examples/powertools-examples-core-utilities/sam-bazel/.bazelrc index 9c723b23e..6cdf014a1 100644 --- a/examples/powertools-examples-core-utilities/sam-bazel/.bazelrc +++ b/examples/powertools-examples-core-utilities/sam-bazel/.bazelrc @@ -1,27 +1,3 @@ # Java compilation settings build --java_language_version=11 build --java_runtime_version=11 - -# Enable worker strategy for better performance -build --strategy=Javac=worker -build --worker_sandboxing - -# Modern Java build optimizations -build --experimental_strict_java_deps=off -build --incompatible_java_common_parameters=false - -# Performance optimizations -build --experimental_reuse_sandbox_directories -build --worker_multiplex - -# Disk cache for better performance -build --disk_cache=~/.cache/bazel-disk-cache - -# Remote cache configuration (optional) -# build --remote_cache=grpc://localhost:9092 - -# Build event stream for better observability -# build --build_event_json_file=build_events.json - -# Strict visibility for better dependency management -build --incompatible_enforce_config_setting_visibility=false diff --git a/examples/powertools-examples-core-utilities/sam-bazel/BUILD.bazel b/examples/powertools-examples-core-utilities/sam-bazel/BUILD.bazel index 7b888e62a..97fa37394 100644 --- a/examples/powertools-examples-core-utilities/sam-bazel/BUILD.bazel +++ b/examples/powertools-examples-core-utilities/sam-bazel/BUILD.bazel @@ -6,11 +6,15 @@ java_library( resources = glob(["src/main/resources/**"]), deps = [ "@maven//:software_amazon_lambda_powertools_tracing", + "@maven//:software_amazon_lambda_powertools_logging", "@maven//:software_amazon_lambda_powertools_logging_log4j", "@maven//:software_amazon_lambda_powertools_metrics", "@maven//:com_amazonaws_aws_lambda_java_core", "@maven//:com_amazonaws_aws_lambda_java_events", "@maven//:org_aspectj_aspectjrt", + "@maven//:org_slf4j_slf4j_api", + "@maven//:com_fasterxml_jackson_core_jackson_databind", + "@maven//:org_apache_logging_log4j_log4j_api", ], ) From c56ef39bd401c979128728fa70ac8f99d9f279bc Mon Sep 17 00:00:00 2001 From: Philipp Page Date: Thu, 7 Aug 2025 16:58:43 +0200 Subject: [PATCH 4/4] Remove unused log method. --- .../sam-bazel/src/main/java/helloworld/App.java | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/examples/powertools-examples-core-utilities/sam-bazel/src/main/java/helloworld/App.java b/examples/powertools-examples-core-utilities/sam-bazel/src/main/java/helloworld/App.java index 1f7d88cbb..2844e50fe 100644 --- a/examples/powertools-examples-core-utilities/sam-bazel/src/main/java/helloworld/App.java +++ b/examples/powertools-examples-core-utilities/sam-bazel/src/main/java/helloworld/App.java @@ -97,11 +97,6 @@ public APIGatewayProxyResponseEvent handleRequest(final APIGatewayProxyRequestEv } } - @Tracing - private void log() { - log.info("inside threaded logging for function"); - } - @Tracing(namespace = "getPageContents", captureMode = CaptureMode.DISABLED) private String getPageContents(String address) throws IOException { URL url = new URL(address); @@ -110,4 +105,4 @@ private String getPageContents(String address) throws IOException { return br.lines().collect(Collectors.joining(System.lineSeparator())); } } -} \ No newline at end of file +}