Class: Statsig::Diagnostics

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
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.



15
16
17
18
# File 'lib/diagnostics.rb', line 15

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

Instance Attribute Details

#markersObject (readonly)

Returns the value of attribute markers.



10
11
12
# File 'lib/diagnostics.rb', line 10

def markers
  @markers
end

#sample_ratesObject

Returns the value of attribute sample_rates.



13
14
15
# File 'lib/diagnostics.rb', line 13

def sample_rates
  @sample_rates
end

Class Method Details

.sample(rate_over_ten_thousand) ⇒ Object



83
84
85
# File 'lib/diagnostics.rb', line 83

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

Instance Method Details

#clear_markers(context) ⇒ Object



79
80
81
# File 'lib/diagnostics.rb', line 79

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

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



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/diagnostics.rb', line 30

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



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/diagnostics.rb', line 64

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



58
59
60
61
62
# File 'lib/diagnostics.rb', line 58

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