Class: Findbug::Performance::TransactionSpan

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

Overview

TransactionSpan represents an in-progress transaction.

Use this when block syntax isn’t convenient:

span = Findbug::Performance::Transaction.start("my_operation")
begin
  do_work
  span.finish
rescue => e
  span.finish(error: e)
  raise
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, tags = {}) ⇒ TransactionSpan

Returns a new instance of TransactionSpan.



140
141
142
143
144
145
# File 'lib/findbug/performance/transaction.rb', line 140

def initialize(name, tags = {})
  @name = name
  @tags = tags
  @start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  @finished = false
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



138
139
140
# File 'lib/findbug/performance/transaction.rb', line 138

def name
  @name
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



138
139
140
# File 'lib/findbug/performance/transaction.rb', line 138

def start_time
  @start_time
end

#tagsObject (readonly)

Returns the value of attribute tags.



138
139
140
# File 'lib/findbug/performance/transaction.rb', line 138

def tags
  @tags
end

Instance Method Details

#current_duration_msObject

Get current duration (for monitoring in-progress transactions)



181
182
183
# File 'lib/findbug/performance/transaction.rb', line 181

def current_duration_ms
  calculate_duration
end

#finish(error: nil) ⇒ Object

Finish the transaction

Parameters:

  • error (Exception, nil) (defaults to: nil)

    optional error if the transaction failed



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/findbug/performance/transaction.rb', line 151

def finish(error: nil)
  return if @finished

  @finished = true
  duration_ms = calculate_duration

  event = {
    transaction_name: name,
    transaction_type: "custom",
    duration_ms: duration_ms,
    success: error.nil?,
    error_class: error&.class&.name,
    tags: tags,
    context: Capture::Context.to_h,
    captured_at: Time.now.utc.iso8601(3),
    environment: Findbug.config.environment,
    release: Findbug.config.release
  }

  Storage::RedisBuffer.push_performance(event)
rescue StandardError => e
  Findbug.logger.debug("[Findbug] Span finish failed: #{e.message}")
end

#finished?Boolean

Check if already finished

Returns:

  • (Boolean)


176
177
178
# File 'lib/findbug/performance/transaction.rb', line 176

def finished?
  @finished
end