Search in sources :

Example 1 with Logging

use of software.amazon.lambda.powertools.logging.Logging in project di-ipv-cri-address-api by alphagov.

the class SessionHandler method handleRequest.

@Override
@Logging(correlationIdPath = CorrelationIdPathConstants.API_GATEWAY_REST)
@Metrics(captureColdStart = true)
public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context) {
    try {
        SessionRequest sessionRequest = addressSessionService.validateSessionRequest(input.getBody());
        eventProbe.addDimensions(Map.of("issuer", sessionRequest.getClientId()));
        UUID sessionId = addressSessionService.createAndSaveAddressSession(sessionRequest);
        eventProbe.counterMetric(EVENT_SESSION_CREATED).auditEvent(sessionRequest);
        return ApiGatewayResponseGenerator.proxyJsonResponse(HttpStatus.SC_CREATED, Map.of(SESSION_ID, sessionId.toString()));
    } catch (SessionValidationException e) {
        eventProbe.log(INFO, e).counterMetric(EVENT_SESSION_CREATED, 0d);
        return ApiGatewayResponseGenerator.proxyJsonResponse(HttpStatus.SC_BAD_REQUEST, ErrorResponse.SESSION_VALIDATION_ERROR);
    } catch (ClientConfigurationException e) {
        eventProbe.log(ERROR, e).counterMetric(EVENT_SESSION_CREATED, 0d);
        return ApiGatewayResponseGenerator.proxyJsonResponse(HttpStatus.SC_INTERNAL_SERVER_ERROR, ErrorResponse.SERVER_CONFIG_ERROR);
    }
}
Also used : SessionValidationException(uk.gov.di.ipv.cri.address.library.exception.SessionValidationException) UUID(java.util.UUID) SessionRequest(uk.gov.di.ipv.cri.address.library.domain.SessionRequest) ClientConfigurationException(uk.gov.di.ipv.cri.address.library.exception.ClientConfigurationException) Logging(software.amazon.lambda.powertools.logging.Logging) Metrics(software.amazon.lambda.powertools.metrics.Metrics)

Example 2 with Logging

use of software.amazon.lambda.powertools.logging.Logging in project aws-lambda-powertools-java by awslabs.

the class TracingLoggingStreamMessageHandler method handleRequest.

@Logging(logEvent = true)
@Tracing
@Override
public void handleRequest(InputStream input, OutputStream output, Context context) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    mapper.writeValue(output, mapper.readValue(input, Map.class));
}
Also used : Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Logging(software.amazon.lambda.powertools.logging.Logging) Tracing(software.amazon.lambda.powertools.tracing.Tracing)

Example 3 with Logging

use of software.amazon.lambda.powertools.logging.Logging in project serverless-webapp-mono-repo-ci-cd-java by aws-samples.

the class CreateCollectionHandler method handleRequest.

@Logging(logEvent = true)
public APIGatewayProxyResponseEvent handleRequest(final Map<String, Object> input, final Context context) {
    String collectionId = (String) ((Map) input.get("ResourceProperties")).get("CollectionName");
    if ("Delete".equals(input.get("RequestType"))) {
        callGracefully(input, context, () -> {
            DeleteCollectionResponse deleteCollectionResponse = client.deleteCollection(DeleteCollectionRequest.builder().collectionId(collectionId).build());
            if (deleteCollectionResponse.statusCode() == 200) {
                LOG.info("Resource deleted");
                sendResponse(input, context, "SUCCESS");
            }
        });
    } else {
        callGracefully(input, context, () -> {
            CreateCollectionResponse response = client.createCollection(CreateCollectionRequest.builder().collectionId(collectionId).build());
            if (response.statusCode() == 200) {
                LOG.info("Resource created");
                sendResponse(input, context, "SUCCESS");
            }
        });
    }
    return null;
}
Also used : CreateCollectionResponse(software.amazon.awssdk.services.rekognition.model.CreateCollectionResponse) DeleteCollectionResponse(software.amazon.awssdk.services.rekognition.model.DeleteCollectionResponse) Logging(software.amazon.lambda.powertools.logging.Logging)

Example 4 with Logging

use of software.amazon.lambda.powertools.logging.Logging in project aws-lambda-powertools-examples by aws-samples.

the class AppStream method handleRequest.

