@@ -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,9 @@ 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 sendToLogfire = resolveSendToLogfire ( cnf . sendToLogfire , token )
170+ const baseUrl = ! sendToLogfire || ! token ? '' : resolveBaseUrl ( cnf . advanced ?. baseUrl , token )
166171
167172 Object . assign ( logfireConfig , {
168173 additionalSpanProcessors : cnf . additionalSpanProcessors ?? [ ] ,
@@ -178,7 +183,7 @@ export function configure(config: LogfireConfigOptions = {}) {
178183 metricExporterUrl : `${ baseUrl } /${ METRIC_ENDPOINT_PATH } ` ,
179184 metrics : cnf . metrics ,
180185 scrubber : resolveScrubber ( cnf . scrubbing ) ,
181- sendToLogfire : resolveSendToLogfire ( cnf . sendToLogfire , token ) ,
186+ sendToLogfire,
182187 serviceName : cnf . serviceName ?? env . LOGFIRE_SERVICE_NAME ,
183188 serviceVersion : cnf . serviceVersion ?? env . LOGFIRE_SERVICE_VERSION ,
184189 token,
@@ -214,8 +219,8 @@ function resolveSendToLogfire(option: LogfireConfigOptions['sendToLogfire'], tok
214219 }
215220}
216221
217- function resolveBaseUrl ( option : string | undefined ) {
218- let url = option ?? process . env . LOGFIRE_BASE_URL ?? DEFAULT_LOGFIRE_BASE_URL
222+ export function resolveBaseUrl ( passedUrl : string | undefined , token : string ) {
223+ let url = passedUrl ?? process . env . LOGFIRE_BASE_URL ?? getBaseUrlFromToken ( token )
219224 if ( url . endsWith ( '/' ) ) {
220225 url = url . slice ( 0 , - 1 )
221226 }
@@ -226,3 +231,30 @@ function resolveDistributedTracing(option: LogfireConfigOptions['distributedTrac
226231 const envDistributedTracing = process . env . LOGFIRE_DISTRIBUTED_TRACING
227232 return ( option ?? envDistributedTracing === undefined ) ? true : envDistributedTracing === 'true'
228233}
234+
235+ 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 ] + ) $ /
236+
237+ const REGIONS : Record < string , RegionData > = {
238+ eu : {
239+ baseUrl : 'https://logfire-eu.pydantic.dev' ,
240+ gcpRegion : 'europe-west4' ,
241+ } ,
242+ us : {
243+ baseUrl : 'https://logfire-us.pydantic.dev' ,
244+ gcpRegion : 'us-east4' ,
245+ } ,
246+ }
247+
248+ function getBaseUrlFromToken ( token : string | undefined ) : string {
249+ let regionKey = 'us'
250+ if ( token ) {
251+ const match = PYDANTIC_LOGFIRE_TOKEN_PATTERN . exec ( token )
252+ if ( match ) {
253+ const region = match . groups ?. region
254+ if ( region && region in REGIONS ) {
255+ regionKey = region
256+ }
257+ }
258+ }
259+ return REGIONS [ regionKey ] . baseUrl
260+ }
0 commit comments