Skip to content
This repository was archived by the owner on Mar 21, 2023. It is now read-only.

Commit 912802a

Browse files
kroepkeedmundoa
authored andcommitted
add rename_field function (#50)
this is a convenience function which renames a field, if present. it takes care not to drop fields if they are renamed to themselves fixes #37
1 parent 2025d36 commit 912802a

File tree

4 files changed

+81
-0
lines changed

4 files changed

+81
-0
lines changed

src/main/java/org/graylog/plugins/pipelineprocessor/functions/ProcessorFunctionsModule.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import org.graylog.plugins.pipelineprocessor.functions.messages.DropMessage;
4545
import org.graylog.plugins.pipelineprocessor.functions.messages.HasField;
4646
import org.graylog.plugins.pipelineprocessor.functions.messages.RemoveField;
47+
import org.graylog.plugins.pipelineprocessor.functions.messages.RenameField;
4748
import org.graylog.plugins.pipelineprocessor.functions.messages.RouteToStream;
4849
import org.graylog.plugins.pipelineprocessor.functions.messages.SetField;
4950
import org.graylog.plugins.pipelineprocessor.functions.messages.SetFields;
@@ -78,6 +79,7 @@ protected void configure() {
7879
addMessageProcessorFunction(HasField.NAME, HasField.class);
7980
addMessageProcessorFunction(SetField.NAME, SetField.class);
8081
addMessageProcessorFunction(SetFields.NAME, SetFields.class);
82+
addMessageProcessorFunction(RenameField.NAME, RenameField.class);
8183
addMessageProcessorFunction(RemoveField.NAME, RemoveField.class);
8284

8385
addMessageProcessorFunction(DropMessage.NAME, DropMessage.class);
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package org.graylog.plugins.pipelineprocessor.functions.messages;
2+
3+
import org.graylog.plugins.pipelineprocessor.EvaluationContext;
4+
import org.graylog.plugins.pipelineprocessor.ast.functions.AbstractFunction;
5+
import org.graylog.plugins.pipelineprocessor.ast.functions.FunctionArgs;
6+
import org.graylog.plugins.pipelineprocessor.ast.functions.FunctionDescriptor;
7+
import org.graylog.plugins.pipelineprocessor.ast.functions.ParameterDescriptor;
8+
import org.graylog2.plugin.Message;
9+
10+
import static org.graylog.plugins.pipelineprocessor.ast.functions.ParameterDescriptor.string;
11+
import static org.graylog.plugins.pipelineprocessor.ast.functions.ParameterDescriptor.type;
12+
13+
public class RenameField extends AbstractFunction<Void> {
14+
15+
public static final String NAME = "rename_field";
16+
17+
private final ParameterDescriptor<String, String> oldFieldParam;
18+
private final ParameterDescriptor<String, String> newFieldParam;
19+
private final ParameterDescriptor<Message, Message> messageParam;
20+
21+
public RenameField() {
22+
oldFieldParam = string("old_field").build();
23+
newFieldParam = string("new_field").build();
24+
messageParam = type("message", Message.class).optional().build();
25+
}
26+
27+
@Override
28+
public Void evaluate(FunctionArgs args, EvaluationContext context) {
29+
final String oldName = oldFieldParam.required(args, context);
30+
final String newName = newFieldParam.required(args, context);
31+
32+
// exit early if the field names are the same (so we don't drop the field)
33+
if (oldName != null && oldName.equals(newName)) {
34+
return null;
35+
}
36+
final Message message = messageParam.optional(args, context).orElse(context.currentMessage());
37+
38+
if (message.hasField(oldName)) {
39+
message.addField(newName, message.getField(oldName));
40+
message.removeField(oldName);
41+
}
42+
43+
return null;
44+
}
45+
46+
@Override
47+
public FunctionDescriptor<Void> descriptor() {
48+
return FunctionDescriptor.<Void>builder()
49+
.name(NAME)
50+
.returnType(Void.class)
51+
.params(oldFieldParam, newFieldParam, messageParam)
52+
.build();
53+
}
54+
}

src/test/java/org/graylog/plugins/pipelineprocessor/functions/FunctionsSnippetsTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import org.graylog.plugins.pipelineprocessor.functions.messages.DropMessage;
5050
import org.graylog.plugins.pipelineprocessor.functions.messages.HasField;
5151
import org.graylog.plugins.pipelineprocessor.functions.messages.RemoveField;
52+
import org.graylog.plugins.pipelineprocessor.functions.messages.RenameField;
5253
import org.graylog.plugins.pipelineprocessor.functions.messages.RouteToStream;
5354
import org.graylog.plugins.pipelineprocessor.functions.messages.SetField;
5455
import org.graylog.plugins.pipelineprocessor.functions.messages.SetFields;
@@ -113,6 +114,7 @@ public static void registerFunctions() {
113114
functions.put(HasField.NAME, new HasField());
114115
functions.put(SetField.NAME, new SetField());
115116
functions.put(SetFields.NAME, new SetFields());
117+
functions.put(RenameField.NAME, new RenameField());
116118
functions.put(RemoveField.NAME, new RemoveField());
117119

118120
functions.put(DropMessage.NAME, new DropMessage());
@@ -452,4 +454,19 @@ public void ipMatchingIssue28() {
452454

453455
assertThat(actionsTriggered.get()).isFalse();
454456
}
457+
458+
@Test
459+
public void fieldRenaming() {
460+
final Rule rule = parser.parseRule(ruleForTest(), false);
461+
462+
final Message in = new Message("some message", "somehost.graylog.org", Tools.nowUTC());
463+
in.addField("field_a", "fieldAContent");
464+
in.addField("field_b", "not deleted");
465+
466+
final Message message = evaluateRule(rule, in);
467+
468+
assertThat(message.hasField("field_1")).isFalse();
469+
assertThat(message.hasField("field_2")).isTrue();
470+
assertThat(message.hasField("field_b")).isTrue();
471+
}
455472
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
rule "fieldRenaming"
2+
when true
3+
then
4+
5+
rename_field("no_such_field", "field_1");
6+
rename_field("field_a", "field_2");
7+
rename_field("field_b", "field_b");
8+
end

0 commit comments

Comments
 (0)