Class: Gitlab::Ci::Pipeline::Logger

Inherits:
Object
  • Object
show all
Includes:
Utils::StrongMemoize
Defined in:
lib/gitlab/ci/pipeline/logger.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project:, destination: Gitlab::AppJsonLogger) {|_self| ... } ⇒ Logger

Returns a new instance of Logger.

Yields:

  • (_self)

Yield Parameters:



13
14
15
16
17
18
19
20
# File 'lib/gitlab/ci/pipeline/logger.rb', line 13

def initialize(project:, destination: Gitlab::AppJsonLogger)
  @started_at = current_monotonic_time
  @project = project
  @destination = destination
  @log_conditions = []

  yield(self) if block_given?
end

Class Method Details

.current_monotonic_timeObject



9
10
11
# File 'lib/gitlab/ci/pipeline/logger.rb', line 9

def self.current_monotonic_time
  ::Gitlab::Metrics::System.monotonic_time
end

Instance Method Details

#commit(pipeline:, caller:) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/gitlab/ci/pipeline/logger.rb', line 61

def commit(pipeline:, caller:)
  return unless log?

  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)
  end
end

#instrument(operation, once: false) ⇒ Object

Raises:

  • (ArgumentError)


26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/gitlab/ci/pipeline/logger.rb', line 26

def instrument(operation, once: false)
  return yield unless enabled?

  raise ArgumentError, 'block not given' unless block_given?

  op_started_at = current_monotonic_time

  result = yield

  observe("#{operation}_duration_s", current_monotonic_time - op_started_at, once: once)

  result
end

#instrument_once_with_sql(operation, &block) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/gitlab/ci/pipeline/logger.rb', line 40

def instrument_once_with_sql(operation, &block)
  op_start_db_counters = current_db_counter_payload

  result = instrument(operation, once: true, &block)

  observe_sql_counters(operation, op_start_db_counters, current_db_counter_payload, once: true)

  result
end

#log_when(&block) ⇒ Object



22
23
24
# File 'lib/gitlab/ci/pipeline/logger.rb', line 22

def log_when(&block)
  log_conditions.push(block)
end

#observations_hashObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/gitlab/ci/pipeline/logger.rb', line 88

def observations_hash
  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
  end.compact
end

#observe(operation, value, once: false) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/gitlab/ci/pipeline/logger.rb', line 50

def observe(operation, value, once: false)
  return unless enabled?

  if once
    observations[operation.to_s] = value
  else
    observations[operation.to_s] ||= []
    observations[operation.to_s].push(value)
  end
end