Class: Statsig::Diagnostics

Inherits:
Object
  • Object
show all
Defined in:
lib/diagnostics.rb

Defined Under Namespace

Classes: Tracker

Constant Summary collapse

API_CALL_KEYS =
%w[check_gate get_config get_experiment get_layer].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDiagnostics

Returns a new instance of Diagnostics.



7
8
9
10
# File 'lib/diagnostics.rb', line 7

def initialize()
  @markers = {:initialize => [], :api_call => [], :config_sync => []}
  @sample_rates = {}
end

Instance Attribute Details

#markersObject (readonly)

Returns the value of attribute markers.



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

def markers
  @markers
end

#sample_ratesObject

Returns the value of attribute sample_rates.



5
6
7
# File 'lib/diagnostics.rb', line 5

def sample_rates
  @sample_rates
end

Class Method Details

.sample(rate_over_ten_thousand) ⇒ Object



57
58
59
# File 'lib/diagnostics.rb', line 57

def self.sample(rate_over_ten_thousand)
  rand * 10_000 < rate_over_ten_thousand
end

Instance Method Details

#clear_markers(context) ⇒ Object



53
54
55
# File 'lib/diagnostics.rb', line 53

def clear_markers(context)
  @markers[context].clear
end

#mark(key, action, step, tags, context) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/diagnostics.rb', line 12

def mark(key, action, step, tags, context)
  marker = {
    key: key,
    action: action,
    timestamp: (Time.now.to_f * 1000).to_i
  }
  if !step.nil?
    marker[:step] = step
  end
  tags.each do |key, val|
    unless val.nil?
      marker[key] = val
    end
  end
  if @markers[context].nil?
    @markers[context] = []
  end
  @markers[context].push(marker)
end

#serialize_with_sampling(context) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/diagnostics.rb', line 38

def serialize_with_sampling(context)
  marker_keys = @markers[context].map { |e| e[:key] }
  unique_marker_keys = marker_keys.uniq { |e| e }
  sampled_marker_keys = unique_marker_keys.select do |key|
    @sample_rates.key?(key) && !self.class.sample(@sample_rates[key])
  end
  final_markers = @markers[context].select do |marker|
    !sampled_marker_keys.include?(marker[:key])
  end
  {
    context: context.clone,
    markers: final_markers.clone
  }
end

#track(context, key, step = nil, tags = {}) ⇒ Object



32
33
34
35
36
# File 'lib/diagnostics.rb', line 32

def track(context, key, step = nil, tags = {})
  tracker = Tracker.new(self, context, key, step, tags)
  tracker.start(**tags)
  tracker
end