star_gates 0.3.2 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,6 +4,7 @@ require_relative 'telemetry_adapter'
4
4
 
5
5
  module StarGates
6
6
  module Telemetry
7
+ # Durations are recorded as Integer nanoseconds, unit 'ns'.
7
8
  class OpentelemetryAdapter < TelemetryAdapter
8
9
  def initialize(config = {})
9
10
  super
@@ -23,12 +24,10 @@ module StarGates
23
24
  'service.name' => @service_name,
24
25
  'service.version' => @service_version
25
26
  }) do |span|
26
- start_time = Time.now
27
+ started = StarGates.monotonic_time
27
28
  result = block.call
28
- duration = ((Time.now - start_time) * 1000).round(2)
29
-
30
29
  span.set_attribute('feature.eligible', result[:eligible]) if result.is_a?(Hash)
31
- span.set_attribute('feature.evaluation.duration_ms', duration)
30
+ span.set_attribute('feature.evaluation.duration_ns', StarGates.monotonic_time - started)
32
31
  span.status = OpenTelemetry::Trace::Status.ok
33
32
 
34
33
  result
@@ -44,19 +43,16 @@ module StarGates
44
43
  'service.name' => @service_name,
45
44
  'service.version' => @service_version
46
45
  }) do |span|
47
- start_time = Time.now
46
+ started = StarGates.monotonic_time
48
47
  begin
49
48
  result = block.call
50
- duration = ((Time.now - start_time) * 1000).round(2)
51
-
52
- span.set_attribute('feature.execution.duration_ms', duration)
49
+ span.set_attribute('feature.execution.duration_ns', StarGates.monotonic_time - started)
53
50
  span.set_attribute('feature.execution.success', true)
54
51
  span.status = OpenTelemetry::Trace::Status.ok
55
52
 
56
53
  result
57
54
  rescue StandardError => e
58
- duration = ((Time.now - start_time) * 1000).round(2)
59
- span.set_attribute('feature.execution.duration_ms', duration)
55
+ span.set_attribute('feature.execution.duration_ns', StarGates.monotonic_time - started)
60
56
  span.set_attribute('feature.execution.success', false)
61
57
  span.set_attribute('feature.execution.error', e.message)
62
58
  span.status = OpenTelemetry::Trace::Status.error(e.message)
@@ -82,6 +78,68 @@ module StarGates
82
78
  end
83
79
  end
84
80
 
81
+ def healthy?
82
+ super && @tracer.respond_to?(:in_span) && @meter.respond_to?(:create_counter)
83
+ end
84
+
85
+ # Override specific methods to use pre-created instruments
86
+ def record_feature_evaluation(feature_id, eligible, _context, execution_time = nil)
87
+ return unless enabled?
88
+
89
+ attributes = {
90
+ 'feature.id' => feature_id.to_s,
91
+ 'feature.eligible' => eligible.to_s,
92
+ 'service.name' => @service_name,
93
+ 'service.version' => @service_version
94
+ }
95
+
96
+ @instruments['feature_evaluations_total'].add(1, attributes: attributes)
97
+
98
+ return unless execution_time
99
+
100
+ @instruments['evaluation_duration'].record(execution_time, attributes: {
101
+ 'feature.id' => feature_id.to_s,
102
+ 'feature.eligible' => eligible.to_s,
103
+ 'service.name' => @service_name,
104
+ 'service.version' => @service_version
105
+ })
106
+ end
107
+
108
+ def record_feature_execution(feature_id, state, success, execution_time, _context = {})
109
+ return unless enabled?
110
+
111
+ attributes = {
112
+ 'feature.id' => feature_id.to_s,
113
+ 'feature.state' => state.to_s,
114
+ 'feature.success' => success.to_s,
115
+ 'service.name' => @service_name,
116
+ 'service.version' => @service_version
117
+ }
118
+
119
+ @instruments['feature_executions_total'].add(1, attributes: attributes)
120
+ @instruments['execution_duration'].record(execution_time, attributes: {
121
+ 'feature.id' => feature_id.to_s,
122
+ 'feature.state' => state.to_s,
123
+ 'service.name' => @service_name,
124
+ 'service.version' => @service_version
125
+ })
126
+ end
127
+
128
+ def record_state_transition(feature_id, from_state, to_state, event_name)
129
+ return unless enabled?
130
+
131
+ attributes = {
132
+ 'feature.id' => feature_id.to_s,
133
+ 'feature.transition.from' => from_state.to_s,
134
+ 'feature.transition.to' => to_state.to_s,
135
+ 'feature.transition.event' => event_name.to_s,
136
+ 'service.name' => @service_name,
137
+ 'service.version' => @service_version
138
+ }
139
+
140
+ @instruments['state_transitions_total'].add(1, attributes: attributes)
141
+ end
142
+
85
143
  protected
