Skip to content

Commit 4d6d22e

Browse files
committed
Restore original example.
1 parent 3a3ccff commit 4d6d22e

File tree

5 files changed

+62
-41
lines changed

5 files changed

+62
-41
lines changed

examples/powertools-examples-core-utilities/sam/pom.xml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
</dependency>
2323
<dependency>
2424
<groupId>software.amazon.lambda</groupId>
25-
<artifactId>powertools-logging-logback</artifactId>
25+
<artifactId>powertools-logging-log4j</artifactId>
2626
<version>${project.version}</version>
2727
</dependency>
2828
<dependency>
@@ -99,10 +99,19 @@
9999
</goals>
100100
<configuration>
101101
<createDependencyReducedPom>false</createDependencyReducedPom>
102+
<transformers>
103+
<transformer implementation="org.apache.logging.log4j.maven.plugins.shade.transformer.Log4j2PluginCacheFileTransformer"/>
104+
</transformers>
102105
</configuration>
103106
</execution>
104107
</executions>
105-
108+
<dependencies>
109+
<dependency>
110+
<groupId>org.apache.logging.log4j</groupId>
111+
<artifactId>log4j-transform-maven-shade-plugin-extensions</artifactId>
112+
<version>0.2.0</version>
113+
</dependency>
114+
</dependencies>
106115
</plugin>
107116
<!-- Don't deploy the example -->
108117
<plugin>

examples/powertools-examples-core-utilities/sam/src/main/java/helloworld/App.java

Lines changed: 50 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,16 @@
1414

1515
package helloworld;
1616

17+
import static software.amazon.lambda.powertools.logging.argument.StructuredArguments.entry;
18+
import static software.amazon.lambda.powertools.tracing.TracingUtils.putMetadata;
19+
20+
import java.io.BufferedReader;
21+
import java.io.IOException;
22+
import java.io.InputStreamReader;
23+
import java.net.URL;
1724
import java.util.HashMap;
1825
import java.util.Map;
26+
import java.util.stream.Collectors;
1927

2028
import org.slf4j.Logger;
2129
import org.slf4j.LoggerFactory;
@@ -27,53 +35,74 @@
2735
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
2836

2937
import software.amazon.lambda.powertools.logging.Logging;
30-
import software.amazon.lambda.powertools.logging.PowertoolsLogging;
38+
import software.amazon.lambda.powertools.metrics.FlushMetrics;
39+
import software.amazon.lambda.powertools.metrics.Metrics;
40+
import software.amazon.lambda.powertools.metrics.MetricsFactory;
41+
import software.amazon.lambda.powertools.metrics.model.DimensionSet;
42+
import software.amazon.lambda.powertools.metrics.model.MetricResolution;
43+
import software.amazon.lambda.powertools.metrics.model.MetricUnit;
44+
import software.amazon.lambda.powertools.tracing.CaptureMode;
45+
import software.amazon.lambda.powertools.tracing.Tracing;
46+
import software.amazon.lambda.powertools.tracing.TracingUtils;
3147

3248
/**
3349
* Handler for requests to Lambda function.
3450
*/
3551
public class App implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
3652
private static final Logger log = LoggerFactory.getLogger(App.class);
53+
private static final Metrics metrics = MetricsFactory.getMetricsInstance();
3754

