-
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
Add truncate string processor #3924
Merged
kkondaka
merged 10 commits into
opensearch-project:main
from
kkondaka:truncate-processor
Jan 11, 2024
Merged
Changes from 2 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
379c6ae
Add truncate string processor
743f347
Addressed review comments
49aa0eb
Added check for negative numbers in the config input
f73f553
Fixed checkstyle error
96c2568
Modified to make truncate processor a top level processor, not specif…
e935916
Addressed review comments
d2f107b
Updated documentation with correct configuration
3ba6e2f
Fixed typos in the documentation
78d5cf6
Modified to allow more than one source keys in the config
9609029
Modified to allow multiple entries under configuration
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 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
47 changes: 0 additions & 47 deletions
47
...va/org/opensearch/dataprepper/plugins/processor/mutatestring/TruncateStringProcessor.java
This file was deleted.
Oops, something went wrong.
83 changes: 0 additions & 83 deletions
83
.../opensearch/dataprepper/plugins/processor/mutatestring/TruncateStringProcessorConfig.java
This file was deleted.
Oops, something went wrong.
This file contains 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,91 @@ | ||
# Truncate Processor | ||
|
||
This is a processor that truncates key's value at the beginning or at the end or at both sides of a string as per the configuration. If the key's value is a list, then each of the string members of the list are truncated. Non-string members of the list are left untouched. If `truncate_when` option is provided, the truncation of the input is done only when the condition specified is true for the event being processed. | ||
|
||
## Basic Usage | ||
To get started, create the following `pipeline.yaml`. | ||
```yaml | ||
pipeline: | ||
source: | ||
file: | ||
path: "/full/path/to/logs_json.log" | ||
record_type: "event" | ||
format: "json" | ||
processor: | ||
- trucate_string: | ||
entries: | ||
- source: "message" | ||
length: 5 | ||
sink: | ||
- stdout: | ||
``` | ||
|
||
Create the following file named `logs_json.log` and replace the `path` in the file source of your `pipeline.yaml` with the path of this file. | ||
|
||
```json | ||
{"message": "hello,world"} | ||
``` | ||
When you run Data Prepper with this `pipeline.yaml`, you should see the following output: | ||
|
||
```json | ||
{"message":["hello"]} | ||
``` | ||
|
||
If the above yaml file has additional config of `start_at: 2`, then the output would be following: | ||
|
||
```json | ||
{"message":["llo,w"]} | ||
``` | ||
|
||
If the above yaml file has additional config of `start_at: 2`, and does not have `length: 5` in the config, then the output would be following: | ||
|
||
```json | ||
{"message":["llo,world"]} | ||
``` | ||
|
||
If the source has an list of strings, then the result will be an array of strings where each of the member of the list is truncated. The following input | ||
```json | ||
{"message": ["hello_one", "hello_two", "hello_three"]} | ||
``` | ||
is transformed to the following: | ||
|
||
```json | ||
{"message": ["hello", "hello", "hello"]} | ||
``` | ||
|
||
Example configuration with `truncate_when` option: | ||
```yaml | ||
pipeline: | ||
source: | ||
file: | ||
path: "/full/path/to/logs_json.log" | ||
record_type: "event" | ||
format: "json" | ||
processor: | ||
- trucate_string: | ||
entries: | ||
- source: "message" | ||
length: 5 | ||
start_at: 7 | ||
truncate_when: '/id == 1' | ||
sink: | ||
- stdout: | ||
``` | ||
|
||
When the pipeline started with the above configuration receives the following two events | ||
```json | ||
{"message": "hello, world", "id": 1} | ||
{"message": "hello, world,not-truncated", "id": 2} | ||
``` | ||
the output would be | ||
```json | ||
{"message": "world", "id": 1} | ||
{"message": "hello, world,not-truncated", "id": 2} | ||
``` | ||
|
||
### Configuration | ||
* `entries` - (required) - A list of entries to add to an event | ||
* `source` - (required) - The key to be modified | ||
* `start_at` - (optional) - starting index of the string. Defaults to 0. | ||
* `length` - (optional) - length of the string after truncation. Defaults to end of the string. | ||
Either `start_at` or `length` or both must be present |
This file contains 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,17 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
plugins { | ||
id 'java' | ||
} | ||
|
||
dependencies { | ||
implementation project(':data-prepper-api') | ||
implementation project(':data-prepper-test-common') | ||
implementation project(':data-prepper-plugins:common') | ||
implementation 'com.fasterxml.jackson.core:jackson-databind' | ||
testImplementation libs.commons.lang3 | ||
} | ||
|
98 changes: 98 additions & 0 deletions
98
...rc/main/java/org/opensearch/dataprepper/plugins/processor/truncate/TruncateProcessor.java
This file contains 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,98 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.dataprepper.plugins.processor.truncate; | ||
|
||
import org.opensearch.dataprepper.expression.ExpressionEvaluator; | ||
import org.opensearch.dataprepper.metrics.PluginMetrics; | ||
import org.opensearch.dataprepper.model.annotations.DataPrepperPlugin; | ||
import org.opensearch.dataprepper.model.annotations.DataPrepperPluginConstructor; | ||
import org.opensearch.dataprepper.model.event.Event; | ||
import org.opensearch.dataprepper.model.record.Record; | ||
import org.opensearch.dataprepper.model.processor.AbstractProcessor; | ||
import org.opensearch.dataprepper.model.processor.Processor; | ||
|
||
import java.util.Collection; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* This processor takes in a key and truncates its value to a string with | ||
* characters from the front or at the end or at both removed. | ||
* If the value is not a string, no action is performed. | ||
*/ | ||
@DataPrepperPlugin(name = "truncate", pluginType = Processor.class, pluginConfigurationType = TruncateProcessorConfig.class) | ||
public class TruncateProcessor extends AbstractProcessor<Record<Event>, Record<Event>>{ | ||
private final ExpressionEvaluator expressionEvaluator; | ||
private final String truncateWhen; | ||
private final int startIndex; | ||
private final Integer length; | ||
private final String source; | ||
|
||
@DataPrepperPluginConstructor | ||
public TruncateProcessor(final PluginMetrics pluginMetrics, final TruncateProcessorConfig config, final ExpressionEvaluator expressionEvaluator) { | ||
super(pluginMetrics); | ||
this.expressionEvaluator = expressionEvaluator; | ||
this.truncateWhen = config.getTruncateWhen(); | ||
this.source = config.getSource(); | ||
this.startIndex = config.getStartAt() == null ? 0 : config.getStartAt(); | ||
this.length = config.getLength(); | ||
} | ||
|
||
private String getTruncatedValue(final String value) { | ||
String truncatedValue = | ||
(length == null || startIndex+length >= value.length()) ? | ||
value.substring(startIndex) : | ||
value.substring(startIndex, startIndex + length); | ||
|
||
return truncatedValue; | ||
} | ||
|
||
@Override | ||
public Collection<Record<Event>> doExecute(final Collection<Record<Event>> records) { | ||
for(final Record<Event> record : records) { | ||
final Event recordEvent = record.getData(); | ||
if (truncateWhen != null && !expressionEvaluator.evaluateConditional(truncateWhen, recordEvent)) { | ||
continue; | ||
} | ||
if (!recordEvent.containsKey(source)) { | ||
continue; | ||
} | ||
|
||
final Object value = recordEvent.get(source, Object.class); | ||
if (value instanceof String) { | ||
recordEvent.put(source, getTruncatedValue((String)value)); | ||
} else if (value instanceof List) { | ||
List<Object> result = new ArrayList<>(); | ||
for (Object listItem: (List)value) { | ||
if (listItem instanceof String) { | ||
result.add(getTruncatedValue((String)listItem)); | ||
} else { | ||
result.add(listItem); | ||
} | ||
} | ||
recordEvent.put(source, result); | ||
} | ||
} | ||
|
||
return records; | ||
} | ||
|
||
@Override | ||
public void prepareForShutdown() { | ||
|
||
} | ||
|
||
@Override | ||
public boolean isReadyForShutdown() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public void shutdown() { | ||
|
||
} | ||
} | ||
|
Oops, something went wrong.
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.
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.
Still says truncate_string here, but there's also a typo in
trucate
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 for finding the typo. Will fix it.