86
144
 
87
145
  def send_metric(name, value, tags, _timestamp)
@@ -113,16 +171,12 @@ module StarGates
113
171
  instrument_name = name.gsub(/\.(histogram|timing)$/, '').gsub('.', '_')
114
172
  histogram = @instruments["#{instrument_name}_duration"] ||=
115
173
  @meter.create_histogram("star_gates_#{instrument_name}_duration",
116
- unit: 'ms',
174
+ unit: 'ns',
117
175
  description: "Duration histogram for #{instrument_name}")
118
176
  histogram.record(value, attributes: attributes)
119
177
  end
120
178
  end
121
179
 
122
- def healthy?
123
- super && @tracer.respond_to?(:in_span) && @meter.respond_to?(:create_counter)
124
- end
125
-
126
180
  private
127
181
 
128
182
  def default_tracer
@@ -166,13 +220,13 @@ module StarGates
166
220
 
167
221
  @instruments['evaluation_duration'] = @meter.create_histogram(
168
222
  'star_gates_evaluation_duration',
169
- unit: 'ms',
223
+ unit: 'ns',
170
224
  description: 'Feature evaluation duration'
171
225
  )
172
226
 
173
227
  @instruments['execution_duration'] = @meter.create_histogram(
174
228
  'star_gates_execution_duration',
175
- unit: 'ms',
229
+ unit: 'ns',
176
230
  description: 'Feature execution duration'
177
231
  )
178
232
 
@@ -189,64 +243,6 @@ module StarGates
189
243
  end
190
244
  end
191
245
  end
192
-
193
- # Override specific methods to use pre-created instruments
194
- def record_feature_evaluation(feature_id, eligible, _context, execution_time_ms = nil)
195
- return unless enabled?
196
-
197
- attributes = {
198
- 'feature.id' => feature_id.to_s,
199
- 'feature.eligible' => eligible.to_s,
200
- 'service.name' => @service_name,
201
- 'service.version' => @service_version
202
- }
203
-
204
- @instruments['feature_evaluations_total'].add(1, attributes: attributes)
205
-
206
- return unless execution_time_ms
207
-
208
- @instruments['evaluation_duration'].record(execution_time_ms, attributes: {
209
- 'feature.id' => feature_id.to_s,
210
- 'feature.eligible' => eligible.to_s,
211
- 'service.name' => @service_name,
212
- 'service.version' => @service_version
213
- })
214
- end
215
-
216
- def record_feature_execution(feature_id, state, success, execution_time_ms, _context = {})
217
- return unless enabled?
218
-
219
- attributes = {
220
- 'feature.id' => feature_id.to_s,
221
- 'feature.state' => state.to_s,
222
- 'feature.success' => success.to_s,
223
- 'service.name' => @service_name,
224
- 'service.version' => @service_version
225
- }
226
-
227
- @instruments['feature_executions_total'].add(1, attributes: attributes)
228
- @instruments['execution_duration'].record(execution_time_ms, attributes: {
229
- 'feature.id' => feature_id.to_s,
230
- 'feature.state' => state.to_s,
231
- 'service.name' => @service_name,
232
- 'service.version' => @service_version
233
- })
234
- end
235
-
236
- def record_state_transition(feature_id, from_state, to_state, event_name)
237
- return unless enabled?
238
-
239
- attributes = {
240
- 'feature.id' => feature_id.to_s,
241
- 'feature.transition.from' => from_state.to_s,
242
- 'feature.transition.to' => to_state.to_s,
243
- 'feature.transition.event' => event_name.to_s,
244
- 'service.name' => @service_name,
245
- 'service.version' => @service_version
246
- }
247
-
248
- @instruments['state_transitions_total'].add(1, attributes: attributes)
249
- end
250
246
  end
