Skip to content
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

Support loading plugins from multiple packages. #948

Merged
merged 2 commits into from
Jan 31, 2022

Conversation

dlvenable
Copy link
Member

@dlvenable dlvenable commented Jan 28, 2022

Description

This PR allows Data Prepper to support plugins in packages other than com.amazon.dataprepper.plugins.

Data Prepper reads all resources (files embedded in jars) of META-INF/data-prepper.plugins.properties. It then looks for a property org.opensearch.dataprepper.plugin.packages from each one. The value of this property is a comma-delimited list of packages.

Data Prepper will scan all of these packages for plugins.

Note that because of the uber-jar, Data Prepper can only have one file for all packages. However, plugin authors can create their own jar files with this file. As long as it is on the classpath, their custom plugin will be available.

It may be worthwhile to move some plugins in future PRs.

Issues Resolved

#379

Check List

  • New functionality includes testing.
  • New functionality has been documented.
    • New functionality has javadoc added
  • Commits are signed with a real name per the DCO

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.

@dlvenable dlvenable requested a review from a team as a code owner January 28, 2022 02:03
@dlvenable
Copy link
Member Author

I created a sample Data Prepper plugin which demonstrates this behavior: https://github.com/dlvenable/data-prepper-sample-plugin

@codecov-commenter
Copy link

codecov-commenter commented Jan 28, 2022

Codecov Report

Merging #948 (b8fc921) into main (f7cdbd7) will decrease coverage by 0.03%.
The diff coverage is 89.74%.

Impacted file tree graph

@@             Coverage Diff              @@
##               main     #948      +/-   ##
============================================
- Coverage     91.40%   91.36%   -0.04%     
- Complexity      673      683      +10     
============================================
  Files            83       84       +1     
  Lines          1955     1993      +38     
  Branches        165      168       +3     
============================================
+ Hits           1787     1821      +34     
- Misses          129      132       +3     
- Partials         39       40       +1     
Impacted Files Coverage Δ
...zon/dataprepper/plugin/PluginPackagesSupplier.java 89.18% <89.18%> (ø)
...on/dataprepper/plugin/ClasspathPluginProvider.java 100.00% <100.00%> (ø)
...va/com/amazon/dataprepper/model/record/Record.java 100.00% <0.00%> (ø)
...mazon/dataprepper/model/record/RecordMetadata.java 100.00% <0.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update f7cdbd7...b8fc921. Read the comment docs.

graytaylor0
graytaylor0 previously approved these changes Jan 28, 2022
Copy link
Member

@graytaylor0 graytaylor0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really cool! Just had a question to help better my understanding.

Comment on lines 75 to 76
pluginPropertiesInputStream = pluginPropertiesUrl.openStream();
pluginProperties.load(pluginPropertiesInputStream);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Help me understand what is happening here a little better. So you add your package name to the end of META-INF/data-prepper.plugins.properties file under the org.opensearch.dataprepper.plugin.packages variable. Then here we are actually connecting to the package URL and downloading the properties of that package (like package name, and the distribution URL) so we know where to actually go to get the package?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not downloading anything. Issue #321 discusses downloading jars.

As a developer, you can create your own plugin jar file. If you do, you will also include the META-INF/data-prepper.plugins.properties file and specify your plugin package. You can see that in my sample.

Then if you run Data Prepper with your plugin in the classpath, Data Prepper will read this file and be able to scan that package from the local jar file.

Now running Data Prepper with a jar in the classpath is a little tricky since we have the uber-jar. You can see how I did it in the sample. I expect that #305 will help with this in the future since Data Prepper could include all jars in a plugins directory.

Comment on lines 73 to 75
final InputStream pluginPropertiesInputStream;
try {
pluginPropertiesInputStream = pluginPropertiesUrl.openStream();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Declaring and assigning pluginPropertiesInputStream can be a single line.

try {
    final InputStream pluginPropertiesInputStream = pluginPropertiesUrl.openStream();

Comment on lines 81 to 83
final String packagesRawString = pluginProperties.getProperty("org.opensearch.dataprepper.plugin.packages");

final String[] currentPackageNames = packagesRawString.split(",");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getProperty will return null if "org.opensearch.dataprepper.plugin.packages" does not exist. That would result in a NPE on line 83.

}

if(packageNames.isEmpty()) {
packageNames.add(DEFAULT_PLUGINS_CLASSPATH);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Take it or leave it: I might suggest adding this warning statement here to be consistent with the messaging you are providing in getUniquePackageNames:

LOG.warn("Unable to load packages from data-prepper.plugins.properties file. Reverting to default plugin package.");

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good suggestion! I took it.

Signed-off-by: David Venable <[email protected]>
@dlvenable dlvenable merged commit e5434f6 into opensearch-project:main Jan 31, 2022
@dlvenable dlvenable deleted the plugin-packages-379 branch May 6, 2023 14:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants