Skip to content

Commit 3a0cd5e

Browse files
feat: support for env variable in tracing capture modes (#249)
1 parent f167395 commit 3a0cd5e

14 files changed

+411
-21
lines changed

docs/content/core/tracing.mdx

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,24 +61,52 @@ public class App implements RequestHandler<APIGatewayProxyRequestEvent, APIGatew
6161
}
6262
```
6363

64-
By default this annotation will automatically record method responses and exceptions.
6564
If you want to customize segment name that appears in traces, use:
66-
`@Tracing(segmentName="yourCustomName")`
65+
66+
```java:title=CustomSegmentName.java
67+
public class App implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
68+
69+
@Tracing(segmentName="yourCustomName")
70+
public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context) {
71+
...
72+
}
73+
```
74+
75+
By default, this annotation will automatically record method responses and exceptions. You can change the default behavior by setting
76+
the environment variables `TRACING_CAPTURE_RESPONSE` and `TRACING_CAPTURE_ERROR` as needed. Optionally, you can override behavior by
77+
different supported `captureMode` to record response, exception or both.
6778

6879
<Note type="warning">
6980
<strong>Returning sensitive information from your Lambda handler or functions, where Tracer is used?</strong>
7081
<br/><br/>
71-
You can disable Tracer from capturing their responses and exception as tracing metadata with <strong><code>captureResponse=false</code></strong> and <strong><code>captureError=false</code></strong>
82+
You can disable annotation from capturing their responses and exception as tracing metadata with <strong><code>captureMode=DISABLED </code></strong>
83+
or globally by setting environment variables <strong><code>TRACING_CAPTURE_RESPONSE</code></strong> and <strong><code>TRACING_CAPTURE_ERROR</code></strong> to <strong><code>false</code></strong>.
7284
</Note><br/>
7385

7486
```java:title=HandlerWithoutCapturingResponseOrError.java
7587
public class App implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
7688

