Class: Flagstack::Telemetry

Inherits:
Object
  • Object
show all
Defined in:
lib/flagstack/telemetry.rb,
lib/flagstack/telemetry/metric.rb,
lib/flagstack/telemetry/submitter.rb,
lib/flagstack/telemetry/metric_storage.rb

Defined Under Namespace

Classes: Metric, MetricStorage, Submitter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, config) ⇒ Telemetry

Returns a new instance of Telemetry.



5
6
7
8
9
10
11
12
# File 'lib/flagstack/telemetry.rb', line 5

def initialize(client, config)
  @client = client
  @config = config
  @storage = MetricStorage.new
  @pid = Process.pid
  @started = false
  @mutex = Mutex.new
end

Instance Attribute Details

#storageObject (readonly)

Returns the value of attribute storage.



3
4
5
# File 'lib/flagstack/telemetry.rb', line 3

def storage
  @storage
end

Instance Method Details

#flushObject



44
45
46
47
48
49
50
51
52
# File 'lib/flagstack/telemetry.rb', line 44

def flush
  return if @storage.empty?

  metrics = @storage.drain
  submitter = Submitter.new(@client, @config)

  # Submit in background thread to not block
  Thread.new { submitter.call(metrics) }
end

#record(feature_key, result) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/flagstack/telemetry.rb', line 34

def record(feature_key, result)
  return unless @config.telemetry_enabled

  # Fork detection - restart if PID changed
  check_fork

  metric = Metric.new(feature_key, result)
  @storage.increment(metric)
end

#running?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/flagstack/telemetry.rb', line 54

def running?
  @started
end

#startObject



14
15
16
17
18
19
20
21
# File 'lib/flagstack/telemetry.rb', line 14

def start
  @mutex.synchronize do
    return if @started
    start_timer
    @started = true
    @config.log("Telemetry started (interval: #{@config.telemetry_interval}s)", level: :info)
  end
end

#stopObject



23
24
25
26
27
28
29
30
31
32
# File 'lib/flagstack/telemetry.rb', line 23

def stop
  @mutex.synchronize do
    return unless @started
    @timer_thread&.kill
    @timer_thread = nil
    flush
    @started = false
    @config.log("Telemetry stopped", level: :info)
  end
end