Module: Codebeacon::Tracer

Defined in:
lib/codebeacon-tracer.rb,
lib/codebeacon/tracer/version.rb,
lib/codebeacon/tracer/src/logger.rb,
lib/codebeacon/tracer/src/tracer.rb,
lib/codebeacon/tracer/src/configuration.rb,
lib/codebeacon/tracer/src/data/database.rb,
lib/codebeacon/tracer/src/rails/railtie.rb,
lib/codebeacon/tracer/src/models/tpklass.rb,
lib/codebeacon/tracer/src/models/call_tree.rb,
lib/codebeacon/tracer/src/models/tree_node.rb,
lib/codebeacon/tracer/src/rails/middleware.rb,
lib/codebeacon/tracer/src/data/type_detector.rb,
lib/codebeacon/tracer/src/models/node_source.rb,
lib/codebeacon/tracer/src/data/trace_metadata.rb,
lib/codebeacon/tracer/src/models/node_builder.rb,
lib/codebeacon/tracer/src/data/metadata_mapper.rb,
lib/codebeacon/tracer/src/data/safe_serializer.rb,
lib/codebeacon/tracer/src/data/tree_node_mapper.rb,
lib/codebeacon/tracer/src/data/node_source_mapper.rb,
lib/codebeacon/tracer/src/data/persistence_manager.rb,
lib/codebeacon/tracer/src/models/thread_local_call_tree_manager.rb

Overview

The Tracer module provides tools to trace and analyze the runtime performance of your Ruby applications. It captures method calls, execution times, and generates reports to help identify bottlenecks.

Examples:

Tracing a block of code

Codebeacon::Tracer.trace("My Trace", "Description of what I'm tracing") do |tracer|
  # Your code to analyze goes here
  some_method_to_analyze
end

Defined Under Namespace

Classes: CallTree, Configuration, DatabaseSchema, Logger, MetadataMapper, Middleware, NodeBuilder, NodeSource, NodeSourceMapper, PersistenceManager, ProgressLogger, Railtie, SafeSerializer, TPKlass, ThreadLocalCallTreeManager, TraceMetadata, Tracer, TreeNode, TreeNodeMapper, TypeDetector

Constant Summary collapse

VERSION =
"0.4.0"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.tree_managerThreadLocalCallTreeManager (readonly)

Returns The current tree manager.

Returns:



27
28
29
# File 'lib/codebeacon-tracer.rb', line 27

def tree_manager
  @tree_manager
end

Class Method Details

.configConfiguration

Returns the configuration object for Codebeacon::Tracer

Returns:



31
32
33
# File 'lib/codebeacon-tracer.rb', line 31

def config 
  @config ||= Configuration.new
end

.current_treeCallTree

Returns the current call tree

Returns:



37
38
39
# File 'lib/codebeacon-tracer.rb', line 37

def current_tree
  @tracer&.tree_manager&.current()
end

.loggerLogger

Returns the logger instance

Returns:

  • (Logger)

    The logger instance



43
44
45
# File 'lib/codebeacon-tracer.rb', line 43

def logger
  config.logger
end

.start(name: nil, description: nil, trigger_type: "manual") ⇒ void

This method returns an undefined value.

Starts tracing without a block

Parameters:

  • name (String, nil) (defaults to: nil)

    Optional name for the trace

  • description (String, nil) (defaults to: nil)

    Optional description for the trace

  • trigger_type (String) (defaults to: "manual")

    The type of trigger that initiated the trace (default: “manual”)



90
91
92
93
94
95
96
97
98
99
# File 'lib/codebeacon-tracer.rb', line 90

def start(name: nil, description: nil, trigger_type: "manual")
  # Capture caller information immediately at entry point
  caller_location = caller_locations(1, 1).first
  
  return unless config.trace_enabled?
  
  setup
  @tracer = Tracer.new(name:, description:, caller_location:, trigger_type:)
  @tracer.start
end

.stopvoid

This method returns an undefined value.

Stops tracing and persists the results



103
104
105
106
107
108
109
# File 'lib/codebeacon-tracer.rb', line 103

def stop
  return unless @tracer # checks whether trace_enabled? was false when start was called or if it was called

  @tracer.stop
  persist(@tracer.)
  cleanup
end

.trace(name: nil, description: nil, trigger_type: "manual") {|tracer| ... } ⇒ Object

Traces a block of code and collects runtime information

Parameters:

  • name (String, nil) (defaults to: nil)

    Optional name for the trace

  • description (String, nil) (defaults to: nil)

    Optional description for the trace

  • trigger_type (String) (defaults to: "manual")

    The type of trigger that initiated the trace (default: “manual”)

Yields:

  • (tracer)

    Yields the tracer object to the block

Yield Parameters:

  • tracer (Tracer)

    The tracer object

Returns:

  • (Object)

    The result of the block



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
# File 'lib/codebeacon-tracer.rb', line 55

def trace(name: nil, description: nil, trigger_type: "manual")
  # Capture caller information immediately at entry point
  caller_location = caller_locations(1, 1).first

  unless config.trace_enabled?
    logger.info("Tracing is disabled. Skipping trace: #{name} - #{description}")
    return yield(nil)
  end
  if config.skip_tracing?(name, description)
    logger.info("Exclusion rules matched. Skipping trace: #{name} - #{description}")
    return yield(nil)
  end

  begin
    setup
    @tracer = Tracer.new(name:, description:, caller_location:, trigger_type:)
    result = @tracer.enable_traces do
      yield @tracer
    end
    persist(@tracer.)
    cleanup
    result
  rescue => e
    Codebeacon::Tracer.logger.error("Error during tracing: #{e.message}")
    Codebeacon::Tracer.logger.error(e.backtrace.join("\n")) if Codebeacon::Tracer.config.debug?
    # Continue execution without crashing the application
    yield nil if block_given?
  end
end