@@ -43,6 +43,11 @@ export interface SrubbingOptions {
4343 extraPatterns ?: string [ ]
4444}
4545
46+ export interface RegionData {
47+ baseUrl : string
48+ gcpRegion : string
49+ }
50+
4651export interface LogfireConfigOptions {
4752 /**
4853 * Additional span processors to be added to the OpenTelemetry SDK
@@ -103,7 +108,6 @@ export interface LogfireConfigOptions {
103108 token ?: string
104109}
105110
106- const DEFAULT_LOGFIRE_BASE_URL = 'https://logfire-api.pydantic.dev/'
107111const DEFAULT_OTEL_SCOPE = 'logfire'
108112const TRACE_ENDPOINT_PATH = 'v1/traces'
109113const METRIC_ENDPOINT_PATH = 'v1/metrics'
@@ -161,8 +165,8 @@ export function configure(config: LogfireConfigOptions = {}) {
161165 logfireApi . configureLogfireApi ( { otelScope } )
162166 }
163167
164- const baseUrl = resolveBaseUrl ( cnf . advanced ?. baseUrl )
165168 const token = cnf . token ?? env . LOGFIRE_TOKEN
169+ const baseUrl = resolveBaseUrl ( cnf . advanced ?. baseUrl , token )
166170
167171 Object . assign ( logfireConfig , {
168172 additionalSpanProcessors : cnf . additionalSpanProcessors ?? [ ] ,
@@ -214,8 +218,8 @@ function resolveSendToLogfire(option: LogfireConfigOptions['sendToLogfire'], tok
214218 }
215219}
216220
217- function resolveBaseUrl ( option : string | undefined ) {
218- let url = option ?? process . env . LOGFIRE_BASE_URL ?? DEFAULT_LOGFIRE_BASE_URL
221+ export function resolveBaseUrl ( passedUrl : string | undefined , token : string | undefined ) {
222+ let url = passedUrl ?? process . env . LOGFIRE_BASE_URL ?? getBaseUrlFromToken ( token )
219223 if ( url . endsWith ( '/' ) ) {
220224 url = url . slice ( 0 , - 1 )
221225 }
@@ -226,3 +230,30 @@ function resolveDistributedTracing(option: LogfireConfigOptions['distributedTrac
226230 const envDistributedTracing = process . env . LOGFIRE_DISTRIBUTED_TRACING
227231 return ( option ?? envDistributedTracing === undefined ) ? true : envDistributedTracing === 'true'
228232}
233+
234+ const PYDANTIC_LOGFIRE_TOKEN_PATTERN = / ^ (?< safe_part > p y l f _ v (?< version > [ 0 - 9 ] + ) _ (?< region > [ a - z ] + ) _ ) (?< token > [ a - z A - Z 0 - 9 ] + ) $ /
235+
236+ const REGIONS : Record < string , RegionData > = {
237+ eu : {
238+ baseUrl : 'https://logfire-eu.pydantic.dev' ,
239+ gcpRegion : 'europe-west4' ,
240+ } ,
241+ us : {
242+ baseUrl : 'https://logfire-us.pydantic.dev' ,
243+ gcpRegion : 'us-east4' ,
244+ } ,
245+ }
246+
247+ function getBaseUrlFromToken ( token : string | undefined ) : string {
248+ let regionKey = 'us'
249+ if ( token ) {
250+ const match = PYDANTIC_LOGFIRE_TOKEN_PATTERN . exec ( token )
251+ if ( match ) {
252+ const region = match . groups ?. region
253+ if ( region && region in REGIONS ) {
254+ regionKey = region
255+ }
256+ }
257+ }
258+ return REGIONS [ regionKey ] . baseUrl
259+ }
0 commit comments