Class: Dontbugme::Store::Postgresql

Inherits:
Base
  • Object
show all
Defined in:
lib/dontbugme/store/postgresql.rb

Instance Method Summary collapse

Constructor Details

#initialize(connection: nil) ⇒ Postgresql

Returns a new instance of Postgresql.

Raises:



8
9
10
11
12
# File 'lib/dontbugme/store/postgresql.rb', line 8

def initialize(connection: nil)
  @connection = connection || Dontbugme.config.postgresql_connection || default_connection
  raise ArgumentError, 'PostgreSQL store requires a database connection' unless @connection
  ensure_schema
end

Instance Method Details

#cleanup(before:) ⇒ Object



90
91
92
93
# File 'lib/dontbugme/store/postgresql.rb', line 90

def cleanup(before:)
  cutoff = before.is_a?(Time) ? before.utc.iso8601 : before.to_s
  exec_params('DELETE FROM dontbugme_traces WHERE started_at < $1', [cutoff])
end

#find_trace(trace_id) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/dontbugme/store/postgresql.rb', line 46

def find_trace(trace_id)
  result = query_result('SELECT * FROM dontbugme_traces WHERE id = $1', [trace_id])
  return nil if result.blank?

  row = result.is_a?(Array) ? result.first : result.to_a.first
  return nil unless row

  row_to_trace(row)
end

#save_trace(trace) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/dontbugme/store/postgresql.rb', line 14

def save_trace(trace)
  data = trace.to_h
  correlation_id = data[:correlation_id] || data[:metadata]&.dig(:correlation_id)
  params = [
    data[:id],
    data[:kind].to_s,
    JsonSafe.sanitize_string(data[:identifier].to_s),
    data[:status].to_s,
    data[:started_at],
    data[:duration_ms],
    correlation_id,
    JsonSafe.sanitize(data[:metadata]).to_json,
    JsonSafe.sanitize(data[:spans]).to_json,
    data[:error] ? JsonSafe.sanitize(data[:error]).to_json : nil
  ]
  exec_params("    INSERT INTO dontbugme_traces\n    (id, kind, identifier, status, started_at, duration_ms, correlation_id, metadata_json, spans_json, error_json)\n    VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)\n    ON CONFLICT (id) DO UPDATE SET\n      kind = EXCLUDED.kind,\n      identifier = EXCLUDED.identifier,\n      status = EXCLUDED.status,\n      started_at = EXCLUDED.started_at,\n      duration_ms = EXCLUDED.duration_ms,\n      correlation_id = EXCLUDED.correlation_id,\n      metadata_json = EXCLUDED.metadata_json,\n      spans_json = EXCLUDED.spans_json,\n      error_json = EXCLUDED.error_json\n  SQL\nend\n", params)

#search(filters = {}) ⇒ Object



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
84
85
86
87
88
# File 'lib/dontbugme/store/postgresql.rb', line 56

def search(filters = {})
  sql = 'SELECT * FROM dontbugme_traces WHERE 1=1'
  params = []
  i = 1

  if filters[:status]
    params << filters[:status].to_s
    sql += " AND status = $#{i}"
    i += 1
  end
  if filters[:kind]
    params << filters[:kind].to_s
    sql += " AND kind = $#{i}"
    i += 1
  end
  if filters[:identifier]
    params << "%#{filters[:identifier]}%"
    sql += " AND identifier LIKE $#{i}"
    i += 1
  end
  if filters[:correlation_id]
    params << filters[:correlation_id].to_s
    sql += " AND correlation_id = $#{i}"
    i += 1
  end

  params << (filters[:limit] || filters['limit'] || 100)
  sql += " ORDER BY started_at DESC LIMIT $#{i}"

  result = query_result(sql, params)
  rows = result.respond_to?(:to_a) ? result.to_a : Array(result)
  rows.map { |row| row_to_trace(normalize_row(row)) }
end