Skip to content

Commit 04ed98a

Browse files
committed
Comply with latest token format
1 parent ba62e80 commit 04ed98a

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

logfire/_internal/config.py

+11-8
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@
116116
COMMON_REQUEST_HEADERS = {'User-Agent': f'logfire/{VERSION}'}
117117
"""Common request headers for requests to the Logfire API."""
118118
PROJECT_NAME_PATTERN = r'^[a-z0-9]+(?:-[a-z0-9]+)*$'
119+
PYDANTIC_LOGFIRE_TOKEN_PATTERN = re.compile(r'^pylf_v(?P<version>[0-9]+)_(?P<region>[a-z]+)_(?P<token>[a-zA-Z0-9]+)$')
119120

120121
METRICS_PREFERRED_TEMPORALITY = {
121122
Counter: AggregationTemporality.DELTA,
@@ -174,18 +175,20 @@ class AdvancedOptions:
174175
"""Configuration for OpenTelemetry logging. This is experimental and may be removed."""
175176

176177
def generate_base_url(self, token: str | None) -> str:
177-
if token and '-' in token:
178-
region, _ = token.split('-', maxsplit=1)
179-
else:
180-
region = None
181178
if self.base_url is not None:
182179
return self.base_url
183-
if region:
184-
return f'https://api-{region}.pydantic.dev'
185-
else:
186-
# default to us region for tokens that were created before regions were added
180+
181+
if token is None:
182+
# default to US region if no token is provided:
187183
return 'https://logfire-api.pydantic.dev'
188184

185+
match = PYDANTIC_LOGFIRE_TOKEN_PATTERN.match(token)
186+
if not match:
187+
# default to US region for tokens that were created before regions were added:
188+
return 'https://logfire-api.pydantic.dev'
189+
190+
return f'https://api-{match.group("region")}.pydantic.dev'
191+
189192

190193
@dataclass
191194
class PydanticPlugin:

logfire/_internal/config_params.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,9 @@ class _DefaultCallback:
9999
IGNORE_NO_CONFIG = ConfigParam(env_vars=['LOGFIRE_IGNORE_NO_CONFIG'], allow_file_config=True, default=False, tp=bool)
100100
"""Whether to show a warning message if logfire if used without calling logfire.configure()"""
101101
BASE_URL = ConfigParam(env_vars=['LOGFIRE_BASE_URL'], allow_file_config=True, default=None, tp=Union[str, None])
102+
"""The base URL of the Logfire backend. Primarily for testing purposes."""
102103
REGION = ConfigParam(env_vars=['LOGFIRE_REGION'], allow_file_config=True, default='us')
103104
"""The region of the Logfire backend."""
104-
"""The base URL of the Logfire backend. Primarily for testing purposes."""
105105
DISTRIBUTED_TRACING = ConfigParam(env_vars=['LOGFIRE_DISTRIBUTED_TRACING'], allow_file_config=True, default=None, tp=bool)
106106
"""Whether to extract incoming trace context. By default, will extract but warn about it."""
107107
# fmt: on

0 commit comments

Comments
 (0)