Class: Capsium::Reactor::Introspection

Inherits:
Object
  • Object
show all
Defined in:
lib/capsium/reactor/introspection.rb,
sig/capsium/reactor/introspection.rbs

Overview

Monitoring HTTP API reports (ARCHITECTURE.md section 7): /api/v1/introspect/* aggregates every mounted package, plus reactor-level /introspect/* and per-package /package/:id/* endpoints resolved by package name.

Constant Summary collapse

METADATA_PATH =

Returns:

"/api/v1/introspect/metadata"
ROUTES_PATH =

Returns:

"/api/v1/introspect/routes"
CONTENT_HASHES_PATH =

Returns:

"/api/v1/introspect/content-hashes"
CONTENT_VALIDITY_PATH =

Returns:

"/api/v1/introspect/content-validity"
PATHS =

Returns:

[METADATA_PATH, ROUTES_PATH, CONTENT_HASHES_PATH,
CONTENT_VALIDITY_PATH].freeze
STATUS_PATH =

Returns:

"/introspect/status"
CONFIG_PATH =

Returns:

"/introspect/config"
METRICS_PATH =

Returns:

"/introspect/metrics"
REACTOR_PATHS =

Returns:

[STATUS_PATH, CONFIG_PATH, METRICS_PATH].freeze
PACKAGE_MOUNT =

Per-package endpoints: "/package//status|metadata|logs". PACKAGE_MOUNT is the mount point (WEBrick longest-prefix matching routes all "/package/..." requests to the reactor); the name resolves against every mounted package, anything else is a 404.

Returns:

"/package"
PACKAGE_PATH_PATTERN =

Returns:

%r{\A/package/(?<name>[^/]+)/(?<report>status|metadata|logs)\z}
DEFAULT_LOG_LINES =

Returns:

100
MAX_LOG_LINES =

Returns:

1000

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(packages, reactor: nil) ⇒ Introspection

The packages this reactor serves (one per mount; ARCHITECTURE.md section 7 introspection aggregates all of them).

Parameters:

  • (defaults to: nil)


43
44
45
46
# File 'lib/capsium/reactor/introspection.rb', line 43

def initialize(packages, reactor: nil)
  @packages = packages
  @reactor = reactor
end

Instance Attribute Details

#packagesArray[Package] (readonly)

Returns the value of attribute packages.

Returns:



39
40
41
# File 'lib/capsium/reactor/introspection.rb', line 39

def packages
  @packages
end

Instance Method Details

#config_reportHash[Symbol, untyped]

Reactor configuration; secrets are never exposed.

Returns:



102
103
104
105
106
107
108
109
110
# File 'lib/capsium/reactor/introspection.rb', line 102

def config_report
  {
    port: @reactor.port,
    storeDir: store_dir,
    cacheControl: @reactor.cache_control,
    authEnabled: @reactor.authenticator.enabled?,
    registry: registry_location
  }
end

#content_hashes_reportHash[Symbol, untyped]

Returns:



85
86
87
88
89
90
# File 'lib/capsium/reactor/introspection.rb', line 85

def content_hashes_report
  entries = @packages.map do |pkg|
    { package: pkg.name, hash: content_hash(pkg) }
  end
  { contentHashes: entries }
end

#content_validity_reportHash[Symbol, untyped]

Returns:



92
93
94
# File 'lib/capsium/reactor/introspection.rb', line 92

def content_validity_report
  { contentValidity: @packages.map { |pkg| content_validity_entry(pkg) } }
end

#endpoint?(path) ⇒ Boolean

Parameters:

Returns:



51
52
53
54
# File 'lib/capsium/reactor/introspection.rb', line 51

def endpoint?(path)
  PATHS.include?(path) || package_endpoint?(path) ||
    (!@reactor.nil? && REACTOR_PATHS.include?(path))
end

#metadata_reportHash[Symbol, untyped]

Returns:



71
72
73
# File 'lib/capsium/reactor/introspection.rb', line 71

def 
  { packages: @packages.map { |pkg| (pkg) } }
end

#metrics_reportHash[Symbol, untyped]

Returns:



112
# File 'lib/capsium/reactor/introspection.rb', line 112

def metrics_report = @reactor.metrics.snapshot.merge(uptime: uptime)

#packagePackage

The first mounted package (the single-package view).

Returns:



49
# File 'lib/capsium/reactor/introspection.rb', line 49

def package = @packages.first

#report_for(path, params: {}) ⇒ Hash[Symbol, untyped]?

The report body for an endpoint, or nil when the path is not an endpoint or the package name does not match (404).

Parameters:

  • (defaults to: {})

Returns:



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/capsium/reactor/introspection.rb', line 58

def report_for(path, params: {})
  case path
  when METADATA_PATH then 
  when ROUTES_PATH then routes_report
  when CONTENT_HASHES_PATH then content_hashes_report
  when CONTENT_VALIDITY_PATH then content_validity_report
  when STATUS_PATH then status_report
  when CONFIG_PATH then config_report
  when METRICS_PATH then metrics_report
  else package_report_for(path, params)
  end
end

#routes_reportHash[Symbol, untyped]

Returns:



75
76
77
78
79
80
81
82
83
# File 'lib/capsium/reactor/introspection.rb', line 75

def routes_report
  entries = @packages.map do |pkg|
    routes = pkg.routes.config.routes.map do |route|
      { method: route.http_method || "GET", path: route.path }
    end
    { package: pkg.name, routes: routes }
  end
  { routes: entries }
end

#status_reportHash[Symbol, untyped]

Returns:



96
97
98
# File 'lib/capsium/reactor/introspection.rb', line 96

def status_report
  { status: "running", uptime: uptime, packagesLoaded: @packages.size }
end