Class: PgReports::Grafana::Exporter

Inherits:
Object
  • Object
show all
Defined in:
lib/pg_reports/grafana/exporter.rb

Overview

Renders selected reports in Prometheus exposition format. Severity is derived from REPORT_CONFIG thresholds in Dashboard::ReportsRegistry.

Constant Summary collapse

SEVERITY_ORDER =
{"ok" => 0, "warning" => 1, "critical" => 2}.freeze
MAX_LABEL_VALUE_LENGTH =
200
RESERVED_LABEL_NAMES =
%w[report severity row error].freeze
MODULES =
{
  queries: -> { Modules::Queries },
  indexes: -> { Modules::Indexes },
  tables: -> { Modules::Tables },
  connections: -> { Modules::Connections },
  system: -> { Modules::System },
  schema_analysis: -> { Modules::SchemaAnalysis }
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(favorites: PgReports.config.grafana_favorites, cache_ttl: PgReports.config.grafana_cache_ttl, clock: Time) ⇒ Exporter

Returns a new instance of Exporter.



25
26
27
28
29
30
31
# File 'lib/pg_reports/grafana/exporter.rb', line 25

def initialize(favorites: PgReports.config.grafana_favorites,
  cache_ttl: PgReports.config.grafana_cache_ttl,
  clock: Time)
  @favorites = normalize(favorites)
  @cache_ttl = cache_ttl
  @clock = clock
end

Class Method Details

.renderObject



21
22
23
# File 'lib/pg_reports/grafana/exporter.rb', line 21

def self.render
  new.render
end

Instance Method Details

#renderObject



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
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/pg_reports/grafana/exporter.rb', line 33

def render
  results = @favorites.map { |key, opts| collect(key, opts) }

  lines = []
  emit(lines, "pg_reports_issues", "Number of rows by severity for the report") do |emit|
    results.each do |r|
      next unless r[:ok]
      r[:severities].each { |sev, count| emit.call({report: r[:key], severity: sev}, count) }
    end
  end

  emit(lines, "pg_reports_rows", "Total rows returned by the report") do |emit|
    results.each { |r| emit.call({report: r[:key]}, r[:rows]) if r[:ok] }
  end

  emit(lines, "pg_reports_run_seconds", "Time spent collecting the report") do |emit|
    results.each { |r| emit.call({report: r[:key]}, r[:duration].round(4)) if r[:ok] }
  end

  emit(lines, "pg_reports_last_run_timestamp", "Unix timestamp of last collection") do |emit|
    results.each { |r| emit.call({report: r[:key]}, r[:timestamp]) if r[:ok] }
  end

  emit(lines, "pg_reports_up", "Whether collection succeeded (1) or failed (0)") do |emit|
    results.each do |r|
      labels = {report: r[:key]}
      labels[:error] = r[:error] unless r[:ok]
      emit.call(labels, r[:ok] ? 1 : 0)
    end
  end

  emit(lines, "pg_reports_row", "One series per row of the report (drives Grafana table panels). Each row column becomes a label.") do |emit|
    results.each do |r|
      next unless r[:ok] && r[:rows_data]
      r[:rows_data].each_with_index do |row_labels, idx|
        emit.call(row_labels.merge(report: r[:key], row: idx), 1)
      end
    end
  end

  (lines << "").join("\n")
end