Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvement to capfire #523

Open
samuelcolvin opened this issue Oct 20, 2024 · 1 comment
Open

Improvement to capfire #523

samuelcolvin opened this issue Oct 20, 2024 · 1 comment
Labels
P3 Less than P2.

Comments

@samuelcolvin
Copy link
Member

samuelcolvin commented Oct 20, 2024

I found this useful, perhaps we should add it to capfire?

class TraceSummary(TypedDict):
    id: int
    message: str
    children: NotRequired[list[TraceSummary]]


@dataclass(init=False)
class LogfireSummary:
    traces: list[TraceSummary]
    attributes: dict[int, dict[str, Any]]

    def __init__(self, capfire: CaptureLogfire):
        spans = capfire.exporter.exported_spans_as_dict()
        spans.sort(key=lambda s: s['start_time'])
        self.traces = []
        span_lookup: dict[tuple[str, str], TraceSummary] = {}
        self.attributes = {}
        id_counter = 0
        for span in spans:
            tid = span['context']['trace_id'], span['context']['span_id']
            span_lookup[tid] = span_summary = TraceSummary(id=id_counter, message=span['attributes']['logfire.msg'])
            self.attributes[id_counter] = span['attributes']
            id_counter += 1
            if parent := span['parent']:
                parent_span = span_lookup[(parent['trace_id'], parent['span_id'])]
                parent_span.setdefault('children', []).append(span_summary)
            else:
                self.traces.append(span_summary)


@pytest.fixture
def get_logfire_summary(capfire: CaptureLogfire) -> Callable[[], LogfireSummary]:
    def get_summary() -> LogfireSummary:
        return LogfireSummary(capfire)

    return get_summary
@samuelcolvin
Copy link
Member Author

see pydantic/pydantic-ai#11

@samuelcolvin samuelcolvin added the P3 Less than P2. label Oct 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P3 Less than P2.
Projects
None yet
Development

No branches or pull requests

1 participant