Class: Flare::SQLiteExporter

Inherits:
Object
  • Object
show all
Defined in:
lib/flare/sqlite_exporter.rb

Constant Summary collapse

SUCCESS =
OpenTelemetry::SDK::Trace::Export::SUCCESS
FAILURE =
OpenTelemetry::SDK::Trace::Export::FAILURE
TIMEOUT =
OpenTelemetry::SDK::Trace::Export::TIMEOUT
PRUNE_PROBABILITY =

Prune roughly every 100 exports (1% chance per export)

0.01
MAX_RETRIES =

Maximum number of retry attempts when the database is busy. Mirrors ActiveRecord’s retry strategy for SQLite.

3

Instance Method Summary collapse

Constructor Details

#initialize(database_path) ⇒ SQLiteExporter

Returns a new instance of SQLiteExporter.



15
16
17
18
19
# File 'lib/flare/sqlite_exporter.rb', line 15

def initialize(database_path)
  @database_path = database_path
  @mutex = Mutex.new
  @setup = false
end

Instance Method Details

#export(span_datas, timeout: nil) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/flare/sqlite_exporter.rb', line 25

def export(span_datas, timeout: nil)
  setup_database unless @setup

  retries = 0
  exported = 0

  begin
    @mutex.synchronize do
      connection.transaction do
        span_datas.each do |span_data|
          next if should_ignore_span?(span_data)

          create_span(span_data)
          exported += 1
        end
      end
    end
  rescue ::SQLite3::BusyException
    retries += 1
    if retries <= MAX_RETRIES
      sleep 0.1 * retries
      retry
    end
    warn "[Flare] SQLite export error: database is busy after #{MAX_RETRIES} retries"
    return FAILURE
  end

  Flare.log "Exported #{exported} spans to SQLite" if exported > 0

  # Periodically prune old data
  maybe_prune

  SUCCESS
rescue => e
  warn "[Flare] SQLite export error: #{e.message}"
  FAILURE
end

#force_flush(timeout: nil) ⇒ Object



63
64
65
# File 'lib/flare/sqlite_exporter.rb', line 63

def force_flush(timeout: nil)
  SUCCESS
end

#shutdown(timeout: nil) ⇒ Object



67
68
69
# File 'lib/flare/sqlite_exporter.rb', line 67

def shutdown(timeout: nil)
  SUCCESS
end