251
247
  end
252
248
  end
@@ -4,7 +4,13 @@ require_relative 'telemetry_adapter'
4
4
 
5
5
  module StarGates
6
6
  module Telemetry
7
+ # Prometheus records durations in seconds, its base unit: the Integer
8
+ # nanoseconds handed in are converted here, at the boundary.
7
9
  class PrometheusAdapter < TelemetryAdapter
10
+ NS_PER_SECOND = 1_000_000_000
11
+ SECONDS_BUCKETS = [0.0001, 0.0005, 0.001, 0.0025, 0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1].freeze
12
+ private_constant :NS_PER_SECOND, :SECONDS_BUCKETS
13
+
8
14
  def initialize(config = {})
9
15
  super
10
16
  @prometheus = config[:prometheus] || default_prometheus
@@ -13,6 +19,70 @@ module StarGates
13
19
  setup_default_metrics
14
20
  end
15
21
 
22
+ def healthy?
23
+ super && @prometheus.respond_to?(:registry)
24
+ end
25
+
26
+ # Use pre-registered metrics for better performance
27
+ def record_feature_evaluation(feature_id, eligible, _context, execution_time = nil)
28
+ return unless enabled?
29
+
30
+ tags = {
31
+ feature_id: feature_id.to_s,
32
+ eligible: eligible.to_s
33
+ }
34
+
35
+ @registry[:feature_evaluations].increment(labels: tags)
36
+
37
+ return unless execution_time
38
+
39
+ @registry[:evaluation_duration].observe(seconds(execution_time), labels: {
40
+ feature_id: feature_id.to_s,
41
+ eligible: eligible.to_s
42
+ })
43
+ end
44
+
45
+ def record_feature_execution(feature_id, state, success, execution_time, _context = {})
46
+ return unless enabled?
47
+
48
+ execution_tags = {
49
+ feature_id: feature_id.to_s,
50
+ state: state.to_s,
51
+ success: success.to_s
52
+ }
53
+
54
+ duration_tags = {
55
+ feature_id: feature_id.to_s,
56
+ state: state.to_s
57
+ }
58
+
59
+ @registry[:feature_executions].increment(labels: execution_tags)
60
+ @registry[:execution_duration].observe(seconds(execution_time), labels: duration_tags)
61
+ end
62
+
63
+ def record_state_transition(feature_id, from_state, to_state, event_name)
64
+ return unless enabled?
65
+
66
+ tags = {
67
+ feature_id: feature_id.to_s,
68
+ from_state: from_state.to_s,
69
+ to_state: to_state.to_s,
70
+ event: event_name.to_s
71
+ }
72
+
73
+ @registry[:state_transitions].increment(labels: tags)
74
+ end
75
+
76
+ def record_system_health(healthy, component_healths = {})
77
+ return unless enabled?
78
+
79
+ @registry[:system_health].set(healthy ? 1 : 0, labels: { component: 'overall' })
80
+
81
+ component_healths.each do |component, health|
82
+ @registry[:system_health].set(health ? 1 : 0, labels: { component: component.to_s })
83
+ end
84
+ end
85
+
16
86
  protected
17
87
 
18
88
  def send_metric(name, value, tags, _timestamp)
@@ -21,7 +91,9 @@ module StarGates
21
91
  case name
22
92
  when /\.gauge$/
23
93
  get_or_create_gauge(metric_name, tags).set(value)
