Class: Fractor::ErrorReporter
- Inherits:
-
Object
- Object
- Fractor::ErrorReporter
- Defined in:
- lib/fractor/error_reporter.rb
Overview
Error reporting and analytics system. Aggregates errors, tracks statistics, and provides actionable insights.
Instance Attribute Summary collapse
-
#start_time ⇒ Object
readonly
Returns the value of attribute start_time.
-
#total_errors ⇒ Object
readonly
Returns the value of attribute total_errors.
-
#total_successes ⇒ Object
readonly
Returns the value of attribute total_successes.
Instance Method Summary collapse
-
#category_stats(category) ⇒ Hash?
Get statistics for a category.
-
#critical_errors ⇒ Array<Hash>
Get critical errors.
-
#errors_by_severity ⇒ Hash
Get errors by severity.
-
#formatted_report ⇒ String
Generate formatted text report.
-
#initialize ⇒ ErrorReporter
constructor
A new instance of ErrorReporter.
-
#job_stats(job_name) ⇒ Hash?
Get statistics for a job.
-
#on_error {|work_result, job_name| ... } ⇒ void
Register an error handler callback.
-
#overall_error_rate ⇒ Float
Get overall error rate.
-
#record(work_result, job_name: nil) ⇒ void
Record a work result.
-
#report ⇒ Hash
Generate comprehensive report.
-
#reset ⇒ void
Reset all statistics.
-
#to_json(*args) ⇒ String
Export to JSON format.
-
#to_prometheus ⇒ String
Export to Prometheus format.
-
#top_categories(limit: 5) ⇒ Hash
Get top error categories.
-
#top_jobs(limit: 5) ⇒ Hash
Get top error jobs.
-
#trending_errors ⇒ Array<Hash>
Get trending errors (increasing error rates).
Constructor Details
#initialize ⇒ ErrorReporter
Returns a new instance of ErrorReporter.
12 13 14 15 16 17 18 19 20 |
# File 'lib/fractor/error_reporter.rb', line 12 def initialize @start_time = Time.now @total_errors = 0 @total_successes = 0 @by_category = {} @by_job = {} @error_handlers = [] @mutex = Mutex.new end |
Instance Attribute Details
#start_time ⇒ Object (readonly)
Returns the value of attribute start_time.
10 11 12 |
# File 'lib/fractor/error_reporter.rb', line 10 def start_time @start_time end |
#total_errors ⇒ Object (readonly)
Returns the value of attribute total_errors.
10 11 12 |
# File 'lib/fractor/error_reporter.rb', line 10 def total_errors @total_errors end |
#total_successes ⇒ Object (readonly)
Returns the value of attribute total_successes.
10 11 12 |
# File 'lib/fractor/error_reporter.rb', line 10 def total_successes @total_successes end |
Instance Method Details
#category_stats(category) ⇒ Hash?
Get statistics for a category
50 51 52 53 54 |
# File 'lib/fractor/error_reporter.rb', line 50 def category_stats(category) @mutex.synchronize do @by_category[category]&.to_h end end |
#critical_errors ⇒ Array<Hash>
Get critical errors
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/fractor/error_reporter.rb', line 122 def critical_errors errors = [] @mutex.synchronize do @by_category.each do |category, stats| critical_count = stats.by_severity[WorkResult::SEVERITY_CRITICAL] || 0 if critical_count.positive? errors << { category: category, count: critical_count, recent: stats.recent_errors.select do |e| e[:error_severity] == WorkResult::SEVERITY_CRITICAL end.last(5), } end end end errors end |
#errors_by_severity ⇒ Hash
Get errors by severity
79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/fractor/error_reporter.rb', line 79 def errors_by_severity result = Hash.new(0) @mutex.synchronize do @by_category.each_value do |stats| stats.by_severity.each do |severity, count| result[severity] += count end end end result end |
#formatted_report ⇒ String
Generate formatted text report
180 181 182 |
# File 'lib/fractor/error_reporter.rb', line 180 def formatted_report ErrorReportGenerator.text_report(report) end |
#job_stats(job_name) ⇒ Hash?
Get statistics for a job
60 61 62 63 64 |
# File 'lib/fractor/error_reporter.rb', line 60 def job_stats(job_name) @mutex.synchronize do @by_job[job_name]&.to_h end end |
#on_error {|work_result, job_name| ... } ⇒ void
This method returns an undefined value.
Register an error handler callback
42 43 44 |
# File 'lib/fractor/error_reporter.rb', line 42 def on_error(&block) @error_handlers << block end |
#overall_error_rate ⇒ Float
Get overall error rate
69 70 71 72 73 74 |
# File 'lib/fractor/error_reporter.rb', line 69 def overall_error_rate total = @total_errors + @total_successes return 0.0 if total.zero? (@total_errors.to_f / total * 100).round(2) end |
#record(work_result, job_name: nil) ⇒ void
This method returns an undefined value.
Record a work result
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/fractor/error_reporter.rb', line 27 def record(work_result, job_name: nil) @mutex.synchronize do if work_result.success? @total_successes += 1 else @total_errors += 1 record_error(work_result, job_name) end end end |
#report ⇒ Hash
Generate comprehensive report
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/fractor/error_reporter.rb', line 160 def report { summary: { uptime: (Time.now - @start_time).round(2), total_errors: @total_errors, total_successes: @total_successes, error_rate: overall_error_rate, errors_by_severity: errors_by_severity, }, top_categories: top_categories, top_jobs: top_jobs, critical_errors: critical_errors, trending_errors: trending_errors, category_breakdown: category_breakdown, } end |
#reset ⇒ void
This method returns an undefined value.
Reset all statistics
202 203 204 205 206 207 208 209 210 |
# File 'lib/fractor/error_reporter.rb', line 202 def reset @mutex.synchronize do @start_time = Time.now @total_errors = 0 @total_successes = 0 @by_category.clear @by_job.clear end end |
#to_json(*args) ⇒ String
Export to JSON format
195 196 197 |
# File 'lib/fractor/error_reporter.rb', line 195 def to_json(*args) ErrorReportGenerator.to_json(report, *args) end |
#to_prometheus ⇒ String
Export to Prometheus format
187 188 189 |
# File 'lib/fractor/error_reporter.rb', line 187 def to_prometheus ErrorReportGenerator.to_prometheus(self) end |
#top_categories(limit: 5) ⇒ Hash
Get top error categories
95 96 97 98 99 100 101 102 103 |
# File 'lib/fractor/error_reporter.rb', line 95 def top_categories(limit: 5) @mutex.synchronize do @by_category .map { |category, stats| [category, stats.total_count] } .sort_by { |_category, count| -count } .first(limit) .to_h end end |
#top_jobs(limit: 5) ⇒ Hash
Get top error jobs
109 110 111 112 113 114 115 116 117 |
# File 'lib/fractor/error_reporter.rb', line 109 def top_jobs(limit: 5) @mutex.synchronize do @by_job .map { |job, stats| [job, stats.total_count] } .sort_by { |_job, count| -count } .first(limit) .to_h end end |
#trending_errors ⇒ Array<Hash>
Get trending errors (increasing error rates)
144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/fractor/error_reporter.rb', line 144 def trending_errors trends = [] @mutex.synchronize do @by_category.each do |category, stats| if stats.increasing? trends << { category: category, stats: stats.to_h } end end end trends end |