77-
@Tracing(captureError = false, captureResponse = false)
89+
@Tracing(captureMode=CaptureMode.DISABLED)
7890
public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context) {
7991
...
8092
}
8193
```
94+
Globally:
95+
96+
```yaml:title=template.yaml
97+
Resources:
98+
HelloWorldFunction:
99+
Type: AWS::Serverless::Function
100+
Properties:
101+
...
102+
Runtime: java8
103+
104+
Tracing: Active
105+
Environment:
106+
Variables:
107+
TRACING_CAPTURE_RESPONSE: false # highlight-line
108+
TRACING_CAPTURE_ERROR: false # highlight-line
109+
```
82110

83111
### Annotations
84112

docs/content/index.mdx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,9 @@ Environment variable | Description | Utility
123123
**POWERTOOLS_SERVICE_NAME** | Sets service name used for tracing namespace, metrics dimension and structured logging | All
124124
**POWERTOOLS_METRICS_NAMESPACE** | Sets namespace used for metrics | [Metrics](./core/metrics)
125125
**POWERTOOLS_LOGGER_SAMPLE_RATE** | Debug log sampling | [Logging](./core/logging)
126-
**LOG_LEVEL** | Sets logging level | [Logging](./core/logger)
126+
**LOG_LEVEL** | Sets logging level | [Logging](./core/logging)
127+
**TRACING_CAPTURE_RESPONSE** | Enables/Disables tracing mode to capture method response | [Tracing](./core/tracing)
128+
**TRACING_CAPTURE_ERROR** | Enables/Disables tracing mode to capture method error | [Tracing](./core/tracing)
127129

128130
## Tenets
129131

powertools-tracing/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@
100100
<artifactId>mockito-core</artifactId>
101101
<scope>test</scope>
102102
</dependency>
103+
<dependency>
104+
<groupId>org.mockito</groupId>
105+
<artifactId>mockito-inline</artifactId>
106+
<scope>test</scope>
107+
</dependency>
103108
<dependency>
104109
<groupId>org.aspectj</groupId>
105110
<artifactId>aspectjweaver</artifactId>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package software.amazon.lambda.powertools.tracing;
2+
3+
public enum CaptureMode {
4+
/**
5+
* Enables annotation to capture only response. If this mode is explicitly overridden
6+
* on {@link Tracing} annotation, it will override value of environment variable TRACING_CAPTURE_RESPONSE
7+
*/
8+
RESPONSE,
9+
/**
10+
* Enabled annotation to capture only error from the method. If this mode is explicitly overridden
11+
* on {@link Tracing} annotation, it will override value of environment variable TRACING_CAPTURE_ERROR
12+
*/
13+
ERROR,
14+
/**
15+
* Enabled annotation to capture both response error from the method. If this mode is explicitly overridden
16+
* on {@link Tracing} annotation, it will override value of environment variables TRACING_CAPTURE_RESPONSE
17+
* and TRACING_CAPTURE_ERROR
18+
*/
19+
RESPONSE_AND_ERROR,
20+
/**
21+
* Disables annotation to capture both response and error from the method. If this mode is explicitly overridden
22+
* on {@link Tracing} annotation, it will override values of environment variable TRACING_CAPTURE_RESPONSE
23+
* and TRACING_CAPTURE_ERROR
24+
*/
25+
DISABLED,
26+
/**
27+
* Enables/Disables annotation to capture response and error from the method based on the value of
28+
* environment variable TRACING_CAPTURE_RESPONSE and TRACING_CAPTURE_ERROR
29+
*/
30+
ENVIRONMENT_VAR
31+
}

powertools-tracing/src/main/java/software/amazon/lambda/powertools/tracing/Tracing.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,20 @@
4848
@Target(ElementType.METHOD)
4949
public @interface Tracing {
5050
String namespace() default "";
51+
/**
52+
* @deprecated As of release 1.2.0, replaced by captureMode()
53+
* in order to support different modes and support via
54+
* environment variables
55+
*/
56+
@Deprecated
5157
boolean captureResponse() default true;
58+
/**
59+
* @deprecated As of release 1.2.0, replaced by captureMode()
60+
* in order to support different modes and support via
61+
* environment variables
62+
*/
63+
@Deprecated
5264
boolean captureError() default true;
5365
String segmentName() default "";
66+
CaptureMode captureMode() default CaptureMode.ENVIRONMENT_VAR;
5467
}

powertools-tracing/src/main/java/software/amazon/lambda/powertools/tracing/internal/LambdaTracingAspect.java

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@
1313
*/
1414
package software.amazon.lambda.powertools.tracing.internal;
1515

16+
import java.util.function.Supplier;
17+
1618
import com.amazonaws.xray.AWSXRay;
1719
import com.amazonaws.xray.entities.Subsegment;
18-
import java.util.function.Supplier;
1920
import org.aspectj.lang.ProceedingJoinPoint;
2021
import org.aspectj.lang.annotation.Around;
2122
import org.aspectj.lang.annotation.Aspect;
@@ -32,7 +33,6 @@
3233

3334
@Aspect
3435
public final class LambdaTracingAspect {
35-
3636
@SuppressWarnings({"EmptyMethod"})
3737
@Pointcut("@annotation(tracing)")
3838
public void callAt(Tracing tracing) {
@@ -52,16 +52,19 @@ public Object around(ProceedingJoinPoint pjp,
5252
segment.putAnnotation("ColdStart", isColdStart());
5353
}
5454

55+
boolean captureResponse = captureResponse(tracing);
56+
boolean captureError = captureError(tracing);
57+
5558
try {
5659
Object methodReturn = pjp.proceed(proceedArgs);
57-
if (tracing.captureResponse()) {
60+
if (captureResponse) {
5861
segment.putMetadata(namespace(tracing), pjp.getSignature().getName() + " response", methodReturn);
5962
}
6063

6164
coldStartDone();
6265
return methodReturn;
6366
} catch (Exception e) {
64-
if (tracing.captureError()) {
67+
if (captureError) {
6568
segment.putMetadata(namespace(tracing), pjp.getSignature().getName() + " error", e);
6669
}
6770
throw e;
@@ -72,6 +75,34 @@ public Object around(ProceedingJoinPoint pjp,
7275
}
7376
}
7477

78+
private boolean captureResponse(Tracing powerToolsTracing) {
79+
switch (powerToolsTracing.captureMode()) {
80+
case ENVIRONMENT_VAR:
81+
Boolean captureResponse = environmentVariable("TRACING_CAPTURE_RESPONSE");
82+
return null != captureResponse ? captureResponse : powerToolsTracing.captureResponse();
83+
case RESPONSE:
84+
case RESPONSE_AND_ERROR:
85+
return true;
86+
case DISABLED:
87+
default:
88+
return false;
89+
}
90+
}
91+
92+
private boolean captureError(Tracing powerToolsTracing) {
93+
switch (powerToolsTracing.captureMode()) {
94+
case ENVIRONMENT_VAR:
95+
Boolean captureError = environmentVariable("TRACING_CAPTURE_ERROR");
96+
return null != captureError ? captureError : powerToolsTracing.captureError();
97+
case ERROR:
98+
case RESPONSE_AND_ERROR:
99+
return true;
100+
case DISABLED:
101+
default:
102+
return false;
103+
}
104+
}
105+
75106
private String customSegmentNameOrDefault(Tracing powerToolsTracing, Supplier<String> defaultSegmentName) {
76107
String segmentName = powerToolsTracing.segmentName();
77108
return segmentName.isEmpty() ? defaultSegmentName.get() : segmentName;
@@ -85,4 +116,9 @@ private boolean placedOnHandlerMethod(ProceedingJoinPoint pjp) {
85116
return isHandlerMethod(pjp)
86117
&& (placedOnRequestHandler(pjp) || placedOnStreamHandler(pjp));
87118
}
119+
120+
private Boolean environmentVariable(String tracing_capture_response) {
121+
return null != SystemWrapper.getenv(tracing_capture_response)
122+
? Boolean.valueOf(SystemWrapper.getenv(tracing_capture_response)) : null;
123+
}
88124
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package software.amazon.lambda.powertools.tracing.internal;
2+
3+
public class SystemWrapper {
4+
public SystemWrapper() {
5+
}
6+
7+
public static String getenv(String name) {
8+
return System.getenv(name);
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2020 Amazon.com, Inc. or its affiliates.
3+
* Licensed under the Apache License, Version 2.0 (the
4+
* "License"); you may not use this file except in compliance
5+
* with the License. You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
* limitations under the License.
12+
*
13+
*/
14+
package software.amazon.lambda.powertools.tracing.handlers;
15+
16+
import com.amazonaws.services.lambda.runtime.Context;
17+
import com.amazonaws.services.lambda.runtime.RequestHandler;
18+
import software.amazon.lambda.powertools.tracing.Tracing;
19+
20+
import static software.amazon.lambda.powertools.tracing.CaptureMode.RESPONSE_AND_ERROR;
21+
22+
public class PowerTracerToolEnabledExplicitlyForResponseAndError implements RequestHandler<Object, Object> {
23+
24+
@Override
25+
@Tracing(namespace = "lambdaHandler", captureMode = RESPONSE_AND_ERROR)
26+
public Object handleRequest(Object input, Context context) {
27+
return null;
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2020 Amazon.com, Inc. or its affiliates.
3+
* Licensed under the Apache License, Version 2.0 (the
4+
* "License"); you may not use this file except in compliance
5+
* with the License. You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
* limitations under the License.
12+
*
13+
*/
14+
package software.amazon.lambda.powertools.tracing.handlers;
15+
16+
import com.amazonaws.services.lambda.runtime.Context;
17+
import com.amazonaws.services.lambda.runtime.RequestHandler;
18+
import software.amazon.lambda.powertools.tracing.Tracing;
19+
20+
import static software.amazon.lambda.powertools.tracing.CaptureMode.ERROR;
21+
22+
public class PowerTracerToolEnabledForError implements RequestHandler<Object, Object> {
23+
24+
@Override
25+
@Tracing(namespace = "lambdaHandler", captureMode = ERROR)
26+
public Object handleRequest(Object input, Context context) {
27+
throw new RuntimeException("I am failing!");
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2020 Amazon.com, Inc. or its affiliates.
3+
* Licensed under the Apache License, Version 2.0 (the
4+
* "License"); you may not use this file except in compliance
5+
* with the License. You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
* limitations under the License.
12+
*
13+
*/
14+
package software.amazon.lambda.powertools.tracing.handlers;
15+
16+
import com.amazonaws.services.lambda.runtime.Context;
17+
import com.amazonaws.services.lambda.runtime.RequestHandler;
18+
import software.amazon.lambda.powertools.tracing.Tracing;
19+
20+
import static software.amazon.lambda.powertools.tracing.CaptureMode.RESPONSE;
21+
22+
public class PowerTracerToolEnabledForResponse implements RequestHandler<Object, Object> {
23+
24+
@Override
25+
@Tracing(namespace = "lambdaHandler", captureMode = RESPONSE)
26+
public Object handleRequest(Object input, Context context) {
27+
return null;
28+
}
29+
}

powertools-tracing/src/test/java/software/amazon/lambda/powertools/tracing/handlers/PowerTracerToolEnabledForStreamWithNoMetaData.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,19 @@
1313
*/
1414
package software.amazon.lambda.powertools.tracing.handlers;
1515

16+
import java.io.InputStream;
17+
import java.io.OutputStream;
18+
1619
import com.amazonaws.services.lambda.runtime.Context;
1720
import com.amazonaws.services.lambda.runtime.RequestStreamHandler;
1821
import software.amazon.lambda.powertools.tracing.Tracing;
1922

20-
import java.io.InputStream;
21-
import java.io.OutputStream;
23+
import static software.amazon.lambda.powertools.tracing.CaptureMode.DISABLED;
2224

2325
public class PowerTracerToolEnabledForStreamWithNoMetaData implements RequestStreamHandler {
2426

2527
@Override
26-
@Tracing(captureResponse = false, captureError = false)
28+
@Tracing(captureMode = DISABLED)
2729
public void handleRequest(InputStream input, OutputStream output, Context context) {
2830

2931
}

powertools-tracing/src/test/java/software/amazon/lambda/powertools/tracing/handlers/PowerTracerToolEnabledWithNoMetaData.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@
1717
import com.amazonaws.services.lambda.runtime.RequestHandler;
1818
import software.amazon.lambda.powertools.tracing.Tracing;
1919

20+
import static software.amazon.lambda.powertools.tracing.CaptureMode.DISABLED;
21+
2022
public class PowerTracerToolEnabledWithNoMetaData implements RequestHandler<Object, Object> {
2123

2224
@Override
23-
@Tracing(captureResponse = false, captureError = false)
25+
@Tracing(captureMode = DISABLED)
2426
public Object handleRequest(Object input, Context context) {
2527
return null;
2628
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2020 Amazon.com, Inc. or its affiliates.
3+
* Licensed under the Apache License, Version 2.0 (the
4+
* "License"); you may not use this file except in compliance
5+
* with the License. You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
* limitations under the License.
12+
*
13+
*/
14+
package software.amazon.lambda.powertools.tracing.handlers;
15+
16+
import com.amazonaws.services.lambda.runtime.Context;
17+
import com.amazonaws.services.lambda.runtime.RequestHandler;
18+
import software.amazon.lambda.powertools.tracing.Tracing;
19+
20+
import static software.amazon.lambda.powertools.tracing.CaptureMode.DISABLED;
21+
22+
public class PowerTracerToolEnabledWithNoMetaDataDeprecated implements RequestHandler<Object, Object> {
23+
24+
@Override
25+
@Tracing(captureResponse = false, captureError = false)
26+
public Object handleRequest(Object input, Context context) {
27+
return null;
28+
}
29+
}

0 commit comments

Comments
 (0)