Skip to content

Commit 01639d9

Browse files
authored
refactor: add a session span (#644)
1 parent 143d23e commit 01639d9

File tree

2 files changed

+56
-6
lines changed

2 files changed

+56
-6
lines changed

mergify_cli/ci/junit.py

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ async def junit_to_spans(
4848
msg = "no testsuite tag found"
4949
raise InvalidJunitXMLError(msg)
5050

51-
spans = []
52-
5351
now = time.time_ns()
5452

5553
common_attributes = {}
@@ -86,6 +84,29 @@ async def junit_to_spans(
8684

8785
trace_id = ID_GENERATOR.generate_trace_id()
8886

87+
session_context = opentelemetry.trace.span.SpanContext(
88+
trace_id=trace_id,
89+
span_id=ID_GENERATOR.generate_span_id(),
90+
is_remote=False,
91+
)
92+
93+
session_span = ReadableSpan(
94+
name="test session",
95+
context=session_context,
96+
parent=None,
97+
# We'll compute start_time later
98+
end_time=now,
99+
resource=resource,
100+
attributes={
101+
"test.scope": "session",
102+
}
103+
| common_attributes,
104+
)
105+
106+
session_start_time = now
107+
108+
spans = [session_span]
109+
89110
for testsuite in testsuites:
90111
min_start_time = now
91112
suite_name = testsuite.get("name", "unnamed testsuite")
@@ -99,7 +120,7 @@ async def junit_to_spans(
99120
testsuite_span = ReadableSpan(
100121
name=suite_name,
101122
context=testsuite_context,
102-
parent=None,
123+
parent=session_context,
103124
# We'll compute start_time later
104125
end_time=now,
105126
resource=resource,
@@ -184,5 +205,8 @@ async def junit_to_spans(
184205
spans.append(span)
185206

186207
testsuite_span._start_time = min_start_time # noqa: SLF001
208+
session_start_time = min(session_start_time, min_start_time)
209+
210+
session_span._start_time = session_start_time # noqa: SLF001
187211

188212
return spans

mergify_cli/tests/ci/test_junit.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,32 @@ async def test_parse(
5757
"telemetry.sdk.version": anys.ANY_STR,
5858
}
5959
assert dictified_spans == [
60+
{
61+
"attributes": {
62+
"test.framework": "unittest",
63+
"test.language": "python",
64+
"test.scope": "session",
65+
},
66+
"context": {
67+
"span_id": anys.ANY_STR,
68+
"trace_id": trace_id,
69+
"trace_state": "[]",
70+
},
71+
"end_time": anys.ANY_DATETIME_STR,
72+
"events": [],
73+
"kind": "SpanKind.INTERNAL",
74+
"links": [],
75+
"name": "test session",
76+
"parent_id": None,
77+
"resource": {
78+
"attributes": resource_attributes,
79+
"schema_url": "",
80+
},
81+
"start_time": anys.ANY_DATETIME_STR,
82+
"status": {
83+
"status_code": "UNSET",
84+
},
85+
},
6086
{
6187
"attributes": {
6288
"test.case.name": "Tests.Registration",
@@ -74,7 +100,7 @@ async def test_parse(
74100
"kind": "SpanKind.INTERNAL",
75101
"links": [],
76102
"name": "Tests.Registration",
77-
"parent_id": None,
103+
"parent_id": anys.ANY_STR,
78104
"resource": {
79105
"attributes": resource_attributes,
80106
"schema_url": "",
@@ -188,7 +214,7 @@ async def test_parse(
188214
"kind": "SpanKind.INTERNAL",
189215
"links": [],
190216
"name": "Tests.Authentication",
191-
"parent_id": None,
217+
"parent_id": anys.ANY_STR,
192218
"resource": {
193219
"attributes": resource_attributes,
194220
"schema_url": "",
@@ -334,7 +360,7 @@ async def test_parse(
334360
"kind": "SpanKind.INTERNAL",
335361
"links": [],
336362
"name": "Tests.Authentication.Login",
337-
"parent_id": None,
363+
"parent_id": anys.ANY_STR,
338364
"resource": {
339365
"attributes": resource_attributes,
340366
"schema_url": "",

0 commit comments

Comments
 (0)