Class: DSPy::Observability

Inherits:
Object
  • Object
show all
Defined in:
lib/dspy/observability.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.enabledObject (readonly)

Returns the value of attribute enabled.



8
9
10
# File 'lib/dspy/observability.rb', line 8

def enabled
  @enabled
end

.endpointObject (readonly)

Returns the value of attribute endpoint.



8
9
10
# File 'lib/dspy/observability.rb', line 8

def endpoint
  @endpoint
end

.tracerObject (readonly)

Returns the value of attribute tracer.



8
9
10
# File 'lib/dspy/observability.rb', line 8

def tracer
  @tracer
end

Class Method Details

.configure!Object



10
11
12
13
14
15
16
17
18
19
20
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
# File 'lib/dspy/observability.rb', line 10

def configure!
  @enabled = false
  
  # Check for required Langfuse environment variables
  public_key = ENV['LANGFUSE_PUBLIC_KEY']
  secret_key = ENV['LANGFUSE_SECRET_KEY']
  
  unless public_key && secret_key
    return
  end

  # Determine endpoint based on host
  host = ENV['LANGFUSE_HOST'] || 'https://cloud.langfuse.com'
  @endpoint = "#{host}/api/public/otel"

  begin
    # Load OpenTelemetry gems
    require 'opentelemetry/sdk'
    require 'opentelemetry/exporter/otlp'

    # Generate Basic Auth header
    auth_string = Base64.strict_encode64("#{public_key}:#{secret_key}")
    
    # Configure OpenTelemetry SDK
    OpenTelemetry::SDK.configure do |config|
      config.service_name = 'dspy-ruby'
      config.service_version = DSPy::VERSION
      
      # Add OTLP exporter for Langfuse
      config.add_span_processor(
        OpenTelemetry::SDK::Trace::Export::BatchSpanProcessor.new(
          OpenTelemetry::Exporter::OTLP::Exporter.new(
            endpoint: @endpoint,
            headers: {
              'Authorization' => "Basic #{auth_string}",
              'Content-Type' => 'application/x-protobuf'
            },
            compression: 'gzip'
          )
        )
      )
      
      # Add resource attributes
      config.resource = OpenTelemetry::SDK::Resources::Resource.create({
        'service.name' => 'dspy-ruby',
        'service.version' => DSPy::VERSION,
        'telemetry.sdk.name' => 'opentelemetry',
        'telemetry.sdk.language' => 'ruby'
      })
    end

    # Create tracer
    @tracer = OpenTelemetry.tracer_provider.tracer('dspy', DSPy::VERSION)
    @enabled = true

  rescue LoadError => e
    DSPy.log('observability.disabled', reason: 'OpenTelemetry gems not available')
  rescue StandardError => e
    DSPy.log('observability.error', error: e.message, class: e.class.name)
  end
end

.enabled?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/dspy/observability.rb', line 72

def enabled?
  @enabled == true
end

.finish_span(span) ⇒ Object



94
95
96
97
98
99
100
# File 'lib/dspy/observability.rb', line 94

def finish_span(span)
  return unless span
  
  span.finish
rescue StandardError => e
  DSPy.log('observability.span_finish_error', error: e.message)
end

.reset!Object



102
103
104
105
106
# File 'lib/dspy/observability.rb', line 102

def reset!
  @enabled = false
  @tracer = nil
  @endpoint = nil
end

.start_span(operation_name, attributes = {}) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/dspy/observability.rb', line 76

def start_span(operation_name, attributes = {})
  return nil unless enabled? && tracer

  # Convert attribute keys to strings and filter out nil values
  string_attributes = attributes.transform_keys(&:to_s)
                               .reject { |k, v| v.nil? }
  string_attributes['operation.name'] = operation_name
  
  tracer.start_span(
    operation_name,
    kind: :internal,
    attributes: string_attributes
  )
rescue StandardError => e
  DSPy.log('observability.span_error', error: e.message, operation: operation_name)
  nil
end