Skip to content
This repository was archived by the owner on Feb 23, 2023. It is now read-only.

Commit 8b20cbb

Browse files
chore: varied example of using core utilities
1 parent a8956e1 commit 8b20cbb

File tree

3 files changed

+65
-12
lines changed

3 files changed

+65
-12
lines changed

java/CoreUtilities/Function/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>powertools</groupId>
5-
<artifactId>CoreUtilities</artifactId>
5+
<artifactId>Function</artifactId>
66
<version>1.0</version>
77
<packaging>jar</packaging>
88
<name>A sample Hello World created for SAM CLI using powertools core utilities</name>

java/CoreUtilities/Function/src/main/java/helloworld/App.java

Lines changed: 63 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,51 +12,104 @@
1212
import com.amazonaws.services.lambda.runtime.RequestHandler;
1313
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
1414
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
15+
import com.amazonaws.xray.AWSXRay;
16+
import com.amazonaws.xray.entities.Entity;
1517
import org.apache.logging.log4j.LogManager;
1618
import org.apache.logging.log4j.Logger;
19+
import software.amazon.cloudwatchlogs.emf.model.DimensionSet;
20+
import software.amazon.cloudwatchlogs.emf.model.Unit;
21+
import software.amazon.lambda.powertools.logging.LoggingUtils;
1722
import software.amazon.lambda.powertools.logging.Logging;
1823
import software.amazon.lambda.powertools.metrics.Metrics;
24+
import software.amazon.lambda.powertools.tracing.CaptureMode;
25+
import software.amazon.lambda.powertools.tracing.TracingUtils;
1926
import software.amazon.lambda.powertools.tracing.Tracing;
2027

21-
import static software.amazon.lambda.powertools.tracing.CaptureMode.DISABLED;
28+
import static software.amazon.lambda.powertools.metrics.MetricsUtils.metricsLogger;
29+
import static software.amazon.lambda.powertools.metrics.MetricsUtils.withSingleMetric;
30+
import static software.amazon.lambda.powertools.tracing.TracingUtils.putMetadata;
31+
import static software.amazon.lambda.powertools.tracing.TracingUtils.withEntitySubsegment;
2232

2333
/**
2434
* Handler for requests to Lambda function.
2535
*/
2636
public class App implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
37+
private final static Logger log = LogManager.getLogger();
2738

28-
Logger log = LogManager.getLogger();
29-
30-
@Logging(logEvent = true)
31-
@Tracing(captureMode = DISABLED)
32-
@Metrics(captureColdStart = true)
39+
@Logging(logEvent = true, samplingRate = 0.7)
40+
@Tracing(captureMode = CaptureMode.RESPONSE_AND_ERROR)
41+
@Metrics(namespace = "ServerlessAirline", service = "payment", captureColdStart = true)
3342
public APIGatewayProxyResponseEvent handleRequest(final APIGatewayProxyRequestEvent input, final Context context) {
3443
Map<String, String> headers = new HashMap<>();
44+
3545
headers.put("Content-Type", "application/json");
3646
headers.put("X-Custom-Header", "application/json");
3747

48+
metricsLogger().putMetric("CustomMetric1", 1, Unit.COUNT);
49+
50+
withSingleMetric("CustomMetrics2", 1, Unit.COUNT, "Another", (metric) -> {
51+
metric.setDimensions(DimensionSet.of("AnotherService", "CustomService"));
52+
metric.setDimensions(DimensionSet.of("AnotherService1", "CustomService1"));
53+
});
54+
55+
LoggingUtils.appendKey("test", "willBeLogged");
56+
3857
APIGatewayProxyResponseEvent response = new APIGatewayProxyResponseEvent()
3958
.withHeaders(headers);
4059
try {
4160
final String pageContents = this.getPageContents("https://checkip.amazonaws.com");
61+
log.info(pageContents);
62+
TracingUtils.putAnnotation("Test", "New");
4263
String output = String.format("{ \"message\": \"hello world\", \"location\": \"%s\" }", pageContents);
4364

65+
TracingUtils.withSubsegment("loggingResponse", subsegment -> {
66+
String sampled = "log something out";
67+
log.info(sampled);
68+
log.info(output);
69+
});
70+
71+
threadOption1();
72+
73+
threadOption2();
74+
75+
log.info("After output");
4476
return response
4577
.withStatusCode(200)
4678
.withBody(output);
47-
} catch (IOException e) {
79+
} catch (IOException | InterruptedException e) {
4880
return response
4981
.withBody("{}")
5082
.withStatusCode(500);
5183
}
5284
}
5385

54-
@Tracing(namespace = "getPageContents")
86+
private void threadOption1() throws InterruptedException {
87+
final Entity traceEntity = AWSXRay.getTraceEntity();
88+
assert traceEntity != null;
89+
traceEntity.run(new Thread(this::log));
90+
}
91+
92+
private void threadOption2() throws InterruptedException {
93+
Entity traceEntity = AWSXRay.getTraceEntity();
94+
Thread anotherThread = new Thread(() -> withEntitySubsegment("inlineLog", traceEntity, subsegment -> {
95+
String var = "somethingToProcess";
96+
log.info("inside threaded logging inline {}", var);
97+
}));
98+
anotherThread.start();
99+
anotherThread.join();
100+
}
101+
102+
@Tracing
103+
private void log() {
104+
log.info("inside threaded logging for function");
105+
}
106+
107+
@Tracing(namespace = "getPageContents", captureMode = CaptureMode.DISABLED)
55108
private String getPageContents(String address) throws IOException {
56-
log.info("Retrieving {}", address);
57109
URL url = new URL(address);
110+
putMetadata("getPageContents", address);
58111
try (BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()))) {
59112
return br.lines().collect(Collectors.joining(System.lineSeparator()));
60113
}
61114
}
62-
}
115+
}

java/CoreUtilities/template.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Resources:
2222
HelloWorldFunction:
2323
Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
2424
Properties:
25-
CodeUri: HelloWorldFunction
25+
CodeUri: Function
2626
Handler: helloworld.App::handleRequest
2727
Environment: # More info about Env Vars: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#environment-object
2828
Variables:

0 commit comments

Comments
 (0)