Skip to content

Commit 6162db7

Browse files
taloricsharang
authored andcommitted
feat: use app-service to split app_spans
1 parent 36662bd commit 6162db7

1 file changed

Lines changed: 29 additions & 8 deletions

File tree

app/app/application/l7_flow_tracing.py

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1423,6 +1423,7 @@ def __init__(self, flow: dict):
14231423
self.tap_side = flow['tap_side']
14241424
self.agent_id = flow['vtap_id']
14251425
self.auto_instance = _get_auto_instance(self)
1426+
self.auto_instance_type = _get_auto_instance_type(self)
14261427
self.is_ps_root = False
14271428
self.is_ps_leaf = False
14281429
self.is_net_root = False
@@ -1517,10 +1518,7 @@ def process_matched(self, other_sys_span: SpanNode) -> bool:
15171518
process_id_match = self_process != 0 and other_process != 0 and self_process == other_process
15181519

15191520
# when auto_instance_type is k8s pod, allow auto_instance match instead of process_id match
1520-
1521-
self_auto_instance_type = _get_auto_instance_type(self)
1522-
other_auto_instance_type = _get_auto_instance_type(other_sys_span)
1523-
auto_instance_match = self_auto_instance_type == other_auto_instance_type == const.AUTO_INSTANCE_POD \
1521+
auto_instance_match = self.auto_instance_type == other_sys_span.auto_instance_type == const.AUTO_INSTANCE_POD \
15241522
and self.auto_instance != 0 and self.auto_instance == other_sys_span.auto_instance
15251523

15261524
return process_id_match or auto_instance_match
@@ -1898,9 +1896,10 @@ def _attach_server_sys_span(self, sys_span: SysSpanNode) -> bool:
18981896

18991897
def _attach_client_sys_span(self, sys_span: SysSpanNode) -> bool:
19001898
span_id_of_sys_span = sys_span.get_span_id()
1899+
if not span_id_of_sys_span:
1900+
return False
19011901
for app_leaf in self.app_span_leafs:
1902-
if span_id_of_sys_span and span_id_of_sys_span == app_leaf.get_span_id(
1903-
):
1902+
if span_id_of_sys_span == app_leaf.get_span_id():
19041903
# app_span 作为 sys_span 的 parent
19051904
self.append_sys_span(sys_span)
19061905
sys_span.set_parent(
@@ -2378,14 +2377,36 @@ def _union_app_spans(
23782377
List[ProcessSpanSet]], app_spans: List[AppSpanNode],
23792378
host_clock_correct_callback: Callable[[SpanNode, SpanNode], None]
23802379
) -> Dict[str, List[ProcessSpanSet]]:
2380+
# 对 auto_instance_type != pod 类型的 app span,有可能所有的 app 都在同一个 instance(i.e.: node) 上
2381+
# 此类情况,允许按照 app_service 划分找到 leaf/root app span
2382+
# 但注意不一定有 app_service attribute,这里只能尽力尝试
2383+
# app_service => index of process_span_map[auto_instance]
2384+
app_service_to_index: Dict[str, int] = {}
23812385
for span in app_spans:
23822386
auto_instance = span.auto_instance
2383-
# auto_instance = _get_auto_instance(span)
2387+
app_service = span.flow.get("app_service", "")
2388+
split_by_app_service = span.auto_instance_type != const.AUTO_INSTANCE_POD and app_service
23842389
if auto_instance not in process_span_map:
23852390
sp_span_pss = ProcessSpanSet(auto_instance)
23862391
sp_span_pss.mounted_callback = host_clock_correct_callback
2392+
if split_by_app_service:
2393+
app_service_to_index[app_service] = 0
23872394
process_span_map[auto_instance] = [sp_span_pss]
2388-
process_span_map[auto_instance][0].append_app_span(span)
2395+
if split_by_app_service and app_service_to_index.get(app_service,
2396+
-1) == -1:
2397+
# 构建一个新的 app_service 分组
2398+
sp_span_pss = ProcessSpanSet(auto_instance)
2399+
sp_span_pss.mounted_callback = host_clock_correct_callback
2400+
app_service_to_index[app_service] = len(
2401+
process_span_map[auto_instance])
2402+
process_span_map[auto_instance].append(sp_span_pss)
2403+
2404+
if split_by_app_service:
2405+
# app_service_to_index.get() 默认值使用 0,避免出错丢弃,即使父子关系挂错也不要少 span
2406+
process_span_map[auto_instance][app_service_to_index.get(
2407+
app_service, 0)].append_app_span(span)
2408+
else:
2409+
process_span_map[auto_instance][0].append_app_span(span)
23892410

23902411
# 一组 app-span 构成的 ProcessSpanSet 可能会有多个 root
23912412
# 如果这些 root 有同一个 parent_span_id: 说明只是还没关联 s-p 作为 parent,不需处理,后续逻辑会关联

0 commit comments

Comments
 (0)