17
17
from logprep .abc .connector import Connector
18
18
from logprep .abc .exceptions import LogprepException
19
19
from logprep .metrics .metrics import Metric
20
- from logprep .util .helper import add_field_to , get_dotted_field_value
20
+ from logprep .processor .base .exceptions import FieldExistsWarning
21
+ from logprep .util .helper import add_fields_to , get_dotted_field_value
21
22
from logprep .util .time import UTC , TimeParser
22
23
from logprep .util .validators import dict_structure_validator
23
24
@@ -280,16 +281,19 @@ def get_next(self, timeout: float) -> dict | None:
280
281
self .metrics .number_of_processed_events += 1
281
282
if not isinstance (event , dict ):
282
283
raise CriticalInputError (self , "not a dict" , event )
283
- if self ._add_hmac :
284
- event = self ._add_hmac_to (event , raw_event )
285
- if self ._add_version_info :
286
- self ._add_version_information_to_event (event )
287
- if self ._add_log_arrival_time_information :
288
- self ._add_arrival_time_information_to_event (event )
289
- if self ._add_log_arrival_timedelta_information :
290
- self ._add_arrival_timedelta_information_to_event (event )
291
- if self ._add_env_enrichment :
292
- self ._add_env_enrichment_to_event (event )
284
+ try :
285
+ if self ._add_hmac :
286
+ event = self ._add_hmac_to (event , raw_event )
287
+ if self ._add_version_info :
288
+ self ._add_version_information_to_event (event )
289
+ if self ._add_log_arrival_time_information :
290
+ self ._add_arrival_time_information_to_event (event )
291
+ if self ._add_log_arrival_timedelta_information :
292
+ self ._add_arrival_timedelta_information_to_event (event )
293
+ if self ._add_env_enrichment :
294
+ self ._add_env_enrichment_to_event (event )
295
+ except FieldExistsWarning as error :
296
+ raise CriticalInputError (self , error .args [0 ], event ) from error
293
297
return event
294
298
295
299
def batch_finished_callback (self ):
@@ -300,13 +304,19 @@ def _add_env_enrichment_to_event(self, event: dict):
300
304
enrichments = self ._config .preprocessing .get ("enrich_by_env_variables" )
301
305
if not enrichments :
302
306
return
303
- for target_field , variable_name in enrichments .items ():
304
- add_field_to (event , target_field , os .environ .get (variable_name , "" ))
307
+ fields = {
308
+ target : os .environ .get (variable_name , "" )
309
+ for target , variable_name in enrichments .items ()
310
+ }
311
+ add_fields_to (event , fields )
305
312
306
313
def _add_arrival_time_information_to_event (self , event : dict ):
307
- now = TimeParser .now ()
308
- target_field = self ._config .preprocessing .get ("log_arrival_time_target_field" )
309
- add_field_to (event , target_field , now .isoformat ())
314
+ new_field = {
315
+ self ._config .preprocessing .get (
316
+ "log_arrival_time_target_field"
317
+ ): TimeParser .now ().isoformat ()
318
+ }
319
+ add_fields_to (event , new_field )
310
320
311
321
def _add_arrival_timedelta_information_to_event (self , event : dict ):
312
322
log_arrival_timedelta_config = self ._config .preprocessing .get ("log_arrival_timedelta" )
@@ -322,16 +332,16 @@ def _add_arrival_timedelta_information_to_event(self, event: dict):
322
332
TimeParser .from_string (log_arrival_time ).astimezone (UTC )
323
333
- TimeParser .from_string (time_reference ).astimezone (UTC )
324
334
).total_seconds ()
325
- add_field_to (event , target_field , delta_time_sec )
335
+ add_fields_to (event , fields = { target_field : delta_time_sec } )
326
336
327
337
def _add_version_information_to_event (self , event : dict ):
328
338
"""Add the version information to the event"""
329
339
target_field = self ._config .preprocessing .get ("version_info_target_field" )
330
340
# pylint: disable=protected-access
331
- add_field_to (event , target_field , self ._config ._version_information )
341
+ add_fields_to (event , fields = { target_field : self ._config ._version_information } )
332
342
# pylint: enable=protected-access
333
343
334
- def _add_hmac_to (self , event_dict , raw_event ) -> Tuple [ dict , str ] :
344
+ def _add_hmac_to (self , event_dict , raw_event ) -> dict :
335
345
"""
336
346
Calculates an HMAC (Hash-based message authentication code) based on a given target field
337
347
and adds it to the given event. If the target field has the value '<RAW_MSG>' the full raw
@@ -357,7 +367,7 @@ def _add_hmac_to(self, event_dict, raw_event) -> Tuple[dict, str]:
357
367
------
358
368
CriticalInputError
359
369
If the hmac could not be added to the event because the desired output field already
360
- exists or cant 't be found.
370
+ exists or can 't be found.
361
371
"""
362
372
hmac_options = self ._config .preprocessing .get ("hmac" , {})
363
373
hmac_target_field_name = hmac_options .get ("target" )
@@ -381,18 +391,11 @@ def _add_hmac_to(self, event_dict, raw_event) -> Tuple[dict, str]:
381
391
digestmod = hashlib .sha256 ,
382
392
).hexdigest ()
383
393
compressed = zlib .compress (received_orig_message , level = - 1 )
384
- hmac_output = {"hmac" : hmac , "compressed_base64" : base64 .b64encode (compressed ).decode ()}
385
- add_was_successful = add_field_to (
386
- event_dict ,
387
- hmac_options .get ("output_field" ),
388
- hmac_output ,
389
- )
390
- if not add_was_successful :
391
- raise CriticalInputError (
392
- self ,
393
- f"Couldn't add the hmac to the input event as the desired "
394
- f"output field '{ hmac_options .get ('output_field' )} ' already "
395
- f"exist." ,
396
- event_dict ,
397
- )
394
+ new_field = {
395
+ hmac_options .get ("output_field" ): {
396
+ "hmac" : hmac ,
397
+ "compressed_base64" : base64 .b64encode (compressed ).decode (),
398
+ }
399
+ }
400
+ add_fields_to (event_dict , new_field )
398
401
return event_dict
0 commit comments