Class: Langfuse::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/langfuse/config.rb

Overview

Configuration object for Langfuse client

Examples:

Global configuration

Langfuse.configure do |config|
  config.public_key = ENV['LANGFUSE_PUBLIC_KEY']
  config.secret_key = ENV['LANGFUSE_SECRET_KEY']
  config.cache_ttl = 120
end

Per-client configuration

config = Langfuse::Config.new do |c|
  c.public_key = "pk_..."
  c.secret_key = "sk_..."
end

Constant Summary collapse

DEFAULT_BASE_URL =

Default values

"https://cloud.langfuse.com"
DEFAULT_TIMEOUT =
5
DEFAULT_CACHE_TTL =
60
DEFAULT_CACHE_MAX_SIZE =
1000
DEFAULT_CACHE_BACKEND =
:memory
DEFAULT_CACHE_LOCK_TIMEOUT =
10
DEFAULT_CACHE_STALE_WHILE_REVALIDATE =
false
DEFAULT_CACHE_REFRESH_THREADS =
5
DEFAULT_TRACING_ASYNC =
true
DEFAULT_BATCH_SIZE =
50
DEFAULT_FLUSH_INTERVAL =
10
DEFAULT_JOB_QUEUE =
:default
INDEFINITE_SECONDS =

Number of seconds representing indefinite cache duration (~1000 years)

1000 * 365 * 24 * 60 * 60

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|config| ... } ⇒ Config

Initialize a new Config object

Yields:

  • (config)

    Optional block for configuration

Yield Parameters:

  • config (Config)

    The config instance



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/langfuse/config.rb', line 92

def initialize
  @public_key = ENV.fetch("LANGFUSE_PUBLIC_KEY", nil)
  @secret_key = ENV.fetch("LANGFUSE_SECRET_KEY", nil)
  @base_url = ENV.fetch("LANGFUSE_BASE_URL", DEFAULT_BASE_URL)
  @timeout = DEFAULT_TIMEOUT
  @cache_ttl = DEFAULT_CACHE_TTL
  @cache_max_size = DEFAULT_CACHE_MAX_SIZE
  @cache_backend = DEFAULT_CACHE_BACKEND
  @cache_lock_timeout = DEFAULT_CACHE_LOCK_TIMEOUT
  @cache_stale_while_revalidate = DEFAULT_CACHE_STALE_WHILE_REVALIDATE
  @cache_stale_ttl = 0 # Default to 0 (SWR disabled, entries expire immediately after TTL)
  @cache_refresh_threads = DEFAULT_CACHE_REFRESH_THREADS
  @tracing_async = DEFAULT_TRACING_ASYNC
  @batch_size = DEFAULT_BATCH_SIZE
  @flush_interval = DEFAULT_FLUSH_INTERVAL
  @job_queue = DEFAULT_JOB_QUEUE
  @logger = default_logger

  yield(self) if block_given?
end

Instance Attribute Details

#base_urlString

Returns Base URL for Langfuse API.

Returns:

  • (String)

    Base URL for Langfuse API



29
30
31
# File 'lib/langfuse/config.rb', line 29

def base_url
  @base_url
end

#batch_sizeInteger

Returns Number of events to batch before sending.

Returns:

  • (Integer)

    Number of events to batch before sending



63
64
65
# File 'lib/langfuse/config.rb', line 63

def batch_size
  @batch_size
end

#cache_backendSymbol

Returns Cache backend (:memory or :rails).

Returns:

  • (Symbol)

    Cache backend (:memory or :rails)



44
45
46
# File 'lib/langfuse/config.rb', line 44

def cache_backend
  @cache_backend
end

#cache_lock_timeoutInteger

Returns Lock timeout in seconds for distributed cache stampede protection.

Returns:

  • (Integer)

    Lock timeout in seconds for distributed cache stampede protection



47
48
49
# File 'lib/langfuse/config.rb', line 47

def cache_lock_timeout
  @cache_lock_timeout
end

#cache_max_sizeInteger

Returns Maximum number of cached items.

Returns:

  • (Integer)

    Maximum number of cached items



41
42
43
# File 'lib/langfuse/config.rb', line 41

def cache_max_size
  @cache_max_size
end

#cache_refresh_threadsInteger

Returns Number of background threads for cache refresh.

Returns:

  • (Integer)

    Number of background threads for cache refresh



57
58
59
# File 'lib/langfuse/config.rb', line 57

def cache_refresh_threads
  @cache_refresh_threads
end

#cache_stale_ttlInteger, Symbol

