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