24
- when /\.histogram$/, /\.timing$/
94
+ when /\.timing$/
95
+ get_or_create_histogram(metric_name, tags).observe(seconds(value))
96
+ when /\.histogram$/
25
97
  get_or_create_histogram(metric_name, tags).observe(value)
26
98
  else
27
99
  # Default to counter
@@ -29,10 +101,6 @@ module StarGates
29
101
  end
30
102
  end
31
103
 
32
- def healthy?
33
- super && @prometheus.respond_to?(:registry)
34
- end
35
-
36
104
  private
37
105
 
38
106
  def default_prometheus
@@ -64,17 +132,17 @@ module StarGates
64
132
  )
65
133
 
66
134
  @registry[:execution_duration] = @prometheus.histogram(
67
- :"#{@metric_prefix}_execution_duration_milliseconds",
68
- docstring: 'Feature execution duration in milliseconds',
135
+ :"#{@metric_prefix}_execution_duration_seconds",
136
+ docstring: 'Feature execution duration in seconds',
69
137
  labels: %i[feature_id state],
70
- buckets: [0.1, 0.5, 1, 2.5, 5, 10, 25, 50, 100, 250, 500, 1000]
138
+ buckets: SECONDS_BUCKETS
71
139
  )
72
140
 
73
141
  @registry[:evaluation_duration] = @prometheus.histogram(
74
- :"#{@metric_prefix}_evaluation_duration_milliseconds",
75
- docstring: 'Feature evaluation duration in milliseconds',
142
+ :"#{@metric_prefix}_evaluation_duration_seconds",
143
+ docstring: 'Feature evaluation duration in seconds',
76
144
  labels: %i[feature_id eligible],
77
- buckets: [0.01, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10]
145
+ buckets: SECONDS_BUCKETS
78
146
  )
79
147
 
80
148
  @registry[:system_health] = @prometheus.gauge(
@@ -110,73 +178,13 @@ module StarGates
110
178
  name.to_sym,
111
179
  docstring: "Histogram metric for #{name}",
112
180
  labels: tags.keys,
113
- buckets: default_timing_buckets
181
+ buckets: SECONDS_BUCKETS
114
182
  )
115
183
  @registry[key].with(tags)
116
184
  end
117
185
 
118
- def default_timing_buckets
119
- [0.1, 0.5, 1, 2.5, 5, 10, 25, 50, 100, 250, 500, 1000]
120
- end
121
-
122
- # Use pre-registered metrics for better performance
123
- def record_feature_evaluation(feature_id, eligible, _context, execution_time_ms = nil)
124
- return unless enabled?
125
-
126
- tags = {
127
- feature_id: feature_id.to_s,
128
- eligible: eligible.to_s
129
- }
130
-
131
- @registry[:feature_evaluations].increment(labels: tags)
132
-
133
- return unless execution_time_ms
134
-
135
- @registry[:evaluation_duration].observe(execution_time_ms, labels: {
136
- feature_id: feature_id.to_s,
137
- eligible: eligible.to_s
138
- })
139
- end
140
-
141
- def record_feature_execution(feature_id, state, success, execution_time_ms, _context = {})
142
- return unless enabled?
143
-
144
- execution_tags = {
145
- feature_id: feature_id.to_s,
146
- state: state.to_s,
147
- success: success.to_s
148
- }
149
-
150
- duration_tags = {
151
- feature_id: feature_id.to_s,
152
- state: state.to_s
153
- }
154
-
155
- @registry[:feature_executions].increment(labels: execution_tags)
156
- @registry[:execution_duration].observe(execution_time_ms, labels: duration_tags)
157
- end
158
-
159
- def record_state_transition(feature_id, from_state, to_state, event_name)
160
- return unless enabled?
161
-
162
- tags = {
163
- feature_id: feature_id.to_s,
164
- from_state: from_state.to_s,
165
- to_state: to_state.to_s,
166
- event: event_name.to_s
167
- }
168
-
169
- @registry[:state_transitions].increment(labels: tags)
170
- end
171
-
172
- def record_system_health(healthy, component_healths = {})
173
- return unless enabled?
174
-
175
- @registry[:system_health].set(healthy ? 1 : 0, labels: { component: 'overall' })
176
-
177
- component_healths.each do |component, health|
178
- @registry[:system_health].set(health ? 1 : 0, labels: { component: component.to_s })
179
- end
186
+ def seconds(nanoseconds)
187
+ Rational(nanoseconds, NS_PER_SECOND).to_f
180
188
  end
