Search in sources :

Example 1 with Tracing

use of software.amazon.lambda.powertools.tracing.Tracing 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 2 with Tracing

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

the class IndexImageHandler method updateIndexDetails.

@Tracing
private void updateIndexDetails(final String faceId, final String fullname) {
    Map<String, AttributeValue> item = new HashMap<>();
    item.put("RekognitionId", AttributeValue.builder().s(faceId).build());
    item.put("FullName", AttributeValue.builder().s(fullname).build());
    PutItemResponse putItemResponse = dynamoDbClient.putItem(PutItemRequest.builder().tableName(TABLE_NAME).item(item).build());
    LOG.debug("Persistance response from dynamo db {}", putItemResponse);
}
Also used : AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) HashMap(java.util.HashMap) PutItemResponse(software.amazon.awssdk.services.dynamodb.model.PutItemResponse) Tracing(software.amazon.lambda.powertools.tracing.Tracing)

Example 3 with Tracing

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

the class RecognizeImageHandler method faceSearch.

@Tracing(captureMode = ERROR)
private List<FaceMatch> faceSearch(byte[] decodedImage) {
    try {
        SearchFacesByImageResponse searchFacesByImageResponse = rekognitionClient.searchFacesByImage(builder -> builder.collectionId(COLLECTION_ID).image(Image.builder().bytes(SdkBytes.fromByteArray(decodedImage)).build()).maxFaces(1).faceMatchThreshold(90f));
        LOG.debug("Service response for find face {}", searchFacesByImageResponse);
        return searchFacesByImageResponse.faceMatches();
    } catch (Exception e) {
        LOG.error("Failed getting find face result. Reason: {}", e.getMessage(), e);
        return emptyList();
    }
}
Also used : SearchFacesByImageResponse(software.amazon.awssdk.services.rekognition.model.SearchFacesByImageResponse) Tracing(software.amazon.lambda.powertools.tracing.Tracing)

Example 4 with Tracing

use of software.amazon.lambda.powertools.tracing.Tracing in project aws-lambda-powertools-examples by aws-samples.

the class App method getPageContents.

@Tracing(namespace = "getPageContents", captureMode = CaptureMode.DISABLED)
private String getPageContents(String address) throws IOException {
    URL url = new URL(address);
    putMetadata("getPageContents", address);
    try (BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()))) {
        return br.lines().collect(Collectors.joining(System.lineSeparator()));
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) URL(java.net.URL) Tracing(software.amazon.lambda.powertools.tracing.Tracing)

Example 5 with Tracing

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

the class ImageUploadHandler method handleRequest.

@Logging(logEvent = true, samplingRate = 0.5)
@Tracing
@Metrics(captureColdStart = true)
@Override
public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context) {
    APIGatewayProxyResponseEvent apiGatewayProxyResponseEvent = new APIGatewayProxyResponseEvent();
    Map<String, String> headers = new HashMap<>();
    headers.put("Access-Control-Allow-Origin", "*");
    apiGatewayProxyResponseEvent.withHeaders(headers);
    String contentType = input.getQueryStringParameters().getOrDefault("content-type", "");
    String fileExtension = input.getQueryStringParameters().getOrDefault("file-extension", "");
    if (contentType.isEmpty() || fileExtension.isEmpty()) {
        return apiGatewayProxyResponseEvent.withStatusCode(400).withBody("{\n" + "  \"message\": \"Both content-type and file-extension need to passed as query param!\" \n" + "}");
    }
    String fileName = UUID.randomUUID() + fileExtension;
    String imagePath = "index/static/" + fileName;
    String personName = input.getQueryStringParameters().getOrDefault("person-name", "");
    LOG.debug("File path to be saved {} in bucket {}", imagePath, S3_BUCKET);
    LOG.debug("Received metadata {}", personName);
    HashMap<String, String> metaData = new HashMap<>();
    metaData.put("fullname", personName);
    PresignedPutObjectRequest requestObject = client.presignPutObject(req -> req.putObjectRequest(obj -> obj.metadata(metaData).bucket(S3_BUCKET).key(imagePath).contentType(contentType)).signatureDuration(Duration.ofSeconds(60)));
    metricsLogger().putMetric("RemainingTime", context.getRemainingTimeInMillis(), Unit.MILLISECONDS);
    LOG.debug("Generated pre signed url {}", requestObject.url());
    LOG.debug("Generated pre signed details meta {}", requestObject.signedHeaders());
    return response(apiGatewayProxyResponseEvent, fileName, requestObject.url());
}
Also used : HashMap(java.util.HashMap) PresignedPutObjectRequest(software.amazon.awssdk.services.s3.presigner.model.PresignedPutObjectRequest) APIGatewayProxyResponseEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent) Logging(software.amazon.lambda.powertools.logging.Logging) Metrics(software.amazon.lambda.powertools.metrics.Metrics) Tracing(software.amazon.lambda.powertools.tracing.Tracing)

Aggregations

Tracing (software.amazon.lambda.powertools.tracing.Tracing)8 Logging (software.amazon.lambda.powertools.logging.Logging)4 HashMap (java.util.HashMap)3 APIGatewayProxyResponseEvent (com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent)2 AttributeValue (software.amazon.awssdk.services.dynamodb.model.AttributeValue)2 IndexFacesResponse (software.amazon.awssdk.services.rekognition.model.IndexFacesResponse)2 Metrics (software.amazon.lambda.powertools.metrics.Metrics)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1 URL (java.net.URL)1 Map (java.util.Map)1 GetItemResponse (software.amazon.awssdk.services.dynamodb.model.GetItemResponse)1 PutItemResponse (software.amazon.awssdk.services.dynamodb.model.PutItemResponse)1 FaceMatch (software.amazon.awssdk.services.rekognition.model.FaceMatch)1 SearchFacesByImageResponse (software.amazon.awssdk.services.rekognition.model.SearchFacesByImageResponse)1 HeadObjectResponse (software.amazon.awssdk.services.s3.model.HeadObjectResponse)1 PresignedPutObjectRequest (software.amazon.awssdk.services.s3.presigner.model.PresignedPutObjectRequest)1