Skip to content

Commit 810ffb5

Browse files
Context injection support (#9)
1 parent daf2d24 commit 810ffb5

File tree

9 files changed

+316
-185
lines changed

9 files changed

+316
-185
lines changed

pom.xml

Lines changed: 106 additions & 174 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,13 @@
7878
<artifactId>log4j-api</artifactId>
7979
<version>${log4j.version}</version>
8080
</dependency>
81+
<dependency>
82+
<groupId>org.aspectj</groupId>
83+
<artifactId>aspectjrt</artifactId>
84+
<version>${aspectj.version}</version>
85+
</dependency>
8186

87+
<!-- Test dependencies -->
8288
<dependency>
8389
<groupId>org.junit.jupiter</groupId>
8490
<artifactId>junit-jupiter-api</artifactId>
@@ -91,173 +97,117 @@
9197
<version>5.6.2</version>
9298
<scope>test</scope>
9399
</dependency>
100+
<dependency>
101+
<groupId>org.mockito</groupId>
102+
<artifactId>mockito-core</artifactId>
103+
<version>3.3.3</version>
104+
<scope>test</scope>
105+
</dependency>
94106
<dependency>
95107
<groupId>org.aspectj</groupId>
96-
<artifactId>aspectjrt</artifactId>
108+
<artifactId>aspectjweaver</artifactId>
97109
<version>${aspectj.version}</version>
110+
<scope>test</scope>
111+
</dependency>
112+
<dependency>
113+
<groupId>org.assertj</groupId>
114+
<artifactId>assertj-core</artifactId>
115+
<version>3.9.1</version>
116+
<scope>test</scope>
98117
</dependency>
99118
</dependencies>
100119

120+
<build>
121+
<plugins>
122+
<plugin>
123+
<groupId>org.apache.maven.plugins</groupId>
124+
<artifactId>maven-compiler-plugin</artifactId>
125+
<version>3.8.1</version>
126+
<configuration>
127+
<source>${maven.compiler.source}</source>
128+
<target>${maven.compiler.target}</target>
129+
<useIncrementalCompilation>false</useIncrementalCompilation>
130+
</configuration>
131+
</plugin>
132+
<plugin>
133+
<!-- We can use official one after https://github.com/mojohaus/aspectj-maven-plugin/pull/45 -->
134+
<groupId>com.nickwongdev</groupId>
135+
<artifactId>aspectj-maven-plugin</artifactId>
136+
<version>1.12.1</version>
137+
<configuration>
138+
<source>${maven.compiler.source}</source>
139+
<target>${maven.compiler.target}</target>
140+
<complianceLevel>${maven.compiler.target}</complianceLevel>
141+
<Xlint>ignore</Xlint>
142+
<encoding>${project.build.sourceEncoding}</encoding>
143+
</configuration>
144+
<executions>
145+
<execution>
146+
<phase>process-sources</phase>
147+
<goals>
148+
<goal>compile</goal>
149+
<goal>test-compile</goal>
150+
</goals>
151+
</execution>
152+
</executions>
153+
<dependencies>
154+
<dependency>
155+
<groupId>org.aspectj</groupId>
156+
<artifactId>aspectjtools</artifactId>
157+
<version>${aspectj.version}</version>
158+
</dependency>
159+
</dependencies>
160+
</plugin>
161+
<plugin>
162+
<groupId>org.apache.maven.plugins</groupId>
163+
<artifactId>maven-surefire-plugin</artifactId>
164+
<version>2.22.2</version>
165+
</plugin>
166+
<plugin>
167+
<groupId>org.jacoco</groupId>
168+
<artifactId>jacoco-maven-plugin</artifactId>
169+
<version>0.8.5</version>
170+
<executions>
171+
<execution>
172+
<goals>
173+
<goal>prepare-agent</goal>
174+
</goals>
175+
</execution>
176+
<!-- attached to Maven test phase -->
177+
<execution>
178+
<id>report</id>
179+
<phase>test</phase>
180+
<goals>
181+
<goal>report</goal>
182+
</goals>
183+
</execution>
184+
</executions>
185+
</plugin>
186+
<plugin>
187+
<groupId>org.apache.maven.plugins</groupId>
188+
<artifactId>maven-javadoc-plugin</artifactId>
189+
<version>2.9.1</version>
190+
<configuration>
191+
<additionalparam>-Xdoclint:none</additionalparam>
192+
<detectJavaApiLink>false</detectJavaApiLink>
193+
</configuration>
194+
<executions>
195+
<execution>
196+
<id>attach-javadocs</id>
197+
<goals>
198+
<goal>jar</goal>
199+
</goals>
200+
</execution>
201+
</executions>
202+
</plugin>
203+
</plugins>
204+
</build>
205+
101206
<profiles>
102-
<profile>
103-
<id>dev</id>
104-
<activation>
105-
<activeByDefault>true</activeByDefault>
106-
</activation>
107-
<build>
108-
<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>
148-
<plugin>
149-
<groupId>org.apache.maven.plugins</groupId>
150-
<artifactId>maven-surefire-plugin</artifactId>
151-
<version>2.22.2</version>
152-
</plugin>
153-
<plugin>
154-
<groupId>org.jacoco</groupId>
155-
<artifactId>jacoco-maven-plugin</artifactId>
156-
<version>0.8.2</version>
157-
<executions>
158-
<execution>
159-
<goals>
160-
<goal>prepare-agent</goal>
161-
</goals>
162-
</execution>
163-
<!-- attached to Maven test phase -->
164-
<execution>
165-
<id>report</id>
166-
<phase>test</phase>
167-
<goals>
168-
<goal>report</goal>
169-
</goals>
170-
</execution>
171-
</executions>
172-
</plugin>
173-
<plugin>
174-
<groupId>org.apache.maven.plugins</groupId>
175-
<artifactId>maven-javadoc-plugin</artifactId>
176-
<version>2.9.1</version>
177-
<configuration>
178-
<additionalparam>-Xdoclint:none</additionalparam>
179-
<detectJavaApiLink>false</detectJavaApiLink>
180-
</configuration>
181-
<executions>
182-
<execution>
183-
<id>attach-javadocs</id>
184-
<goals>
185-
<goal>jar</goal>
186-
</goals>
187-
</execution>
188-
</executions>
189-
</plugin>
190-
</plugins>
191-
</build>
192-
</profile>
193207
<profile>
194208
<id>release</id>
195209
<build>
196210
<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>
236-
<plugin>
237-
<groupId>org.apache.maven.plugins</groupId>
238-
<artifactId>maven-surefire-plugin</artifactId>
239-
<version>2.22.1</version>
240-
</plugin>
241-
<plugin>
242-
<groupId>org.jacoco</groupId>
243-
<artifactId>jacoco-maven-plugin</artifactId>
244-
<version>0.8.2</version>
245-
<executions>
246-
<execution>
247-
<goals>
248-
<goal>prepare-agent</goal>
249-
</goals>
250-
</execution>
251-
<!-- attached to Maven test phase -->
252-
<execution>
253-
<id>report</id>
254-
<phase>test</phase>
255-
<goals>
256-
<goal>report</goal>
257-
</goals>
258-
</execution>
259-
</executions>
260-
</plugin>
261211
<plugin>
262212
<groupId>org.apache.maven.plugins</groupId>
263213
<artifactId>maven-source-plugin</artifactId>
@@ -271,23 +221,6 @@
271221
</execution>
272222
</executions>
273223
</plugin>
274-
<plugin>
275-
<groupId>org.apache.maven.plugins</groupId>
276-
<artifactId>maven-javadoc-plugin</artifactId>
277-
<version>2.9.1</version>
278-
<configuration>
279-
<additionalparam>-Xdoclint:none</additionalparam>
280-
<detectJavaApiLink>false</detectJavaApiLink>
281-
</configuration>
282-
<executions>
283-
<execution>
284-
<id>attach-javadocs</id>
285-
<goals>
286-
<goal>jar</goal>
287-
</goals>
288-
</execution>
289-
</executions>
290-
</plugin>
291224
<plugin>
292225
<groupId>org.apache.maven.plugins</groupId>
293226
<artifactId>maven-gpg-plugin</artifactId>
@@ -318,5 +251,4 @@
318251
</profile>
319252
</profiles>
320253

321-
322254
</project>

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ enum DefaultLambdaFields {
1717
this.name = name;
1818
}
1919

20+
public String getName() {
21+
return name;
22+
}
23+
2024
static Map<String, String> values(Context context) {
2125
Map<String, String> hashMap = new HashMap<>();
2226

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
package software.aws.lambda.logging;
22

3+
import java.util.Optional;
4+
35
import com.amazonaws.services.lambda.runtime.Context;
6+
import com.amazonaws.services.lambda.runtime.RequestHandler;
7+
import com.amazonaws.services.lambda.runtime.RequestStreamHandler;
48
import org.apache.logging.log4j.ThreadContext;
59
import org.aspectj.lang.ProceedingJoinPoint;
610
import org.aspectj.lang.annotation.Around;
711
import org.aspectj.lang.annotation.Aspect;
812
import org.aspectj.lang.annotation.Pointcut;
913

14+
import static java.util.Optional.empty;
15+
import static java.util.Optional.of;
16+
1017
@Aspect
1118
public final class LambdaAspect {
12-
private static Boolean IS_COLD_START = null;
19+
static Boolean IS_COLD_START = null;
1320

1421
@Pointcut("@annotation(powerToolsLogging)")
1522
public void callAt(PowerToolsLogging powerToolsLogging) {
@@ -19,17 +26,32 @@ public void callAt(PowerToolsLogging powerToolsLogging) {
1926
public Object around(ProceedingJoinPoint pjp,
2027
PowerToolsLogging powerToolsLogging) throws Throwable {
2128

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");
29+
extractContext(pjp)
30+
.ifPresent(context -> {
31+
ThreadContext.putAll(DefaultLambdaFields.values(context));
32+
ThreadContext.put("coldStart", null == IS_COLD_START ? "true" : "false");
33+
});
3034

3135
IS_COLD_START = false;
3236

37+
3338
return pjp.proceed();
3439
}
40+
41+
private Optional<Context> extractContext(ProceedingJoinPoint pjp) {
42+
43+
if ("handleRequest".equals(pjp.getSignature().getName())) {
44+
if (RequestHandler.class.isAssignableFrom(pjp.getSignature().getDeclaringType())
45+
&& pjp.getArgs().length == 2 && pjp.getArgs()[1] instanceof Context) {
46+
return of((Context) pjp.getArgs()[1]);
47+
}
48+
49+
if (RequestStreamHandler.class.isAssignableFrom(pjp.getSignature().getDeclaringType())
50+
&& pjp.getArgs().length == 3 && pjp.getArgs()[2] instanceof Context) {
51+
return of((Context) pjp.getArgs()[2]);
52+
}
53+
}
54+
55+
return empty();
56+
}
3557
}

0 commit comments

Comments
 (0)