Class: Desiru::Core::TraceContext

Inherits:
Object
  • Object
show all
Defined in:
lib/desiru/core/trace.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collector = nil) ⇒ TraceContext

Returns a new instance of TraceContext.



195
196
197
198
# File 'lib/desiru/core/trace.rb', line 195

def initialize(collector = nil)
  @collector = collector || TraceCollector.new
  @stack = []
end

Instance Attribute Details

#collectorObject (readonly)

Returns the value of attribute collector.



193
194
195
# File 'lib/desiru/core/trace.rb', line 193

def collector
  @collector
end

Class Method Details

.currentObject

Get the current trace context



221
222
223
# File 'lib/desiru/core/trace.rb', line 221

def self.current
  Core.trace_context
end

.with_collector(collector) ⇒ Object

Class method to temporarily use a specific collector



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/desiru/core/trace.rb', line 201

def self.with_collector(collector)
  # Save current context if it exists
  previous_context = Thread.current[:desiru_trace_context]

  # Create new context with the provided collector
  context = new(collector)
  Thread.current[:desiru_trace_context] = context

  # Store context for thread propagation
  Thread.current[:desiru_trace_collector_for_threads] = collector

  # Execute the block
  yield
ensure
  # Restore previous context
  Thread.current[:desiru_trace_context] = previous_context
  Thread.current[:desiru_trace_collector_for_threads] = nil
end

Instance Method Details

#add_metadata(metadata) ⇒ Object



236
237
238
239
240
241
# File 'lib/desiru/core/trace.rb', line 236

def ()
  return if @stack.empty?

  current_trace = @stack.last
  current_trace[:metadata] = (current_trace[:metadata] || {}).merge()
end

#end_trace(outputs: {}, metadata: {}) ⇒ Object



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/desiru/core/trace.rb', line 243

def end_trace(outputs: {}, metadata: {})
  return if @stack.empty?

  trace_data = @stack.pop
  duration = Time.now - trace_data[:start_time]

  # Merge accumulated metadata with end trace metadata
   = (trace_data[:metadata] || {}).merge().merge(
    duration: duration,
    success: true
  )

  trace = Trace.new(
    module_name: trace_data[:module_name],
    signature: trace_data[:signature],
    inputs: trace_data[:inputs],
    outputs: outputs,
    metadata: 
  )

  @collector.collect(trace)
  trace
end

#record_error(error, outputs: {}, metadata: {}) ⇒ Object



267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/desiru/core/trace.rb', line 267

def record_error(error, outputs: {}, metadata: {})
  return if @stack.empty?

  trace_data = @stack.pop
  duration = Time.now - trace_data[:start_time]

  # Merge accumulated metadata with error metadata
   = (trace_data[:metadata] || {}).merge().merge(
    duration: duration,
    success: false,
    error: error.message,
    error_class: error.class.name
  )

  trace = Trace.new(
    module_name: trace_data[:module_name],
    signature: trace_data[:signature],
    inputs: trace_data[:inputs],
    outputs: outputs,
    metadata: 
  )

  @collector.collect(trace)
  trace
end

#start_trace(module_name:, signature:, inputs: {}) ⇒ Object



225
226
227
228
229
230
231
232
233
234
# File 'lib/desiru/core/trace.rb', line 225

def start_trace(module_name:, signature:, inputs: {})
  trace_data = {
    module_name: module_name,
    signature: signature,
    inputs: inputs,
    start_time: Time.now,
    metadata: {}
  }
  @stack.push(trace_data)
end

#with_trace(module_name:, signature:, inputs: {}) ⇒ Object



293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/desiru/core/trace.rb', line 293

def with_trace(module_name:, signature:, inputs: {})
  start_trace(module_name: module_name, signature: signature, inputs: inputs)

  begin
    outputs = yield
    end_trace(outputs: outputs)
    outputs
  rescue StandardError => e
    record_error(e)
    raise
  end
end