debian-mirror-gitlab/lib/gitlab/ci/pipeline/logger.rb

146 lines
4 KiB
Ruby
Raw Normal View History

2022-01-26 12:08:38 +05:30
# frozen_string_literal: true
module Gitlab
module Ci
module Pipeline
class Logger
include ::Gitlab::Utils::StrongMemoize
def self.current_monotonic_time
::Gitlab::Metrics::System.monotonic_time
end
def initialize(project:, destination: Gitlab::AppJsonLogger)
@started_at = current_monotonic_time
@project = project
@destination = destination
@log_conditions = []
yield(self) if block_given?
end
def log_when(&block)
log_conditions.push(block)
end
2023-03-04 22:38:38 +05:30
def instrument(operation, once: false)
2022-01-26 12:08:38 +05:30
return yield unless enabled?
raise ArgumentError, 'block not given' unless block_given?
op_started_at = current_monotonic_time
result = yield
2023-03-04 22:38:38 +05:30
observe("#{operation}_duration_s", current_monotonic_time - op_started_at, once: once)
2022-01-26 12:08:38 +05:30
result
end
2023-03-04 22:38:38 +05:30
def instrument_once_with_sql(operation, &block)
2022-03-02 08:16:31 +05:30
op_start_db_counters = current_db_counter_payload
2023-03-04 22:38:38 +05:30
result = instrument(operation, once: true, &block)
2022-03-02 08:16:31 +05:30
2023-03-04 22:38:38 +05:30
observe_sql_counters(operation, op_start_db_counters, current_db_counter_payload, once: true)
2022-03-02 08:16:31 +05:30
result
end
2023-03-04 22:38:38 +05:30
def observe(operation, value, once: false)
2022-01-26 12:08:38 +05:30
return unless enabled?
2023-03-04 22:38:38 +05:30
if once
observations[operation.to_s] = value
else
observations[operation.to_s] ||= []
observations[operation.to_s].push(value)
end
2022-01-26 12:08:38 +05:30
end
def commit(pipeline:, caller:)
return unless log?
2023-03-04 22:38:38 +05:30
Gitlab::ApplicationContext.with_context(project: project) do
attributes = Gitlab::ApplicationContext.current.merge(
class: self.class.name.to_s,
pipeline_creation_caller: caller,
project_id: project&.id, # project is not available when called from `/ci/lint`
pipeline_persisted: pipeline.persisted?,
pipeline_source: pipeline.source,
pipeline_creation_service_duration_s: age
)
if pipeline.persisted?
attributes[:pipeline_builds_tags_count] = pipeline.tags_count
attributes[:pipeline_builds_distinct_tags_count] = pipeline.distinct_tags_count
attributes[:pipeline_id] = pipeline.id
end
attributes.compact!
attributes.stringify_keys!
attributes.merge!(observations_hash)
destination.info(attributes)
2022-03-02 08:16:31 +05:30
end
2022-01-26 12:08:38 +05:30
end
def observations_hash
2023-03-04 22:38:38 +05:30
observations.transform_values do |observation|
next if observation.blank?
if observation.is_a?(Array)
{
'count' => observation.size,
'max' => observation.max,
'sum' => observation.sum
}
else
observation
end
2022-01-26 12:08:38 +05:30
end.compact
end
private
attr_reader :project, :destination, :started_at, :log_conditions
2022-05-07 20:08:51 +05:30
2022-01-26 12:08:38 +05:30
delegate :current_monotonic_time, to: :class
def age
current_monotonic_time - started_at
end
def log?
return false unless enabled?
return true if log_conditions.empty?
log_conditions.any? { |cond| cond.call(observations) }
end
def enabled?
2023-03-04 22:38:38 +05:30
::Feature.enabled?(:ci_pipeline_creation_logger, project, type: :ops)
2022-01-26 12:08:38 +05:30
end
2023-03-17 16:20:25 +05:30
strong_memoize_attr :enabled?
2022-01-26 12:08:38 +05:30
def observations
2023-03-04 22:38:38 +05:30
@observations ||= {}
2022-01-26 12:08:38 +05:30
end
2022-03-02 08:16:31 +05:30
2023-03-04 22:38:38 +05:30
def observe_sql_counters(operation, start_db_counters, end_db_counters, once: false)
2022-03-02 08:16:31 +05:30
end_db_counters.each do |key, value|
result = value - start_db_counters.fetch(key, 0)
next if result == 0
2023-03-04 22:38:38 +05:30
observe("#{operation}_#{key}", result, once: once)
2022-03-02 08:16:31 +05:30
end
end
def current_db_counter_payload
::Gitlab::Metrics::Subscribers::ActiveRecord.db_counter_payload
end
2022-01-26 12:08:38 +05:30
end
end
end
end