rubyllm-observ 0.6.4 → 0.6.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a648839b815f45ded484d1b4e97c83a583fff99b3922d4920694c30500e566d4
4
- data.tar.gz: 6bda76d81a50e4171fbc400c4195e76a6db1c270fe57aab6d2d9182e872f5613
3
+ metadata.gz: 94f902979484dc837e40bf42dbfc66381eaf4326b3f03831b31aa36478a55c81
4
+ data.tar.gz: bfbdeaf7ecf11149bcb6d22832267d8959d827b4680aac05c25d762d48310293
5
5
  SHA512:
6
- metadata.gz: 8adeb19ae2ea22fc7aef0190b89d87ca60921a34ef21ab7dc385c8882a0590316a2cc29f1d5cefeb7ab0c5cf27cfa766766a1507f4496aef2c14196e967b57e5
7
- data.tar.gz: ed4287385ce15431b945d223300213d26a678290fb48a156108a968a76e52476a22dcac1777fbe287842e6fb855bd5c6563de946d0d98e0b2f0eaac410aa66cb
6
+ metadata.gz: e6d87df6d6e07ed28f8b5df8ed07d25077705764c5535176e9a142e58a59a6f023b313add55a8e25e37f0aebe3400d7d1631b326554431f9087c37806e8d93b0
7
+ data.tar.gz: 86c2026e946ee56ff3089294a53952eee7c93c4a238ca9553596bd51e8fe098e36fd3c50db586e6d2d50b69515a07f71b11ddcc8374cb0a18ae118e967766404
@@ -0,0 +1,143 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Observ
4
+ module Concerns
5
+ # Concern for adding observability to service objects
6
+ #
7
+ # This module provides automatic observability session management for services
8
+ # that perform LLM operations. It handles session creation, lifecycle management,
9
+ # and chat instrumentation.
10
+ #
11
+ # Usage:
12
+ # class MyService
13
+ # include Observ::Concerns::ObservableService
14
+ #
15
+ # def initialize(observability_session: nil)
16
+ # initialize_observability(
17
+ # observability_session,
18
+ # service_name: "my_service",
19
+ # metadata: { custom: "data" }
20
+ # )
21
+ # end
22
+ #
23
+ # def perform(input)
24
+ # with_observability do |session|
25
+ # # Your service logic here
26
+ # # Session automatically finalized on success/error
27
+ # end
28
+ # end
29
+ # end
30
+ module ObservableService
31
+ extend ActiveSupport::Concern
32
+
33
+ included do
34
+ attr_reader :observability
35
+ end
36
+
37
+ # Initialize observability for the service
38
+ #
39
+ # @param session_or_false [Observ::Session, false, nil] Session to use, false to disable, nil to auto-create
40
+ # @param service_name [String] Name of the service (used in session metadata)
41
+ # @param metadata [Hash] Additional metadata to include in the session
42
+ def initialize_observability(session_or_false = nil, service_name:, metadata: {})
43
+ if session_or_false == false
44
+ # Explicitly disable observability
45
+ @observability = nil
46
+ @owns_session = false
47
+ elsif session_or_false
48
+ # Use provided session
49
+ @observability = session_or_false
50
+ @owns_session = false
51
+ else
52
+ # Auto-create session for standalone use
53
+ @observability = create_service_session(service_name, metadata)
54
+ @owns_session = @observability.present?
55
+ end
56
+ end
57
+
58
+ # Execute a block with automatic session lifecycle management
59
+ #
60
+ # The session will be finalized automatically after the block completes,
61
+ # whether it succeeds or raises an error. Only sessions owned by this
62
+ # service instance (i.e., auto-created sessions) will be finalized.
63
+ #
64
+ # @yield [session] The observability session (may be nil if disabled)
65
+ # @return The result of the block
66
+ #
67
+ # @example
68
+ # with_observability do |session|
69
+ # # Your service logic here
70
+ # process_data(session)
71
+ # end
72
+ def with_observability(&block)
73
+ result = block.call(@observability)
74
+ finalize_service_session if @owns_session
75
+ result
76
+ rescue StandardError
77
+ finalize_service_session if @owns_session
78
+ raise
79
+ end
80
+
81
+ # Instrument a RubyLLM chat instance for observability
82
+ #
83
+ # This wraps the chat's ask method to automatically create traces
84
+ # and track LLM calls within the observability session.
85
+ #
86
+ # @param chat [RubyLLM::Chat] The chat instance to instrument
87
+ # @param context [Hash] Additional context to include in traces
88
+ # @return [Observ::ChatInstrumenter, nil] The instrumenter or nil if observability is disabled
89
+ #
90
+ # @example
91
+ # chat = RubyLLM.chat(model: "gpt-4")
92
+ # instrument_chat(chat, context: { operation: "summarize" })
93
+ # response = chat.ask("Summarize this text")
94
+ def instrument_chat(chat, context: {})
95
+ return unless @observability
96
+
97
+ @observability.instrument_chat(chat, context: context)
98
+ end
99
+
100
+ private
101
+
102
+ # Create a new observability session for this service
103
+ #
104
+ # @param service_name [String] Name of the service
105
+ # @param metadata [Hash] Additional metadata for the session
106
+ # @return [Observ::Session, nil] The created session or nil if observability is disabled/failed
107
+ def create_service_session(service_name, metadata = {})
108
+ return nil unless Rails.configuration.observability.enabled
109
+
110
+ Observ::Session.create!(
111
+ user_id: "#{service_name}_service",
112
+ metadata: metadata.merge(
113
+ agent_type: service_name,
114
+ standalone: true,
115
+ created_at: Time.current.iso8601
116
+ )
117
+ )
118
+ rescue StandardError => e
119
+ Rails.logger.error(
120
+ "[#{self.class.name}] Failed to create observability session: #{e.message}"
121
+ )
122
+ nil
123
+ end
124
+
125
+ # Finalize the observability session if we own it
126
+ #
127
+ # This marks the session as complete and triggers aggregation of metrics.
128
+ # Only called for sessions created by this service instance.
129
+ def finalize_service_session
130
+ return unless @observability
131
+
132
+ @observability.finalize
133
+ Rails.logger.debug(
134
+ "[#{self.class.name}] Session finalized: #{@observability.session_id}"
135
+ )
136
+ rescue StandardError => e
137
+ Rails.logger.error(
138
+ "[#{self.class.name}] Failed to finalize session: #{e.message}"
139
+ )
140
+ end
141
+ end
142
+ end
143
+ end
@@ -1,3 +1,3 @@
1
1
  module Observ
2
- VERSION = "0.6.4"
2
+ VERSION = "0.6.5"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubyllm-observ
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.4
4
+ version: 0.6.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Franck D'agostini
@@ -309,6 +309,7 @@ files:
309
309
  - app/services/observ/agent_provider.rb
310
310
  - app/services/observ/agent_selection_service.rb
311
311
  - app/services/observ/chat_instrumenter.rb
312
+ - app/services/observ/concerns/observable_service.rb
312
313
  - app/services/observ/dataset_runner_service.rb
313
314
  - app/services/observ/evaluator_runner_service.rb
314
315
  - app/services/observ/evaluators/base_evaluator.rb