@Override
@Logging(logEvent = true)
@Metrics(namespace = "ServerlessAirline", service = "payment", captureColdStart = true)
public void handleRequest(InputStream input, OutputStream output, Context context) throws IOException {
    Map map = mapper.readValue(input, Map.class);
    System.out.println(map.size());
}
Also used : Map(java.util.Map) Logging(software.amazon.lambda.powertools.logging.Logging) Metrics(software.amazon.lambda.powertools.metrics.Metrics)

Example 5 with Logging

use of software.amazon.lambda.powertools.logging.Logging in project aws-lambda-powertools-examples by aws-samples.

the class SqsMessageSender method handleRequest.

@Logging(logEvent = true)
public String handleRequest(final ScheduledEvent input, final Context context) {
    String queueUrl = System.getenv("QUEUE_URL");
    // Push 5 messages on each invoke.
    List<SendMessageBatchRequestEntry> batchRequestEntries = IntStream.range(0, 5).mapToObj(value -> {
        Map<String, MessageAttributeValue> attributeValueHashMap = new HashMap<>();
        attributeValueHashMap.put("Key" + value, MessageAttributeValue.builder().dataType("String").stringValue("Value" + value).build());
        byte[] array = new byte[7];
        random.nextBytes(array);
        return SendMessageBatchRequestEntry.builder().messageAttributes(attributeValueHashMap).id(input.getId() + value).messageBody("Sample Message " + value).build();
    }).collect(toList());
    SendMessageBatchResponse sendMessageBatchResponse = sqsClient.sendMessageBatch(SendMessageBatchRequest.builder().queueUrl(queueUrl).entries(batchRequestEntries).build());
    log.info("Sent Message {}", sendMessageBatchResponse);
    return "Success";
}
Also used : UrlConnectionHttpClient(software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient) IntStream(java.util.stream.IntStream) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Context(com.amazonaws.services.lambda.runtime.Context) SqsClient(software.amazon.awssdk.services.sqs.SqsClient) HashMap(java.util.HashMap) Random(java.util.Random) RequestHandler(com.amazonaws.services.lambda.runtime.RequestHandler) SendMessageBatchResponse(software.amazon.awssdk.services.sqs.model.SendMessageBatchResponse) LoggingUtils(software.amazon.lambda.powertools.logging.LoggingUtils) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Logger(org.apache.logging.log4j.Logger) SendMessageBatchRequestEntry(software.amazon.awssdk.services.sqs.model.SendMessageBatchRequestEntry) MessageAttributeValue(software.amazon.awssdk.services.sqs.model.MessageAttributeValue) Map(java.util.Map) ScheduledEvent(com.amazonaws.services.lambda.runtime.events.ScheduledEvent) JodaModule(com.fasterxml.jackson.datatype.joda.JodaModule) LogManager(org.apache.logging.log4j.LogManager) Logging(software.amazon.lambda.powertools.logging.Logging) SendMessageBatchRequest(software.amazon.awssdk.services.sqs.model.SendMessageBatchRequest) SendMessageBatchRequestEntry(software.amazon.awssdk.services.sqs.model.SendMessageBatchRequestEntry) HashMap(java.util.HashMap) Map(java.util.Map) SendMessageBatchResponse(software.amazon.awssdk.services.sqs.model.SendMessageBatchResponse) Logging(software.amazon.lambda.powertools.logging.Logging)

Aggregations

Logging (software.amazon.lambda.powertools.logging.Logging)11 HashMap (java.util.HashMap)5 Map (java.util.Map)5 Metrics (software.amazon.lambda.powertools.metrics.Metrics)5 Tracing (software.amazon.lambda.powertools.tracing.Tracing)5 APIGatewayProxyResponseEvent (com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 Context (com.amazonaws.services.lambda.runtime.Context)2 RequestHandler (com.amazonaws.services.lambda.runtime.RequestHandler)2 IOException (java.io.IOException)2 LogManager (org.apache.logging.log4j.LogManager)2 Logger (org.apache.logging.log4j.Logger)2 LoggingUtils (software.amazon.lambda.powertools.logging.LoggingUtils)2 APIGatewayProxyRequestEvent (com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent)1 ScheduledEvent (com.amazonaws.services.lambda.runtime.events.ScheduledEvent)1 AWSXRay (com.amazonaws.xray.AWSXRay)1 Entity (com.amazonaws.xray.entities.Entity)1 JodaModule (com.fasterxml.jackson.datatype.joda.JodaModule)1 BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1