Returns Stale TTL in seconds (grace period for serving stale data, default: 0 when SWR disabled, cache_ttl when SWR enabled) Accepts :indefinite which is automatically normalized to 1000 years (31,536,000,000 seconds) for practical “never expire” behavior.

Returns:

  • (Integer, Symbol)

    Stale TTL in seconds (grace period for serving stale data, default: 0 when SWR disabled, cache_ttl when SWR enabled) Accepts :indefinite which is automatically normalized to 1000 years (31,536,000,000 seconds) for practical “never expire” behavior.



54
55
56
# File 'lib/langfuse/config.rb', line 54

def cache_stale_ttl
  @cache_stale_ttl
end

#cache_stale_while_revalidateBoolean

Returns Enable stale-while-revalidate caching (when true, sets cache_stale_ttl to cache_ttl if not customized).

Returns:

  • (Boolean)

    Enable stale-while-revalidate caching (when true, sets cache_stale_ttl to cache_ttl if not customized)



50
51
52
# File 'lib/langfuse/config.rb', line 50

def cache_stale_while_revalidate
  @cache_stale_while_revalidate
end

#cache_ttlInteger

Returns Cache TTL in seconds.

Returns:

  • (Integer)

    Cache TTL in seconds



38
39
40
# File 'lib/langfuse/config.rb', line 38

def cache_ttl
  @cache_ttl
end

#flush_intervalInteger

Returns Interval in seconds to flush buffered events.

Returns:

  • (Integer)

    Interval in seconds to flush buffered events



66
67
68
# File 'lib/langfuse/config.rb', line 66

def flush_interval
  @flush_interval
end

#job_queueSymbol

Returns ActiveJob queue name for async processing.

Returns:

  • (Symbol)

    ActiveJob queue name for async processing



69
70
71
# File 'lib/langfuse/config.rb', line 69

def job_queue
  @job_queue
end

#loggerLogger

Returns Logger instance for debugging.

Returns:

  • (Logger)

    Logger instance for debugging



35
36
37
# File 'lib/langfuse/config.rb', line 35

def logger
  @logger
end

#public_keyString?

Returns Langfuse public API key.

Returns:

  • (String, nil)

    Langfuse public API key



23
24
25
# File 'lib/langfuse/config.rb', line 23

def public_key
  @public_key
end

#secret_keyString?

Returns Langfuse secret API key.

Returns:

  • (String, nil)

    Langfuse secret API key



26
27
28
# File 'lib/langfuse/config.rb', line 26

def secret_key
  @secret_key
end

#timeoutInteger

Returns HTTP request timeout in seconds.

Returns:

  • (Integer)

    HTTP request timeout in seconds



32
33
34
# File 'lib/langfuse/config.rb', line 32

def timeout
  @timeout
end

#tracing_asyncBoolean

Returns Use async processing for traces (requires ActiveJob).

Returns:

  • (Boolean)

    Use async processing for traces (requires ActiveJob)



60
61
62
# File 'lib/langfuse/config.rb', line 60

def tracing_async
  @tracing_async
end

Instance Method Details

#normalized_stale_ttlInteger

Normalize stale_ttl value

Converts :indefinite to 1000 years in seconds for practical “never expire” behavior while keeping the value finite for calculations.

Examples:

config.cache_stale_ttl = 300
config.normalized_stale_ttl # => 300

config.cache_stale_ttl = :indefinite
config.normalized_stale_ttl # => 31536000000

Returns:

  • (Integer)

    Normalized stale TTL in seconds



150
151
152
# File 'lib/langfuse/config.rb', line 150

def normalized_stale_ttl
  cache_stale_ttl == :indefinite ? INDEFINITE_SECONDS : cache_stale_ttl
end

#validate!void

This method returns an undefined value.

Validate the configuration

rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity

Raises:



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/langfuse/config.rb', line 118

def validate!
  raise ConfigurationError, "public_key is required" if public_key.nil? || public_key.empty?
  raise ConfigurationError, "secret_key is required" if secret_key.nil? || secret_key.empty?
  raise ConfigurationError, "base_url cannot be empty" if base_url.nil? || base_url.empty?
  raise ConfigurationError, "timeout must be positive" if timeout.nil? || timeout <= 0
  raise ConfigurationError, "cache_ttl must be non-negative" if cache_ttl.nil? || cache_ttl.negative?
  raise ConfigurationError, "cache_max_size must be positive" if cache_max_size.nil? || cache_max_size <= 0

  if cache_lock_timeout.nil? || cache_lock_timeout <= 0
    raise ConfigurationError,
          "cache_lock_timeout must be positive"
  end

  validate_swr_config!

  validate_cache_backend!
end