Class: NitroIntelligence::LangfuseExtension

Inherits:
Object
  • Object
show all
Defined in:
lib/nitro_intelligence/langfuse_extension.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of LangfuseExtension.

Yields:



14
15
16
17
18
19
20
21
# File 'lib/nitro_intelligence/langfuse_extension.rb', line 14

def initialize
  config = Langfuse::Config.new
  yield(config) if block_given?

  @config = config
  @client = Langfuse::Client.new(config)
  @tracer_provider = NitroIntelligence::LangfuseTracerProvider.new(config)
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



12
13
14
# File 'lib/nitro_intelligence/langfuse_extension.rb', line 12

def config
  @config
end

Instance Method Details

#create_score(name:, value:, id: nil, trace_id: nil, session_id: nil, observation_id: nil, comment: nil, metadata: nil, environment: nil, data_type: :numeric, dataset_run_id: nil, config_id: nil) ⇒ Object

rubocop:disable Metrics/ParameterLists



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/nitro_intelligence/langfuse_extension.rb', line 28

def create_score(name:, value:, id: nil, trace_id: nil, session_id: nil, observation_id: nil, comment: nil, # rubocop:disable Metrics/ParameterLists
                 metadata: nil, environment: nil, data_type: :numeric, dataset_run_id: nil, config_id: nil)
  @client.create_score(
    name:,
    value:,
    id:,
    trace_id:,
    session_id:,
    observation_id:,
    comment:,
    metadata:,
    environment:,
    data_type:,
    dataset_run_id:,
    config_id:
  )
end

#observe(name, attrs = {}, as_type: :span, trace_id: nil, **kwargs, &block) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/nitro_intelligence/langfuse_extension.rb', line 75

def observe(name, attrs = {}, as_type: :span, trace_id: nil, **kwargs, &block)
  merged_attrs = attrs.to_h.merge(kwargs)
  observation = start_observation(name, merged_attrs, as_type:, trace_id:)
  return observation unless block

  observation.send(:run_in_context, &block)
end

#shutdown(timeout: 30) ⇒ Object



23
24
25
26
# File 'lib/nitro_intelligence/langfuse_extension.rb', line 23

def shutdown(timeout: 30)
  @client.shutdown
  @tracer_provider.shutdown(timeout:)
end

#start_observation(name, attrs = {}, as_type: :span, trace_id: nil, parent_span_context: nil, start_time: nil, skip_validation: false) ⇒ Object

rubocop:disable Metrics/ParameterLists



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
# File 'lib/nitro_intelligence/langfuse_extension.rb', line 46

def start_observation(name, attrs = {}, as_type: :span, trace_id: nil, parent_span_context: nil, start_time: nil, # rubocop:disable Metrics/ParameterLists
                      skip_validation: false)
  parent_span_context = Langfuse.send(:resolve_trace_context, trace_id, parent_span_context)
  type_str = as_type.to_s
  Langfuse.send(:validate_observation_type!, as_type, type_str) unless skip_validation

  otel_tracer = @tracer_provider.tracer
  otel_span = Langfuse.send(:create_otel_span,
                            name:,
                            start_time:,
                            parent_span_context:,
                            otel_tracer:)

  # Serialize attributes
  # Only set attributes if span is still recording (should always be true here, but guard for safety)
  if otel_span.recording?
    otel_attrs = Langfuse::OtelAttributes.create_observation_attributes(type_str, attrs.to_h, mask: nil)
    otel_attrs.each { |key, value| otel_span.set_attribute(key, value) }
  end

  # Wrap in appropriate class (attributes already set on span above — pass nil to avoid double-masking)
  observation = Langfuse.send(:wrap_otel_span, otel_span, type_str, otel_tracer)

  # Events auto-end immediately when created
  observation.end if type_str == Langfuse::OBSERVATION_TYPES[:event]

  observation
end