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 |
# 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'] 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
81 82 83 |
# File 'lib/dspy/observability.rb', line 81 def enabled? @enabled == true end |
.finish_span(span) ⇒ Object
107 108 109 110 111 112 113 |
# File 'lib/dspy/observability.rb', line 107 def finish_span(span) return unless span span.finish rescue StandardError => e DSPy.log('observability.span_finish_error', error: e.) end |
.flush! ⇒ Object
115 116 117 118 119 120 121 122 |
# File 'lib/dspy/observability.rb', line 115 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
124 125 126 127 128 |
# File 'lib/dspy/observability.rb', line 124 def reset! @enabled = false @tracer = nil @endpoint = nil end |
.start_span(operation_name, attributes = {}) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/dspy/observability.rb', line 89 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 |