181
189
  end
182
190
  end
@@ -2,6 +2,9 @@
2
2
 
3
3
  module StarGates
4
4
  module Telemetry
5
+ # Metrics sink. Every duration handed to it is Integer nanoseconds and
6
+ # every timestamp wall-clock Integer nanoseconds; a backend converts at
7
+ # its own boundary.
5
8
  class TelemetryAdapter
6
9
  def initialize(config = {})
7
10
  @config = config
@@ -9,7 +12,7 @@ module StarGates
9
12
  end
10
13
 
11
14
  # Core telemetry methods
12
- def record_metric(name, value, tags: {}, timestamp: Time.now)
15
+ def record_metric(name, value, tags: {}, timestamp: StarGates.wall_time)
13
16
  return unless enabled?
14
17
 
15
18
  send_metric(name, value, tags, timestamp)
@@ -33,14 +36,14 @@ module StarGates
33
36
  record_metric("#{name}.histogram", value, tags: tags)
34
37
  end
35
38
 
36
- def record_timing(name, duration_ms, tags: {})
39
+ def record_timing(name, duration, tags: {})
37
40
  return unless enabled?
38
41
 
39
- record_metric("#{name}.timing", duration_ms, tags: tags)
42
+ record_metric("#{name}.timing", duration, tags: tags)
40
43
  end
41
44
 
42
45
  # Feature flag specific metrics
43
- def record_feature_evaluation(feature_id, eligible, _context, execution_time_ms = nil)
46
+ def record_feature_evaluation(feature_id, eligible, _context, execution_time = nil)
44
47
  return unless enabled?
45
48
 
