|
| 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.codec.multiline; |
| 11 | + |
| 12 | +import org.opensearch.dataprepper.model.annotations.DataPrepperPlugin; |
| 13 | +import org.opensearch.dataprepper.model.annotations.DataPrepperPluginConstructor; |
| 14 | +import org.opensearch.dataprepper.model.codec.InputCodec; |
| 15 | +import org.opensearch.dataprepper.model.event.Event; |
| 16 | +import org.opensearch.dataprepper.model.event.EventFactory; |
| 17 | +import org.opensearch.dataprepper.model.event.LogEventBuilder; |
| 18 | +import org.opensearch.dataprepper.model.log.Log; |
| 19 | +import org.opensearch.dataprepper.model.record.Record; |
| 20 | +import org.slf4j.Logger; |
| 21 | +import org.slf4j.LoggerFactory; |
| 22 | + |
| 23 | +import java.io.BufferedReader; |
| 24 | +import java.io.IOException; |
| 25 | +import java.io.InputStream; |
| 26 | +import java.io.InputStreamReader; |
| 27 | +import java.util.Collections; |
| 28 | +import java.util.Objects; |
| 29 | +import java.util.function.Consumer; |
| 30 | +import java.util.regex.Pattern; |
| 31 | + |
| 32 | +/** |
| 33 | + * An implementation of {@link InputCodec} which groups multiple lines from an input stream |
| 34 | + * into single events based on a configurable regex pattern. |
| 35 | + * |
| 36 | + * <p>This is useful for ingesting logs where a single logical event spans multiple lines, |
| 37 | + * such as Java stack traces, Python tracebacks, or any log format where entries begin with |
| 38 | + * a recognizable pattern (e.g., a timestamp).</p> |
| 39 | + * |
| 40 | + * <p>The codec supports two grouping modes via the {@code what} configuration:</p> |
| 41 | + * <ul> |
| 42 | + * <li>{@code previous}: Continuation lines are appended to the preceding event.</li> |
| 43 | + * <li>{@code next}: Continuation lines are prepended to the following event.</li> |
| 44 | + * </ul> |
| 45 | + * |
| 46 | + * <p>The {@code negate} option controls which lines are considered continuation lines:</p> |
| 47 | + * <ul> |
| 48 | + * <li>{@code negate=false}: Lines matching the pattern are continuation lines.</li> |
| 49 | + * <li>{@code negate=true}: Lines NOT matching the pattern are continuation lines.</li> |
| 50 | + * </ul> |
| 51 | + */ |
| 52 | +@DataPrepperPlugin(name = "multiline", pluginType = InputCodec.class, pluginConfigurationType = MultilineInputCodecConfig.class) |
| 53 | +public class MultilineInputCodec implements InputCodec { |
| 54 | + |
| 55 | + private static final Logger LOG = LoggerFactory.getLogger(MultilineInputCodec.class); |
| 56 | + static final String MESSAGE_FIELD_NAME = "message"; |
| 57 | + |
| 58 | + private final Pattern pattern; |
| 59 | + private final boolean negate; |
| 60 | + private final MultilineWhat what; |
| 61 | + private final int maxLines; |
| 62 | + private final int maxLength; |
| 63 | + private final String lineSeparator; |
| 64 | + private final EventFactory eventFactory; |
| 65 | + |
| 66 | + @DataPrepperPluginConstructor |
| 67 | + public MultilineInputCodec(final MultilineInputCodecConfig config, final EventFactory eventFactory) { |
| 68 | + Objects.requireNonNull(config, "config must not be null"); |
| 69 | + this.eventFactory = Objects.requireNonNull(eventFactory, "eventFactory must not be null"); |
| 70 | + try { |
| 71 | + this.pattern = Pattern.compile(config.getMatch()); |
| 72 | + } catch (final Exception e) { |
| 73 | + throw new IllegalArgumentException("Invalid regex pattern for 'match': " + config.getMatch(), e); |
| 74 | + } |
| 75 | + this.negate = config.getNegate(); |
| 76 | + this.what = config.getWhat(); |
| 77 | + this.maxLines = config.getMaxLines(); |
| 78 | + this.maxLength = config.getMaxLength(); |
| 79 | + this.lineSeparator = config.getLineSeparator(); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public void parse(final InputStream inputStream, final Consumer<Record<Event>> eventConsumer) throws IOException { |
| 84 | + Objects.requireNonNull(inputStream, "inputStream must not be null"); |
| 85 | + Objects.requireNonNull(eventConsumer, "eventConsumer must not be null"); |
| 86 | + |
| 87 | + try (final BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) { |
| 88 | + if (what == MultilineWhat.PREVIOUS) { |
| 89 | + parsePreviousMode(reader, eventConsumer); |
| 90 | + } else { |
| 91 | + parseNextMode(reader, eventConsumer); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * In PREVIOUS mode, continuation lines are appended to the preceding event. |
| 98 | + * A new event boundary is detected when a line is NOT a continuation line |
| 99 | + * (i.e., it's a "start" line). |
| 100 | + */ |
| 101 | + private void parsePreviousMode(final BufferedReader reader, final Consumer<Record<Event>> eventConsumer) throws IOException { |
| 102 | + final StringBuilder buffer = new StringBuilder(); |
| 103 | + int lineCount = 0; |
| 104 | + String line; |
| 105 | + |
| 106 | + while ((line = reader.readLine()) != null) { |
| 107 | + final boolean isContinuation = isContinuationLine(line); |
| 108 | + |
| 109 | + if (!isContinuation && buffer.length() > 0) { |
| 110 | + emitEvent(buffer.toString(), eventConsumer); |
| 111 | + buffer.setLength(0); |
| 112 | + lineCount = 0; |
| 113 | + } |
| 114 | + |
| 115 | + if (shouldFlush(buffer, lineCount, line)) { |
| 116 | + if (buffer.length() > 0) { |
| 117 | + emitEvent(buffer.toString(), eventConsumer); |
| 118 | + buffer.setLength(0); |
| 119 | + lineCount = 0; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + if (buffer.length() > 0) { |
| 124 | + buffer.append(lineSeparator); |
| 125 | + } |
| 126 | + buffer.append(line); |
| 127 | + lineCount++; |
| 128 | + } |
| 129 | + |
| 130 | + if (buffer.length() > 0) { |
| 131 | + emitEvent(buffer.toString(), eventConsumer); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * In NEXT mode, continuation lines are prepended to the following event. |
| 137 | + * A new event boundary is detected when a line is NOT a continuation line, |
| 138 | + * and the buffer (containing prior continuation lines) is combined with this line. |
| 139 | + */ |
| 140 | + private void parseNextMode(final BufferedReader reader, final Consumer<Record<Event>> eventConsumer) throws IOException { |
| 141 | + final StringBuilder buffer = new StringBuilder(); |
| 142 | + int lineCount = 0; |
| 143 | + boolean bufferHasNonContinuation = false; |
| 144 | + String line; |
| 145 | + |
| 146 | + while ((line = reader.readLine()) != null) { |
| 147 | + final boolean isContinuation = isContinuationLine(line); |
| 148 | + |
| 149 | + if (!isContinuation) { |
| 150 | + if (bufferHasNonContinuation) { |
| 151 | + // The buffer already has a complete event (non-continuation at end). |
| 152 | + // Emit it and start fresh. |
| 153 | + emitEvent(buffer.toString(), eventConsumer); |
| 154 | + buffer.setLength(0); |
| 155 | + lineCount = 0; |
| 156 | + bufferHasNonContinuation = false; |
| 157 | + } |
| 158 | + // Append this non-continuation line to the buffer (with any preceding continuations). |
| 159 | + if (buffer.length() > 0) { |
| 160 | + buffer.append(lineSeparator); |
| 161 | + } |
| 162 | + buffer.append(line); |
| 163 | + lineCount++; |
| 164 | + bufferHasNonContinuation = true; |
| 165 | + continue; |
| 166 | + } |
| 167 | + |
| 168 | + // This is a continuation line. |
| 169 | + if (bufferHasNonContinuation) { |
| 170 | + // Buffer has a complete event ending with non-continuation. |
| 171 | + // Emit it, then start collecting continuations for the next event. |
| 172 | + emitEvent(buffer.toString(), eventConsumer); |
| 173 | + buffer.setLength(0); |
| 174 | + lineCount = 0; |
| 175 | + bufferHasNonContinuation = false; |
| 176 | + } |
| 177 | + |
| 178 | + if (shouldFlush(buffer, lineCount, line)) { |
| 179 | + if (buffer.length() > 0) { |
| 180 | + emitEvent(buffer.toString(), eventConsumer); |
| 181 | + buffer.setLength(0); |
| 182 | + lineCount = 0; |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + if (buffer.length() > 0) { |
| 187 | + buffer.append(lineSeparator); |
| 188 | + } |
| 189 | + buffer.append(line); |
| 190 | + lineCount++; |
| 191 | + } |
| 192 | + |
| 193 | + if (buffer.length() > 0) { |
| 194 | + emitEvent(buffer.toString(), eventConsumer); |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + /** |
| 199 | + * Determines if a line is a continuation line based on the pattern and negate settings. |
| 200 | + * |
| 201 | + * <p>When {@code negate=false}: a line matching the pattern IS a continuation line.</p> |
| 202 | + * <p>When {@code negate=true}: a line NOT matching the pattern IS a continuation line.</p> |
| 203 | + */ |
| 204 | + boolean isContinuationLine(final String line) { |
| 205 | + final boolean matches = pattern.matcher(line).find(); |
| 206 | + return negate != matches; |
| 207 | + } |
| 208 | + |
| 209 | + private boolean shouldFlush(final StringBuilder buffer, final int lineCount, final String nextLine) { |
| 210 | + if (lineCount >= maxLines) { |
| 211 | + LOG.debug("Flushing multiline event due to max_lines limit of {}", maxLines); |
| 212 | + return true; |
| 213 | + } |
| 214 | + if (buffer.length() + lineSeparator.length() + nextLine.length() > maxLength) { |
| 215 | + LOG.debug("Flushing multiline event due to max_length limit of {}", maxLength); |
| 216 | + return true; |
| 217 | + } |
| 218 | + return false; |
| 219 | + } |
| 220 | + |
| 221 | + private void emitEvent(final String message, final Consumer<Record<Event>> eventConsumer) { |
| 222 | + final Log event = eventFactory.eventBuilder(LogEventBuilder.class) |
| 223 | + .withData(Collections.singletonMap(MESSAGE_FIELD_NAME, message)) |
| 224 | + .build(); |
| 225 | + eventConsumer.accept(new Record<>(event)); |
| 226 | + } |
| 227 | +} |
0 commit comments