Class: Gitlab::Ci::Pipeline::Logger
- Inherits:
-
Object
- Object
- Gitlab::Ci::Pipeline::Logger
- Includes:
- Utils::StrongMemoize
- Defined in:
- lib/gitlab/ci/pipeline/logger.rb
Class Method Summary collapse
Instance Method Summary collapse
- #commit(pipeline:, caller:) ⇒ Object
-
#initialize(project:, destination: Gitlab::AppJsonLogger) {|_self| ... } ⇒ Logger
constructor
A new instance of Logger.
- #instrument(operation, once: false) ⇒ Object
- #instrument_once_with_sql(operation, &block) ⇒ Object
- #log_when(&block) ⇒ Object
- #observations_hash ⇒ Object
- #observe(operation, value, once: false) ⇒ Object
Constructor Details
#initialize(project:, destination: Gitlab::AppJsonLogger) {|_self| ... } ⇒ Logger
Returns a new instance of Logger.
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_time ⇒ Object
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. attributes[:pipeline_builds_distinct_tags_count] = pipeline. 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
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_hash ⇒ Object
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 |