opensearch-project / opensearch-project/data-prepper
Provide an EventFactory
@dlvenable is already working on this.
Since Feb 12, 2024.
- Dominant language
- Java
- Stars
- 374
- Forks
- 354
- Avg merge
- 3d 18h
- Merged PRs (30d)
- 8
Description
Is your feature request related to a problem? Please describe.
Creating events using a static factory has some problems:
- The types must be known at compile-time for any source that uses it.
- Cloning events is fragile and leaks code. This can be seen in peer forwarder.
- We have to expose implementations like
JacksonEvent. - Changing the implementation for testing is not easy with the current approach.
Describe the solution you'd like
Create an EventFactory class which can provide a more flexible mechanism for creating events.
It might look like the following initially:
public interface EventFactory {
<T extends Builder<T>> Builder<T> builder();
Event fromMessage(String message);
Event copy(Event event);
}
We also might want to change the builder() interface to take in require arguments (e.g. type). This makes it easier to catch errors at compile-time rather than runtime.
We could eventually have a class which allows for registering new types. I suggest that we follow on with this later. But, to help convey the concept, I put together a possible interface. In this way, sources could register a type using a string and have it implemented with a concrete class.
public interface EventTypeRegistry {
<T extends Event> void registerType(String type, Class<T> interfaceClass, Class<? extends T> implementationClass);
}
Describe alternatives you've considered (Optional)
We could add static builder methods to the Event class directly. And we could even support registration on these static methods. Such changes would cross all of Data Prepper, and that is probably acceptable. But, we have a DI framework which can make a factory class easy enough to implement. And this solution would be be as easy to for clients to use when writing unit tests.
Tasks
- Initial class model with Log model
- Support as test class
- Split Gradle packages for event model and testing
- Support Metric events
- Support Trace events
- Support Document event
- Support OTel Log event
- Update sources to use new EventFactory
- Update codecs to use new EventFactory
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.