-
Notifications
You must be signed in to change notification settings - Fork 395
Expand file tree
/
Copy pathdatadog_matcher.py
More file actions
51 lines (38 loc) · 1.72 KB
/
datadog_matcher.py
File metadata and controls
51 lines (38 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# Unless explicitly stated otherwise all files in this repository are licensed
# under the Apache License Version 2.0.
# This product includes software developed at Datadog (https://www.datadoghq.com/).
# Copyright 2021 Datadog, Inc.
import logging
import os
import re
from logs.exceptions import ScrubbingException
from logs.helpers import compileRegex
logger = logging.getLogger()
logger.setLevel(logging.getLevelName(os.environ.get("DD_LOG_LEVEL", "INFO").upper()))
class DatadogMatcher(object):
def __init__(self, include_pattern=None, exclude_pattern=None):
self._include_regex = None
self._exclude_regex = None
if include_pattern is not None:
logger.debug(f"Applying include pattern: {include_pattern}")
self._include_regex = compileRegex("INCLUDE_AT_MATCH", include_pattern)
if exclude_pattern is not None:
logger.debug(f"Applying exclude pattern: {exclude_pattern}")
self._exclude_regex = compileRegex("EXCLUDE_AT_MATCH", exclude_pattern)
def match(self, log):
try:
if self._exclude_regex is not None and re.search(
self._exclude_regex, str(log)
):
logger.debug("Exclude pattern matched, excluding log event")
return False
if self._include_regex is not None and not re.search(
self._include_regex, str(log)
):
logger.debug("Include pattern did not match, excluding log event")
return False
return True
except ScrubbingException as e:
raise Exception(f"Failed to filter log: {e}")
except Exception as e:
raise Exception(f"Failed to filter log: {e}")