Class: Quonfig::Telemetry::ContextShapeAggregator
- Inherits:
-
Object
- Object
- Quonfig::Telemetry::ContextShapeAggregator
- Defined in:
- lib/quonfig/telemetry/context_shape_aggregator.rb
Overview
Aggregates the set of context shapes observed during config evaluation. Each unique (context-name, property, type) tuple is stored once. On sync, the set is folded into a hash grouped by context name and emitted as api-telemetry’s ‘contextShapes` event.
Matches the sdk-node/sdk-go JSON wire format — NOT the old Prefab protobuf serialization.
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
Instance Method Summary collapse
-
#drain_event ⇒ Object
Drain accumulated shapes into a single telemetry event payload, matching api-telemetry’s ContextShapesSchema.
-
#initialize(max_shapes:) ⇒ ContextShapeAggregator
constructor
A new instance of ContextShapeAggregator.
-
#prepare_data ⇒ Object
Fold the raw tuples into { name => { key => type, … }, … }.
-
#push(context) ⇒ Object
Record every property of every named context in
context.
Constructor Details
#initialize(max_shapes:) ⇒ ContextShapeAggregator
Returns a new instance of ContextShapeAggregator.
15 16 17 18 |
# File 'lib/quonfig/telemetry/context_shape_aggregator.rb', line 15 def initialize(max_shapes:) @max_shapes = max_shapes @data = Concurrent::Set.new end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
13 14 15 |
# File 'lib/quonfig/telemetry/context_shape_aggregator.rb', line 13 def data @data end |
Instance Method Details
#drain_event ⇒ Object
Drain accumulated shapes into a single telemetry event payload, matching api-telemetry’s ContextShapesSchema. Returns nil when there is nothing to ship — the reporter should skip empty events.
55 56 57 58 59 60 61 62 63 |
# File 'lib/quonfig/telemetry/context_shape_aggregator.rb', line 55 def drain_event return nil if @data.size.zero? shapes = prepare_data.map do |name, field_types| { 'name' => name, 'fieldTypes' => field_types } end { 'contextShapes' => { 'shapes' => shapes } } end |
#prepare_data ⇒ Object
Fold the raw tuples into { name => { key => type, … }, … }. Clears the underlying set.
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/quonfig/telemetry/context_shape_aggregator.rb', line 41 def prepare_data duped = @data.dup @data.clear duped.inject({}) do |acc, (name, key, type)| acc[name] ||= {} acc[name][key] = type acc end end |
#push(context) ⇒ Object
Record every property of every named context in context. context may be a Quonfig::Context or a bare Hash ({ ‘user’ => { ‘key’ => …, ‘email’ => … }, … }).
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/quonfig/telemetry/context_shape_aggregator.rb', line 23 def push(context) return if @max_shapes <= 0 return if context.nil? return if @data.size >= @max_shapes each_named_context(context) do |name, hash| next unless hash.is_a?(Hash) hash.each_pair do |key, value| next if @data.size >= @max_shapes @data.add [name.to_s, key.to_s, Quonfig::Telemetry::ContextShape.field_type_number(value)] end end end |