Module: DSPy::Observability::Adapters::Langfuse

Defined in:
lib/dspy/o11y/langfuse.rb

Class Method Summary collapse

Class Method Details

.configure(obs) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/dspy/o11y/langfuse.rb', line 21

def configure(obs)
  return obs.disable!(reason: 'Explicitly disabled via DSPY_DISABLE_OBSERVABILITY') if ENV['DSPY_DISABLE_OBSERVABILITY'] == 'true'

  public_key = ENV['LANGFUSE_PUBLIC_KEY']
  secret_key = ENV['LANGFUSE_SECRET_KEY']

  if test_environment? && !(public_key && secret_key)
    return obs.disable!(reason: 'Test environment detected - OTLP disabled')
  end

  return false unless public_key && secret_key

  require_opentelemetry!
  patch_frozen_ssl_context_for_otlp!

  endpoint = langfuse_endpoint
  auth_string = Base64.strict_encode64("#{public_key}:#{secret_key}")

  OpenTelemetry::SDK.configure do |config|
    config.service_name = 'dspy-ruby'
    config.service_version = DSPy::VERSION

    exporter = OpenTelemetry::Exporter::OTLP::Exporter.new(
      endpoint: endpoint,
      headers: {
        'Authorization' => "Basic #{auth_string}",
        'Content-Type' => 'application/x-protobuf'
      },
      compression: 'gzip'
    )

    async_config = {
      queue_size: (ENV['DSPY_TELEMETRY_QUEUE_SIZE'] || DSPy::Observability::AsyncSpanProcessor::DEFAULT_QUEUE_SIZE).to_i,
      export_interval: (ENV['DSPY_TELEMETRY_EXPORT_INTERVAL'] || DSPy::Observability::AsyncSpanProcessor::DEFAULT_EXPORT_INTERVAL).to_f,
      export_batch_size: (ENV['DSPY_TELEMETRY_BATCH_SIZE'] || DSPy::Observability::AsyncSpanProcessor::DEFAULT_EXPORT_BATCH_SIZE).to_i,
      shutdown_timeout: (ENV['DSPY_TELEMETRY_SHUTDOWN_TIMEOUT'] || DSPy::Observability::AsyncSpanProcessor::DEFAULT_SHUTDOWN_TIMEOUT).to_f
    }

    config.add_span_processor(
      DSPy::Observability::AsyncSpanProcessor.new(exporter, **async_config)
    )

    config.resource = OpenTelemetry::SDK::Resources::Resource.create({
      'service.name' => 'dspy-ruby',
      'service.version' => DSPy::VERSION,
      'telemetry.sdk.name' => 'opentelemetry',
      'telemetry.sdk.language' => 'ruby'
    })
  end

  tracer = OpenTelemetry.tracer_provider.tracer('dspy', DSPy::VERSION)
  obs.enable!(tracer: tracer, endpoint: endpoint)
  true
rescue LoadError
  obs.disable!(reason: 'OpenTelemetry gems not available')
  false
rescue StandardError => e
  DSPy.log('observability.error', error: e.message, adapter: 'langfuse', class: e.class.name)
  obs.disable!
  false
end

.register!Object



15
16
17
18
19
# File 'lib/dspy/o11y/langfuse.rb', line 15

def register!
  DSPy::Observability.register_configurator(:langfuse) do |obs|
    configure(obs)
  end
end