Skip to content

Commit f772eb2

Browse files
committed
style: fix error [SIM102] - Use a single if statement instead of nested if statements.
Used Ruff (vscode and pre-commit) to: - Black-compatible code formatting. - fix all auto-fixable violations. - isort-compatible import sorting. - flake8-simplify manual fixes. Signed-off-by: Paulo Vital <paulo.vital@ibm.com>
1 parent f3a1de9 commit f772eb2

7 files changed

Lines changed: 115 additions & 111 deletions

File tree

src/instana/autoprofile/frame_cache.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,8 @@ def is_profiler_frame(self, filename: str) -> bool:
3333

3434
profiler_frame = False
3535

36-
if not self.include_profiler_frames:
37-
if filename.startswith(self.profiler_dir):
38-
profiler_frame = True
36+
if not self.include_profiler_frames and filename.startswith(self.profiler_dir):
37+
profiler_frame = True
3938

4039
if len(self.profiler_frame_cache) < self.MAX_CACHE_SIZE:
4140
self.profiler_frame_cache[filename] = profiler_frame

src/instana/autoprofile/samplers/cpu_sampler.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,12 @@ def build_profile(self, duration: int, timespan: int) -> Profile:
8282
return profile
8383

8484
def process_sample(self, signal_frame: "FrameType") -> None:
85-
if self.top:
86-
if signal_frame:
87-
stack = self.recover_stack(signal_frame)
88-
if stack:
89-
self.update_profile(self.top, stack)
85+
if self.top and signal_frame:
86+
stack = self.recover_stack(signal_frame)
87+
if stack:
88+
self.update_profile(self.top, stack)
9089

91-
stack = None
90+
stack = None
9291

