Class: SplitIoClient::Cache::Repositories::Metrics::RedisRepository

Inherits:
SplitIoClient::Cache::Repository show all
Defined in:
lib/splitclient-rb/cache/repositories/metrics/redis_repository.rb

Instance Method Summary collapse

Methods inherited from SplitIoClient::Cache::Repository

#set_string, #string

Constructor Details

#initialize(config) ⇒ RedisRepository

Returns a new instance of RedisRepository.



6
7
8
9
# File 'lib/splitclient-rb/cache/repositories/metrics/redis_repository.rb', line 6

def initialize(config)
  @config = config
  @adapter = config.metrics_adapter
end

Instance Method Details

#add_count(counter, delta) ⇒ Object



11
12
13
14
15
16
# File 'lib/splitclient-rb/cache/repositories/metrics/redis_repository.rb', line 11

def add_count(counter, delta)
  prefixed_name = impressions_metrics_key("count.#{counter}")
  counts = @adapter.find_strings_by_prefix(prefixed_name)

  @adapter.inc(prefixed_name, delta)
end

#add_gauge(gauge, value) ⇒ Object



24
25
26
# File 'lib/splitclient-rb/cache/repositories/metrics/redis_repository.rb', line 24

def add_gauge(gauge, value)
  # TODO
end

#add_latency(operation, time_in_ms, binary_search) ⇒ Object



18
19
20
21
22
# File 'lib/splitclient-rb/cache/repositories/metrics/redis_repository.rb', line 18

def add_latency(operation, time_in_ms, binary_search)
  prefixed_name = impressions_metrics_key("latency.#{operation}")

  @adapter.inc("#{prefixed_name}.bucket.#{binary_search.add_latency_millis(time_in_ms, true)}")
end

#clearObject



106
107
108
109
110
# File 'lib/splitclient-rb/cache/repositories/metrics/redis_repository.rb', line 106

def clear
  clear_counts
  clear_latencies
  clear_gauges
end

#clear_countsObject



58
59
60
61
# File 'lib/splitclient-rb/cache/repositories/metrics/redis_repository.rb', line 58

def clear_counts
  keys = @adapter.find_strings_by_prefix(impressions_metrics_key('count'))
  @adapter.delete(keys)
end

#clear_gaugesObject



102
103
104
# File 'lib/splitclient-rb/cache/repositories/metrics/redis_repository.rb', line 102

def clear_gauges
  # TODO
end

#clear_latenciesObject



63
64
65
66
# File 'lib/splitclient-rb/cache/repositories/metrics/redis_repository.rb', line 63

def clear_latencies
  keys = @adapter.find_strings_by_prefix(impressions_metrics_key('latency'))
  @adapter.delete(keys)
end

#countsObject



28
29
30
31
32
33
34
35
36
# File 'lib/splitclient-rb/cache/repositories/metrics/redis_repository.rb', line 28

def counts
  keys = @adapter.find_strings_by_prefix(impressions_metrics_key("count"))

  return [] if keys.empty?

  @adapter.multiple_strings(keys).map do |name, data|
    [name.gsub(impressions_metrics_key('count.'), ''), data]
  end.to_h
end

#fix_latenciesObject

introduced to fix incorrect latencies



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/splitclient-rb/cache/repositories/metrics/redis_repository.rb', line 69

def fix_latencies
  latencies_cleaned_key =  namespace_key('/latencies.cleaned')
  return if @adapter.exists?(latencies_cleaned_key)

  keys =[]

  23.times do |index|
    keys.concat @adapter.find_strings_by_pattern(latencies_to_be_deleted_key_pattern_prefix("sdk.get_treatment.#{index}"))
  end

  keys.concat @adapter.find_strings_by_pattern(latencies_to_be_deleted_key_pattern_prefix('sdk.get_treatments'))
  keys.concat @adapter.find_strings_by_pattern(latencies_to_be_deleted_key_pattern_prefix('sdk.get_treatment_with_config'))
  keys.concat @adapter.find_strings_by_pattern(latencies_to_be_deleted_key_pattern_prefix('sdk.get_treatments_with_config'))

  keys.concat @adapter.find_strings_by_pattern(latencies_to_be_deleted_key_pattern_prefix('*.time'))

  @config.logger.info("Found incorrect latency keys, deleting. Keys: #{keys}") unless keys.size == 0

  keys.each_slice(500) do |chunk|
    @adapter.pipelined do
      chunk.each do |key|
        @adapter.delete key
      end
    end
  end

  @adapter.set_string(latencies_cleaned_key, '1')
end

#gaugesObject



54
55
56
# File 'lib/splitclient-rb/cache/repositories/metrics/redis_repository.rb', line 54

def gauges
  # TODO
end

#latenciesObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/splitclient-rb/cache/repositories/metrics/redis_repository.rb', line 38

def latencies
  keys = @adapter.find_strings_by_prefix(impressions_metrics_key('latency'))
  return [] if keys.empty?

  collected_latencies = collect_latencies(keys)

  collected_latencies.keys.each_with_object({}) do |operation, latencies|
    operation_latencies = Array.new(BinarySearchLatencyTracker::BUCKETS.length, 0)
    collected_latencies[operation].each do |bucket, value|
      operation_latencies[bucket.to_i] = value.to_i
    end

    latencies[operation] = operation_latencies
  end
end

#latencies_to_be_deleted_key_pattern_prefix(key) ⇒ Object



98
99
100
# File 'lib/splitclient-rb/cache/repositories/metrics/redis_repository.rb', line 98

def latencies_to_be_deleted_key_pattern_prefix(key)
  "#{@config.redis_namespace}/#{@config.language}-*/latency.#{key}"
end