Class: Findbug::Performance::Transaction

Inherits:
Object
  • Object
show all
Defined in:
lib/findbug/performance/transaction.rb

Overview

Transaction provides manual performance tracking for custom operations.

WHY MANUAL TRANSACTIONS?

Automatic instrumentation catches HTTP requests, but what about:

  • External API calls

  • Background job processing

  • Custom business logic

  • Third-party service calls

With transactions, you can track anything:

Findbug.track_performance("stripe_charge") do
  Stripe::Charge.create(...)
end

Findbug.track_performance("pdf_generation") do
  generate_report_pdf(...)
end

NESTING

Transactions can be nested. Child transactions contribute to parent timing:

Findbug.track_performance("checkout") do
  Findbug.track_performance("payment") do
    process_payment
  end
  Findbug.track_performance("fulfillment") do
    create_shipment
  end
end

This creates a tree of timings you can analyze.

Class Method Summary collapse

Class Method Details

.start(name, tags: {}) ⇒ TransactionSpan

Start a transaction manually (for cases where block syntax doesn’t work)

Examples:

span = Findbug::Performance::Transaction.start("long_operation")
# ... do work ...
span.finish

Parameters:

  • name (String)

    transaction name

Returns:



89
90
91
# File 'lib/findbug/performance/transaction.rb', line 89

def start(name, tags: {})
  TransactionSpan.new(name, tags)
end

.track(name, tags: {}) { ... } ⇒ Object

Track a block’s performance

Parameters:

  • name (String)

    name for this transaction

  • tags (Hash) (defaults to: {})

    optional tags for filtering

Yields:

  • the block to track

Returns:

  • (Object)

    the block’s return value



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
# File 'lib/findbug/performance/transaction.rb', line 51

def track(name, tags: {}, &block)
  return yield unless Findbug.enabled?
  return yield unless Findbug.config.performance_enabled

  start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)

  begin
    # Execute the block
    result = yield

    # Calculate duration
    duration_ms = calculate_duration(start_time)

    # Record the transaction
    record_transaction(name, duration_ms, tags, success: true)

    result
  rescue StandardError => e
    # Calculate duration even on error
    duration_ms = calculate_duration(start_time)

    # Record as failed
    record_transaction(name, duration_ms, tags, success: false, error: e.class.name)

    raise
  end
end