9392
def recover_stack(
9493
self, signal_frame: "FrameType"

src/instana/collector/helpers/fargate/process.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,22 @@
66

77

88
class FargateProcessHelper(ProcessHelper):
9-
""" Helper class to extend the generic process helper class with the corresponding fargate attributes """
9+
"""Helper class to extend the generic process helper class with the corresponding fargate attributes"""
1010

1111
def collect_metrics(self, **kwargs):
1212
plugin_data = dict()
1313
try:
1414
plugin_data = super(FargateProcessHelper, self).collect_metrics(**kwargs)
1515
plugin_data["data"]["containerType"] = "docker"
1616
if self.collector.root_metadata is not None:
17-
plugin_data["data"]["container"] = self.collector.root_metadata.get("DockerId")
17+
plugin_data["data"]["container"] = self.collector.root_metadata.get(
18+
"DockerId"
19+
)
1820

19-
if kwargs.get("with_snapshot"):
20-
if self.collector.task_metadata is not None:
21-
plugin_data["data"]["com.instana.plugin.host.name"] = self.collector.task_metadata.get("TaskArn")
21+
if kwargs.get("with_snapshot") and self.collector.task_metadata is not None:
22+
plugin_data["data"]["com.instana.plugin.host.name"] = (
23+
self.collector.task_metadata.get("TaskArn")
24+
)
2225
except Exception:
2326
logger.debug("FargateProcessHelper.collect_metrics: ", exc_info=True)
2427
return [plugin_data]

src/instana/fsm.py

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -33,25 +33,23 @@ def __init__(self, agent: "HostAgent") -> None:
3333
self._warned_periodic = False
3434

3535
self.agent = agent
36-
self.fsm = Fysom(
37-
{
38-
"initial": "*",
39-
"events": [
40-
("lookup", "*", "found"),
41-
("announce", "found", "announced"),
42-
("pending", "announced", "wait4init"),
43-
("ready", "wait4init", "good2go"),
44-
],
45-
"callbacks": {
46-
# Can add the following to debug
47-
# "onchangestate": self.print_state_change,
48-
"onlookup": self.lookup_agent_host,
49-
"onannounce": self.announce_sensor,
50-
"onpending": self.on_ready,
51-
"ongood2go": self.on_good2go,
52-
},
53-
}
54-
)
36+
self.fsm = Fysom({
37+
"initial": "*",
38+
"events": [
39+
("lookup", "*", "found"),
40+
("announce", "found", "announced"),
41+
("pending", "announced", "wait4init"),
42+
("ready", "wait4init", "good2go"),
43+
],
44+
"callbacks": {
45+
# Can add the following to debug
46+
# "onchangestate": self.print_state_change,
47+
"onlookup": self.lookup_agent_host,
48+
"onannounce": self.announce_sensor,
49+
"onpending": self.on_ready,
50+
"ongood2go": self.on_good2go,
51+
},
52+
})
5553

5654
with self._lock:
5755
self.timer = threading.Timer(1, self._safe_fsm_lookup)
@@ -103,12 +101,11 @@ def lookup_agent_host(self, e: Any) -> bool:
103101

104102
if os.path.exists("/proc/"):
105103
host = get_default_gateway()
106-
if host:
107-
if self.agent.is_agent_listening(host, port):
108-
self.agent.options.agent_host = host
109-
self.agent.options.agent_port = port
110-
self._safe_fsm_announce()
111-
return True
104+
if host and self.agent.is_agent_listening(host, port):
105+
self.agent.options.agent_host = host
106+
self.agent.options.agent_port = port
107+
self._safe_fsm_announce()
108+
return True
112109

113110
with self._lock:
114111
if self._warned_periodic is False:
@@ -141,9 +138,10 @@ def announce_sensor(self, e: Any) -> bool:
141138
# PermissionError: [Errno 13] Permission denied: '/proc/6/fd/8'
142139
# Use a try/except as a safety
143140
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
144-
sock.connect(
145-
(self.agent.options.agent_host, self.agent.options.agent_port)
146-
)
141+
sock.connect((
142+
self.agent.options.agent_host,
143+
self.agent.options.agent_port,
144+
))
147145
path = f"/proc/{pid}/fd/{sock.fileno()}"
148146
d.fd = sock.fileno()
149147
d.inode = os.readlink(path)

src/instana/instrumentation/kafka/kafka_python.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def trace_kafka_send(
6060
# Context propagation
6161
headers = kwargs.get("headers", [])
6262

63-
if not is_suppressed and ("x_instana_l_s", b"0") in headers:
63+
if not is_suppressed and headers and ("x_instana_l_s", b"0") in headers:
6464
is_suppressed = True
6565

6666
suppression_header = {"x_instana_l_s": "0" if is_suppressed else "1"}
@@ -112,9 +112,8 @@ def create_span(
112112
attributes_to_check
113113
)
114114

115-
if not is_suppressed and headers:
116-
if ("x_instana_l_s", b"0") in headers:
117-
is_suppressed = True
115+
if not is_suppressed and headers and ("x_instana_l_s", b"0") in headers:
116+
is_suppressed = True
118117

119118
if is_suppressed:
120119
return

src/instana/options.py

Lines changed: 58 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@
2828
get_stack_trace_config_from_yaml,
2929
is_truthy,
3030
parse_filter_rules,
31+
parse_filter_rules_env_vars,
3132
parse_filter_rules_yaml,
3233
parse_span_disabling,
33-
parse_filter_rules_env_vars,
3434
parse_technology_stack_trace_config,
3535
validate_stack_trace_length,
3636
validate_stack_trace_level,
@@ -123,45 +123,45 @@ def _add_instana_agent_span_filter(self) -> None:
123123
"""Add Instana agent span filter to exclude internal spans."""
124124
if "exclude" not in self.span_filters:
125125
self.span_filters["exclude"] = []
126-
self.span_filters["exclude"].extend(
127-
[
128-
{
129-
"name": "filter-internal-spans-by-url",
130-
"attributes": [
131-
{
132-
"key": "http.url",
133-
"values": ["com.instana"],
134-
"match_type": "contains",
135-
}
136-
],
137-
},
138-
{
139-
"name": "filter-internal-spans-by-host",
140-
"attributes": [
141-
{
142-
"key": "http.host",
143-
"values": ["com.instana"],
144-
"match_type": "contains",
145-
}
146-
],
147-
},
148-
]
149-
)
126+
self.span_filters["exclude"].extend([
127+
{
128+
"name": "filter-internal-spans-by-url",
129+
"attributes": [
130+
{
131+
"key": "http.url",
132+
"values": ["com.instana"],
133+
"match_type": "contains",
134+
}
135+
],
136+
},
137+
{
138+
"name": "filter-internal-spans-by-host",
139+
"attributes": [
140+
{
141+
"key": "http.host",
142+
"values": ["com.instana"],
143+
"match_type": "contains",
144+
}
145+
],
146+
},
147+
])
150148

151149
def _apply_env_stack_trace_config(self) -> None:
152150
"""Apply stack trace configuration from environment variables."""
153-
if "INSTANA_STACK_TRACE" in os.environ:
154-
if validated_level := validate_stack_trace_level(
151+
if "INSTANA_STACK_TRACE" in os.environ and (
152+
validated_level := validate_stack_trace_level(
155153
os.environ["INSTANA_STACK_TRACE"], "from INSTANA_STACK_TRACE"
156-
):
157-
self.stack_trace_level = validated_level
154+
)
155+
):
156+
self.stack_trace_level = validated_level
158157

159-
if "INSTANA_STACK_TRACE_LENGTH" in os.environ:
160-
if validated_length := validate_stack_trace_length(
158+
if "INSTANA_STACK_TRACE_LENGTH" in os.environ and (
159+
validated_length := validate_stack_trace_length(
161160
os.environ["INSTANA_STACK_TRACE_LENGTH"],
162161
"from INSTANA_STACK_TRACE_LENGTH",
163-
):
164-
self.stack_trace_length = validated_length
162+
)
163+
):
164+
self.stack_trace_length = validated_length
165165

166166
def _apply_yaml_stack_trace_config(self) -> None:
167167
"""Apply stack trace configuration from YAML file."""
@@ -182,20 +182,26 @@ def _apply_in_code_stack_trace_config(self) -> None:
182182

183183
global_config = config["tracing"]["global"]
184184

185-
if "INSTANA_STACK_TRACE" not in os.environ and "stack_trace" in global_config:
186-
if validated_level := validate_stack_trace_level(
187-
global_config["stack_trace"], "from in-code config"
188-
):
189-
self.stack_trace_level = validated_level
185+
if (
186+
"INSTANA_STACK_TRACE" not in os.environ
187+
and "stack_trace" in global_config
188+
and (
189+
validated_level := validate_stack_trace_level(
190+
global_config["stack_trace"], "from in-code config"
191+
)
192+
)
193+
):
194+
self.stack_trace_level = validated_level
190195

191196
if (
192197
"INSTANA_STACK_TRACE_LENGTH" not in os.environ
193198
and "stack_trace_length" in global_config
194-
):
195-
if validated_length := validate_stack_trace_length(
199+
) and (
200+
validated_length := validate_stack_trace_length(
196201
global_config["stack_trace_length"], "from in-code config"
197-
):
198-
self.stack_trace_length = validated_length
202+
)
203+
):
204+
self.stack_trace_length = validated_length
199205

200206
# Technology-specific overrides from in-code config
201207
for tech_name, tech_data in config["tracing"].items():
@@ -429,17 +435,19 @@ def _apply_agent_global_stack_trace_config(
429435
self, global_config: Dict[str, Any]
430436
) -> None:
431437
"""Apply global stack trace configuration from agent config."""
432-
if "stack-trace" in global_config:
433-
if validated_level := validate_stack_trace_level(
438+
if "stack-trace" in global_config and (
439+
validated_level := validate_stack_trace_level(
434440
global_config["stack-trace"], "in agent config"
435-
):
436-
self.stack_trace_level = validated_level
441+
)
442+
):
443+
self.stack_trace_level = validated_level
437444

438-
if "stack-trace-length" in global_config:
439-
if validated_length := validate_stack_trace_length(
445+
if "stack-trace-length" in global_config and (
446+
validated_length := validate_stack_trace_length(
440447
global_config["stack-trace-length"], "in agent config"
441-
):
442-
self.stack_trace_length = validated_length
448+
)
449+
):
450+
self.stack_trace_length = validated_length
443451

444452
def _apply_agent_tech_stack_trace_config(self, tracing: Dict[str, Any]) -> None:
445453
"""Apply technology-specific stack trace configuration from agent config."""

src/instana/util/span_utils.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,8 @@ def matches_rule(rule_attributes: List[Any], span_attributes: List[Any]) -> bool
3232
rule_matched = True
3333

3434
elif key == "type":
35-
if "type" in span_attributes:
36-
if span_attributes["type"] in target_values:
37-
rule_matched = True
35+
if "type" in span_attributes and span_attributes["type"] in target_values:
36+
rule_matched = True
3837

3938
else:
4039
if key in span_attributes:
@@ -56,18 +55,17 @@ def match_key_filter(span_value: str, rule_value: str, match_type: str) -> bool:
5655
if span_value is None:
5756
return False
5857

59-
if rule_value == "*":
60-
return True
61-
elif match_type == "strict" and span_value == rule_value:
62-
return True
63-
elif match_type == "contains" and rule_value in span_value:
64-
return True
65-
elif match_type == "startswith" and span_value.startswith(rule_value):
66-
return True
67-
elif match_type == "endswith" and span_value.endswith(rule_value):
68-
return True
69-
70-
return False
58+
return bool(
59+
rule_value == "*"
60+
or match_type == "strict"
61+
and span_value == rule_value
62+
or match_type == "contains"
63+
and rule_value in span_value
64+
or match_type == "startswith"
65+
and span_value.startswith(rule_value)
66+
or match_type == "endswith"
67+
and span_value.endswith(rule_value)
68+
)
7169

7270

7371
def get_span_kind(span_kind: Any) -> str:

0 commit comments

Comments
 (0)