Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: advanced logging #1539

Merged
merged 14 commits into from
Feb 28, 2024
359 changes: 133 additions & 226 deletions docs/core/logging.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import software.amazon.cloudwatchlogs.emf.model.DimensionSet;
import software.amazon.cloudwatchlogs.emf.model.Unit;
import software.amazon.lambda.powertools.logging.Logging;
import software.amazon.lambda.powertools.logging.LoggingUtils;
import software.amazon.lambda.powertools.metrics.Metrics;
import software.amazon.lambda.powertools.tracing.CaptureMode;
import software.amazon.lambda.powertools.tracing.Tracing;
Expand Down Expand Up @@ -62,7 +62,7 @@ public APIGatewayProxyResponseEvent handleRequest(final APIGatewayProxyRequestEv
metric.setDimensions(DimensionSet.of("AnotherService1", "CustomService1"));
});

LoggingUtils.appendKey("test", "willBeLogged");
MDC.put("test", "willBeLogged");

APIGatewayProxyResponseEvent response = new APIGatewayProxyResponseEvent()
.withHeaders(headers);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

package helloworld;

import static software.amazon.lambda.powertools.logging.argument.StructuredArguments.entry;
import static software.amazon.lambda.powertools.metrics.MetricsUtils.metricsLogger;
import static software.amazon.lambda.powertools.metrics.MetricsUtils.withSingleMetric;
import static software.amazon.lambda.powertools.tracing.TracingUtils.putMetadata;
Expand All @@ -31,10 +32,10 @@
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import software.amazon.cloudwatchlogs.emf.model.DimensionSet;
import software.amazon.cloudwatchlogs.emf.model.Unit;
import software.amazon.lambda.powertools.logging.Logging;
import software.amazon.lambda.powertools.logging.LoggingUtils;
import software.amazon.lambda.powertools.metrics.Metrics;
import software.amazon.lambda.powertools.tracing.CaptureMode;
import software.amazon.lambda.powertools.tracing.Tracing;
Expand Down Expand Up @@ -63,13 +64,13 @@ public APIGatewayProxyResponseEvent handleRequest(final APIGatewayProxyRequestEv
metric.setDimensions(DimensionSet.of("AnotherService1", "CustomService1"));
});

LoggingUtils.appendKey("test", "willBeLogged");
MDC.put("test", "willBeLogged");

