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

Propagate tags to child spans #492

Open
LumaC0 opened this issue Oct 10, 2024 · 5 comments
Open

Propagate tags to child spans #492

LumaC0 opened this issue Oct 10, 2024 · 5 comments
Assignees

Comments

@LumaC0
Copy link

LumaC0 commented Oct 10, 2024

Description

A logfire.span argument that allows child spans and records to inherit tags. All child spans (a relationship that looks to be defined by index in OPEN_SPANS) would inherit tags from surrounding spans that have propagate_tags=True. Spans that set the argument to False (the default) do not pass tags to child spans.

Proposal

logfire.config()

with logfire.span("first span", _tags=["one"], _propagate_tags=True): # has tag ["one"]
    logfire.info("first") # has tag ["one"]

    with logfire.span("second span", _tags=["two"]): # has tags ["one", "two"]
        logfire.info("second", _tags=["t1"]) # has tag ["one", "t1"]

        with logfire.span("third span", _tags=["three"], _propagate_tags=True) # has tags ["one", "three"]
            logfire.info("third") # has tags ["one", "three"]
@alexmojaki
Copy link
Contributor

Try logfire.with_tags

@LumaC0
Copy link
Author

LumaC0 commented Oct 10, 2024

@alexmojaki I don't think that will work across files. logfire.with_tags creates a new instance of logfire. if I create that new instance in file1.py and, within that file, create a span that encapsulates ClassInFile2().create() which exists in file2.py, I can't import the logfire instance from file1.py because of circular import issues.

Sorry if this is unclear or I am missing something

@alexmojaki
Copy link
Contributor

Can't you define the instance in another file that both file1 and file2 import?

@LumaC0
Copy link
Author

LumaC0 commented Oct 10, 2024

I could hack this together in each of my projects.

(I would instantiate a Logfire instance (not using with_tags) in file3 and just keep updating its internal _tags attribute)

this is fine but it's not really a long term solution.

@alexmojaki
Copy link
Contributor

OK, I guess you're using tags in a way that's more dynamic than what with_tags was intended for.

I would recommend creating a context variable and passing the value to _tags rather than modifying a global shared logfire instance. But I understand that this is still inconvenient. We should create an API that automates this, without requiring opening a span, and that also propagates things like attributes and levels.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants