Skip to content

Commit e69b1c7

Browse files
committed
Unfinished implementation of the POWERTOOLS_SERVICE_NAME env var.
1 parent 47f8876 commit e69b1c7

File tree

4 files changed

+27
-8
lines changed

4 files changed

+27
-8
lines changed

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,11 @@
188188
<groupId>org.apache.maven.plugins</groupId>
189189
<artifactId>maven-surefire-plugin</artifactId>
190190
<version>2.22.2</version>
191+
<configuration>
192+
<environmentVariables>
193+
<POWERTOOLS_SERVICE_NAME>testService</POWERTOOLS_SERVICE_NAME>
194+
</environmentVariables>
195+
</configuration>
191196
</plugin>
192197
<plugin>
193198
<groupId>org.jacoco</groupId>

src/main/java/software/amazon/lambda/internal/LambdaHandlerProcessor.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.aspectj.lang.ProceedingJoinPoint;
1010

1111
public final class LambdaHandlerProcessor {
12+
public static String SERVICE_NAME = System.getenv("POWERTOOLS_SERVICE_NAME");
1213
public static Boolean IS_COLD_START = null;
1314

1415
public static boolean isHandlerMethod(ProceedingJoinPoint pjp) {

src/main/java/software/amazon/lambda/logging/internal/LambdaLoggingAspect.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import static java.util.Optional.empty;
2525
import static java.util.Optional.of;
2626
import static software.amazon.lambda.internal.LambdaHandlerProcessor.IS_COLD_START;
27+
import static software.amazon.lambda.internal.LambdaHandlerProcessor.SERVICE_NAME;
2728
import static software.amazon.lambda.internal.LambdaHandlerProcessor.isHandlerMethod;
2829
import static software.amazon.lambda.internal.LambdaHandlerProcessor.placedOnRequestHandler;
2930
import static software.amazon.lambda.internal.LambdaHandlerProcessor.placedOnStreamHandler;
@@ -45,6 +46,7 @@ public Object around(ProceedingJoinPoint pjp,
4546
.ifPresent(context -> {
4647
ThreadContext.putAll(DefaultLambdaFields.values(context));
4748
ThreadContext.put("coldStart", null == IS_COLD_START ? "true" : "false");
49+
ThreadContext.put("service", SERVICE_NAME);
4850
});
4951

5052

src/test/java/software/amazon/lambda/logging/internal/LambdaLoggingAspectTest.java

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,13 @@ void shouldSetLambdaContextWhenEnabled() {
5050
requestHandler.handleRequest(new Object(), context);
5151

5252
assertThat(ThreadContext.getImmutableContext())
53-
.hasSize(5)
53+
.hasSize(6)
5454
.containsEntry(DefaultLambdaFields.FUNCTION_ARN.getName(), "testArn")
5555
.containsEntry(DefaultLambdaFields.FUNCTION_MEMORY_SIZE.getName(), "10")
5656
.containsEntry(DefaultLambdaFields.FUNCTION_VERSION.getName(), "1")
5757
.containsEntry(DefaultLambdaFields.FUNCTION_NAME.getName(), "testFunction")
58-
.containsKey("coldStart");
58+
.containsKey("coldStart")
59+
.containsKey("service");
5960
}
6061

6162
@Test
@@ -65,26 +66,27 @@ void shouldSetLambdaContextForStreamHandlerWhenEnabled() throws IOException {
6566
requestStreamHandler.handleRequest(new ByteArrayInputStream(new byte[]{}), new ByteArrayOutputStream(), context);
6667

6768
assertThat(ThreadContext.getImmutableContext())
68-
.hasSize(5)
69+
.hasSize(6)
6970
.containsEntry(DefaultLambdaFields.FUNCTION_ARN.getName(), "testArn")
7071
.containsEntry(DefaultLambdaFields.FUNCTION_MEMORY_SIZE.getName(), "10")
7172
.containsEntry(DefaultLambdaFields.FUNCTION_VERSION.getName(), "1")
7273
.containsEntry(DefaultLambdaFields.FUNCTION_NAME.getName(), "testFunction")
73-
.containsKey("coldStart");
74+
.containsKey("coldStart")
75+
.containsKey("service");
7476
}
7577

7678
@Test
7779
void shouldSetColdStartFlag() throws IOException {
7880
requestStreamHandler.handleRequest(new ByteArrayInputStream(new byte[]{}), new ByteArrayOutputStream(), context);
7981

8082
assertThat(ThreadContext.getImmutableContext())
81-
.hasSize(5)
83+
.hasSize(6)
8284
.containsEntry("coldStart", "true");
8385

8486
requestStreamHandler.handleRequest(new ByteArrayInputStream(new byte[]{}), new ByteArrayOutputStream(), context);
8587

8688
assertThat(ThreadContext.getImmutableContext())
87-
.hasSize(5)
89+
.hasSize(6)
8890
.containsEntry("coldStart", "false");
8991
}
9092

@@ -125,7 +127,7 @@ void shouldLogEventForHandler() {
125127
requestHandler.handleRequest(new Object(), context);
126128

127129
assertThat(ThreadContext.getImmutableContext())
128-
.hasSize(5);
130+
.hasSize(6);
129131
}
130132

131133
@Test
@@ -142,7 +144,16 @@ void shouldLogEventForStreamAndLambdaStreamIsValid() throws IOException {
142144
.isEqualTo("{\"test\":\"payload\"}");
143145

144146
assertThat(ThreadContext.getImmutableContext())
145-
.hasSize(5);
147+
.hasSize(6);
148+
}
149+
150+
@Test
151+
void shouldLogServiceNameWhenEnvVarSet() {
152+
requestHandler.handleRequest(new Object(), context);
153+
154+
assertThat(ThreadContext.getImmutableContext())
155+
.hasSize(6)
156+
.containsEntry("service", "testService");
146157
}
147158

148159
private void setupContext() {

0 commit comments

Comments
 (0)