summaryrefslogtreecommitdiff
path: root/lib/gitlab/ci/trace/metrics.rb
blob: fcd70634630ca2b159be89f8894eb34e1edba68c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# frozen_string_literal: true

module Gitlab
  module Ci
    class Trace
      class Metrics
        extend Gitlab::Utils::StrongMemoize

        OPERATIONS = [
          :appended,  # new trace data has been written to a chunk
          :streamed,  # new trace data has been sent by a runner
          :chunked,   # new trace chunk has been created
          :mutated,   # trace has been mutated when removing secrets
          :accepted,  # scheduled chunks for migration and responded with 202
          :finalized, # all live build trace chunks have been persisted
          :discarded, # failed to persist live chunks before timeout
          :conflict,  # runner has sent unrecognized build state details
          :locked,    # build trace has been locked by a different mechanism
          :stalled,   # failed to migrate chunk due to a worker duplication
          :invalid,   # invalid build trace has been detected using CRC32
          :corrupted  # malformed trace found after comparing CRC32 and size
        ].freeze

        def increment_trace_operation(operation: :unknown)
          unless OPERATIONS.include?(operation)
            raise ArgumentError, "unknown trace operation: #{operation}"
          end

          self.class.trace_operations.increment(operation: operation)
        end

        def increment_trace_bytes(size)
          self.class.trace_bytes.increment({}, size.to_i)
        end

        def observe_migration_duration(seconds)
          self.class.finalize_histogram.observe({}, seconds.to_f)
        end

        def self.trace_operations
          strong_memoize(:trace_operations) do
            name = :gitlab_ci_trace_operations_total
            comment = 'Total amount of different operations on a build trace'

            Gitlab::Metrics.counter(name, comment)
          end
        end

        def self.trace_bytes
          strong_memoize(:trace_bytes) do
            name = :gitlab_ci_trace_bytes_total
            comment = 'Total amount of build trace bytes transferred'

            Gitlab::Metrics.counter(name, comment)
          end
        end

        def self.finalize_histogram
          strong_memoize(:finalize_histogram) do
            name = :gitlab_ci_trace_finalize_duration_seconds
            comment = 'Duration of build trace chunks migration to object storage'
            buckets = [0.1, 0.5, 1.0, 2.0, 3.0, 5.0, 7.0, 10.0, 20.0, 30.0, 60.0, 300.0]
            labels = {}

            ::Gitlab::Metrics.histogram(name, comment, labels, buckets)
          end
        end
      end
    end
  end
end