Skip to content

Commit daf2d24

Browse files
Initial custom annotation support (#8)
1 parent f37cc87 commit daf2d24

File tree

8 files changed

+197
-10
lines changed

8 files changed

+197
-10
lines changed

.gitignore

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,12 @@ Desktop.ini
8989
######################
9090
# Others
9191
######################
92-
*.class
9392
*.*~
9493
*~
9594
.merge_file*
9695

9796
docs/node_modules
9897
docs/.cache
99-
docs/public
98+
docs/public
99+
/example/.aws-sam/
100+
/example/HelloWorldFunction/.aws-sam/

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,35 @@ Powertools is available in Maven Central. You can use your favourite dependency
2323
...
2424
</dependencies>
2525
```
26+
27+
And configure the aspectj-maven-plugin to compile-time weave (CTW) the aws-lambda-powertools-java aspects into your project:
28+
29+
```xml
30+
<plugin>
31+
<groupId>com.nickwongdev</groupId>
32+
<artifactId>aspectj-maven-plugin</artifactId>
33+
<version>1.12.1</version>
34+
<configuration>
35+
<source>1.8</source>
36+
<target>1.8</target>
37+
<complianceLevel>1.8</complianceLevel>
38+
<aspectLibraries>
39+
<aspectLibrary>
40+
<groupId>software.aws.lambda</groupId>
41+
<artifactId>aws-lambda-powertools-java</artifactId>
42+
</aspectLibrary>
43+
</aspectLibraries>
44+
</configuration>
45+
<executions>
46+
<execution>
47+
<goals>
48+
<goal>compile</goal>
49+
</goals>
50+
</execution>
51+
</executions>
52+
</plugin>
53+
54+
```
2655
* [gradle](https://gradle.org/):
2756
```
2857
repositories {

example/HelloWorldFunction/pom.xml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,30 @@
5454

5555
<build>
5656
<plugins>
57+
<!-- We can use official one after https://github.com/mojohaus/aspectj-maven-plugin/pull/45 -->
58+
<plugin>
59+
<groupId>com.nickwongdev</groupId>
60+
<artifactId>aspectj-maven-plugin</artifactId>
61+
<version>1.12.1</version>
62+
<configuration>
63+
<source>${maven.compiler.source}</source>
64+
<target>${maven.compiler.target}</target>
65+
<complianceLevel>${maven.compiler.target}</complianceLevel>
66+
<aspectLibraries>
67+
<aspectLibrary>
68+
<groupId>software.aws.lambda</groupId>
69+
<artifactId>aws-lambda-powertools-java</artifactId>
70+
</aspectLibrary>
71+
</aspectLibraries>
72+
</configuration>
73+
<executions>
74+
<execution>
75+
<goals>
76+
<goal>compile</goal>
77+
</goals>
78+
</execution>
79+
</executions>
80+
</plugin>
5781
<plugin>
5882
<groupId>org.apache.maven.plugins</groupId>
5983
<artifactId>maven-shade-plugin</artifactId>

example/HelloWorldFunction/src/main/java/helloworld/App.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
1515
import org.apache.logging.log4j.LogManager;
1616
import org.apache.logging.log4j.Logger;
17-
import org.apache.logging.log4j.ThreadContext;
18-
import software.aws.lambda.logging.DefaultLambdaFields;
17+
import software.aws.lambda.logging.PowerToolsLogging;
1918

2019
/**
2120
* Handler for requests to Lambda function.
@@ -24,9 +23,8 @@ public class App implements RequestHandler<APIGatewayProxyRequestEvent, APIGatew
2423

2524
Logger log = LogManager.getLogger();
2625

26+
@PowerToolsLogging
2727
public APIGatewayProxyResponseEvent handleRequest(final APIGatewayProxyRequestEvent input, final Context context) {
28-
ThreadContext.putAll(DefaultLambdaFields.values(context));
29-
3028
Map<String, String> headers = new HashMap<>();
3129
headers.put("Content-Type", "application/json");
3230
headers.put("X-Custom-Header", "application/json");
@@ -49,9 +47,9 @@ public APIGatewayProxyResponseEvent handleRequest(final APIGatewayProxyRequestEv
4947
}
5048
}
5149

52-
private String getPageContents(String address) throws IOException{
50+
private String getPageContents(String address) throws IOException {
5351
URL url = new URL(address);
54-
try(BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()))) {
52+
try (BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()))) {
5553
return br.lines().collect(Collectors.joining(System.lineSeparator()));
5654
}
5755
}

pom.xml

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
<maven.compiler.target>1.8</maven.compiler.target>
4242
<log4j.version>2.13.3</log4j.version>
4343
<jackson.version>2.11.0</jackson.version>
44+
<aspectj.version>1.9.6</aspectj.version>
4445
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
4546
</properties>
4647

@@ -90,13 +91,60 @@
9091
<version>5.6.2</version>
9192
<scope>test</scope>
9293
</dependency>
94+
<dependency>
95+
<groupId>org.aspectj</groupId>
96+
<artifactId>aspectjrt</artifactId>
97+
<version>${aspectj.version}</version>
98+
</dependency>
9399
</dependencies>
94100

95101
<profiles>
96102
<profile>
97103
<id>dev</id>
104+
<activation>
105+
<activeByDefault>true</activeByDefault>
106+
</activation>
98107
<build>
99108
<plugins>
109+
<plugin>
110+
<groupId>org.apache.maven.plugins</groupId>
111+
<artifactId>maven-compiler-plugin</artifactId>
112+
<version>3.8.1</version>
113+
<configuration>
114+
<source>${maven.compiler.source}</source>
115+
<target>${maven.compiler.target}</target>
116+
<useIncrementalCompilation>false</useIncrementalCompilation>
117+
</configuration>
118+
</plugin>
119+
<plugin>
120+
<!-- We can use official one after https://github.com/mojohaus/aspectj-maven-plugin/pull/45 -->
121+
<groupId>com.nickwongdev</groupId>
122+
<artifactId>aspectj-maven-plugin</artifactId>
123+
<version>1.12.1</version>
124+
<configuration>
125+
<source>${maven.compiler.source}</source>
126+
<target>${maven.compiler.target}</target>
127+
<complianceLevel>${maven.compiler.target}</complianceLevel>
128+
<Xlint>ignore</Xlint>
129+
<encoding>${project.build.sourceEncoding}</encoding>
130+
</configuration>
131+
<executions>
132+
<execution>
133+
<phase>process-sources</phase>
134+
<goals>
135+
<goal>compile</goal>
136+
<goal>test-compile</goal>
137+
</goals>
138+
</execution>
139+
</executions>
140+
<dependencies>
141+
<dependency>
142+
<groupId>org.aspectj</groupId>
143+
<artifactId>aspectjtools</artifactId>
144+
<version>${aspectj.version}</version>
145+
</dependency>
146+
</dependencies>
147+
</plugin>
100148
<plugin>
101149
<groupId>org.apache.maven.plugins</groupId>
102150
<artifactId>maven-surefire-plugin</artifactId>
@@ -146,6 +194,45 @@
146194
<id>release</id>
147195
<build>
148196
<plugins>
197+
<plugin>
198+
<groupId>org.apache.maven.plugins</groupId>
199+
<artifactId>maven-compiler-plugin</artifactId>
200+
<version>3.8.1</version>
201+
<configuration>
202+
<source>${maven.compiler.source}</source>
203+
<target>${maven.compiler.target}</target>
204+
<useIncrementalCompilation>false</useIncrementalCompilation>
205+
</configuration>
206+
</plugin>
207+
<plugin>
208+
<!--We can use official one after https://github.com/mojohaus/aspectj-maven-plugin/pull/45-->
209+
<groupId>com.nickwongdev</groupId>
210+
<artifactId>aspectj-maven-plugin</artifactId>
211+
<version>1.12.1</version>
212+
<configuration>
213+
<source>${maven.compiler.source}</source>
214+
<target>${maven.compiler.target}</target>
215+
<complianceLevel>${maven.compiler.target}</complianceLevel>
216+
<Xlint>ignore</Xlint>
217+
<encoding>${project.build.sourceEncoding}</encoding>
218+
</configuration>
219+
<executions>
220+
<execution>
221+
<phase>process-sources</phase>
222+
<goals>
223+
<goal>compile</goal>
224+
<goal>test-compile</goal>
225+
</goals>
226+
</execution>
227+
</executions>
228+
<dependencies>
229+
<dependency>
230+
<groupId>org.aspectj</groupId>
231+
<artifactId>aspectjtools</artifactId>
232+
<version>${aspectj.version}</version>
233+
</dependency>
234+
</dependencies>
235+
</plugin>
149236
<plugin>
150237
<groupId>org.apache.maven.plugins</groupId>
151238
<artifactId>maven-surefire-plugin</artifactId>

src/main/java/software/aws/lambda/logging/DefaultLambdaFields.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import java.util.HashMap;
66
import java.util.Map;
77

8-
public enum DefaultLambdaFields {
8+
enum DefaultLambdaFields {
99
FUNCTION_NAME("functionName"),
1010
FUNCTION_VERSION("functionVersion"),
1111
FUNCTION_ARN("functionArn"),
@@ -17,7 +17,7 @@ public enum DefaultLambdaFields {
1717
this.name = name;
1818
}
1919

20-
public static Map<String, String> values(Context context) {
20+
static Map<String, String> values(Context context) {
2121
Map<String, String> hashMap = new HashMap<>();
2222

2323
hashMap.put(FUNCTION_NAME.name, context.getFunctionName());
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package software.aws.lambda.logging;
2+
3+
import com.amazonaws.services.lambda.runtime.Context;
4+
import org.apache.logging.log4j.ThreadContext;
5+
import org.aspectj.lang.ProceedingJoinPoint;
6+
import org.aspectj.lang.annotation.Around;
7+
import org.aspectj.lang.annotation.Aspect;
8+
import org.aspectj.lang.annotation.Pointcut;
9+
10+
@Aspect
11+
public final class LambdaAspect {
12+
private static Boolean IS_COLD_START = null;
13+
14+
@Pointcut("@annotation(powerToolsLogging)")
15+
public void callAt(PowerToolsLogging powerToolsLogging) {
16+
}
17+
18+
@Around(value = "callAt(powerToolsLogging)")
19+
public Object around(ProceedingJoinPoint pjp,
20+
PowerToolsLogging powerToolsLogging) throws Throwable {
21+
22+
// TODO JoinPoint that annotation is used on entry method of lambda or do we want it to work anywhere
23+
if(powerToolsLogging.injectContextInfo()) {
24+
if(pjp.getArgs().length == 2 && pjp.getArgs()[1] instanceof Context) {
25+
ThreadContext.putAll(DefaultLambdaFields.values((Context) pjp.getArgs()[1]));
26+
}
27+
}
28+
29+
ThreadContext.put("coldStart", null == IS_COLD_START? "true" : "false");
30+
31+
IS_COLD_START = false;
32+
33+
return pjp.proceed();
34+
}
35+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package software.aws.lambda.logging;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
@Retention(RetentionPolicy.RUNTIME)
9+
@Target(ElementType.METHOD)
10+
public @interface PowerToolsLogging {
11+
12+
boolean injectContextInfo() default true;
13+
}

0 commit comments

Comments
 (0)