2020-05-21 20:08:07 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# Usage data utilities
|
|
|
|
#
|
|
|
|
# * distinct_count(relation, column = nil, batch: true, start: nil, finish: nil)
|
|
|
|
# Does a distinct batch count, smartly reduces batch_size and handles errors
|
|
|
|
#
|
|
|
|
# Examples:
|
|
|
|
# issues_using_zoom_quick_actions: distinct_count(ZoomMeeting, :issue_id),
|
|
|
|
#
|
|
|
|
# * count(relation, column = nil, batch: true, start: nil, finish: nil)
|
|
|
|
# Does a non-distinct batch count, smartly reduces batch_size and handles errors
|
|
|
|
#
|
|
|
|
# Examples:
|
|
|
|
# active_user_count: count(User.active)
|
|
|
|
#
|
|
|
|
# * alt_usage_data method
|
2020-05-27 20:08:37 -04:00
|
|
|
# handles StandardError and fallbacks by default into -1 this way not all measures fail if we encounter one exception
|
|
|
|
# there might be cases where we need to set a specific fallback in order to be aligned wih what version app is expecting as a type
|
2020-05-21 20:08:07 -04:00
|
|
|
#
|
|
|
|
# Examples:
|
|
|
|
# alt_usage_data { Gitlab::VERSION }
|
|
|
|
# alt_usage_data { Gitlab::CurrentSettings.uuid }
|
2020-05-27 20:08:37 -04:00
|
|
|
# alt_usage_data(fallback: nil) { Gitlab.config.registry.enabled }
|
2020-05-21 20:08:07 -04:00
|
|
|
#
|
|
|
|
# * redis_usage_data method
|
|
|
|
# handles ::Redis::CommandError, Gitlab::UsageDataCounters::BaseCounter::UnknownEvent
|
|
|
|
# returns -1 when a block is sent or hash with all values -1 when a counter is sent
|
|
|
|
# different behaviour due to 2 different implementations of redis counter
|
|
|
|
#
|
|
|
|
# Examples:
|
|
|
|
# redis_usage_data(Gitlab::UsageDataCounters::WikiPageCounter)
|
|
|
|
# redis_usage_data { ::Gitlab::UsageCounters::PodLogs.usage_totals[:total] }
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Utils
|
|
|
|
module UsageData
|
|
|
|
extend self
|
|
|
|
|
|
|
|
FALLBACK = -1
|
2020-12-03 10:09:46 -05:00
|
|
|
DISTRIBUTED_HLL_FALLBACK = -2
|
2020-05-21 20:08:07 -04:00
|
|
|
|
2020-09-28 14:09:40 -04:00
|
|
|
def count(relation, column = nil, batch: true, batch_size: nil, start: nil, finish: nil)
|
2020-06-04 11:08:21 -04:00
|
|
|
if batch
|
2020-09-28 14:09:40 -04:00
|
|
|
Gitlab::Database::BatchCount.batch_count(relation, column, batch_size: batch_size, start: start, finish: finish)
|
2020-05-21 20:08:07 -04:00
|
|
|
else
|
|
|
|
relation.count
|
|
|
|
end
|
|
|
|
rescue ActiveRecord::StatementInvalid
|
|
|
|
FALLBACK
|
|
|
|
end
|
|
|
|
|
2020-06-02 20:08:38 -04:00
|
|
|
def distinct_count(relation, column = nil, batch: true, batch_size: nil, start: nil, finish: nil)
|
2020-06-04 11:08:21 -04:00
|
|
|
if batch
|
2020-06-02 20:08:38 -04:00
|
|
|
Gitlab::Database::BatchCount.batch_distinct_count(relation, column, batch_size: batch_size, start: start, finish: finish)
|
2020-05-21 20:08:07 -04:00
|
|
|
else
|
|
|
|
relation.distinct_count_by(column)
|
|
|
|
end
|
|
|
|
rescue ActiveRecord::StatementInvalid
|
|
|
|
FALLBACK
|
|
|
|
end
|
|
|
|
|
2020-12-03 10:09:46 -05:00
|
|
|
def estimate_batch_distinct_count(relation, column = nil, batch_size: nil, start: nil, finish: nil)
|
2020-12-23 07:10:26 -05:00
|
|
|
Gitlab::Database::PostgresHll::BatchDistinctCounter
|
|
|
|
.new(relation, column)
|
|
|
|
.execute(batch_size: batch_size, start: start, finish: finish)
|
|
|
|
.estimated_distinct_count
|
2020-12-03 10:09:46 -05:00
|
|
|
rescue ActiveRecord::StatementInvalid
|
|
|
|
FALLBACK
|
|
|
|
# catch all rescue should be removed as a part of feature flag rollout issue
|
|
|
|
# https://gitlab.com/gitlab-org/gitlab/-/issues/285485
|
|
|
|
rescue StandardError => error
|
|
|
|
Gitlab::ErrorTracking.track_and_raise_for_dev_exception(error)
|
|
|
|
DISTRIBUTED_HLL_FALLBACK
|
|
|
|
end
|
|
|
|
|
2020-09-16 02:09:24 -04:00
|
|
|
def sum(relation, column, batch_size: nil, start: nil, finish: nil)
|
|
|
|
Gitlab::Database::BatchCount.batch_sum(relation, column, batch_size: batch_size, start: start, finish: finish)
|
|
|
|
rescue ActiveRecord::StatementInvalid
|
|
|
|
FALLBACK
|
|
|
|
end
|
|
|
|
|
2020-05-21 20:08:07 -04:00
|
|
|
def alt_usage_data(value = nil, fallback: FALLBACK, &block)
|
|
|
|
if block_given?
|
|
|
|
yield
|
|
|
|
else
|
|
|
|
value
|
|
|
|
end
|
|
|
|
rescue
|
|
|
|
fallback
|
|
|
|
end
|
|
|
|
|
|
|
|
def redis_usage_data(counter = nil, &block)
|
|
|
|
if block_given?
|
|
|
|
redis_usage_counter(&block)
|
|
|
|
elsif counter.present?
|
|
|
|
redis_usage_data_totals(counter)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-09-16 14:09:47 -04:00
|
|
|
def with_prometheus_client(fallback: nil, verify: true)
|
|
|
|
client = prometheus_client(verify: verify)
|
|
|
|
return fallback unless client
|
2020-07-01 20:09:51 -04:00
|
|
|
|
2020-09-16 14:09:47 -04:00
|
|
|
yield client
|
2020-06-03 08:08:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def measure_duration
|
|
|
|
result = nil
|
|
|
|
duration = Benchmark.realtime do
|
|
|
|
result = yield
|
|
|
|
end
|
|
|
|
[result, duration]
|
|
|
|
end
|
|
|
|
|
2020-06-17 08:08:42 -04:00
|
|
|
def with_finished_at(key, &block)
|
2020-08-19 02:10:04 -04:00
|
|
|
yield.merge(key => Time.current)
|
2020-06-17 08:08:42 -04:00
|
|
|
end
|
|
|
|
|
2020-09-17 23:09:28 -04:00
|
|
|
# @param event_name [String] the event name
|
|
|
|
# @param values [Array|String] the values counted
|
|
|
|
def track_usage_event(event_name, values)
|
|
|
|
return unless Feature.enabled?(:"usage_data_#{event_name}", default_enabled: true)
|
2020-09-10 17:08:28 -04:00
|
|
|
|
2021-01-07 19:10:44 -05:00
|
|
|
Gitlab::UsageDataCounters::HLLRedisCounter.track_event(event_name.to_s, values: values)
|
2020-09-10 17:08:28 -04:00
|
|
|
end
|
|
|
|
|
2020-05-21 20:08:07 -04:00
|
|
|
private
|
|
|
|
|
2020-09-16 14:09:47 -04:00
|
|
|
def prometheus_client(verify:)
|
|
|
|
server_address = prometheus_server_address
|
|
|
|
|
|
|
|
return unless server_address
|
|
|
|
|
|
|
|
# There really is not a way to discover whether a Prometheus connection is using TLS or not
|
|
|
|
# Try TLS first because HTTPS will return fast if failed.
|
|
|
|
%w[https http].find do |scheme|
|
|
|
|
api_url = "#{scheme}://#{server_address}"
|
|
|
|
client = Gitlab::PrometheusClient.new(api_url, allow_local_requests: true, verify: verify)
|
|
|
|
break client if client.ready?
|
|
|
|
rescue
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def prometheus_server_address
|
2020-09-11 17:08:44 -04:00
|
|
|
if Gitlab::Prometheus::Internal.prometheus_enabled?
|
2021-01-05 04:10:15 -05:00
|
|
|
# Stripping protocol from URI
|
|
|
|
Gitlab::Prometheus::Internal.uri&.strip&.sub(%r{^https?://}, '')
|
2020-09-11 17:08:44 -04:00
|
|
|
elsif Gitlab::Consul::Internal.api_url
|
2020-09-16 14:09:47 -04:00
|
|
|
Gitlab::Consul::Internal.discover_prometheus_server_address
|
2020-09-11 17:08:44 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-21 20:08:07 -04:00
|
|
|
def redis_usage_counter
|
|
|
|
yield
|
|
|
|
rescue ::Redis::CommandError, Gitlab::UsageDataCounters::BaseCounter::UnknownEvent
|
|
|
|
FALLBACK
|
|
|
|
end
|
|
|
|
|
|
|
|
def redis_usage_data_totals(counter)
|
|
|
|
counter.totals
|
|
|
|
rescue ::Redis::CommandError, Gitlab::UsageDataCounters::BaseCounter::UnknownEvent
|
|
|
|
counter.fallback_totals
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|