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

Spans: capture code snippets #7930

Merged
merged 5 commits into from Jun 21, 2023
Merged

Spans: capture code snippets #7930

merged 5 commits into from Jun 21, 2023

Conversation

crusaderky
Copy link
Collaborator

Part of #7860

@crusaderky crusaderky requested a review from fjetter as a code owner June 19, 2023 08:13
@crusaderky crusaderky self-assigned this Jun 19, 2023
@github-actions
Copy link
Contributor

github-actions bot commented Jun 19, 2023

Unit Test Results

See test report for an extended history of previous test failures. This is useful for diagnosing flaky tests.

       20 files  +       1         20 suites  +1   12h 56m 41s ⏱️ + 40m 1s
  3 689 tests +       2    3 578 ✔️ +       3     106 💤 ±  0  5  - 1 
35 680 runs  +1 360  33 921 ✔️ +1 275  1 754 💤 +86  5  - 1 

For more details on these failures, see this check.

Results for commit 7f8c0fd. ± Comparison against base commit 49437c2.

♻️ This comment has been updated with latest results.

@crusaderky crusaderky removed the request for review from fjetter June 20, 2023 10:18
Copy link
Member

@fjetter fjetter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm OK moving forward even without the "split code by span" hack I mentioned in the thread. @crusaderky up to you

Comment on lines 604 to 608
assert "def _run(self)" in code[0][-2].code
# Code snippets are in first-seen order
assert "inc, 1" in code[0][-1].code
assert "inc, 2" in code[1][-1].code
assert "inc, 3" in code[2][-1].code
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on my testing, all spans have the same code. This is not a blocker but a bit unforunate.

I played briefly with ast to see if we can improve this. This code is a bit of a hack but works and we could apply something like this on a best-effort basis to only attach the code that is relevant to the given span.

I'm basing this on the span function name in the contextmanager. This could be refined further but should be good enough for most cases.

import ast
def parse_frame_by_span(frame):
    code_by_span = {}
    ast_fr = ast.parse(inspect.getsource(fr))
    for node in ast.walk(ast_fr):
        if isinstance(node, ast.With) and node.items[0].context_expr.func.id == "span":
            try:
                # This is a literal
                label = node.items[0].context_expr.args[0].value
            except:
                # This is a variable. We can't get all of them, only whatever
                # is stored in the locals at the time. Still, nice.
                label = node.items[0].context_expr.args[0].id
                label = fr.f_locals[label]
            code_by_span[label] = ast.unparse(node)
    return code_by_span

image

@mrocklin
Copy link
Member

Disclaimer: I haven't looked at this PR at all, so sorry for the potentially uninformed comment.

I'm pretty ok capturing more code than we need to if we can also capture the line number. Getting line numbers would, I think, help in many cases that I've seen. Often a big frame has many different compute calls within it (such as in a script or a large Jupyter cell). Getting line numbers would help us to disambiguate.

This is an expansion of scope, so maybe it makes sense to defer to another PR, but I thought I'd mention it as an alternative (or addition to) Florian's suggestion above (although I like Florian's thought as well).

@fjetter
Copy link
Member

fjetter commented Jun 20, 2023

Getting line numbers would help us to disambiguate.

We should be capturing the line of code already since #7786 (The fact that this is not shown on the Coiled UI, yet, is a Coiled issue)

@mrocklin
Copy link
Member

mrocklin commented Jun 20, 2023 via email

@crusaderky
Copy link
Collaborator Author

crusaderky commented Jun 20, 2023

I would rather keep the scope as small as possible and just ensure that spans receive the same code as computations here.

I think that the AST code snippet above is quite fragile. Namely, it wouldn't work with the @span decorators. The round-trip text->AST->text will make it very likely that a copy-paste from the dashboard to your IDE's search box won't give you the expected results - particularly if you're not using black.

@crusaderky
Copy link
Collaborator Author

I've updated the test to reflect that the three frames are different in line number

@crusaderky crusaderky merged commit 3de722a into dask:main Jun 21, 2023
22 of 27 checks passed
@crusaderky crusaderky deleted the spans_code branch June 21, 2023 13:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants