2021-01-05 04:10:15 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Usage
|
|
|
|
class Metric
|
2021-08-10 11:10:03 -04:00
|
|
|
attr_reader :definition
|
2021-01-05 04:10:15 -05:00
|
|
|
|
2021-08-10 11:10:03 -04:00
|
|
|
def initialize(definition)
|
|
|
|
@definition = definition
|
|
|
|
end
|
2021-01-05 04:10:15 -05:00
|
|
|
|
2021-08-10 11:10:03 -04:00
|
|
|
class << self
|
|
|
|
def all
|
|
|
|
@all ||= Gitlab::Usage::MetricDefinition.with_instrumentation_class.map do |definition|
|
|
|
|
self.new(definition)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-01-05 04:10:15 -05:00
|
|
|
|
2021-08-10 11:10:03 -04:00
|
|
|
def with_value
|
2022-04-21 05:10:19 -04:00
|
|
|
with_availability(proc { instrumentation_object.value })
|
2021-01-05 04:10:15 -05:00
|
|
|
end
|
|
|
|
|
2021-08-10 11:10:03 -04:00
|
|
|
def with_instrumentation
|
2022-04-21 05:10:19 -04:00
|
|
|
with_availability(proc { instrumentation_object.instrumentation })
|
2021-01-05 04:10:15 -05:00
|
|
|
end
|
|
|
|
|
2021-10-25 14:12:16 -04:00
|
|
|
def with_suggested_name
|
2022-04-21 05:10:19 -04:00
|
|
|
with_availability(proc { instrumentation_object.suggested_name })
|
2021-10-25 14:12:16 -04:00
|
|
|
end
|
|
|
|
|
2021-08-10 11:10:03 -04:00
|
|
|
private
|
2021-01-05 04:10:15 -05:00
|
|
|
|
2022-04-21 05:10:19 -04:00
|
|
|
def with_availability(value_proc)
|
|
|
|
return {} unless instrumentation_object.available?
|
|
|
|
|
|
|
|
unflatten_key_path(value_proc.call)
|
|
|
|
end
|
|
|
|
|
2021-08-10 11:10:03 -04:00
|
|
|
def unflatten_key_path(value)
|
|
|
|
::Gitlab::Usage::Metrics::KeyPathProcessor.process(definition.key_path, value)
|
2021-01-05 04:10:15 -05:00
|
|
|
end
|
|
|
|
|
2021-08-10 11:10:03 -04:00
|
|
|
def instrumentation_class
|
|
|
|
"Gitlab::Usage::Metrics::Instrumentations::#{definition.instrumentation_class}"
|
|
|
|
end
|
2021-01-05 04:10:15 -05:00
|
|
|
|
2022-04-21 05:10:19 -04:00
|
|
|
def instrumentation_object
|
|
|
|
@instrumentation_object ||= instrumentation_class.constantize.new(
|
2021-08-10 11:10:03 -04:00
|
|
|
time_frame: definition.time_frame,
|
|
|
|
options: definition.attributes[:options]
|
|
|
|
)
|
2021-01-05 04:10:15 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|