Module: Gitlab::UsageDataCounters::RedisCounter

Constant Summary collapse

KEY_OVERRIDES_PATH =

This file overrides (or aliases) some keys for legacy Redis metric counters to delay migrating them to new names for now, because doing that in bulk will be a lot easier.

Rails.root.join('lib/gitlab/usage_data_counters/total_counter_redis_key_overrides.yml')

Instance Method Summary collapse

Instance Method Details

#increment(redis_counter_key, expiry: nil) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/gitlab/usage_data_counters/redis_counter.rb', line 15

def increment(redis_counter_key, expiry: nil)
  if batch_mode?
    batch_key_count[redis_counter_key] += 1
    batch_expires[redis_counter_key] = expiry
    return
  end

  legacy_redis_counter_key = legacy_key(redis_counter_key)

  Gitlab::Redis::SharedState.with do |redis|
    redis.incr(legacy_redis_counter_key)

    unless expiry.nil?
      existing_expiry = redis.ttl(legacy_redis_counter_key) > 0
      redis.expire(legacy_redis_counter_key, expiry) unless existing_expiry
    end
  end
end

#increment_by(redis_counter_key, incr, expiry: nil) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/gitlab/usage_data_counters/redis_counter.rb', line 34

def increment_by(redis_counter_key, incr, expiry: nil)
  if batch_mode?
    batch_key_count[redis_counter_key] += incr
    batch_expires[redis_counter_key] = expiry
    return
  end

  legacy_redis_counter_key = legacy_key(redis_counter_key)

  Gitlab::Redis::SharedState.with do |redis|
    redis.incrby(legacy_redis_counter_key, incr)

    unless expiry.nil?
      existing_expiry = redis.ttl(legacy_redis_counter_key) > 0
      redis.expire(legacy_redis_counter_key, expiry) unless existing_expiry
    end
  end
end

#total_count(redis_counter_key) ⇒ Object



53
54
55
56
# File 'lib/gitlab/usage_data_counters/redis_counter.rb', line 53

def total_count(redis_counter_key)
  legacy_redis_counter_key = legacy_key(redis_counter_key)
  Gitlab::Redis::SharedState.with { |redis| redis.get(legacy_redis_counter_key).to_i }
end

#with_batched_redis_writesObject



58
59
60
61
62
63
64
65
66
# File 'lib/gitlab/usage_data_counters/redis_counter.rb', line 58

def with_batched_redis_writes
  Thread.current[:redis_counter_batch_mode] = true
  yield
ensure
  Thread.current[:redis_counter_batch_mode] = false
  flush_redis_batch
  Thread.current[:redis_counter_batch_key_count] = nil
  Thread.current[:redis_counter_batch_expires] = nil
end