Skip to content
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

chore: checkstyle formater & linter #1316

Merged
merged 5 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
427 changes: 427 additions & 0 deletions checkstyle.xml

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions examples/pom.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ 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.
~
-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ public class PowertoolsExamplesCloudformationCdkApp {
public static void main(final String[] args) {
App app = new App();

new PowertoolsExamplesCloudformationCdkStack(app, "PowertoolsExamplesCloudformationCdkStack", StackProps.builder()
.build());
new PowertoolsExamplesCloudformationCdkStack(app, "PowertoolsExamplesCloudformationCdkStack",
StackProps.builder()
.build());

app.synth();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ public App() {
protected Response create(CloudFormationCustomResourceEvent cloudFormationCustomResourceEvent, Context context) {
// Validate the CloudFormation Custom Resource event
Objects.requireNonNull(cloudFormationCustomResourceEvent, "cloudFormationCustomResourceEvent cannot be null.");
Objects.requireNonNull(cloudFormationCustomResourceEvent.getResourceProperties().get("BucketName"), "BucketName cannot be null.");
Objects.requireNonNull(cloudFormationCustomResourceEvent.getResourceProperties().get("BucketName"),
"BucketName cannot be null.");

log.info(cloudFormationCustomResourceEvent);
String bucketName = (String) cloudFormationCustomResourceEvent.getResourceProperties().get("BucketName");
Expand Down Expand Up @@ -70,7 +71,8 @@ protected Response create(CloudFormationCustomResourceEvent cloudFormationCustom
protected Response update(CloudFormationCustomResourceEvent cloudFormationCustomResourceEvent, Context context) {
// Validate the CloudFormation Custom Resource event
Objects.requireNonNull(cloudFormationCustomResourceEvent, "cloudFormationCustomResourceEvent cannot be null.");
Objects.requireNonNull(cloudFormationCustomResourceEvent.getResourceProperties().get("BucketName"), "BucketName cannot be null.");
Objects.requireNonNull(cloudFormationCustomResourceEvent.getResourceProperties().get("BucketName"),
"BucketName cannot be null.");

log.info(cloudFormationCustomResourceEvent);
// Get the physicalResourceId. physicalResourceId is the value returned to CloudFormation in the Create request, and passed in on subsequent requests (e.g. UPDATE or DELETE)
Expand Down Expand Up @@ -112,7 +114,8 @@ protected Response update(CloudFormationCustomResourceEvent cloudFormationCustom
protected Response delete(CloudFormationCustomResourceEvent cloudFormationCustomResourceEvent, Context context) {
// Validate the CloudFormation Custom Resource event
Objects.requireNonNull(cloudFormationCustomResourceEvent, "cloudFormationCustomResourceEvent cannot be null.");
Objects.requireNonNull(cloudFormationCustomResourceEvent.getPhysicalResourceId(), "PhysicalResourceId cannot be null.");
Objects.requireNonNull(cloudFormationCustomResourceEvent.getPhysicalResourceId(),
"PhysicalResourceId cannot be null.");

log.info(cloudFormationCustomResourceEvent);
// Get the physicalResourceId. physicalResourceId is the value provided to CloudFormation in the Create request.
Expand Down Expand Up @@ -142,7 +145,8 @@ protected Response delete(CloudFormationCustomResourceEvent cloudFormationCustom

private boolean bucketExists(String bucketName) {
try {
HeadBucketResponse headBucketResponse = s3Client.headBucket(HeadBucketRequest.builder().bucket(bucketName).build());
HeadBucketResponse headBucketResponse =
s3Client.headBucket(HeadBucketRequest.builder().bucket(bucketName).build());
if (headBucketResponse.sdkHttpResponse().isSuccessful()) {
return true;
}
Expand All @@ -157,7 +161,8 @@ private void createBucket(String bucketName) {
S3Waiter waiter = s3Client.waiter();
CreateBucketRequest createBucketRequest = CreateBucketRequest.builder().bucket(bucketName).build();
s3Client.createBucket(createBucketRequest);
WaiterResponse<HeadBucketResponse> waiterResponse = waiter.waitUntilBucketExists(HeadBucketRequest.builder().bucket(bucketName).build());
WaiterResponse<HeadBucketResponse> waiterResponse =
waiter.waitUntilBucketExists(HeadBucketRequest.builder().bucket(bucketName).build());
waiterResponse.matched().response().ifPresent(log::info);
log.info("Bucket Created {}", bucketName);
}
Expand Down
70 changes: 43 additions & 27 deletions examples/powertools-examples-core/src/main/java/helloworld/App.java
Original file line number Diff line number Diff line change
@@ -1,34 +1,47 @@
/*
* 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 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 static software.amazon.lambda.powertools.metrics.MetricsUtils.metricsLogger;
import static software.amazon.lambda.powertools.metrics.MetricsUtils.withSingleMetric;
import static software.amazon.lambda.powertools.tracing.TracingUtils.putMetadata;
import static software.amazon.lambda.powertools.tracing.TracingUtils.withEntitySubsegment;

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 com.amazonaws.xray.AWSXRay;
import com.amazonaws.xray.entities.Entity;
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.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import software.amazon.cloudwatchlogs.emf.model.DimensionSet;
import software.amazon.cloudwatchlogs.emf.model.Unit;
import software.amazon.lambda.powertools.logging.LoggingUtils;
import software.amazon.lambda.powertools.logging.Logging;
import software.amazon.lambda.powertools.logging.LoggingUtils;
import software.amazon.lambda.powertools.metrics.Metrics;
import software.amazon.lambda.powertools.tracing.CaptureMode;
import software.amazon.lambda.powertools.tracing.TracingUtils;
import software.amazon.lambda.powertools.tracing.Tracing;

import static software.amazon.lambda.powertools.metrics.MetricsUtils.metricsLogger;
import static software.amazon.lambda.powertools.metrics.MetricsUtils.withSingleMetric;
import static software.amazon.lambda.powertools.tracing.TracingUtils.putMetadata;
import static software.amazon.lambda.powertools.tracing.TracingUtils.withEntitySubsegment;
import software.amazon.lambda.powertools.tracing.TracingUtils;

/**
* Handler for requests to Lambda function.
Expand All @@ -47,10 +60,11 @@ public APIGatewayProxyResponseEvent handleRequest(final APIGatewayProxyRequestEv

metricsLogger().putMetric("CustomMetric1", 1, Unit.COUNT);

withSingleMetric("CustomMetrics2", 1, Unit.COUNT, "Another", (metric) -> {
metric.setDimensions(DimensionSet.of("AnotherService", "CustomService"));
metric.setDimensions(DimensionSet.of("AnotherService1", "CustomService1"));
});
withSingleMetric("CustomMetrics2", 1, Unit.COUNT, "Another", (metric) ->
{
metric.setDimensions(DimensionSet.of("AnotherService", "CustomService"));
metric.setDimensions(DimensionSet.of("AnotherService1", "CustomService1"));
});

LoggingUtils.appendKey("test", "willBeLogged");

Expand All @@ -62,11 +76,12 @@ public APIGatewayProxyResponseEvent handleRequest(final APIGatewayProxyRequestEv
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);
});
TracingUtils.withSubsegment("loggingResponse", subsegment ->
{
String sampled = "log something out";
log.info(sampled);
log.info(output);
});

threadOption1();

Expand All @@ -91,10 +106,11 @@ private void threadOption1() throws InterruptedException {

private void threadOption2() throws InterruptedException {
Entity traceEntity = AWSXRay.getTraceEntity();
Thread anotherThread = new Thread(() -> withEntitySubsegment("inlineLog", traceEntity, subsegment -> {
String var = "somethingToProcess";
log.info("inside threaded logging inline {}", var);
}));
Thread anotherThread = new Thread(() -> withEntitySubsegment("inlineLog", traceEntity, subsegment ->
{
String var = "somethingToProcess";
log.info("inside threaded logging inline {}", var);
}));
anotherThread.start();
anotherThread.join();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
/*
* 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.util.Map;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestStreamHandler;
import com.fasterxml.jackson.databind.ObjectMapper;
import software.amazon.lambda.powertools.logging.Logging;
import software.amazon.lambda.powertools.metrics.Metrics;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,43 +1,59 @@
/*
* 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 org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
import com.amazonaws.xray.AWSXRay;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.*;

public class AppTest {

@Before
public void setup() {
if(null == System.getenv("LAMBDA_TASK_ROOT")) {
AWSXRay.beginSegment("test");
@Before
public void setup() {
if (null == System.getenv("LAMBDA_TASK_ROOT")) {
AWSXRay.beginSegment("test");
}
}
}

@After
public void tearDown() {
if (AWSXRay.getCurrentSubsegmentOptional().isPresent()) {
AWSXRay.endSubsegment();
@After
public void tearDown() {
if (AWSXRay.getCurrentSubsegmentOptional().isPresent()) {
AWSXRay.endSubsegment();
}

if (null == System.getenv("LAMBDA_TASK_ROOT")) {
AWSXRay.endSegment();
}
}

if(null == System.getenv("LAMBDA_TASK_ROOT")) {
AWSXRay.endSegment();
@Test
public void successfulResponse() {
App app = new App();
APIGatewayProxyResponseEvent result = app.handleRequest(null, null);
assertEquals(result.getStatusCode().intValue(), 200);
assertEquals(result.getHeaders().get("Content-Type"), "application/json");
String content = result.getBody();
assertNotNull(content);
assertTrue(content.contains("\"message\""));
assertTrue(content.contains("\"hello world\""));
assertTrue(content.contains("\"location\""));
}
}

@Test
public void successfulResponse() {
App app = new App();
APIGatewayProxyResponseEvent result = app.handleRequest(null, null);
assertEquals(result.getStatusCode().intValue(), 200);
assertEquals(result.getHeaders().get("Content-Type"), "application/json");
String content = result.getBody();
assertNotNull(content);
assertTrue(content.contains("\"message\""));
assertTrue(content.contains("\"hello world\""));
assertTrue(content.contains("\"location\""));
}
}
14 changes: 14 additions & 0 deletions examples/powertools-examples-idempotency/pom.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
<!--
~ 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.
~
-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
Expand Down
Loading
Loading