Class: Statsig::Diagnostics

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/diagnostics.rb

Defined Under Namespace

Classes: Context, 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

#initialize(context) ⇒ Diagnostics

Returns a new instance of Diagnostics.



18
19
20
21
22
# File 'lib/diagnostics.rb', line 18

def initialize(context)
  @context = context
  @markers = []
  @sample_rates = {}
end

Instance Attribute Details

#contextObject

Returns the value of attribute context.



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

def context
  @context
end

#markersObject (readonly)

Returns the value of attribute markers.



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

def markers
  @markers
end

#sample_ratesObject

Returns the value of attribute sample_rates.



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

def sample_rates
  @sample_rates
end

Class Method Details

.sample(rate_over_ten_thousand) ⇒ Object



91
92
93
# File 'lib/diagnostics.rb', line 91

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

Instance Method Details

#clear_markersObject



87
88
89
# File 'lib/diagnostics.rb', line 87

def clear_markers
  @markers.clear
end

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



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

def mark(key, action, step, tags)
  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
  @markers.push(marker)
end

#serializeObject



65
66
67
68
69
70
# File 'lib/diagnostics.rb', line 65

def serialize
  {
    context: @context.clone,
    markers: @markers.clone
  }
end

#serialize_with_samplingObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/diagnostics.rb', line 72

def serialize_with_sampling
  marker_keys = @markers.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.select do |marker|
    !sampled_marker_keys.include?(marker[:key])
  end
  {
    context: @context.clone,
    markers: final_markers
  }
end

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



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

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