Skip to content

Account for new EU region - #901

Merged
alexmojaki merged 6 commits into
mainfrom
regions
Mar 25, 2025
Merged

alexmojaki merged 6 commits into
mainfrom
regions

Conversation

@adriangb

Copy link
Copy Markdown
Member

This goes hand in hand with a backend PR (private) that adds a region prefix to tokens.

@adriangb
adriangb requested a review from alexmojaki February 26, 2025 00:09
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Feb 26, 2025 •

Copy link
Copy Markdown

Deploying logfire-docs with  Cloudflare Pages  Cloudflare Pages

Latest commit: 9749707
Status: ✅  Deploy successful!
Preview URL: https://005d3841.logfire-docs.pages.dev
Branch Preview URL: https://regions.logfire-docs.pages.dev

View logs

Comment thread logfire/_internal/config.py Resolved
Comment thread logfire/_internal/config_params.py Resolved
@Viicos

Viicos commented Mar 24, 2025

Copy link
Copy Markdown
Member

I changed the logic related to regions and tokens in the last commit. The implementation in LogfireCredentials was already slightly convoluted so I've tried making as little changes as possible:

logfire.configure()

  • if no token is provided and no credentials file is present:
    • try to create a new project. To do so, we need a user token (present in ~/.logfire/default.toml) 1:
      • if an explicit base URL was provided in logfire.configure(), try to find a token for this base URL. If none, prompt the user to logfire auth.
      • if no explicit base URL was provided, iterate over the available user tokens:
        • if only one exists, use it (this will cover most use cases, as users are expected to have only one user token).
        • if multiple tokens exists, prompt the user into selecting one.

CLI

Up until now, the --logfire-url (undocumented) option defaulted to logfire.pydantic.dev. In this PR, it defaults to an empty string so we need to update the logic to select a correct base URL.

For most CLI commands (e.g. logfire list), the logic is delegated to LogfireCredentials._get_token_data() and so the same behavior described below applies.

For logfire auth, we look into the user tokens (present in ~/.logfire/default.toml):

  • if an explicit --logfire-url was provided, try to find a token for this base URL. If it exists, exit saying the user is already logged in.
  • if no explicit --logfire-url was provided, assume the user is logged in if any of the available user tokens is still valid (and then also exits).
  • else, the user is not logged in. We prompt the user into selecting a region between eu/us, and determine the appropriate base URL to use to do the device code logic.

Footnotes

  1. The following logic describes what's in LogfireCredentials._get_token_data(). ↩

Comment thread logfire/_internal/config.py Resolved
Comment thread logfire/_internal/config.py Resolved
Comment thread logfire/_internal/config.py Resolved

@alexmojaki alexmojaki left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think that deriving the base URL from the write token is still entirely separate from dealing with user tokens, in which case this should be split into two PRs

Comment thread logfire/_internal/config.py Resolved
Comment thread logfire/_internal/config.py Resolved
Comment thread logfire/_internal/config.py Resolved
@ChristopherGS ChristopherGS changed the title derive base urls from tokens' regions EU region: derive base urls from tokens' regions Mar 24, 2025
@Viicos
Viicos force-pushed the regions branch 2 times, most recently from b751aed to 4a93a76 Compare March 24, 2025 20:24
Comment thread logfire/_internal/cli.py Outdated
logfire_url = args.logfire_url
logfire_url = _get_logfire_url(args.logfire_url, args.region)
if logfire_url is None:
_, logfire_url = LogfireCredentials._get_token_data() # type: ignore

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Perhaps _get_token_data() should be made "public".

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LogfireCredentials.create_new_project already calls _get_token_data, which makes sense, so you shouldn't need this.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The issue is I need the URL to then call _write_credentials() 😕 :

logfire_url = _get_logfire_url(args.logfire_url, args.region)
if logfire_url is None:
_, logfire_url = LogfireCredentials._get_token_data() # type: ignore
project_name = args.project_name
organization = args.org
default_organization = args.default_org
project_info = LogfireCredentials.create_new_project(
session=args._session,
logfire_api_url=logfire_url,
organization=organization,
default_organization=default_organization,
project_name=project_name,
)
credentials = _write_credentials(project_info, data_dir, logfire_url)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

ok that doesn't need to be fixed here but this is a sign of messy logic that doesn't need to be this way, the other place where create_new_project gets called ends up following a similar path including write_creds_file

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yes I mentioned this in #901 (comment), after the region launch we can revisit

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

