Sha256: 3c6551d92668ae0ea76a624863a646bf9a3d302df149abc3e464b046377229bf

Contents?: true

Size: 833 Bytes

Versions: 13

Compression:

Stored size: 833 Bytes

Contents

# frozen_string_literal: true

module Labkit
  module Correlation
    # CorrelationId module provides access the Correlation-ID
    # of the current request
    module CorrelationId
      LOG_KEY = "correlation_id"

      class << self
        def use_id(correlation_id, &_blk)
          # always generate a id if null is passed
          correlation_id ||= new_id

          ids.push(correlation_id || new_id)

          begin
            yield(current_id)
          ensure
            ids.pop
          end
        end

        def current_id
          ids.last
        end

        def current_or_new_id
          current_id || new_id
        end

        private

        def ids
          Thread.current[:correlation_id] ||= []
        end

        def new_id
          SecureRandom.uuid
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
gitlab-labkit-0.7.0 lib/labkit/correlation/correlation_id.rb
gitlab-labkit-0.6.0 lib/labkit/correlation/correlation_id.rb
gitlab-labkit-0.5.2 lib/labkit/correlation/correlation_id.rb
gitlab-labkit-0.5.1 lib/labkit/correlation/correlation_id.rb
gitlab-labkit-0.5.0 lib/labkit/correlation/correlation_id.rb
gitlab-labkit-0.4.2 lib/labkit/correlation/correlation_id.rb
gitlab-labkit-0.4.1 lib/labkit/correlation/correlation_id.rb
gitlab-labkit-0.4.0 lib/labkit/correlation/correlation_id.rb
gitlab-labkit-0.3.0 lib/labkit/correlation/correlation_id.rb
gitlab-labkit-0.2.0 lib/labkit/correlation/correlation_id.rb
gitlab-labkit-0.1.2 lib/labkit/correlation/correlation_id.rb
gitlab-labkit-0.1.0.pre.1.pre.gcb57c95 lib/labkit/correlation/correlation_id.rb
gitlab-labkit-0.1.0 lib/labkit/correlation/correlation_id.rb