1
+ import itertools
1
2
from typing import Any , Dict , List , Union
3
+
4
+ import yaml
5
+
2
6
from instana .log import logger
3
7
4
8
@@ -7,19 +11,19 @@ def parse_service_pair(pair: str) -> List[str]:
7
11
Parses a pair string to prepare a list of ignored endpoints.
8
12
9
13
@param pair: String format:
10
- - "service1:endpoint1,endpoint2 " or "service1:endpoint1 " or "service1"
11
- @return: List of strings in format ["service1.endpoint1 ", "service1.endpoint2 ", "service2"]
14
+ - "service1:method1,method2 " or "service1:method1 " or "service1"
15
+ @return: List of strings in format ["service1.method1 ", "service1.method2 ", "service2.* "]
12
16
"""
13
17
pair_list = []
14
18
if ":" in pair :
15
- service , endpoints = pair .split (":" , 1 )
19
+ service , methods = pair .split (":" , 1 )
16
20
service = service .strip ()
17
- endpoint_list = [ep .strip () for ep in endpoints .split ("," ) if ep .strip ()]
21
+ method_list = [ep .strip () for ep in methods .split ("," ) if ep .strip ()]
18
22
19
- for endpoint in endpoint_list :
20
- pair_list .append (f"{ service } .{ endpoint } " )
23
+ for method in method_list :
24
+ pair_list .append (f"{ service } .{ method } " )
21
25
else :
22
- pair_list .append (pair )
26
+ pair_list .append (f" { pair } .*" )
23
27
return pair_list
24
28
25
29
@@ -28,8 +32,8 @@ def parse_ignored_endpoints_string(params: str) -> List[str]:
28
32
Parses a string to prepare a list of ignored endpoints.
29
33
30
34
@param params: String format:
31
- - "service1:endpoint1,endpoint2 ;service2:endpoint3 " or "service1;service2"
32
- @return: List of strings in format ["service1.endpoint1 ", "service1.endpoint2 ", "service2"]
35
+ - "service1:method1,method2 ;service2:method3 " or "service1;service2"
36
+ @return: List of strings in format ["service1.method1 ", "service1.method2 ", "service2.* "]
33
37
"""
34
38
ignore_endpoints = []
35
39
if params :
@@ -46,18 +50,45 @@ def parse_ignored_endpoints_dict(params: Dict[str, Any]) -> List[str]:
46
50
Parses a dictionary to prepare a list of ignored endpoints.
47
51
48
52
@param params: Dict format:
49
- - {"service1": ["endpoint1 ", "endpoint2 "], "service2": ["endpoint3 "]}
50
- @return: List of strings in format ["service1.endpoint1 ", "service1.endpoint2 ", "service2"]
53
+ - {"service1": ["method1 ", "method2 "], "service2": ["method3 "]}
54
+ @return: List of strings in format ["service1.method1 ", "service1.method2 ", "service2.* "]
51
55
"""
52
56
ignore_endpoints = []
53
57
54
- for service , endpoints in params .items ():
55
- if not endpoints : # filtering all service
56
- ignore_endpoints .append (service .lower ())
58
+ for service , methods in params .items ():
59
+ if not methods : # filtering all service
60
+ ignore_endpoints .append (f" { service .lower ()} .*" )
57
61
else : # filtering specific endpoints
58
- for endpoint in endpoints :
59
- ignore_endpoints .append (f"{ service .lower ()} .{ endpoint .lower ()} " )
62
+ ignore_endpoints = parse_endpoints_of_service (
63
+ ignore_endpoints , service , methods
64
+ )
65
+
66
+ return ignore_endpoints
67
+
68
+
69
+ def parse_endpoints_of_service (
70
+ ignore_endpoints : List [str ],
71
+ service : str ,
72
+ methods : Union [str , List [str ]],
73
+ ) -> List [str ]:
74
+ """
75
+ Parses endpoints of each service.
60
76
77
+ @param ignore_endpoints: A list of rules for endpoints to be filtered.
78
+ @param service: The name of the service to be filtered.
79
+ @param methods: A list of specific endpoints of the service to be filtered.
80
+ """
81
+ if service == "kafka" and isinstance (methods , list ):
82
+ for rule in methods :
83
+ for method , endpoint in itertools .product (
84
+ rule ["methods" ], rule ["endpoints" ]
85
+ ):
86
+ ignore_endpoints .append (
87
+ f"{ service .lower ()} .{ method .lower ()} .{ endpoint .lower ()} "
88
+ )
89
+ else :
90
+ for method in methods :
91
+ ignore_endpoints .append (f"{ service .lower ()} .{ method .lower ()} " )
61
92
return ignore_endpoints
62
93
63
94
@@ -66,9 +97,9 @@ def parse_ignored_endpoints(params: Union[Dict[str, Any], str]) -> List[str]:
66
97
Parses input to prepare a list for ignored endpoints.
67
98
68
99
@param params: Can be either:
69
- - String: "service1:endpoint1,endpoint2 ;service2:endpoint3 " or "service1;service2"
70
- - Dict: {"service1": ["endpoint1 ", "endpoint2 "], "service2": ["endpoint3 "]}
71
- @return: List of strings in format ["service1.endpoint1 ", "service1.endpoint2 ", "service2"]
100
+ - String: "service1:method1,method2 ;service2:method3 " or "service1;service2"
101
+ - Dict: {"service1": ["method1 ", "method2 "], "service2": ["method3 "]}
102
+ @return: List of strings in format ["service1.method1 ", "service1.method2 ", "service2.* "]
72
103
"""
73
104
try :
74
105
if isinstance (params , str ):
@@ -80,3 +111,29 @@ def parse_ignored_endpoints(params: Union[Dict[str, Any], str]) -> List[str]:
80
111
except Exception as e :
81
112
logger .debug ("Error parsing ignored endpoints: %s" , str (e ))
82
113
return []
114
+
115
+
116
+ def parse_ignored_endpoints_from_yaml (configuration : str ) -> List [str ]:
117
+ """
118
+ Parses configuration yaml file and prepares a list of ignored endpoints.
119
+
120
+ @param configuration: Path of the file as a string
121
+ @param is_yaml: True if the given configuration is yaml string. False if it's the path of the file.
122
+ @return: List of strings in format ["service1.method1", "service1.method2", "service2.*", "kafka.method.topic", "kafka.*.topic", "kafka.method.*"]
123
+ """
124
+ ignored_endpoints = []
125
+ with open (configuration , "r" ) as configuration_file :
126
+ yaml_configuration = yaml .safe_load (configuration_file )
127
+ configuration_key = (
128
+ "tracing" if "tracing" in yaml_configuration else "com.instana.tracing"
129
+ )
130
+ if (
131
+ configuration_key in yaml_configuration
132
+ and "ignore-endpoints" in yaml_configuration [configuration_key ]
133
+ ):
134
+ ignored_endpoints = parse_ignored_endpoints (
135
+ yaml_configuration [configuration_key ]["ignore-endpoints" ]
136
+ )
137
+ if configuration_key == "com.instana.tracing" :
138
+ logger .debug ('Please use "tracing" instead of "com.instana.tracing"' )
139
+ return ignored_endpoints
0 commit comments