46
49
  tags = {
@@ -50,12 +53,12 @@ module StarGates
50
53
 
51
54
  record_counter('star_gates.feature.evaluation', 1, tags: tags)
52
55
 
53
- return unless execution_time_ms
56
+ return unless execution_time
54
57
 
55
- record_timing('star_gates.feature.evaluation.duration', execution_time_ms, tags: tags)
58
+ record_timing('star_gates.feature.evaluation.duration', execution_time, tags: tags)
56
59
  end
57
60
 
58
- def record_feature_execution(feature_id, state, success, execution_time_ms, _context = {})
61
+ def record_feature_execution(feature_id, state, success, execution_time, _context = {})
59
62
  return unless enabled?
60
63
 
61
64
  tags = {
@@ -65,7 +68,7 @@ module StarGates
65
68
  }
66
69
 
67
70
  record_counter('star_gates.feature.execution', 1, tags: tags)
68
- record_timing('star_gates.feature.execution.duration', execution_time_ms, tags: tags)
71
+ record_timing('star_gates.feature.execution.duration', execution_time, tags: tags)
69
72
  end
70
73
 
71
74
  def record_state_transition(feature_id, from_state, to_state, event_name)
@@ -105,26 +108,24 @@ module StarGates
105
108
  end
106
109
  end
107
110
 
108
- def record_event_store_metrics(event_count, aggregate_count, projection_lag_ms = nil)
111
+ def record_event_store_metrics(event_count, aggregate_count, projection_lag = nil)
109
112
  return unless enabled?
110
113
 
111
114
  record_gauge('star_gates.event_store.events', event_count)
112
115
  record_gauge('star_gates.event_store.aggregates', aggregate_count)
113
116
 
114
- return unless projection_lag_ms
117
+ return unless projection_lag
115
118
 
116
- record_timing('star_gates.event_store.projection_lag', projection_lag_ms)
119
+ record_timing('star_gates.event_store.projection_lag', projection_lag)
117
120
  end
118
121
 
119
122
  # Time a block and record the duration
120
123
  def time(metric_name, tags: {}, &block)
121
124
  return block.call unless enabled?
122
125
 
123
- start_time = Time.now
126
+ started = StarGates.monotonic_time
124
127
  result = block.call
125
- duration_ms = ((Time.now - start_time) * 1000).round(2)
126
-
127
- record_timing(metric_name, duration_ms, tags: tags)
128
+ record_timing(metric_name, StarGates.monotonic_time - started, tags: tags)
128
129
  result
129
130
  end
130
131
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module StarGates
4
- VERSION = '0.3.2'
4
+ VERSION = '0.4.0'
5
5
  end
data/lib/star_gates.rb CHANGED
@@ -32,6 +32,9 @@ loader.ignore("#{__dir__}/star_gates/hexagonal")
32
32
  loader.ignore("#{__dir__}/star_gates/projections")
33
33
  loader.ignore("#{__dir__}/star_gates/telemetry")
34
34
  loader.setup
35
+ # Defines StarGates.configuration and StarGates.configure, which no constant
36
+ # reference would autoload.
37
+ require_relative 'star_gates/configuration'
35
38
 
36
39
  module StarGates
37
40
  @registry = Registry.new
@@ -50,11 +53,20 @@ module StarGates
50
53
  context.instance_eval(&) if block_given?
51
54
  end
52
55
 
56
+ # KeyError for a name with no gate.
53
57
  def [](name)
54
- gate = registry[name]
55
- raise GateNotFoundError, "Gate #{name} not found" unless gate
58
+ registry.fetch(name)
59
+ end
60
+
61
+ # Monotonic Integer nanoseconds: every in-process instant and span.
62
+ def monotonic_time
63
+ Process.clock_gettime(Process::CLOCK_MONOTONIC, :nanosecond)
64
+ end
56
65
 
57
- gate
66
+ # Integer nanoseconds since the Unix epoch, for records that outlive the
67
+ # process (event-sourced events).
68
+ def wall_time
69
+ Process.clock_gettime(Process::CLOCK_REALTIME, :nanosecond)
58
70
  end
59
71
 
60
72
  def activate(name, context = {})
@@ -81,8 +93,12 @@ module StarGates
81
93
  @test_mode = false
82
94
  end
83
95
 
96
+ # Forgets every gate and the configured feature service and telemetry
97
+ # adapter, so the next caller starts from nothing.
84
98
  def reset!
85
99
  registry.clear
100
+ @feature_service = nil
101
+ @telemetry_adapter = nil
86
102
  end
87
103
 
88
104
  # Instrumentation for feature flag events
@@ -116,7 +132,7 @@ module StarGates
116
132
  gate_info = {
117
133
  name: name,
118
134
  feature: feature&.to_h,
119
- circuit: circuit ? circuit_info(circuit) : nil,
135
+ circuit: circuit&.to_h,
120
136
  status: gate.status
121
137
  }
122
138
 
@@ -143,7 +159,7 @@ module StarGates
143
159
  features_by_status: Hash.new(0),
144
160
  average_rollout: 0,
145
161
  rollout_distribution: {},
146
- generated_at: Time.now
162
+ generated_at: monotonic_time
147
163
  }
148
164
 
149
165
  rollout_percentages = []
@@ -172,7 +188,7 @@ module StarGates
172
188
  end
173
189
 
174
190
  stats[:average_rollout] =
175
- rollout_percentages.any? ? (rollout_percentages.sum / rollout_percentages.size).round(2) : 0
191
+ rollout_percentages.any? ? rollout_percentages.sum.fdiv(rollout_percentages.size).round(2) : 0
176
192
 
177
193
  stats
178
194
  end
@@ -199,46 +215,43 @@ module StarGates
199
215
  }
200
216
  end
201
217
 
