Builds the in-memory span tree, attaching logs by span id. Used for clear diffs with pytest assertions.
(
cls,
spans: tuple[ReadableSpan, ...],
logs: tuple[ReadableLogRecord, ...] = (),
)
| 182 | |
| 183 | @classmethod |
| 184 | def build( |
| 185 | cls, |
| 186 | spans: tuple[ReadableSpan, ...], |
| 187 | logs: tuple[ReadableLogRecord, ...] = (), |
| 188 | ) -> SpanDigest: |
| 189 | """Builds the in-memory span tree, attaching logs by span id. |
| 190 | |
| 191 | Used for clear diffs with pytest assertions. |
| 192 | """ |
| 193 | digest_by_id: dict[int, SpanDigest] = {} |
| 194 | for span in spans: |
| 195 | if span.context is None: |
| 196 | continue |
| 197 | digest_by_id[span.context.span_id] = cls.from_span(span) |
| 198 | |
| 199 | # Attach each log to its enclosing span (matched by span_id). |
| 200 | for log in logs: |
| 201 | span_id = log.log_record.span_id |
| 202 | if span_id is None or span_id == 0: |
| 203 | continue |
| 204 | digest = digest_by_id.get(span_id) |
| 205 | if digest is None: |
| 206 | continue |
| 207 | digest.logs.append(LogDigest.from_log(log)) |
| 208 | |
| 209 | root: SpanDigest | None = None |
| 210 | for span in spans: |
| 211 | if span.context is None: |
| 212 | continue |
| 213 | digest = digest_by_id[span.context.span_id] |
| 214 | if span.parent and span.parent.span_id in digest_by_id: |
| 215 | parent_digest = digest_by_id[span.parent.span_id] |
| 216 | parent_digest.children.append(digest) |
| 217 | else: |
| 218 | if root is not None: |
| 219 | raise ValueError("Multiple root spans found.") |
| 220 | root = digest |
| 221 | |
| 222 | # Sort for deterministic comparisons. |
| 223 | for digest in digest_by_id.values(): |
| 224 | digest.children.sort(key=lambda s: s.name) |
| 225 | digest.logs[:] = sorted_log_digests(digest.logs) |
| 226 | |
| 227 | if root is None: |
| 228 | raise ValueError("No root span found in the provided spans.") |
| 229 | return root |
| 230 | |
| 231 | def all_logs(self) -> list[LogDigest]: |
| 232 | """Returns all log digests in the tree, sorted deterministically.""" |
no test coverage detected