|
14 | 14 |
|
15 | 15 | package helloworld; |
16 | 16 |
|
| 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; |
17 | 24 | import java.util.HashMap; |
18 | 25 | import java.util.Map; |
| 26 | +import java.util.stream.Collectors; |
19 | 27 |
|
20 | 28 | import org.slf4j.Logger; |
21 | 29 | import org.slf4j.LoggerFactory; |
|
27 | 35 | import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent; |
28 | 36 |
|
29 | 37 | 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; |
31 | 47 |
|
32 | 48 | /** |
33 | 49 | * Handler for requests to Lambda function. |
34 | 50 | */ |
35 | 51 | public class App implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> { |
36 | 52 | private static final Logger log = LoggerFactory.getLogger(App.class); |
| 53 | + private static final Metrics metrics = MetricsFactory.getMetricsInstance(); |
37 | 54 |
|
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) |
45 | 58 | 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 | | - |
50 | 59 | Map<String, String> headers = new HashMap<>(); |
51 | 60 |
|
52 | 61 | headers.put("Content-Type", "application/json"); |
53 | 62 | headers.put("X-Custom-Header", "application/json"); |
54 | 63 |
|
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); |
59 | 70 |
|
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"); |
63 | 74 |
|
64 | 75 | APIGatewayProxyResponseEvent response = new APIGatewayProxyResponseEvent() |
65 | 76 | .withHeaders(headers); |
66 | 77 | 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 | + }); |
68 | 88 |
|
| 89 | + log.info("After output"); |
69 | 90 | return response |
70 | 91 | .withStatusCode(200) |
71 | 92 | .withBody(output); |
72 | | - } catch (RuntimeException e) { |
| 93 | + } catch (RuntimeException | IOException e) { |
73 | 94 | return response |
74 | 95 | .withBody("{}") |
75 | 96 | .withStatusCode(500); |
76 | 97 | } |
77 | 98 | } |
78 | 99 |
|
| 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 | + } |
79 | 108 | } |
0 commit comments