38-
public App() {
39-
// Flush immediately because no trace ID is set yet
40-
log.debug("Constructor DEBUG - should not be buffered (no trace ID)");
41-
log.info("Constructor INFO - should not be buffered (no trace ID)");
42-
}
43-
44-
@Logging(logEvent = false)
55+
@Logging(logEvent = true, samplingRate = 0.7)
56+
@Tracing(captureMode = CaptureMode.RESPONSE_AND_ERROR)
57+
@FlushMetrics(namespace = "ServerlessAirline", service = "payment", captureColdStart = true)
4558
public APIGatewayProxyResponseEvent handleRequest(final APIGatewayProxyRequestEvent input, final Context context) {
46-
// Manually set trace ID for testing in SAM local
47-
System.setProperty("com.amazonaws.xray.traceHeader",
48-
"Root=1-63441c4a-abcdef012345678912345678;Parent=0123456789abcdef;Sampled=1");
49-
5059
Map<String, String> headers = new HashMap<>();
5160

5261
headers.put("Content-Type", "application/json");
5362
headers.put("X-Custom-Header", "application/json");
5463

55-
log.debug("DEBUG 1");
56-
MDC.put("test", "willBeLogged");
57-
log.debug("DEBUG 2");
58-
log.info("INFO 1");
64+
metrics.addMetric("CustomMetric1", 1, MetricUnit.COUNT);
65+
66+
DimensionSet dimensionSet = new DimensionSet();
67+
dimensionSet.addDimension("AnotherService", "CustomService");
68+
dimensionSet.addDimension("AnotherService1", "CustomService1");
69+
metrics.flushSingleMetric("CustomMetric2", 1, MetricUnit.COUNT, "Another", dimensionSet);
5970

60-
// Manually flush buffer to show buffered debug logs
61-
// PowertoolsLogging.flushBuffer();
62-
log.error("Some error happened");
71+
metrics.addMetric("CustomMetric3", 1, MetricUnit.COUNT, MetricResolution.HIGH);
72+
73+
MDC.put("test", "willBeLogged");
6374

6475
APIGatewayProxyResponseEvent response = new APIGatewayProxyResponseEvent()
6576
.withHeaders(headers);
6677
try {
67-
String output = String.format("{ \"message\": \"hello world\", \"location\": \"%s\" }", "Test");
78+
final String pageContents = this.getPageContents("https://checkip.amazonaws.com");
79+
log.info("", entry("ip", pageContents));
80+
TracingUtils.putAnnotation("Test", "New");
81+
String output = String.format("{ \"message\": \"hello world\", \"location\": \"%s\" }", pageContents);
82+
83+
TracingUtils.withSubsegment("loggingResponse", subsegment -> {
84+
String sampled = "log something out";
85+
log.info(sampled);
86+
log.info(output);
87+
});
6888

89+
log.info("After output");
6990
return response
7091
.withStatusCode(200)
7192
.withBody(output);
72-
} catch (RuntimeException e) {
93+
} catch (RuntimeException | IOException e) {
7394
return response
7495
.withBody("{}")
7596
.withStatusCode(500);
7697
}
7798
}
7899

100+
@Tracing(namespace = "getPageContents", captureMode = CaptureMode.DISABLED)
101+
private String getPageContents(String address) throws IOException {
102+
URL url = new URL(address);
103+
putMetadata("getPageContents", address);
104+
try (BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()))) {
105+
return br.lines().collect(Collectors.joining(System.lineSeparator()));
106+
}
107+
}
79108
}

examples/powertools-examples-core-utilities/sam/src/main/java/helloworld/AppStream.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ public void handleRequest(InputStream input, OutputStream output, Context contex
5656

5757
writer.write("{\"body\": \"" + System.currentTimeMillis() + "\"} ");
5858
} catch (IOException e) {
59-
log.error("Exception caught in handler", e);
6059
log.error("Something has gone wrong: ", e);
6160
}
6261
}

examples/powertools-examples-core-utilities/sam/src/main/resources/log4j2.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<Console name="JsonAppender" target="SYSTEM_OUT">
55
<JsonTemplateLayout eventTemplateUri="classpath:LambdaJsonLayout.json" />
66
</Console>
7-
<BufferingAppender name="BufferedAppender" bufferAtVerbosity="DEBUG" maxBytes="8">
7+
<BufferingAppender name="BufferedAppender" bufferAtVerbosity="DEBUG">
88
<AppenderRef ref="JsonAppender" />
99
</BufferingAppender>
1010
</Appenders>

examples/powertools-examples-core-utilities/sam/src/main/resources/logback.xml

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)