|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + * |
| 5 | + * The OpenSearch Contributors require contributions made to |
| 6 | + * this file be licensed under the Apache-2.0 license or a |
| 7 | + * compatible open source license. |
| 8 | + */ |
| 9 | + |
| 10 | +package org.opensearch.dataprepper.plugins.processor.mutateevent; |
| 11 | + |
| 12 | +import org.opensearch.dataprepper.expression.ExpressionEvaluator; |
| 13 | +import org.opensearch.dataprepper.metrics.PluginMetrics; |
| 14 | +import org.opensearch.dataprepper.model.annotations.DataPrepperPlugin; |
| 15 | +import org.opensearch.dataprepper.model.annotations.DataPrepperPluginConstructor; |
| 16 | +import org.opensearch.dataprepper.model.event.Event; |
| 17 | +import org.opensearch.dataprepper.model.event.JacksonEvent; |
| 18 | +import org.opensearch.dataprepper.model.plugin.InvalidPluginConfigurationException; |
| 19 | +import org.opensearch.dataprepper.model.processor.AbstractProcessor; |
| 20 | +import org.opensearch.dataprepper.model.processor.Processor; |
| 21 | +import org.opensearch.dataprepper.model.record.Record; |
| 22 | +import org.slf4j.Logger; |
| 23 | +import org.slf4j.LoggerFactory; |
| 24 | + |
| 25 | +import java.util.ArrayList; |
| 26 | +import java.util.Collection; |
| 27 | +import java.util.Collections; |
| 28 | +import java.util.List; |
| 29 | +import java.util.Map; |
| 30 | +import java.util.Objects; |
| 31 | + |
| 32 | +import static org.opensearch.dataprepper.logging.DataPrepperMarkers.EVENT; |
| 33 | +import static org.opensearch.dataprepper.logging.DataPrepperMarkers.NOISY; |
| 34 | + |
| 35 | +@DataPrepperPlugin(name = "filter_list", pluginType = Processor.class, pluginConfigurationType = FilterListProcessorConfig.class) |
| 36 | +public class FilterListProcessor extends AbstractProcessor<Record<Event>, Record<Event>> { |
| 37 | + |
| 38 | + private static final Logger LOG = LoggerFactory.getLogger(FilterListProcessor.class); |
| 39 | + private final FilterListProcessorConfig config; |
| 40 | + private final ExpressionEvaluator expressionEvaluator; |
| 41 | + private final String target; |
| 42 | + |
| 43 | + @DataPrepperPluginConstructor |
| 44 | + public FilterListProcessor(final PluginMetrics pluginMetrics, final FilterListProcessorConfig config, final ExpressionEvaluator expressionEvaluator) { |
| 45 | + super(pluginMetrics); |
| 46 | + this.config = config; |
| 47 | + this.expressionEvaluator = expressionEvaluator; |
| 48 | + this.target = config.getTarget() != null ? config.getTarget() : config.getSource(); |
| 49 | + |
| 50 | + if (config.getFilterListWhen() != null |
| 51 | + && !expressionEvaluator.isValidExpressionStatement(config.getFilterListWhen())) { |
| 52 | + throw new InvalidPluginConfigurationException( |
| 53 | + String.format("filter_list_when %s is not a valid expression statement. " + |
| 54 | + "See https://opensearch.org/docs/latest/data-prepper/pipelines/expression-syntax/ for valid expression syntax", |
| 55 | + config.getFilterListWhen())); |
| 56 | + } |
| 57 | + |
| 58 | + if (!expressionEvaluator.isValidExpressionStatement(config.getKeepWhen())) { |
| 59 | + throw new InvalidPluginConfigurationException( |
| 60 | + String.format("keep_when %s is not a valid expression statement. " + |
| 61 | + "See https://opensearch.org/docs/latest/data-prepper/pipelines/expression-syntax/ for valid expression syntax", |
| 62 | + config.getKeepWhen())); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public Collection<Record<Event>> doExecute(final Collection<Record<Event>> records) { |
| 68 | + for (final Record<Event> record : records) { |
| 69 | + final Event recordEvent = record.getData(); |
| 70 | + |
| 71 | + try { |
| 72 | + if (Objects.nonNull(config.getFilterListWhen()) && !expressionEvaluator.evaluateConditional(config.getFilterListWhen(), recordEvent)) { |
| 73 | + continue; |
| 74 | + } |
| 75 | + |
| 76 | + final List<Object> sourceList; |
| 77 | + try { |
| 78 | + sourceList = recordEvent.get(config.getSource(), List.class); |
| 79 | + } catch (final Exception e) { |
| 80 | + LOG.warn(EVENT, "Given source path [{}] is not valid on record [{}]", |
| 81 | + config.getSource(), recordEvent, e); |
| 82 | + addTagsOnFailure(recordEvent); |
| 83 | + continue; |
| 84 | + } |
| 85 | + |
| 86 | + if (sourceList == null) { |
| 87 | + LOG.debug("Source list at path [{}] is null, skipping event", config.getSource()); |
| 88 | + continue; |
| 89 | + } |
| 90 | + |
| 91 | + final List<Object> filteredList = new ArrayList<>(); |
| 92 | + final JacksonEvent.Builder contextBuilder = JacksonEvent.builder() |
| 93 | + .withEventType("event"); |
| 94 | + |
| 95 | + for (final Object element : sourceList) { |
| 96 | + @SuppressWarnings("unchecked") |
| 97 | + final Map<String, Object> contextMap = element instanceof Map |
| 98 | + ? (Map<String, Object>) element |
| 99 | + : Collections.singletonMap("value", element); |
| 100 | + |
| 101 | + try { |
| 102 | + final Event elementEvent = contextBuilder |
| 103 | + .withData(contextMap) |
| 104 | + .build(); |
| 105 | + |
| 106 | + if (expressionEvaluator.evaluateConditional(config.getKeepWhen(), elementEvent)) { |
| 107 | + filteredList.add(element); |
| 108 | + } |
| 109 | + } catch (final Exception e) { |
| 110 | + LOG.warn(EVENT, "Error evaluating keep_when expression [{}] for element in source list at path [{}]", |
| 111 | + config.getKeepWhen(), config.getSource(), e); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + recordEvent.put(target, filteredList); |
| 116 | + |
| 117 | + } catch (final Exception e) { |
| 118 | + LOG.atError() |
| 119 | + .addMarker(EVENT) |
| 120 | + .addMarker(NOISY) |
| 121 | + .setMessage("There was an exception while processing Event [{}]") |
| 122 | + .addArgument(recordEvent) |
| 123 | + .setCause(e) |
| 124 | + .log(); |
| 125 | + addTagsOnFailure(recordEvent); |
| 126 | + } |
| 127 | + } |
| 128 | + return records; |
| 129 | + } |
| 130 | + |
| 131 | + private void addTagsOnFailure(final Event event) { |
| 132 | + if (config.getTagsOnFailure() != null) { |
| 133 | + event.getMetadata().addTags(config.getTagsOnFailure()); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + public void prepareForShutdown() { |
| 139 | + } |
| 140 | + |
| 141 | + @Override |
| 142 | + public boolean isReadyForShutdown() { |
| 143 | + return true; |
| 144 | + } |
| 145 | + |
| 146 | + @Override |
| 147 | + public void shutdown() { |
| 148 | + } |
| 149 | +} |
0 commit comments