22
33import itertools
44import os
5- from typing import Any , Dict , List , Union
5+ from typing import Any , Dict , List , Sequence , Tuple , Union
66
7+ from instana .configurator import config
78from instana .log import logger
89from instana .util .config_reader import ConfigReader
910
11+ # List of supported span categories (technology or protocol)
12+ SPAN_CATEGORIES = [
13+ "logging" ,
14+ "databases" ,
15+ "messaging" ,
16+ "protocols" , # http, grpc, etc.
17+ ]
18+
19+ # Mapping of span type calls (framework, library name, instrumentation name) to categories
20+ SPAN_TYPE_TO_CATEGORY = {
21+ # Database types
22+ "redis" : "databases" ,
23+ "mysql" : "databases" ,
24+ "postgresql" : "databases" ,
25+ "mongodb" : "databases" ,
26+ "cassandra" : "databases" ,
27+ "couchbase" : "databases" ,
28+ "dynamodb" : "databases" ,
29+ "sqlalchemy" : "databases" ,
30+ # Messaging types
31+ "kafka" : "messaging" ,
32+ "rabbitmq" : "messaging" ,
33+ "pika" : "messaging" ,
34+ "aio_pika" : "messaging" ,
35+ "aioamqp" : "messaging" ,
36+ # Protocol types
37+ "http" : "protocols" ,
38+ "grpc" : "protocols" ,
39+ "graphql" : "protocols" ,
40+ }
41+
1042
1143def parse_service_pair (pair : str ) -> List [str ]:
1244 """
@@ -151,10 +183,10 @@ def parse_ignored_endpoints_from_yaml(file_path: str) -> List[str]:
151183def is_truthy (value : Any ) -> bool :
152184 """
153185 Check if a value is truthy, accepting various formats.
154-
186+
155187 @param value: The value to check
156188 @return: True if the value is considered truthy, False otherwise
157-
189+
158190 Accepts the following as True:
159191 - True (Python boolean)
160192 - "True", "true" (case-insensitive string)
@@ -163,17 +195,128 @@ def is_truthy(value: Any) -> bool:
163195 """
164196 if value is None :
165197 return False
166-
198+
167199 if isinstance (value , bool ):
168200 return value
169-
201+
170202 if isinstance (value , int ):
171203 return value == 1
172-
204+
173205 if isinstance (value , str ):
174206 value_lower = value .lower ()
175207 return value_lower == "true" or value == "1"
176-
208+
177209 return False
178210
211+
212+ def parse_span_disabling (
213+ disable_list : Sequence [Union [str , Dict [str , Any ]]],
214+ ) -> Tuple [List [str ], List [str ]]:
215+ """
216+ Process a list of span disabling configurations and return lists of disabled and enabled spans.
217+
218+ @param disable_list: List of span disabling configurations
219+ @return: Tuple of (disabled_spans, enabled_spans)
220+ """
221+ if not isinstance (disable_list , list ):
222+ logger .debug (
223+ f"parse_span_disabling: Invalid disable_list type: { type (disable_list )} "
224+ )
225+ return [], []
226+
227+ disabled_spans = []
228+ enabled_spans = []
229+
230+ for item in disable_list :
231+ if isinstance (item , str ):
232+ disabled = parse_span_disabling_str (item )
233+ disabled_spans .extend (disabled )
234+ elif isinstance (item , dict ):
235+ disabled , enabled = parse_span_disabling_dict (item )
236+ disabled_spans .extend (disabled )
237+ enabled_spans .extend (enabled )
238+ else :
239+ logger .debug (
240+ f"parse_span_disabling: Invalid disable_list item type: { type (item )} "
241+ )
242+
243+ return disabled_spans , enabled_spans
244+
245+
246+ def parse_span_disabling_str (item : str ) -> List [str ]:
247+ """
248+ Process a string span disabling configuration and return a list of disabled spans.
249+
250+ @param item: String span disabling configuration
251+ @return: List of disabled spans
252+ """
253+ if item .lower () in SPAN_CATEGORIES or item .lower () in SPAN_TYPE_TO_CATEGORY .keys ():
254+ return [item .lower ()]
255+ else :
256+ logger .debug (f"set_span_disabling_str: Invalid span category/type: { item } " )
257+ return []
258+
259+
260+ def parse_span_disabling_dict (items : Dict [str , bool ]) -> Tuple [List [str ], List [str ]]:
261+ """
262+ Process a dictionary span disabling configuration and return lists of disabled and enabled spans.
263+
264+ @param items: Dictionary span disabling configuration
265+ @return: Tuple of (disabled_spans, enabled_spans)
266+ """
267+ disabled_spans = []
268+ enabled_spans = []
269+
270+ for key , value in items .items ():
271+ if key in SPAN_CATEGORIES or key in SPAN_TYPE_TO_CATEGORY .keys ():
272+ if is_truthy (value ):
273+ disabled_spans .append (key )
274+ else :
275+ enabled_spans .append (key )
276+ else :
277+ logger .debug (f"set_span_disabling_dict: Invalid span category/type: { key } " )
278+
279+ return disabled_spans , enabled_spans
280+
281+
282+ def get_disable_trace_configurations_from_env () -> Tuple [List [str ], List [str ]]:
283+ # Read INSTANA_TRACING_DISABLE environment variable
284+ if tracing_disable := os .environ .get ("INSTANA_TRACING_DISABLE" , None ):
285+ if is_truthy (tracing_disable ):
286+ # INSTANA_TRACING_DISABLE is True/true/1, then we disable all tracing
287+ disabled_spans = []
288+ for category in SPAN_CATEGORIES :
289+ disabled_spans .append (category )
290+ return disabled_spans , []
291+ else :
292+ # INSTANA_TRACING_DISABLE is a comma-separated list of span categories/types
293+ tracing_disable_list = [x .strip () for x in tracing_disable .split ("," )]
294+ return parse_span_disabling (tracing_disable_list )
295+ return [], []
296+
297+
298+ def get_disable_trace_configurations_from_yaml () -> Tuple [List [str ], List [str ]]:
299+ config_reader = ConfigReader (os .environ .get ("INSTANA_CONFIG_PATH" , "" ))
300+
301+ if "tracing" in config_reader .data :
302+ root_key = "tracing"
303+ elif "com.instana.tracing" in config_reader .data :
304+ logger .warning (
305+ 'Please use "tracing" instead of "com.instana.tracing" for local configuration file.'
306+ )
307+ root_key = "com.instana.tracing"
308+ else :
309+ return [], []
310+
311+ tracing_disable_config = config_reader .data [root_key ].get ("disable" , "" )
312+ return parse_span_disabling (tracing_disable_config )
313+
314+
315+ def get_disable_trace_configurations_from_local () -> Tuple [List [str ], List [str ]]:
316+ if "tracing" in config :
317+ if tracing_disable_config := config ["tracing" ].get ("disable" , None ):
318+ return parse_span_disabling (tracing_disable_config )
319+ return [], []
320+
321+
179322# Made with Bob
0 commit comments