Class: DSPy::Observability
- Inherits:
-
Object
- Object
- DSPy::Observability
- Defined in:
- lib/dspy/observability.rb,
lib/dspy/observability/async_span_processor.rb
Defined Under Namespace
Classes: AsyncSpanProcessor
Class Attribute Summary collapse
-
.enabled ⇒ Object
readonly
Returns the value of attribute enabled.
-
.endpoint ⇒ Object
readonly
Returns the value of attribute endpoint.
-
.tracer ⇒ Object
readonly
Returns the value of attribute tracer.
Class Method Summary collapse
- .configure! ⇒ Object
- .enabled? ⇒ Boolean
- .finish_span(span) ⇒ Object
- .flush! ⇒ Object
- .reset! ⇒ Object
- .start_span(operation_name, attributes = {}) ⇒ Object
Class Attribute Details
.enabled ⇒ Object (readonly)
Returns the value of attribute enabled.
9 10 11 |
# File 'lib/dspy/observability.rb', line 9 def enabled @enabled end |
.endpoint ⇒ Object (readonly)
Returns the value of attribute endpoint.
9 10 11 |
# File 'lib/dspy/observability.rb', line 9 def endpoint @endpoint end |
.tracer ⇒ Object (readonly)
Returns the value of attribute tracer.
9 10 11 |
# File 'lib/dspy/observability.rb', line 9 def tracer @tracer end |
Class Method Details
.configure! ⇒ Object
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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/dspy/observability.rb', line 11 def configure! @enabled = false # Check for required Langfuse environment variables public_key = ENV['LANGFUSE_PUBLIC_KEY'] secret_key = ENV['LANGFUSE_SECRET_KEY'] # Skip OTLP configuration in test environment UNLESS Langfuse credentials are explicitly provided # This allows observability tests to run while protecting general tests from network calls if (ENV['RACK_ENV'] == 'test' || ENV['RAILS_ENV'] == 'test' || defined?(RSpec)) && !(public_key && secret_key) DSPy.log('observability.disabled', reason: 'Test environment detected - OTLP disabled') return end 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/v1/traces" 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 using AsyncSpanProcessor exporter = OpenTelemetry::Exporter::OTLP::Exporter.new( endpoint: @endpoint, headers: { 'Authorization' => "Basic #{auth_string}", 'Content-Type' => 'application/x-protobuf' }, compression: 'gzip' ) # Configure AsyncSpanProcessor with environment variables async_config = { queue_size: (ENV['DSPY_TELEMETRY_QUEUE_SIZE'] || AsyncSpanProcessor::DEFAULT_QUEUE_SIZE).to_i, export_interval: (ENV['DSPY_TELEMETRY_EXPORT_INTERVAL'] || AsyncSpanProcessor::DEFAULT_EXPORT_INTERVAL).to_f, export_batch_size: (ENV['DSPY_TELEMETRY_BATCH_SIZE'] || AsyncSpanProcessor::DEFAULT_EXPORT_BATCH_SIZE).to_i, shutdown_timeout: (ENV['DSPY_TELEMETRY_SHUTDOWN_TIMEOUT'] || AsyncSpanProcessor::DEFAULT_SHUTDOWN_TIMEOUT).to_f } config.add_span_processor( AsyncSpanProcessor.new(exporter, **async_config) ) # 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., class: e.class.name) end end |
.enabled? ⇒ Boolean
88 89 90 |
# File 'lib/dspy/observability.rb', line 88 def enabled? @enabled == true end |
.finish_span(span) ⇒ Object
114 115 116 117 118 119 120 |
# File 'lib/dspy/observability.rb', line 114 def finish_span(span) return unless span span.finish rescue StandardError => e DSPy.log('observability.span_finish_error', error: e.) end |
.flush! ⇒ Object
122 123 124 125 126 127 128 129 |
# File 'lib/dspy/observability.rb', line 122 def flush! return unless enabled? # Force flush any pending spans OpenTelemetry.tracer_provider.force_flush rescue StandardError => e DSPy.log('observability.flush_error', error: e.) end |
.reset! ⇒ Object
131 132 133 134 135 |
# File 'lib/dspy/observability.rb', line 131 def reset! @enabled = false @tracer = nil @endpoint = nil end |
.start_span(operation_name, attributes = {}) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/dspy/observability.rb', line 96 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., operation: operation_name) nil end |