-
Notifications
You must be signed in to change notification settings - Fork 90
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
...lambda/powertools/tracing/handlers/PowerTracerToolEnabledForResponseWithCustomMapper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.