Class: Mammoth::ObservabilitySnapshot

Inherits:
Object
  • Object
show all
Defined in:
lib/mammoth/observability_snapshot.rb

Overview

Builds health, readiness, and metrics snapshots from Mammoth's local operational state.

ObservabilitySnapshot is intentionally read-only. It does not start the relay, mutate checkpoints, replay dead letters, or inspect PostgreSQL. The health and metrics endpoints use this object to expose Mammoth process and SQLite operational-state status in a predictable format.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, sqlite_store: nil, clock: -> { Time.now.utc }) ⇒ ObservabilitySnapshot

Returns a new instance of ObservabilitySnapshot.

Parameters:

  • config (Mammoth::Configuration)

    loaded configuration

  • sqlite_store (Mammoth::SQLiteStore, nil) (defaults to: nil)

    optional operational store

  • clock (#call) (defaults to: -> { Time.now.utc })

    time source returning a Time-like object



19
20
21
22
23
# File 'lib/mammoth/observability_snapshot.rb', line 19

def initialize(config, sqlite_store: nil, clock: -> { Time.now.utc })
  @config = config
  @sqlite_store = sqlite_store
  @clock = clock
end

Instance Attribute Details

#clockObject (readonly)

Returns the value of attribute clock.



14
15
16
# File 'lib/mammoth/observability_snapshot.rb', line 14

def clock
  @clock
end

#configObject (readonly)

Returns the value of attribute config.



14
15
16
# File 'lib/mammoth/observability_snapshot.rb', line 14

def config
  @config
end

#sqlite_storeObject (readonly)

Returns the value of attribute sqlite_store.



14
15
16
# File 'lib/mammoth/observability_snapshot.rb', line 14

def sqlite_store
  @sqlite_store
end

Instance Method Details

#healthHash

Build a liveness response.

Returns:

  • (Hash)

    health payload



28
29
30
31
32
33
34
35
36
# File 'lib/mammoth/observability_snapshot.rb', line 28

def health
  {
    status: "ok",
    service: "mammoth",
    name: mammoth_name,
    version: Mammoth::VERSION,
    checked_at: checked_at
  }
end

#prometheusString

Build a Prometheus text exposition document.

Returns:

  • (String)

    Prometheus metrics text



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/mammoth/observability_snapshot.rb', line 68

def prometheus
  store = operational_store.bootstrap!
  checkpoint_store = CheckpointStore.new(store)
  dead_letter_store = DeadLetterStore.new(store)
  delivered_store = DeliveredEnvelopeStore.new(store)

  lines = metric_headers + aggregate_metric_lines(
    checkpoint_store: checkpoint_store,
    dead_letter_store: dead_letter_store,
    delivered_store: delivered_store
  ) + destination_metric_lines(dead_letter_store:, delivered_store:)
  "#{lines.join("\n")}\n"
rescue Mammoth::Error, SQLite3::Exception
  "#{(metric_headers + [metric_line("mammoth_up", 0)]).join("\n")}\n"
end

#readinessHash

Build a readiness response.

Returns:

  • (Hash)

    readiness payload



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/mammoth/observability_snapshot.rb', line 41

def readiness
  store = operational_store
  store.bootstrap!

  {
    status: "ready",
    service: "mammoth",
    name: mammoth_name,
    sqlite: "ok",
    tables: store.tables,
    checked_at: checked_at
  }
rescue Mammoth::Error, SQLite3::Exception => e
  {
    status: "unready",
    service: "mammoth",
    name: mammoth_name,
    sqlite: "error",
    error_class: e.class.name,
    error_message: e.message,
    checked_at: checked_at
  }
end