1+ import itertools
12from typing import Any , Dict , List , Union
3+
4+ import yaml
5+
26from instana .log import logger
37
48
@@ -7,19 +11,19 @@ def parse_service_pair(pair: str) -> List[str]:
711 Parses a pair string to prepare a list of ignored endpoints.
812
913 @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.* "]
1216 """
1317 pair_list = []
1418 if ":" in pair :
15- service , endpoints = pair .split (":" , 1 )
19+ service , methods = pair .split (":" , 1 )
1620 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 ()]
1822
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 } " )
2125 else :
22- pair_list .append (pair )
26+ pair_list .append (f" { pair } .*" )
2327 return pair_list
2428
2529
@@ -28,8 +32,8 @@ def parse_ignored_endpoints_string(params: str) -> List[str]:
2832 Parses a string to prepare a list of ignored endpoints.
2933
3034 @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.* "]
3337 """
3438 ignore_endpoints = []
3539 if params :
@@ -46,18 +50,45 @@ def parse_ignored_endpoints_dict(params: Dict[str, Any]) -> List[str]:
4650 Parses a dictionary to prepare a list of ignored endpoints.
4751
4852 @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.* "]
5155 """
5256 ignore_endpoints = []
5357
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 ()} .*" )
5761 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.
6076
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 ()} " )
6192 return ignore_endpoints
6293
6394
@@ -66,9 +97,9 @@ def parse_ignored_endpoints(params: Union[Dict[str, Any], str]) -> List[str]:
6697 Parses input to prepare a list for ignored endpoints.
6798
6899 @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.* "]
72103 """
73104 try :
74105 if isinstance (params , str ):
@@ -80,3 +111,29 @@ def parse_ignored_endpoints(params: Union[Dict[str, Any], str]) -> List[str]:
80111 except Exception as e :
81112 logger .debug ("Error parsing ignored endpoints: %s" , str (e ))
82113 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