-
Notifications
You must be signed in to change notification settings - Fork 207
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
added default mapper for mapping logstash config models to data prepper plugin model #559
Conversation
Codecov Report
@@ Coverage Diff @@
## main #559 +/- ##
=========================================
Coverage 92.28% 92.28%
Complexity 569 569
=========================================
Files 71 71
Lines 1725 1725
Branches 144 144
=========================================
Hits 1592 1592
Misses 102 102
Partials 31 31 Continue to review full report at Codecov.
|
@@ -12,5 +14,5 @@ | |||
* @param logstashPlugin A Logstash plugin with its attributes | |||
* @return A Data Prepper plugin with its attributes | |||
*/ | |||
PluginModel mapPlugin(LogstashPlugin logstashPlugin); | |||
PluginModel mapPlugin(LogstashPlugin logstashPlugin) throws IOException; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should not throw IOException
. We should wrap any such exceptions in the LogstashMappingException
.
throw new LogstashMappingException("file not found! " + logstashPlugin.getPluginName() + ".mapping.yaml"); | ||
} | ||
|
||
LogstashMappingModel logstashMappingModel = objectMapper.readValue(inputStream, LogstashMappingModel.class); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please catch this IOException
and wrap it in a LogstashMappingException
.
Something like:
try {
logstashMappingModel = objectMapper.readValue(inputStream, LogstashMappingModel.class);
} catch(IOException ex) {
throw new LogstashMappingException("Unable to parse mapping file " + logstashPlugin.getPluginName() + ".mapping.yaml", ex);
}
You will need to add an overloaded constructor to LogstashMappingException
.
*/ | ||
public class LogstashMappingException extends LogstashConfigurationException{ | ||
|
||
public LogstashMappingException(String errorMessage) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Per my comment on wrapping IOException
, you will need another constructor with (String message, Exception inner)
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, I'll update LogstashMappingException
@Override | ||
public PluginModel mapPlugin(LogstashPlugin logstashPlugin) throws IOException { | ||
|
||
InputStream inputStream = getClass().getClassLoader().getResourceAsStream(logstashPlugin.getPluginName() + ".mapping.yaml"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please extract a variable for this: logstashPlugin.getPluginName() + ".mapping.yaml"
.
You use it below.
And another comment suggests another place to use it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
|
||
InputStream inputStream = getClass().getClassLoader().getResourceAsStream(logstashPlugin.getPluginName() + ".mapping.yaml"); | ||
if (inputStream == null) { | ||
throw new LogstashMappingException("file not found! " + logstashPlugin.getPluginName() + ".mapping.yaml"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please rename this to: "Unable to find mapping resource " + ...
.
|
||
LogstashMappingModel logstashMappingModel = objectMapper.readValue(inputStream, LogstashMappingModel.class); | ||
if (logstashMappingModel.getPluginName() == null) | ||
throw new LogstashMappingException("plugin name cannot be null in mapping.yaml"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to see the following in this message:
- The actual name of the mapping resources (using extracted variable I suggested above).
- The property name as defined in the LogstashMappingModel:
pluginName
.
Perhaps something like: "The mapping file " + mappingResourceName + " has a null value for 'pluginName'."
.
(String) logstashMappingModel.getMappedAttributes().get(logstashAttribute.getAttributeName()), | ||
logstashAttribute.getAttributeValue().getValue() | ||
); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we may wish to log a warning for unmapped Logstash attributes, in an else
.
logstashPlugin.getAttributes().forEach(logstashAttribute -> { | ||
if (logstashMappingModel.getMappedAttributes().containsKey(logstashAttribute.getAttributeName())) { | ||
pluginSettings.put( | ||
(String) logstashMappingModel.getMappedAttributes().get(logstashAttribute.getAttributeName()), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should change the model for getMappedAttributes
. It should be Map<String, String>
since it always maps names.
Also, I think it might be clearer to rename it to attributeNames
.
* | ||
* @since 1.2 | ||
*/ | ||
public class DefaultPluginMapper implements LogstashPluginMapper { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This class should have unit tests as well.
bfa8cd1
to
90af350
Compare
@@ -11,6 +11,9 @@ repositories { | |||
dependencies { | |||
antlr "org.antlr:antlr4:4.9.2" | |||
implementation project(':data-prepper-api') | |||
implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-yaml' | |||
implementation 'com.fasterxml.jackson.core:jackson-databind' | |||
implementation 'org.slf4j:slf4j-simple:1.7.32' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be testImplementation
if we use it all. Otherwise, it goes into the main classpath, but there should be only one SLF4J in the classpath.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Without this there's a warning and it's not showing the Log.warn message in log
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
|
||
@BeforeEach | ||
void createObjectUnderTest() { | ||
defaultPluginMapper = spy(new DefaultPluginMapper()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this need to be a Spy?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, It's not required
90af350
to
fa9dd0c
Compare
Signed-off-by: Asif Sohail Mohammed <[email protected]>
Signed-off-by: Asif Sohail Mohammed <[email protected]>
Signed-off-by: Asif Sohail Mohammed <[email protected]>
fa9dd0c
to
e880210
Compare
@@ -0,0 +1,8 @@ | |||
pluginName: opensearch |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor: Would it be clearer if the file name is something like: logstash_to_dataprepper_config.mapping.yaml
Minor: it would be nicer to cover more valid configuration mappings in the unit tests, such as username, password, and many more.
It's unit test. I am OK with you adding more tests later. I will approve the pull request now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, I'll will add more tests later
Signed-off-by: Asif Sohail Mohammed [email protected]
Description
Added default plugin mapper to convert Logstash config to Data Prepper models.
Added LogstashMappingException class for exceptions during mapping
Issues Resolved
#466
Check List
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.