yes but what i'm saying is that unlike what you said there this logic is not necessary

@Viicos

Viicos commented Mar 24, 2025

Copy link
Copy Markdown
Member

I think that deriving the base URL from the write token is still entirely separate from dealing with user tokens, in which case this should be split into two PRs

I'm not sure this is possible as changes seems to be tied up and inter-dependent.

I'll note that the logic is starting to be quite confusing, lots of indirection is happening especially on logic related to base URL creation (for instance in some CLI commands such as projects list, we use either --logfire-url, URL from --region, or LogfireCredentials._get_token_data() which looks into ~/.logfire tokens — this logic is necessary but hard to follow).

Comment thread logfire/_internal/cli.py Resolved
Comment thread logfire/_internal/cli.py Resolved
Comment thread logfire/_internal/cli.py Resolved
Comment thread logfire/_internal/cli.py Outdated
data_dir = Path(args.data_dir)
param_manager = ParamManager.create(data_dir)
base_url = param_manager.load_param('base_url', args.logfire_url)
base_url = param_manager.load_param('base_url', _get_logfire_url(args.logfire_url, args.region))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Not for this PR, but load_param should probably be used for all commands, so that the env var LOGFIRE_BASE_URL always works.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Same for region.

Comment thread logfire/_internal/cli.py Outdated
logfire_url = args.logfire_url
logfire_url = _get_logfire_url(args.logfire_url, args.region)
if logfire_url is None:
_, logfire_url = LogfireCredentials._get_token_data() # type: ignore

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LogfireCredentials.create_new_project already calls _get_token_data, which makes sense, so you shouldn't need this.

Comment thread logfire/_internal/cli.py Resolved
Comment thread logfire/_internal/config.py Resolved
Comment thread logfire/_internal/config.py Resolved
Comment thread logfire/_internal/config.py Resolved
Comment thread logfire/_internal/cli.py Resolved
Comment thread logfire/_internal/config.py Resolved
Comment thread logfire/_internal/cli.py
global_opts.add_argument('--logfire-url', default=LOGFIRE_BASE_URL, help=argparse.SUPPRESS)
url_or_region_grp = global_opts.add_mutually_exclusive_group()
url_or_region_grp.add_argument('--logfire-url', help=argparse.SUPPRESS)
url_or_region_grp.add_argument('--region', choices=REGIONS, help='the region to use')

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

logfire auth -h doesn't show --region

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

--region is attached to the global parser, it will show up when doing logfire --help (and should be specified as logfire --region eu/us auth).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can we not improve this?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

anyway i guess we leave it for another PR

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can we not improve this?

I can relatively easily move the two mutually exclusive --logfire-url and --region options for each command.

Comment thread logfire/_internal/cli.py
cmd_info.set_defaults(func=parse_info)

namespace = parser.parse_args(args)
namespace.logfire_url = _get_logfire_url(namespace.logfire_url, namespace.region)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

ok this might have been a mistake, e.g. logfire whoami now asks me which region i want, when i want it to tell me based on the write token

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It seems to me that something is inconsistent here: the ParamManager instance created in the whoami command (that then load_param the token) is reading from a pyproject.toml file. Is it expected for a token to be present in pyproject.toml files? Should we then also call LogfireCredentials.load_creds_file()?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

the token can't be in pyproject, but everything else can, and the token can be in env vars. and yes we also want load_creds_file.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hum so as it stands whoami will try to read token from the pyproject. I looked and it is the only place where param_manager.load_param('token') is used, so I can easily replace the logic with load_creds_file().

Comment thread tests/conftest.py


@pytest.fixture
def multiple_credentials(tmp_path: Path) -> Path:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

These two new fixtures should be used in both test_cli and test_configure

@Viicos Viicos Mar 25, 2025 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

test_cli monkeypatches _get_user_token_data() everywhere as _get_user_token_data() is unit tested in test_configure (and these fixtures are relevant to test it). Not ideal, but this would have introduced more changes to this PR.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

ok but at least we should test that logfire whoami doesn't ask the user for a region when there are multiple credentials.

@Viicos
Viicos marked this pull request as ready for review March 25, 2025 14:28
@alexmojaki alexmojaki changed the title EU region: derive base urls from tokens' regions Account for new EU region Mar 25, 2025
@alexmojaki
alexmojaki merged commit 2073a0c into main Mar 25, 2025
@alexmojaki
alexmojaki deleted the regions branch March 25, 2025 15:19
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.

3 participants