Skip to content

feat(tracing): ability to override object mapper #698

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

Merged
merged 3 commits into from
Jan 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions docs/core/tracing.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,38 @@ context for an operation using any native object.
}
```

## Override default object mapper

You can optionally choose to override default object mapper which is used to serialize method response and exceptions when enabled. You might
want to supply custom object mapper in order to control how serialisation is done, for example, when you want to log only
specific fields from received event due to security.

=== "App.java"

```java hl_lines="10-14"
import software.amazon.lambda.powertools.tracing.Tracing;
import software.amazon.lambda.powertools.tracing.TracingUtils;
import static software.amazon.lambda.powertools.tracing.CaptureMode.RESPONSE;

/**
* Handler for requests to Lambda function.
*/
public class App implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
static {
ObjectMapper objectMapper = new ObjectMapper();
SimpleModule simpleModule = new SimpleModule();
objectMapper.registerModule(simpleModule);

TracingUtils.defaultObjectMapper(objectMapper);
}

@Tracing(captureMode = RESPONSE)
public APIGatewayProxyResponseEvent handleRequest(final APIGatewayProxyRequestEvent input, final Context context) {
...
}
}
```

## Utilities

Tracing modules comes with certain utility method when you don't want to use annotation for capturing a code block
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
*/
package software.amazon.lambda.powertools.tracing;

import java.util.function.Consumer;
import com.amazonaws.xray.AWSXRay;
import com.amazonaws.xray.entities.Entity;
import com.amazonaws.xray.entities.Subsegment;

import java.util.function.Consumer;
import com.fasterxml.jackson.databind.ObjectMapper;

import static software.amazon.lambda.powertools.core.internal.LambdaHandlerProcessor.serviceName;

Expand All @@ -27,6 +27,7 @@
*
*/
public final class TracingUtils {
private static ObjectMapper objectMapper;

/**
* Put an annotation to the current subsegment with a String value.
Expand Down Expand Up @@ -155,4 +156,18 @@ public static void withSubsegment(String namespace, String name, Consumer<Subseg
AWSXRay.endSubsegment();
}
}

/**
* Sets the instance of ObjectMapper object which is used for serialising object response when
* {@code @Tracing(captureMode=CaptureMode.RESPONSE)}.
*
* @param objectMapper Custom implementation of object mapper to be used for serializing response
*/
public static void defaultObjectMapper(ObjectMapper objectMapper) {
TracingUtils.objectMapper = objectMapper;
}

public static ObjectMapper objectMapper() {
return objectMapper;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
package software.amazon.lambda.powertools.tracing.internal;

import java.util.function.Supplier;

import com.amazonaws.xray.AWSXRay;
import com.amazonaws.xray.entities.Subsegment;
import org.aspectj.lang.ProceedingJoinPoint;
Expand All @@ -30,6 +29,7 @@
import static software.amazon.lambda.powertools.core.internal.LambdaHandlerProcessor.placedOnRequestHandler;
import static software.amazon.lambda.powertools.core.internal.LambdaHandlerProcessor.placedOnStreamHandler;
import static software.amazon.lambda.powertools.core.internal.LambdaHandlerProcessor.serviceName;
import static software.amazon.lambda.powertools.tracing.TracingUtils.objectMapper;

@Aspect
public final class LambdaTracingAspect {
Expand Down Expand Up @@ -59,7 +59,7 @@ public Object around(ProceedingJoinPoint pjp,
try {
Object methodReturn = pjp.proceed(proceedArgs);
if (captureResponse) {
segment.putMetadata(namespace(tracing), pjp.getSignature().getName() + " response", methodReturn);
segment.putMetadata(namespace(tracing), pjp.getSignature().getName() + " response", null != objectMapper() ? objectMapper().writeValueAsString(methodReturn): methodReturn);
}

if (placedOnHandlerMethod(pjp)) {
Expand All @@ -69,7 +69,7 @@ public Object around(ProceedingJoinPoint pjp,
return methodReturn;
} catch (Exception e) {
if (captureError) {
segment.putMetadata(namespace(tracing), pjp.getSignature().getName() + " error", e);
segment.putMetadata(namespace(tracing), pjp.getSignature().getName() + " error", null != objectMapper() ? objectMapper().writeValueAsString(e) : e);
}
throw e;
} finally {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright 2020 Amazon.com, Inc. or its affiliates.
* Licensed under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package software.amazon.lambda.powertools.tracing.handlers;

import java.io.IOException;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import software.amazon.lambda.powertools.tracing.Tracing;
import software.amazon.lambda.powertools.tracing.TracingUtils;

import static software.amazon.lambda.powertools.tracing.CaptureMode.RESPONSE;

public class PowerTracerToolEnabledForResponseWithCustomMapper implements RequestHandler<Object, Object> {
static {
ObjectMapper objectMapper = new ObjectMapper();
SimpleModule simpleModule = new SimpleModule();
simpleModule.addSerializer(ChildClass.class, new ChildSerializer());
objectMapper.registerModule(simpleModule);

TracingUtils.defaultObjectMapper(objectMapper);
}
@Override
@Tracing(namespace = "lambdaHandler", captureMode = RESPONSE)
public Object handleRequest(Object input, Context context) {
ParentClass parentClass = new ParentClass("parent");
ChildClass childClass = new ChildClass("child", parentClass);
parentClass.setC(childClass);
return parentClass;
}

public class ParentClass {
public String name;
public ChildClass c;

public ParentClass(String name) {
this.name = name;
}

public void setC(ChildClass c) {
this.c = c;
}
}

public class ChildClass {
public String name;
public ParentClass p;

public ChildClass(String name, ParentClass p) {
this.name = name;
this.p = p;
}
}

public static class ChildSerializer extends StdSerializer<ChildClass> {

public ChildSerializer() {
this(null);
}

public ChildSerializer(Class<ChildClass> t) {
super(t);
}

@Override
public void serialize(ChildClass value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
jgen.writeStartObject();
jgen.writeStringField("name", value.name);
jgen.writeStringField("p", value.p.name);
jgen.writeEndObject();
}
}
}
Loading