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

Add truncate string processor #3924

Merged
merged 10 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 0 additions & 51 deletions data-prepper-plugins/mutate-string-processors/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,57 +196,6 @@ When you run Data Prepper with this `pipeline.yaml`, you should see the followin
### Configuration
* `with_keys` - (required) - A list of keys to trim the whitespace from

## TruncateStringProcessor
A processor that truncates string by removing user configured number of characters at beginning or at the end or both sides of a string.

### 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"]}
```

### 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

---

## Developer Guide
Expand Down

This file was deleted.

This file was deleted.

91 changes: 91 additions & 0 deletions data-prepper-plugins/truncate-processor/README.md
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:
Copy link
Member

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

Copy link
Collaborator Author

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.

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
17 changes: 17 additions & 0 deletions data-prepper-plugins/truncate-processor/build.gradle
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
}

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() {

}
}

Loading
Loading