APIGatewayProxyResponseEvent response = new APIGatewayProxyResponseEvent()
.withHeaders(headers);
try {
final String pageContents = this.getPageContents("https://checkip.amazonaws.com");
log.info(pageContents);
log.info("", entry("ip", pageContents));
TracingUtils.putAnnotation("Test", "New");
String output = String.format("{ \"message\": \"hello world\", \"location\": \"%s\" }", pageContents);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,21 @@ import com.amazonaws.services.lambda.runtime.RequestHandler
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent
import com.amazonaws.xray.entities.Subsegment
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.slf4j.MDC
import software.amazon.cloudwatchlogs.emf.logger.MetricsLogger
import software.amazon.cloudwatchlogs.emf.model.DimensionSet
import software.amazon.cloudwatchlogs.emf.model.Unit
import software.amazon.lambda.powertools.logging.Logging
import software.amazon.lambda.powertools.logging.LoggingUtils
import software.amazon.lambda.powertools.logging.argument.StructuredArguments.entry
import software.amazon.lambda.powertools.metrics.Metrics
import software.amazon.lambda.powertools.metrics.MetricsUtils
import software.amazon.lambda.powertools.tracing.CaptureMode
import software.amazon.lambda.powertools.tracing.Tracing
import software.amazon.lambda.powertools.tracing.TracingUtils
import java.io.BufferedReader
import java.io.IOException
import java.io.InputStreamReader
import java.net.URL
import java.util.stream.Collectors

/**
* Handler for requests to Lambda function.
Expand All @@ -44,19 +42,18 @@ class App : RequestHandler<APIGatewayProxyRequestEvent?, APIGatewayProxyResponse
@Logging(logEvent = true, samplingRate = 0.7)
@Tracing(captureMode = CaptureMode.RESPONSE_AND_ERROR)
@Metrics(namespace = "ServerlessAirline", service = "payment", captureColdStart = true)

override fun handleRequest(input: APIGatewayProxyRequestEvent?, context: Context?): APIGatewayProxyResponseEvent {
val headers = mapOf("Content-Type" to "application/json", "X-Custom-Header" to "application/json")
MetricsUtils.metricsLogger().putMetric("CustomMetric1", 1.0, Unit.COUNT)
MetricsUtils.withSingleMetric("CustomMetrics2", 1.0, Unit.COUNT, "Another") { metric: MetricsLogger ->
metric.setDimensions(DimensionSet.of("AnotherService", "CustomService"))
metric.setDimensions(DimensionSet.of("AnotherService1", "CustomService1"))
}
LoggingUtils.appendKey("test", "willBeLogged")
MDC.put("test", "willBeLogged")
val response = APIGatewayProxyResponseEvent().withHeaders(headers)
return try {
val pageContents = getPageContents("https://checkip.amazonaws.com")
log.info(pageContents)
log.info("", entry("ip", pageContents));
TracingUtils.putAnnotation("Test", "New")
val output = """
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

package helloworld;

import static software.amazon.lambda.powertools.logging.argument.StructuredArguments.entry;
import static software.amazon.lambda.powertools.metrics.MetricsUtils.metricsLogger;
import static software.amazon.lambda.powertools.metrics.MetricsUtils.withSingleMetric;
import static software.amazon.lambda.powertools.tracing.TracingUtils.putMetadata;
Expand All @@ -31,10 +32,10 @@
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import software.amazon.cloudwatchlogs.emf.model.DimensionSet;
import software.amazon.cloudwatchlogs.emf.model.Unit;
import software.amazon.lambda.powertools.logging.Logging;
import software.amazon.lambda.powertools.logging.LoggingUtils;
import software.amazon.lambda.powertools.metrics.Metrics;
import software.amazon.lambda.powertools.tracing.CaptureMode;
import software.amazon.lambda.powertools.tracing.Tracing;
Expand Down Expand Up @@ -63,13 +64,13 @@ public APIGatewayProxyResponseEvent handleRequest(final APIGatewayProxyRequestEv
metric.setDimensions(DimensionSet.of("AnotherService1", "CustomService1"));
});

LoggingUtils.appendKey("test", "willBeLogged");
MDC.put("test", "willBeLogged");

APIGatewayProxyResponseEvent response = new APIGatewayProxyResponseEvent()
.withHeaders(headers);
try {
final String pageContents = this.getPageContents("https://checkip.amazonaws.com");
log.info(pageContents);
log.info("", entry("ip", pageContents));
TracingUtils.putAnnotation("Test", "New");
String output = String.format("{ \"message\": \"hello world\", \"location\": \"%s\" }", pageContents);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

package helloworld;

import static software.amazon.lambda.powertools.logging.argument.StructuredArguments.entry;
import static software.amazon.lambda.powertools.metrics.MetricsUtils.metricsLogger;
import static software.amazon.lambda.powertools.metrics.MetricsUtils.withSingleMetric;
import static software.amazon.lambda.powertools.tracing.TracingUtils.putMetadata;
Expand All @@ -31,10 +32,10 @@
import java.util.stream.Collectors;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.slf4j.MDC;
import software.amazon.cloudwatchlogs.emf.model.DimensionSet;
import software.amazon.cloudwatchlogs.emf.model.Unit;
import software.amazon.lambda.powertools.logging.Logging;
import software.amazon.lambda.powertools.logging.LoggingUtils;
import software.amazon.lambda.powertools.metrics.Metrics;
import software.amazon.lambda.powertools.tracing.CaptureMode;
import software.amazon.lambda.powertools.tracing.Tracing;
Expand All @@ -44,8 +45,11 @@
* Handler for requests to Lambda function.
*/
public class App implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
private final static Logger log = LogManager.getLogger(App.class);
private static final Logger log = LogManager.getLogger(App.class);

@Logging(logEvent = true, samplingRate = 0.7)
@Tracing(captureMode = CaptureMode.RESPONSE_AND_ERROR)
@Metrics(namespace = "ServerlessAirline", service = "payment", captureColdStart = true)
public APIGatewayProxyResponseEvent handleRequest(final APIGatewayProxyRequestEvent input, final Context context) {
Map<String, String> headers = new HashMap<>();

Expand All @@ -60,13 +64,13 @@ public APIGatewayProxyResponseEvent handleRequest(final APIGatewayProxyRequestEv
metric.setDimensions(DimensionSet.of("AnotherService1", "CustomService1"));
});

LoggingUtils.appendKey("test", "willBeLogged");
MDC.put("test", "willBeLogged");

APIGatewayProxyResponseEvent response = new APIGatewayProxyResponseEvent()
.withHeaders(headers);
try {
final String pageContents = this.getPageContents("https://checkip.amazonaws.com");
log.info(pageContents);
log.info("", entry("ip", pageContents));
TracingUtils.putAnnotation("Test", "New");
String output = String.format("{ \"message\": \"hello world\", \"location\": \"%s\" }", pageContents);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

package helloworld;

import static software.amazon.lambda.powertools.logging.argument.StructuredArguments.entry;
import static software.amazon.lambda.powertools.metrics.MetricsUtils.metricsLogger;
import static software.amazon.lambda.powertools.metrics.MetricsUtils.withSingleMetric;
import static software.amazon.lambda.powertools.tracing.TracingUtils.putMetadata;
Expand All @@ -31,10 +32,10 @@
import java.util.stream.Collectors;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.slf4j.MDC;
import software.amazon.cloudwatchlogs.emf.model.DimensionSet;
import software.amazon.cloudwatchlogs.emf.model.Unit;
import software.amazon.lambda.powertools.logging.Logging;
import software.amazon.lambda.powertools.logging.LoggingUtils;
import software.amazon.lambda.powertools.metrics.Metrics;
import software.amazon.lambda.powertools.tracing.CaptureMode;
import software.amazon.lambda.powertools.tracing.Tracing;
Expand All @@ -44,14 +45,11 @@
* Handler for requests to Lambda function.
*/
public class App implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
private final static Logger log = LogManager.getLogger(App.class);
private static final Logger log = LogManager.getLogger(App.class);

// This is controlled by POWERTOOLS_LOGGER_SAMPLE_RATE environment variable
// @Logging(logEvent = true, samplingRate = 0.7)
// This is controlled by POWERTOOLS_METRICS_NAMESPACE environment variable
// @Metrics(namespace = "ServerlessAirline", service = "payment", captureColdStart = true)
// This is controlled by POWERTOOLS_TRACER_CAPTURE_ERROR environment variable
@Logging(logEvent = true, samplingRate = 0.7)
@Tracing(captureMode = CaptureMode.RESPONSE_AND_ERROR)
@Metrics(namespace = "ServerlessAirline", service = "payment", captureColdStart = true)
public APIGatewayProxyResponseEvent handleRequest(final APIGatewayProxyRequestEvent input, final Context context) {
Map<String, String> headers = new HashMap<>();

Expand All @@ -66,13 +64,13 @@ public APIGatewayProxyResponseEvent handleRequest(final APIGatewayProxyRequestEv
metric.setDimensions(DimensionSet.of("AnotherService1", "CustomService1"));
});

LoggingUtils.appendKey("test", "willBeLogged");
MDC.put("test", "willBeLogged");

APIGatewayProxyResponseEvent response = new APIGatewayProxyResponseEvent()
.withHeaders(headers);
try {
final String pageContents = this.getPageContents("https://checkip.amazonaws.com");
log.info(pageContents);
log.info("", entry("ip", pageContents));
TracingUtils.putAnnotation("Test", "New");
String output = String.format("{ \"message\": \"hello world\", \"location\": \"%s\" }", pageContents);

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@
<module>powertools-large-messages</module>
<module>powertools-e2e-tests</module>
<module>powertools-batch</module>
<module>examples</module>
<module>powertools-parameters/powertools-parameters-ssm</module>
<module>powertools-parameters/powertools-parameters-secrets</module>
<module>powertools-parameters/powertools-parameters-dynamodb</module>
<module>powertools-parameters/powertools-parameters-appconfig</module>
<module>powertools-parameters/powertools-parameters-tests</module>
<module>examples</module>
</modules>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
import com.amazonaws.services.lambda.runtime.RequestHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import software.amazon.lambda.powertools.logging.Logging;
import software.amazon.lambda.powertools.logging.LoggingUtils;

public class Function implements RequestHandler<Input, String> {
private static final Logger LOG = LoggerFactory.getLogger(Function.class);

@Logging
public String handleRequest(Input input, Context context) {
LoggingUtils.appendKeys(input.getKeys());
input.getKeys().forEach(MDC::put);
LOG.info(input.getMessage());

return "OK";
Expand Down
Loading
Loading