# Copyright (c) HashiCorp, Inc.# SPDX-License-Identifier: MPL-2.0"""Grove processor to split a log entry into N log entries by the specified JMESPath.This processor is intended to allow "fanning-out" a single log entry which containsseveral related operations into distinct log entries per item. The remainder of thelog entry outside of the split path will not be modified.It is important to note that the list of elements which is fanned-out will be convertedinto a dictionary, rather than a list.As an example, in following sample: { "events": [ {"name": "First", "value": 1}, {"name": "Second", "value": 2}, ] }After splitting based on "events", two records would be generated, which contain thefollowing: { "events": {"name": "First", "value": 1}, } { "events": {"name": "Second", "value": 2}, }"""fromtypingimportAny,Dict,ListimportjmespathfrompydanticimportExtrafromgrove.helpersimportparsingfromgrove.modelsimportProcessorConfigfromgrove.processorsimportBaseProcessor
[docs]classHandler(BaseProcessor):"""Split a log entry into N log entries by the specified JMESPath."""
[docs]classConfiguration(ProcessorConfig,extra=Extra.forbid):"""Expresses the configuration and associated validators for the processor."""# Source defines the path to split the log entry by. This should be defined as a# JMESPath. The field referenced by this path should be a list.source:str
[docs]defprocess(self,entry:Dict[str,Any])->List[Dict[str,Any]]:"""Attempt to extract and map fields from the log entry. :param entry: A collected log entry. :return: The processed log entry. """# In this instance we WANT to mutate the copy outside of the processor.processed=[]children=jmespath.search(self.configuration.source,entry)# To ensure we don't accidentally try and split a string or dictionary we need# to make sure that the type of the found children - if any - is correct.ifnotchildrenornotisinstance(children,list)orlen(children)<1:return[entry]forchildinchildren:processed.append(parsing.update_path(parsing.quick_copy(entry),parsing.quote_aware_split(self.configuration.source),child,replace=True,))returnprocessed