202
- # Recent state transitions across all features
218
+ # Recent state transitions across all features, newest first, as
219
+ # `{ transitions:, total_found:, limit:, generated_at: }` in both modes;
220
+ # `generated_at` is the monotonic instant. Every transition is
221
+ # `{ feature:, from:, to:, event:, occurred_at: }` in both modes: `feature`
222
+ # the String feature id (the gate name without event sourcing), `from`,
223
+ # `to` and `event` Symbols, `occurred_at` wall-clock Integer nanoseconds.
224
+ # With event sourcing `total_found` counts the transitions the projection
225
+ # keeps; otherwise the registered features' transitions.
203
226
  def recent_transitions(limit: 10)
204
- if feature_service_enabled?
205
- feature_service.recent_transitions(limit: limit)
206
- else
207
- # Fallback to legacy implementation
208
- gates = registry.to_h
209
- all_transitions = []
210
-
211
- gates.each_value do |gate|
212
- feature = gate.feature
213
- next unless feature
214
-
215
- feature_transitions = feature.instance_variable_get(:@transition_history)
216
- next unless feature_transitions
227
+ recent, total = feature_service_enabled? ? projected_transitions(limit) : registered_transitions(limit)
228
+ { transitions: recent, total_found: total, limit: limit, generated_at: monotonic_time }
229
+ end
217
230
 
218
- all_transitions.concat(feature_transitions.to_a)
219
- end
231
+ def projected_transitions(limit)
232
+ recent = feature_service.recent_transitions(limit: limit).map do |transition|
233
+ transition_entry(transition[:feature_id], transition[:from_state], transition[:to_state],
234
+ transition[:event_name], transition[:occurred_at])
235
+ end
236
+ [recent, feature_service.transition_count]
237
+ end
220
238
 
221
- # Sort by timestamp descending and take the most recent
222
- recent = all_transitions.sort_by { |t| -t[:timestamp].to_f }.first(limit)
223
-
224
- {
225
- transitions: recent.map do |transition|
226
- {
227
- timestamp: transition[:timestamp],
228
- feature_name: transition[:feature_name],
229
- feature_class: transition[:feature_class],
230
- from_state: transition[:from_state],
231
- to_state: transition[:to_state],
232
- event: transition[:event],
233
- duration_since: Time.now - transition[:timestamp]
234
- }
235
- end,
236
- total_found: all_transitions.size,
237
- limit: limit,
238
- generated_at: Time.now
239
- }
239
+ def registered_transitions(limit)
240
+ gates = registry.values.select { |gate| gate.feature.respond_to?(:transition_history) }
241
+ recent = gates.flat_map do |gate|
242
+ gate.feature.transition_history(limit: limit)[:transitions].map { |transition| [gate.name, transition] }
240
243
  end
244
+ recent = recent.sort_by { |_, transition| -transition[:at] }.first(limit).map do |name, transition|
245
+ transition_entry(name, transition[:from_state], transition[:to_state], transition[:event],
246
+ transition[:occurred_at])
247
+ end
248
+ [recent, gates.sum { |gate| gate.feature.transition_count }]
249
+ end
250
+
251
+ def transition_entry(feature, from, to, event, occurred_at)
252
+ { feature: feature.to_s, from: from, to: to, event: event, occurred_at: occurred_at }
241
253
  end
254
+ private :projected_transitions, :registered_transitions, :transition_entry
242
255
 
243
256
  # Configure the foundational architecture
244
257
  def configure_foundation(event_store: nil, adapter: nil, telemetry: nil)
@@ -282,23 +295,6 @@ module StarGates
282
295
  def configure_telemetry(adapter)
283
296
  @telemetry_adapter = adapter
284
297
  end
285
-
286
- # Check if jsonrpc-rails is available
287
- def jsonrpc_rails_available?
288
- defined?(JSON_RPC) && defined?(ActionController::Base)
289
- end
290
-
291
- private
292
-
293
- def circuit_info(circuit)
294
- if circuit.respond_to?(:to_h)
295
- circuit.to_h
296
- elsif circuit.respond_to?(:stats)
297
- circuit.stats
298
- else
299
- { status: circuit.respond_to?(:status) ? circuit.status : 'unknown' }
300
- end
301
- end
302
298
  end
303
299
  end
304
300