star_gates 0.3.1 → 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.
- checksums.yaml +4 -4
- data/.release-please-manifest.json +1 -1
- data/CHANGELOG.md +26 -0
- data/README.md +86 -22
- data/Rakefile +6 -1
- data/lib/star_gates/adapters/base_adapter.rb +2 -0
- data/lib/star_gates/adapters/redis_adapter.rb +15 -10
- data/lib/star_gates/application_star_gate.rb +107 -117
- data/lib/star_gates/circuit.rb +74 -109
- data/lib/star_gates/configuration.rb +26 -9
- data/lib/star_gates/dsl.rb +7 -10
- data/lib/star_gates/errors.rb +15 -3
- data/lib/star_gates/event_store/event_store.rb +6 -16
- data/lib/star_gates/event_store/memory_adapter.rb +7 -1
- data/lib/star_gates/events/base_event.rb +3 -1
- data/lib/star_gates/feature.rb +36 -60
- data/lib/star_gates/flags.rb +102 -25
- data/lib/star_gates/gate.rb +58 -46
- data/lib/star_gates/hexagonal/feature_aggregate.rb +49 -55
- data/lib/star_gates/hexagonal/feature_repository.rb +5 -27
- data/lib/star_gates/hexagonal/feature_service.rb +31 -33
- data/lib/star_gates/jsonrpc_rails_adapter.rb +24 -7
- data/lib/star_gates/projections/feature_projection.rb +40 -13
- data/lib/star_gates/registry.rb +3 -2
- data/lib/star_gates/retry_policy.rb +23 -0
- data/lib/star_gates/rollout.rb +15 -0
- data/lib/star_gates/telemetry/opentelemetry_adapter.rb +71 -75
- data/lib/star_gates/telemetry/prometheus_adapter.rb +82 -74
- data/lib/star_gates/telemetry/telemetry_adapter.rb +16 -15
- data/lib/star_gates/version.rb +1 -1
- data/lib/star_gates.rb +56 -58
- data/sig/star_gates.rbs +832 -0
- metadata +42 -10
|
@@ -2,7 +2,18 @@
|
|
|
2
2
|
|
|
3
3
|
module StarGates
|
|
4
4
|
module Projections
|
|
5
|
+
# The read model built from feature events. Instants are the events'
|
|
6
|
+
# wall-clock Integer nanoseconds.
|
|
7
|
+
#
|
|
8
|
+
# Records are normalised on read, whatever the adapter hands back: a
|
|
9
|
+
# feature and a transition have Symbol keys, and their state and event
|
|
10
|
+
# names are Symbols, so an adapter that serialises to JSON (RedisAdapter:
|
|
11
|
+
# String keys, String values) reads like the memory one. Caller data
|
|
12
|
+
# nested inside (metadata, context) comes back as the adapter keeps it.
|
|
5
13
|
class FeatureProjection
|
|
14
|
+
NAMED_FIELDS = %i[state from_state to_state event_name].freeze
|
|
15
|
+
private_constant :NAMED_FIELDS
|
|
16
|
+
|
|
6
17
|
def initialize(adapter)
|
|
7
18
|
@adapter = adapter
|
|
8
19
|
@mutex = Mutex.new
|
|
@@ -26,7 +37,7 @@ module StarGates
|
|
|
26
37
|
|
|
27
38
|
# Query methods
|
|
28
39
|
def find_feature(feature_id)
|
|
29
|
-
@adapter.fetch("feature:#{feature_id}")
|
|
40
|
+
normalized(@adapter.fetch("feature:#{feature_id}"))
|
|
30
41
|
end
|
|
31
42
|
|
|
32
43
|
def find_features_by_state(state)
|
|
@@ -50,10 +61,12 @@ module StarGates
|
|
|
50
61
|
end
|
|
51
62
|
|
|
52
63
|
def recent_transitions(limit: 10)
|
|
53
|
-
|
|
54
|
-
transitions.last(limit).reverse
|
|
64
|
+
stored_transitions.last(limit).reverse
|
|
55
65
|
end
|
|
56
66
|
|
|
67
|
+
# How many transitions the projection keeps (the newest 100).
|
|
68
|
+
def transition_count = stored_transitions.size
|
|
69
|
+
|
|
57
70
|
def feature_exists?(feature_id)
|
|
58
71
|
@adapter.exists?("feature:#{feature_id}")
|
|
59
72
|
end
|
|
@@ -93,7 +106,7 @@ module StarGates
|
|
|
93
106
|
|
|
94
107
|
def handle_state_changed(event)
|
|
95
108
|
@mutex.synchronize do
|
|
96
|
-
feature_data =
|
|
109
|
+
feature_data = find_feature(event.aggregate_id)
|
|
97
110
|
return unless feature_data
|
|
98
111
|
|
|
99
112
|
old_state = feature_data[:state]
|
|
@@ -117,7 +130,7 @@ module StarGates
|
|
|
117
130
|
|
|
118
131
|
def handle_configuration_changed(event)
|
|
119
132
|
@mutex.synchronize do
|
|
120
|
-
feature_data =
|
|
133
|
+
feature_data = find_feature(event.aggregate_id)
|
|
121
134
|
return unless feature_data
|
|
122
135
|
|
|
123
136
|
feature_data[:updated_at] = event.occurred_at
|
|
@@ -127,7 +140,7 @@ module StarGates
|
|
|
127
140
|
|
|
128
141
|
def handle_execution_recorded(event)
|
|
129
142
|
@mutex.synchronize do
|
|
130
|
-
feature_data =
|
|
143
|
+
feature_data = find_feature(event.aggregate_id)
|
|
131
144
|
return unless feature_data
|
|
132
145
|
|
|
133
146
|
feature_data[:execution_count] += 1
|
|
@@ -138,7 +151,7 @@ module StarGates
|
|
|
138
151
|
|
|
139
152
|
def handle_evaluation_recorded(event)
|
|
140
153
|
@mutex.synchronize do
|
|
141
|
-
feature_data =
|
|
154
|
+
feature_data = find_feature(event.aggregate_id)
|
|
142
155
|
return unless feature_data
|
|
143
156
|
|
|
144
157
|
feature_data[:evaluation_count] += 1
|
|
@@ -176,7 +189,7 @@ module StarGates
|
|
|
176
189
|
end
|
|
177
190
|
|
|
178
191
|
def update_recent_transitions(event)
|
|
179
|
-
transitions =
|
|
192
|
+
transitions = stored_transitions
|
|
180
193
|
|
|
181
194
|
transition_data = {
|
|
182
195
|
feature_id: event.aggregate_id,
|
|
@@ -207,12 +220,26 @@ module StarGates
|
|
|
207
220
|
counts
|
|
208
221
|
end
|
|
209
222
|
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
223
|
+
RECENT_SPAN = 7 * 24 * 60 * 60 * 1_000_000_000
|
|
224
|
+
private_constant :RECENT_SPAN
|
|
225
|
+
|
|
226
|
+
# Transitions in the last week of wall-clock time.
|
|
227
|
+
def recent_transitions_count
|
|
228
|
+
cutoff = StarGates.wall_time - RECENT_SPAN
|
|
229
|
+
stored_transitions.count { |transition| transition[:occurred_at] > cutoff }
|
|
230
|
+
end
|
|
231
|
+
|
|
232
|
+
def stored_transitions
|
|
233
|
+
(@adapter.fetch('recent_transitions') || []).map { |transition| normalized(transition) }
|
|
234
|
+
end
|
|
235
|
+
|
|
236
|
+
# The record with Symbol keys and Symbol state and event names.
|
|
237
|
+
def normalized(record)
|
|
238
|
+
return unless record
|
|
213
239
|
|
|
214
|
-
|
|
215
|
-
|
|
240
|
+
record.to_h do |key, value|
|
|
241
|
+
key = key.to_sym
|
|
242
|
+
[key, NAMED_FIELDS.include?(key) && value.is_a?(String) ? value.to_sym : value]
|
|
216
243
|
end
|
|
217
244
|
end
|
|
218
245
|
|
data/lib/star_gates/registry.rb
CHANGED
|
@@ -18,8 +18,9 @@ module StarGates
|
|
|
18
18
|
@gates[name.to_sym]
|
|
19
19
|
end
|
|
20
20
|
|
|
21
|
-
|
|
22
|
-
|
|
21
|
+
# KeyError for an unknown name unless a default or block is given.
|
|
22
|
+
def fetch(name, *default, &)
|
|
23
|
+
@gates.fetch(name.to_sym, *default, &)
|
|
23
24
|
end
|
|
24
25
|
|
|
25
26
|
def include?(name)
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'chrono_machines'
|
|
4
|
+
|
|
5
|
+
module StarGates
|
|
6
|
+
# How a circuit retries a failed call: a chrono_machines strategy
|
|
7
|
+
# (Exponential, Constant or Fibonacci; spans in Integer nanoseconds) and an
|
|
8
|
+
# attempt budget that counts the first try (1..255, or nil for unbounded).
|
|
9
|
+
# The fields and their order are ChronoMachines::Retry's.
|
|
10
|
+
#
|
|
11
|
+
# Validated when built, by chrono's core: TypeError for the wrong type,
|
|
12
|
+
# RangeError for a budget outside 1..255.
|
|
13
|
+
RetryPolicy = Data.define(:strategy, :max_attempts) do
|
|
14
|
+
def initialize(strategy:, max_attempts:)
|
|
15
|
+
ChronoMachines::Retry.new(strategy, max_attempts)
|
|
16
|
+
super
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# Sealed like the sibling values: `initialize` is where it validates.
|
|
21
|
+
RetryPolicy.singleton_class.undef_method(:allocate)
|
|
22
|
+
RetryPolicy.define_singleton_method(:inherited) { |_| raise TypeError, "#{RetryPolicy} cannot be subclassed" }
|
|
23
|
+
end
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module StarGates
|
|
4
|
+
# The rollout percentage rule shared by Feature and the event-sourced
|
|
5
|
+
# FeatureAggregate.
|
|
6
|
+
module Rollout
|
|
7
|
+
# The rollout itself: TypeError unless an Integer, RangeError outside 0..100.
|
|
8
|
+
def self.validated(rollout)
|
|
9
|
+
raise TypeError, "rollout must be an Integer percentage, got #{rollout.inspect}" unless rollout.is_a?(Integer)
|
|
10
|
+
raise RangeError, "rollout must be in 0..100, got #{rollout}" unless (0..100).cover?(rollout)
|
|
11
|
+
|
|
12
|
+
rollout
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
@@ -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
|
-
|
|
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.
|
|
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
|
-
|
|
46
|
+
started = StarGates.monotonic_time
|
|
48
47
|
begin
|
|
49
48
|
result = block.call
|
|
50
|
-
|
|
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
|
-
|
|
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: '
|
|
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: '
|
|
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: '
|
|
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 /\.
|
|
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}
|
|
68
|
-
docstring: 'Feature execution duration in
|
|
135
|
+
:"#{@metric_prefix}_execution_duration_seconds",
|
|
136
|
+
docstring: 'Feature execution duration in seconds',
|
|
69
137
|
labels: %i[feature_id state],
|
|
70
|
-
buckets:
|
|
138
|
+
buckets: SECONDS_BUCKETS
|
|
71
139
|
)
|
|
72
140
|
|
|
73
141
|
@registry[:evaluation_duration] = @prometheus.histogram(
|
|
74
|
-
:"#{@metric_prefix}
|
|
75
|
-
docstring: 'Feature evaluation duration in
|
|
142
|
+
:"#{@metric_prefix}_evaluation_duration_seconds",
|
|
143
|
+
docstring: 'Feature evaluation duration in seconds',
|
|
76
144
|
labels: %i[feature_id eligible],
|
|
77
|
-
buckets:
|
|
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:
|
|
181
|
+
buckets: SECONDS_BUCKETS
|
|
114
182
|
)
|
|
115
183
|
@registry[key].with(tags)
|
|
116
184
|
end
|
|
117
185
|
|
|
118
|
-
def
|
|
119
|
-
|
|
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:
|
|
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,
|
|
39
|
+
def record_timing(name, duration, tags: {})
|
|
37
40
|
return unless enabled?
|
|
38
41
|
|
|
39
|
-
record_metric("#{name}.timing",
|
|
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,
|
|
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
|
|
56
|
+
return unless execution_time
|
|
54
57
|
|
|
55
|
-
record_timing('star_gates.feature.evaluation.duration',
|
|
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,
|
|
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',
|
|
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,
|
|
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
|
|
117
|
+
return unless projection_lag
|
|
115
118
|
|
|
116
|
-
record_timing('star_gates.event_store.projection_lag',
|
|
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
|
-
|
|
126
|
+
started = StarGates.monotonic_time
|
|
124
127
|
result = block.call
|
|
125
|
-
|
|
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
|
|